rib-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
29 
30 #include <boost/range/adaptor/reversed.hpp>
31 
32 namespace ndn {
33 namespace nfd {
34 
35 BOOST_CONCEPT_ASSERT((StatusDatasetItem<Route>));
36 BOOST_CONCEPT_ASSERT((StatusDatasetItem<RibEntry>));
37 
39  : m_faceId(INVALID_FACE_ID)
40  , m_origin(ROUTE_ORIGIN_APP)
41  , m_cost(0)
42  , m_flags(ROUTE_FLAG_CHILD_INHERIT)
43 {
44 }
45 
46 Route::Route(const Block& block)
47 {
48  this->wireDecode(block);
49 }
50 
51 Route&
52 Route::setFaceId(uint64_t faceId)
53 {
54  m_faceId = faceId;
55  m_wire.reset();
56  return *this;
57 }
58 
59 Route&
61 {
62  m_origin = origin;
63  m_wire.reset();
64  return *this;
65 }
66 
67 Route&
68 Route::setCost(uint64_t cost)
69 {
70  m_cost = cost;
71  m_wire.reset();
72  return *this;
73 }
74 
75 Route&
76 Route::setFlags(uint64_t flags)
77 {
78  m_flags = flags;
79  m_wire.reset();
80  return *this;
81 }
82 
83 Route&
85 {
86  if (expirationPeriod == time::milliseconds::max())
87  return unsetExpirationPeriod();
88 
89  m_expirationPeriod = expirationPeriod;
90  m_wire.reset();
91  return *this;
92 }
93 
94 Route&
96 {
97  m_expirationPeriod = nullopt;
98  m_wire.reset();
99  return *this;
100 }
101 
102 template<encoding::Tag TAG>
103 size_t
105 {
106  size_t totalLength = 0;
107 
108  if (m_expirationPeriod) {
110  static_cast<uint64_t>(m_expirationPeriod->count()));
111  }
112  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Flags, m_flags);
113  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost);
114  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Origin, m_origin);
115  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId);
116 
117  totalLength += block.prependVarNumber(totalLength);
118  totalLength += block.prependVarNumber(ndn::tlv::nfd::Route);
119  return totalLength;
120 }
121 
123 
124 const Block&
126 {
127  if (m_wire.hasWire())
128  return m_wire;
129 
130  EncodingEstimator estimator;
131  size_t estimatedSize = wireEncode(estimator);
132 
133  EncodingBuffer buffer(estimatedSize, 0);
134  wireEncode(buffer);
135 
136  m_wire = buffer.block();
137  return m_wire;
138 }
139 
140 void
142 {
143  if (block.type() != tlv::nfd::Route) {
144  NDN_THROW(Error("Route", block.type()));
145  }
146 
147  m_wire = block;
148  m_wire.parse();
149  auto val = m_wire.elements_begin();
150 
151  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
152  m_faceId = readNonNegativeInteger(*val);
153  ++val;
154  }
155  else {
156  NDN_THROW(Error("missing required FaceId field"));
157  }
158 
159  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) {
160  m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
161  ++val;
162  }
163  else {
164  NDN_THROW(Error("missing required Origin field"));
165  }
166 
167  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) {
168  m_cost = readNonNegativeInteger(*val);
169  ++val;
170  }
171  else {
172  NDN_THROW(Error("missing required Cost field"));
173  }
174 
175  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
176  m_flags = readNonNegativeInteger(*val);
177  ++val;
178  }
179  else {
180  NDN_THROW(Error("missing required Flags field"));
181  }
182 
183  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
184  m_expirationPeriod.emplace(readNonNegativeInteger(*val));
185  ++val;
186  }
187  else {
188  m_expirationPeriod = nullopt;
189  }
190 }
191 
192 bool
193 operator==(const Route& a, const Route& b)
194 {
195  return a.getFaceId() == b.getFaceId() &&
196  a.getOrigin() == b.getOrigin() &&
197  a.getCost() == b.getCost() &&
198  a.getFlags() == b.getFlags() &&
200 }
201 
202 std::ostream&
203 operator<<(std::ostream& os, const Route& route)
204 {
205  os << "Route("
206  << "FaceId: " << route.getFaceId() << ", "
207  << "Origin: " << route.getOrigin() << ", "
208  << "Cost: " << route.getCost() << ", "
209  << "Flags: " << AsHex{route.getFlags()} << ", ";
210 
211  if (route.hasExpirationPeriod()) {
212  os << "ExpirationPeriod: " << route.getExpirationPeriod();
213  }
214  else {
215  os << "ExpirationPeriod: infinite";
216  }
217 
218  return os << ")";
219 }
220 
222 
223 RibEntry::RibEntry() = default;
224 
226 {
227  this->wireDecode(block);
228 }
229 
230 RibEntry&
231 RibEntry::setName(const Name& prefix)
232 {
233  m_prefix = prefix;
234  m_wire.reset();
235  return *this;
236 }
237 
238 RibEntry&
240 {
241  m_routes.push_back(route);
242  m_wire.reset();
243  return *this;
244 }
245 
246 RibEntry&
248 {
249  m_routes.clear();
250  m_wire.reset();
251  return *this;
252 }
253 
254 template<encoding::Tag TAG>
255 size_t
257 {
258  size_t totalLength = 0;
259 
260  for (const auto& route : m_routes | boost::adaptors::reversed) {
261  totalLength += route.wireEncode(block);
262  }
263  totalLength += m_prefix.wireEncode(block);
264 
265  totalLength += block.prependVarNumber(totalLength);
266  totalLength += block.prependVarNumber(tlv::nfd::RibEntry);
267  return totalLength;
268 }
269 
271 
272 const Block&
274 {
275  if (m_wire.hasWire())
276  return m_wire;
277 
278  EncodingEstimator estimator;
279  size_t estimatedSize = wireEncode(estimator);
280 
281  EncodingBuffer buffer(estimatedSize, 0);
282  wireEncode(buffer);
283 
284  m_wire = buffer.block();
285  return m_wire;
286 }
287 
288 void
290 {
291  if (block.type() != tlv::nfd::RibEntry) {
292  NDN_THROW(Error("RibEntry", block.type()));
293  }
294 
295  m_wire = block;
296  m_wire.parse();
297  auto val = m_wire.elements_begin();
298 
299  if (val == m_wire.elements_end()) {
300  NDN_THROW(Error("unexpected end of RibEntry"));
301  }
302  else if (val->type() != tlv::Name) {
303  NDN_THROW(Error("Name", val->type()));
304  }
305  m_prefix.wireDecode(*val);
306  ++val;
307 
308  m_routes.clear();
309  for (; val != m_wire.elements_end(); ++val) {
310  if (val->type() != tlv::nfd::Route) {
311  NDN_THROW(Error("Route", val->type()));
312  }
313  m_routes.emplace_back(*val);
314  }
315 }
316 
317 bool
318 operator==(const RibEntry& a, const RibEntry& b)
319 {
320  const auto& aRoutes = a.getRoutes();
321  const auto& bRoutes = b.getRoutes();
322 
323  if (a.getName() != b.getName() ||
324  aRoutes.size() != bRoutes.size())
325  return false;
326 
327  std::vector<bool> matched(bRoutes.size(), false);
328  return std::all_of(aRoutes.begin(), aRoutes.end(),
329  [&] (const Route& route) {
330  for (size_t i = 0; i < bRoutes.size(); ++i) {
331  if (!matched[i] && bRoutes[i] == route) {
332  matched[i] = true;
333  return true;
334  }
335  }
336  return false;
337  });
338 }
339 
340 std::ostream&
341 operator<<(std::ostream& os, const RibEntry& entry)
342 {
343  os << "RibEntry(Prefix: " << entry.getName() << ",\n"
344  << " Routes: [";
345 
346  std::copy(entry.getRoutes().begin(), entry.getRoutes().end(),
347  make_ostream_joiner(os, ",\n "));
348 
349  os << "]\n";
350 
351  return os << " )";
352 }
353 
354 } // namespace nfd
355 } // namespace ndn
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:418
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:410
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:265
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
Represents an absolute name.
Definition: name.hpp:46
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
size_t size() const
Returns the number of components.
Definition: name.hpp:155
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
represents an item in NFD RIB dataset
Definition: rib-entry.hpp:154
const Name & getName() const
Definition: rib-entry.hpp:168
RibEntry & addRoute(const Route &route)
Definition: rib-entry.cpp:239
RibEntry & clearRoutes()
Definition: rib-entry.cpp:247
void wireDecode(const Block &block)
Definition: rib-entry.cpp:289
const std::vector< Route > & getRoutes() const
Definition: rib-entry.hpp:177
const Block & wireEncode() const
Definition: rib-entry.cpp:273
RibEntry & setName(const Name &prefix)
Definition: rib-entry.cpp:231
represents a route in a RibEntry
Definition: rib-entry.hpp:43
Route & unsetExpirationPeriod()
Definition: rib-entry.cpp:95
Route & setFlags(uint64_t flags)
Definition: rib-entry.cpp:76
bool hasExpirationPeriod() const
Definition: rib-entry.hpp:93
RouteOrigin getOrigin() const
Definition: rib-entry.hpp:66
Route & setCost(uint64_t cost)
Definition: rib-entry.cpp:68
Route & setOrigin(RouteOrigin origin)
Definition: rib-entry.cpp:60
uint64_t getFaceId() const
Definition: rib-entry.hpp:57
Route & setFaceId(uint64_t faceId)
Definition: rib-entry.cpp:52
const Block & wireEncode() const
Definition: rib-entry.cpp:125
uint64_t getFlags() const
Definition: rib-entry.hpp:84
uint64_t getCost() const
Definition: rib-entry.hpp:75
Route & setExpirationPeriod(time::milliseconds expirationPeriod)
Definition: rib-entry.cpp:84
time::milliseconds getExpirationPeriod() const
Definition: rib-entry.hpp:99
void wireDecode(const Block &block)
Definition: rib-entry.cpp:141
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
@ ROUTE_FLAG_CHILD_INHERIT
EncodingImpl< EstimatorTag > EncodingEstimator
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
EncodingImpl< EncoderTag > EncodingBuffer
const uint64_t INVALID_FACE_ID
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ ExpirationPeriod
Definition: tlv-nfd.hpp:45
@ Name
Definition: tlv.hpp:67
Definition: data.cpp:25
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
Backport of ostream_joiner from the Library Fundamentals v2 TS.