blob: 7523504fe4f6489464e1564704e5341c07189eac [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "pit-entry.hpp"
8#include <algorithm>
9
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080010namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070011namespace pit {
12
13Entry::Entry(const Interest& interest)
14 : m_interest(interest)
15{
16}
17
18const Name&
19Entry::getName() const
20{
21 return m_interest.getName();
22}
23
24const InRecordCollection&
25Entry::getInRecords() const
26{
27 return m_inRecords;
28}
29
30const OutRecordCollection&
31Entry::getOutRecords() const
32{
33 return m_outRecords;
34}
35
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036static inline bool
Junxiao Shi11bd9c22014-03-13 20:44:13 -070037predicate_InRecord_isLocal(const InRecord& inRecord)
38{
39 return inRecord.getFace()->isLocal();
40}
41
42bool
43Entry::hasLocalInRecord() const
44{
45 InRecordCollection::const_iterator it = std::find_if(
46 m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal);
47 return it != m_inRecords.end();
48}
49
50static inline bool
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070051predicate_FaceRecord_Face(const FaceRecord& faceRecord, shared_ptr<Face> face)
52{
53 return faceRecord.getFace() == face;
54}
55
56static inline bool
57predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord,
58 shared_ptr<Face> face, time::Point now)
59{
60 return faceRecord.getFace() != face && faceRecord.getExpiry() >= now;
61}
62
63bool
64Entry::canForwardTo(shared_ptr<Face> face) const
65{
66 OutRecordCollection::const_iterator outIt = std::find_if(
67 m_outRecords.begin(), m_outRecords.end(),
68 bind(&predicate_FaceRecord_Face, _1, face));
69 bool hasUnexpiredOutRecord = outIt != m_outRecords.end() &&
70 outIt->getExpiry() >= time::now();
71 if (hasUnexpiredOutRecord) {
72 return false;
73 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -070074
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070075 InRecordCollection::const_iterator inIt = std::find_if(
76 m_inRecords.begin(), m_inRecords.end(),
77 bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, face, time::now()));
78 bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end();
79 return hasUnexpiredOtherInRecord;
80}
81
Junxiao Shicbba04c2014-01-26 14:21:22 -070082bool
Junxiao Shid3c792f2014-01-30 00:46:13 -070083Entry::addNonce(uint32_t nonce)
Junxiao Shicbba04c2014-01-26 14:21:22 -070084{
Junxiao Shid3c792f2014-01-30 00:46:13 -070085 std::pair<std::set<uint32_t>::iterator, bool> insertResult =
86 m_nonces.insert(nonce);
87
88 return insertResult.second;
Junxiao Shicbba04c2014-01-26 14:21:22 -070089}
90
Junxiao Shicbba04c2014-01-26 14:21:22 -070091InRecordCollection::iterator
92Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
93{
94 InRecordCollection::iterator it = std::find_if(m_inRecords.begin(),
95 m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
96 if (it == m_inRecords.end()) {
97 m_inRecords.push_front(InRecord(face));
98 it = m_inRecords.begin();
99 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700100
Junxiao Shicbba04c2014-01-26 14:21:22 -0700101 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700102 return it;
103}
104
105void
106Entry::deleteInRecords()
107{
108 m_inRecords.clear();
109}
110
111OutRecordCollection::iterator
112Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
113{
114 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
115 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
116 if (it == m_outRecords.end()) {
117 m_outRecords.push_front(OutRecord(face));
118 it = m_outRecords.begin();
119 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700120
Junxiao Shicbba04c2014-01-26 14:21:22 -0700121 it->update(interest);
122 m_nonces.insert(interest.getNonce());
123 return it;
124}
125
126void
127Entry::deleteOutRecord(shared_ptr<Face> face)
128{
129 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
130 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
131 if (it != m_outRecords.end()) {
132 m_outRecords.erase(it);
133 }
134}
135
136
137} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800138} // namespace nfd