NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
pit-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "pit-entry.hpp"
27 #include <algorithm>
28 
29 namespace nfd {
30 namespace pit {
31 
32 const Name Entry::LOCALHOST_NAME("ndn:/localhost");
33 const Name Entry::LOCALHOP_NAME("ndn:/localhop");
34 
35 Entry::Entry(const Interest& interest)
36  : m_interest(interest.shared_from_this())
37 {
38 }
39 
40 const Name&
42 {
43  return m_interest->getName();
44 }
45 
46 bool
48 {
49  return std::any_of(m_inRecords.begin(), m_inRecords.end(),
50  [] (const InRecord& inRecord) { return inRecord.getFace()->getScope() == ndn::nfd::FACE_SCOPE_LOCAL; });
51 }
52 
53 bool
54 Entry::canForwardTo(const Face& face) const
55 {
57 
58  bool hasUnexpiredOutRecord = std::any_of(m_outRecords.begin(), m_outRecords.end(),
59  [&face, &now] (const OutRecord& outRecord) {
60  return outRecord.getFace().get() == &face && outRecord.getExpiry() >= now;
61  });
62  if (hasUnexpiredOutRecord) {
63  return false;
64  }
65 
66  bool hasUnexpiredOtherInRecord = std::any_of(m_inRecords.begin(), m_inRecords.end(),
67  [&face, &now] (const InRecord& inRecord) {
68  return inRecord.getFace().get() != &face && inRecord.getExpiry() >= now;
69  });
70  if (!hasUnexpiredOtherInRecord) {
71  return false;
72  }
73 
74  return !this->violatesScope(face);
75 }
76 
77 bool
78 Entry::violatesScope(const Face& face) const
79 {
80  // /localhost scope
81  bool isViolatingLocalhost = face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
82  LOCALHOST_NAME.isPrefixOf(this->getName());
83  if (isViolatingLocalhost) {
84  return true;
85  }
86 
87  // /localhop scope
88  bool isViolatingLocalhop = face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
89  LOCALHOP_NAME.isPrefixOf(this->getName()) &&
90  !this->hasLocalInRecord();
91  if (isViolatingLocalhop) {
92  return true;
93  }
94 
95  return false;
96 }
97 
98 int
99 Entry::findNonce(uint32_t nonce, const Face& face) const
100 {
101  // TODO should we ignore expired in/out records?
102 
103  int dnw = DUPLICATE_NONCE_NONE;
104 
105  for (const InRecord& inRecord : m_inRecords) {
106  if (inRecord.getLastNonce() == nonce) {
107  if (inRecord.getFace().get() == &face) {
109  }
110  else {
112  }
113  }
114  }
115 
116  for (const OutRecord& outRecord : m_outRecords) {
117  if (outRecord.getLastNonce() == nonce) {
118  if (outRecord.getFace().get() == &face) {
120  }
121  else {
123  }
124  }
125  }
126 
127  return dnw;
128 }
129 
131 Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
132 {
133  auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(),
134  [&face] (const InRecord& inRecord) { return inRecord.getFace() == face; });
135  if (it == m_inRecords.end()) {
136  m_inRecords.emplace_front(face);
137  it = m_inRecords.begin();
138  }
139 
140  it->update(interest);
141  return it;
142 }
143 
144 InRecordCollection::const_iterator
145 Entry::getInRecord(const Face& face) const
146 {
147  return std::find_if(m_inRecords.begin(), m_inRecords.end(),
148  [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; });
149 }
150 
151 void
153 {
154  auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(),
155  [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; });
156  if (it != m_inRecords.end()) {
157  m_inRecords.erase(it);
158  }
159 }
160 
161 void
163 {
164  m_inRecords.clear();
165 }
166 
168 Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
169 {
170  auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
171  [&face] (const OutRecord& outRecord) { return outRecord.getFace() == face; });
172  if (it == m_outRecords.end()) {
173  m_outRecords.emplace_front(face);
174  it = m_outRecords.begin();
175  }
176 
177  it->update(interest);
178  return it;
179 }
180 
183 {
184  return std::find_if(m_outRecords.begin(), m_outRecords.end(),
185  [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
186 }
187 
188 void
190 {
191  auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
192  [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
193  if (it != m_outRecords.end()) {
194  m_outRecords.erase(it);
195  }
196 }
197 
198 bool
200 {
202 
203  return std::any_of(m_outRecords.begin(), m_outRecords.end(),
204  [&now] (const OutRecord& outRecord) { return outRecord.getExpiry() >= now; });
205 }
206 
207 } // namespace pit
208 } // namespace nfd
time_point TimePoint
Definition: time.hpp:108
in-record of other face
Definition: pit-entry.hpp:60
OutRecordCollection::iterator getOutRecord(const Face &face)
get the OutRecord for face
Definition: pit-entry.cpp:182
static time_point now() noexcept
Definition: time.cpp:79
contains information about an Interest from an incoming face
bool violatesScope(const Face &face) const
decides whether forwarding Interest to face would violate scope
Definition: pit-entry.cpp:78
InRecordCollection::iterator insertOrUpdateInRecord(shared_ptr< Face > face, const Interest &interest)
inserts a InRecord for face, and updates it with interest
Definition: pit-entry.cpp:131
represents an Interest packet
Definition: interest.hpp:45
const Name & getName() const
Definition: pit-entry.cpp:41
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
bool hasUnexpiredOutRecords() const
Definition: pit-entry.cpp:199
out-record of same face
Definition: pit-entry.hpp:62
void deleteOutRecord(const Face &face)
deletes one OutRecord for face if exists
Definition: pit-entry.cpp:189
Entry(const Interest &interest)
Definition: pit-entry.cpp:35
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:320
void deleteInRecord(const Face &face)
deletes one InRecord for face if exists
Definition: pit-entry.cpp:152
in-record of same face
Definition: pit-entry.hpp:58
bool canForwardTo(const Face &face) const
decides whether Interest can be forwarded to face
Definition: pit-entry.cpp:54
void deleteInRecords()
deletes all InRecords
Definition: pit-entry.cpp:162
out-record of other face
Definition: pit-entry.hpp:64
contains information about an Interest toward an outgoing face
OutRecordCollection::iterator insertOrUpdateOutRecord(shared_ptr< Face > face, const Interest &interest)
inserts a OutRecord for face, and updates it with interest
Definition: pit-entry.cpp:168
InRecordCollection::const_iterator getInRecord(const Face &face) const
get the InRecord for face
Definition: pit-entry.cpp:145
bool hasLocalInRecord() const
determines whether any InRecord is a local Face
Definition: pit-entry.cpp:47
int findNonce(uint32_t nonce, const Face &face) const
finds where a duplicate Nonce appears
Definition: pit-entry.cpp:99