NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
rib-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "rib-entry.hpp"
27 
28 #include "core/logger.hpp"
29 
30 #include <ndn-cxx/mgmt/nfd/control-command.hpp>
31 
32 NFD_LOG_INIT("RibEntry");
33 
34 namespace nfd {
35 namespace rib {
36 
39 {
40  return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
41 }
42 
43 RibEntry::RouteList::const_iterator
44 RibEntry::findRoute(const Route& route) const
45 {
46  return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
47 }
48 
49 std::pair<RibEntry::iterator, bool>
51 {
52  iterator it = findRoute(route);
53 
54  if (it == end()) {
55  if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
56  m_nRoutesWithCaptureSet++;
57  }
58 
59  m_routes.push_back(route);
60 
61  return std::make_pair(std::prev(m_routes.end()), true);
62  }
63  else {
64  return std::make_pair(it, false);
65  }
66 }
67 
68 void
70 {
71  RibEntry::iterator it = findRoute(route);
72  eraseRoute(it);
73 }
74 
75 bool
77 {
79 
80  return it != end();
81 }
82 
83 bool
84 RibEntry::hasFaceId(const uint64_t faceId) const
85 {
86  RibEntry::const_iterator it = std::find_if(begin(), end(), bind(&compareFaceId, _1, faceId));
87 
88  return it != end();
89 }
90 
91 size_t
93 {
94  return m_routes.size();
95 }
96 
97 void
98 RibEntry::addChild(shared_ptr<RibEntry> child)
99 {
100  BOOST_ASSERT(!static_cast<bool>(child->getParent()));
101  child->setParent(this->shared_from_this());
102  m_children.push_back(child);
103 }
104 
105 void
106 RibEntry::removeChild(shared_ptr<RibEntry> child)
107 {
108  BOOST_ASSERT(child->getParent().get() == this);
109  child->setParent(shared_ptr<RibEntry>());
110  m_children.remove(child);
111 }
112 
115 {
116  if (route != m_routes.end()) {
117  if (route->flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
118  m_nRoutesWithCaptureSet--;
119  }
120 
121  // Cancel any scheduled event
122  NFD_LOG_TRACE("Cancelling expiration eventId: " << route->getExpirationEvent());
123  scheduler::cancel(route->getExpirationEvent());
124 
125  return m_routes.erase(route);
126  }
127 
128  return m_routes.end();
129 }
130 
131 void
133 {
134  m_inheritedRoutes.push_back(route);
135 }
136 
137 void
139 {
140  RouteList::iterator it = std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
141  bind(&compareFaceId, _1, route.faceId));
142  m_inheritedRoutes.erase(it);
143 }
144 
145 RibEntry::RouteList::const_iterator
147 {
148  return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
149  bind(&compareFaceId, _1, route.faceId));
150 }
151 
152 bool
154 {
155  RouteList::const_iterator it = findInheritedRoute(route);
156 
157  return (it != m_inheritedRoutes.end());
158 }
159 
160 bool
162 {
163  return m_nRoutesWithCaptureSet > 0;
164 }
165 
166 bool
167 RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const
168 {
169  for (const Route& route : m_routes) {
170  if (route.faceId == faceId && (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)) {
171  return true;
172  }
173  }
174 
175  return false;
176 }
177 
178 const Route*
180 {
181  const Route* candidate = nullptr;
182 
183  for (const Route& route : m_routes) {
184  // Matching face ID
185  if (route.faceId == faceId) {
186  // If this is the first route with this Face ID found
187  if (candidate == nullptr) {
188  candidate = &route;
189  }
190  else if (route.cost < candidate->cost) {
191  // Found a route with a lower cost
192  candidate = &route;
193  }
194  }
195  }
196 
197  return candidate;
198 }
199 
200 const Route*
202 {
203  std::vector<const Route*> matches;
204 
205  // Copy routes which have faceId
206  for (const Route& route : m_routes) {
207  if (route.faceId == faceId) {
208  matches.push_back(&route);
209  }
210  }
211 
212  // If there are less than 2 routes, there is no second lowest
213  if (matches.size() < 2) {
214  return nullptr;
215  }
216 
217  // Get second lowest cost
218  std::nth_element(matches.begin(), matches.begin() + 1, matches.end(),
219  [] (const Route* lhs, const Route* rhs) { return lhs->cost < rhs->cost; });
220 
221  return matches.at(1);
222 }
223 
224 const Route*
226 {
227  const Route* candidate = nullptr;
228 
229  for (const Route& route : m_routes) {
230  // Correct face ID and Child Inherit flag set
231  if (route.faceId == faceId &&
233  {
234  // If this is the first route with this Face ID found
235  if (candidate == nullptr) {
236  candidate = &route;
237  }
238  else if (route.cost < candidate->cost) {
239  // Found a route with a lower cost
240  candidate = &route;
241  }
242  }
243  }
244 
245  return candidate;
246 }
247 
248 std::ostream&
249 operator<<(std::ostream& os, const RibEntry& entry)
250 {
251  os << "RibEntry {\n";
252  os << " Name: " << entry.getName() << "\n";
253 
254  for (const Route& route : entry) {
255  os << " " << route << "\n";
256  }
257 
258  os << "}";
259 
260  return os;
261 }
262 
263 } // namespace rib
264 } // namespace nfd
void removeChild(shared_ptr< RibEntry > child)
Definition: rib-entry.cpp:106
iterator findRoute(const Route &route)
Definition: rib-entry.cpp:38
void addChild(shared_ptr< RibEntry > child)
Definition: rib-entry.cpp:98
bool hasFaceId(const uint64_t faceId) const
Definition: rib-entry.cpp:84
bool hasRoute(const Route &route)
Definition: rib-entry.cpp:76
void cancel(const EventId &eventId)
Cancel a scheduled event.
Definition: scheduler.cpp:54
std::ostream & operator<<(std::ostream &os, const FibUpdate &update)
Definition: fib-update.hpp:74
size_t getNRoutes() const
Definition: rib-entry.cpp:92
std::underlying_type< ndn::nfd::RouteFlags >::type flags
Definition: route.hpp:68
const Route * getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const
Definition: rib-entry.cpp:201
uint64_t cost
Definition: route.hpp:67
uint64_t faceId
Definition: route.hpp:65
bool compareFaceIdAndOrigin(const Route &lhs, const Route &rhs)
Definition: route.hpp:85
RouteList::iterator iterator
Definition: rib-entry.hpp:40
const Name & getName() const
Definition: rib-entry.hpp:205
void removeInheritedRoute(const Route &route)
Definition: rib-entry.cpp:138
Table::const_iterator iterator
Definition: cs-internal.hpp:41
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
void addInheritedRoute(const Route &route)
Definition: rib-entry.cpp:132
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
const Route * getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const
Returns the route with the lowest cost that has the passed face ID and its child inherit flag set...
Definition: rib-entry.cpp:225
represents a RIB entry, which contains one or more Routes with the same prefix
Definition: rib-entry.hpp:36
represents a route for a name prefix
Definition: route.hpp:41
bool hasInheritedRoute(const Route &route) const
Determines if the entry has an inherited route with a matching face ID.
Definition: rib-entry.cpp:153
RouteList::const_iterator findInheritedRoute(const Route &route) const
Finds an inherited route with a matching face ID.
Definition: rib-entry.cpp:146
const_iterator begin() const
Definition: rib-entry.hpp:241
bool hasChildInheritOnFaceId(uint64_t faceId) const
Determines if the entry has an inherited route with the passed face ID and its child inherit flag set...
Definition: rib-entry.cpp:167
std::pair< RibEntry::iterator, bool > insertRoute(const Route &route)
inserts a new route into the entry&#39;s route list If another route already exists with the same faceId ...
Definition: rib-entry.cpp:50
RouteList::const_iterator const_iterator
Definition: rib-entry.hpp:41
void eraseRoute(const Route &route)
erases a Route with the same faceId and origin
Definition: rib-entry.cpp:69
bool compareFaceId(const Route &route, const uint64_t faceId)
Definition: route.hpp:91
bool hasCapture() const
Definition: rib-entry.cpp:161
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
const Route * getRouteWithLowestCostByFaceId(uint64_t faceId) const
Returns the route with the lowest cost that has the passed face ID.
Definition: rib-entry.cpp:179
const_iterator end() const
Definition: rib-entry.hpp:247