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/management/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 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 true;
62  }
63  else {
64  return 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<Route> matches;
204 
205  // Copy routes which have faceId
206  std::copy_if(m_routes.begin(), m_routes.end(), std::back_inserter(matches),
207  [faceId] (const Route& route) { return route.faceId == faceId; });
208 
209  // If there are not at least 2 routes, there is no second lowest
210  if (matches.size() < 2) {
211  return nullptr;
212  }
213 
214  // Get second lowest cost
215  std::nth_element(matches.begin(), matches.begin() + 1, matches.end(),
216  [] (const Route& lhs, const Route& rhs) { return lhs.cost < rhs.cost; });
217 
218  return &matches.at(1);
219 }
220 
221 const Route*
223 {
224  const Route* candidate = nullptr;
225 
226  for (const Route& route : m_routes) {
227  // Correct face ID and Child Inherit flag set
228  if (route.faceId == faceId &&
230  {
231  // If this is the first route with this Face ID found
232  if (candidate == nullptr) {
233  candidate = &route;
234  }
235  else if (route.cost < candidate->cost) {
236  // Found a route with a lower cost
237  candidate = &route;
238  }
239  }
240  }
241 
242  return candidate;
243 }
244 
245 std::ostream&
246 operator<<(std::ostream& os, const RibEntry& entry)
247 {
248  os << "RibEntry {\n";
249  os << "\tName: " << entry.getName() << "\n";
250 
251  for (const Route& route : entry) {
252  os << "\t" << route << "\n";
253  }
254 
255  os << "}";
256 
257  return os;
258 }
259 
260 } // namespace rib
261 } // 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:58
std::ostream & operator<<(std::ostream &os, const FibUpdate &update)
Definition: fib-update.hpp:74
size_t getNRoutes() const
Definition: rib-entry.cpp:92
const Route * getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const
Definition: rib-entry.cpp:201
uint64_t cost
Definition: route.hpp:82
uint64_t faceId
Definition: route.hpp:79
bool compareFaceIdAndOrigin(const Route &lhs, const Route &rhs)
Definition: route.hpp:90
RouteList::iterator iterator
Definition: rib-entry.hpp:40
const Name & getName() const
Definition: rib-entry.hpp:202
void removeInheritedRoute(const Route &route)
Definition: rib-entry.cpp:138
Table::const_iterator iterator
Definition: cs-internal.hpp:41
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:222
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:37
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:238
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
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
uint64_t flags
Definition: route.hpp:81
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:35
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:96
bool hasCapture() const
Definition: rib-entry.cpp:161
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
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:244