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 
28 
29 #include <boost/range/adaptor/reversed.hpp>
30 
31 namespace ndn {
32 namespace nfd {
33 
34 BOOST_CONCEPT_ASSERT((StatusDatasetItem<Route>));
35 BOOST_CONCEPT_ASSERT((StatusDatasetItem<RibEntry>));
36 
38  : m_faceId(INVALID_FACE_ID)
39  , m_origin(ROUTE_ORIGIN_APP)
40  , m_cost(0)
41  , m_flags(ROUTE_FLAG_CHILD_INHERIT)
42 {
43 }
44 
45 Route::Route(const Block& block)
46 {
47  this->wireDecode(block);
48 }
49 
50 Route&
51 Route::setFaceId(uint64_t faceId)
52 {
53  m_faceId = faceId;
54  m_wire.reset();
55  return *this;
56 }
57 
58 Route&
60 {
61  m_origin = origin;
62  m_wire.reset();
63  return *this;
64 }
65 
66 Route&
67 Route::setCost(uint64_t cost)
68 {
69  m_cost = cost;
70  m_wire.reset();
71  return *this;
72 }
73 
74 Route&
75 Route::setFlags(uint64_t flags)
76 {
77  m_flags = flags;
78  m_wire.reset();
79  return *this;
80 }
81 
82 Route&
83 Route::setExpirationPeriod(time::milliseconds expirationPeriod)
84 {
85  if (expirationPeriod == time::milliseconds::max())
86  return unsetExpirationPeriod();
87 
88  m_expirationPeriod = expirationPeriod;
89  m_wire.reset();
90  return *this;
91 }
92 
93 Route&
95 {
96  m_expirationPeriod = nullopt;
97  m_wire.reset();
98  return *this;
99 }
100 
101 template<encoding::Tag TAG>
102 size_t
103 Route::wireEncode(EncodingImpl<TAG>& block) const
104 {
105  size_t totalLength = 0;
106 
107  if (m_expirationPeriod) {
109  static_cast<uint64_t>(m_expirationPeriod->count()));
110  }
111  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Flags, m_flags);
112  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost);
113  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Origin, m_origin);
114  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId);
115 
116  totalLength += block.prependVarNumber(totalLength);
117  totalLength += block.prependVarNumber(ndn::tlv::nfd::Route);
118  return totalLength;
119 }
120 
122 
123 const Block&
125 {
126  if (m_wire.hasWire())
127  return m_wire;
128 
129  EncodingEstimator estimator;
130  size_t estimatedSize = wireEncode(estimator);
131 
132  EncodingBuffer buffer(estimatedSize, 0);
133  wireEncode(buffer);
134 
135  m_wire = buffer.block();
136  return m_wire;
137 }
138 
139 void
141 {
142  if (block.type() != tlv::nfd::Route) {
143  NDN_THROW(Error("Route", block.type()));
144  }
145 
146  m_wire = block;
147  m_wire.parse();
148  auto val = m_wire.elements_begin();
149 
150  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
151  m_faceId = readNonNegativeInteger(*val);
152  ++val;
153  }
154  else {
155  NDN_THROW(Error("missing required FaceId field"));
156  }
157 
158  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) {
159  m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
160  ++val;
161  }
162  else {
163  NDN_THROW(Error("missing required Origin field"));
164  }
165 
166  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) {
167  m_cost = readNonNegativeInteger(*val);
168  ++val;
169  }
170  else {
171  NDN_THROW(Error("missing required Cost field"));
172  }
173 
174  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
175  m_flags = readNonNegativeInteger(*val);
176  ++val;
177  }
178  else {
179  NDN_THROW(Error("missing required Flags field"));
180  }
181 
182  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
183  m_expirationPeriod.emplace(readNonNegativeInteger(*val));
184  ++val;
185  }
186  else {
187  m_expirationPeriod = nullopt;
188  }
189 }
190 
191 bool
192 operator==(const Route& a, const Route& b)
193 {
194  return a.getFaceId() == b.getFaceId() &&
195  a.getOrigin() == b.getOrigin() &&
196  a.getCost() == b.getCost() &&
197  a.getFlags() == b.getFlags() &&
199 }
200 
201 std::ostream&
202 operator<<(std::ostream& os, const Route& route)
203 {
204  os << "Route("
205  << "FaceId: " << route.getFaceId() << ", "
206  << "Origin: " << route.getOrigin() << ", "
207  << "Cost: " << route.getCost() << ", "
208  << "Flags: " << AsHex{route.getFlags()} << ", ";
209 
210  if (route.hasExpirationPeriod()) {
211  os << "ExpirationPeriod: " << route.getExpirationPeriod();
212  }
213  else {
214  os << "ExpirationPeriod: infinite";
215  }
216 
217  return os << ")";
218 }
219 
221 
222 RibEntry::RibEntry() = default;
223 
225 {
226  this->wireDecode(block);
227 }
228 
229 RibEntry&
230 RibEntry::setName(const Name& prefix)
231 {
232  m_prefix = prefix;
233  m_wire.reset();
234  return *this;
235 }
236 
237 RibEntry&
239 {
240  m_routes.push_back(route);
241  m_wire.reset();
242  return *this;
243 }
244 
245 RibEntry&
247 {
248  m_routes.clear();
249  m_wire.reset();
250  return *this;
251 }
252 
253 template<encoding::Tag TAG>
254 size_t
255 RibEntry::wireEncode(EncodingImpl<TAG>& block) const
256 {
257  size_t totalLength = 0;
258 
259  for (const auto& route : m_routes | boost::adaptors::reversed) {
260  totalLength += route.wireEncode(block);
261  }
262  totalLength += m_prefix.wireEncode(block);
263 
264  totalLength += block.prependVarNumber(totalLength);
265  totalLength += block.prependVarNumber(tlv::nfd::RibEntry);
266  return totalLength;
267 }
268 
270 
271 const Block&
273 {
274  if (m_wire.hasWire())
275  return m_wire;
276 
277  EncodingEstimator estimator;
278  size_t estimatedSize = wireEncode(estimator);
279 
280  EncodingBuffer buffer(estimatedSize, 0);
281  wireEncode(buffer);
282 
283  m_wire = buffer.block();
284  return m_wire;
285 }
286 
287 void
289 {
290  if (block.type() != tlv::nfd::RibEntry) {
291  NDN_THROW(Error("RibEntry", block.type()));
292  }
293 
294  m_wire = block;
295  m_wire.parse();
296  auto val = m_wire.elements_begin();
297 
298  if (val == m_wire.elements_end()) {
299  NDN_THROW(Error("unexpected end of RibEntry"));
300  }
301  else if (val->type() != tlv::Name) {
302  NDN_THROW(Error("Name", val->type()));
303  }
304  m_prefix.wireDecode(*val);
305  ++val;
306 
307  m_routes.clear();
308  for (; val != m_wire.elements_end(); ++val) {
309  if (val->type() != tlv::nfd::Route) {
310  NDN_THROW(Error("Route", val->type()));
311  }
312  m_routes.emplace_back(*val);
313  }
314 }
315 
316 bool
317 operator==(const RibEntry& a, const RibEntry& b)
318 {
319  const auto& aRoutes = a.getRoutes();
320  const auto& bRoutes = b.getRoutes();
321 
322  if (a.getName() != b.getName() ||
323  aRoutes.size() != bRoutes.size())
324  return false;
325 
326  std::vector<bool> matched(bRoutes.size(), false);
327  return std::all_of(aRoutes.begin(), aRoutes.end(),
328  [&] (const Route& route) {
329  for (size_t i = 0; i < bRoutes.size(); ++i) {
330  if (!matched[i] && bRoutes[i] == route) {
331  matched[i] = true;
332  return true;
333  }
334  }
335  return false;
336  });
337 }
338 
339 std::ostream&
340 operator<<(std::ostream& os, const RibEntry& entry)
341 {
342  os << "RibEntry(Prefix: " << entry.getName() << ",\n"
343  << " Routes: [";
344 
345  std::copy(entry.getRoutes().begin(), entry.getRoutes().end(),
346  make_ostream_joiner(os, ",\n "));
347 
348  os << "]\n";
349 
350  return os << " )";
351 }
352 
353 } // namespace nfd
354 } // namespace ndn
Definition: data.cpp:26
uint64_t getCost() const
Definition: rib-entry.hpp:75
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Route & setFlags(uint64_t flags)
Definition: rib-entry.cpp:75
uint64_t getFaceId() const
Definition: rib-entry.hpp:57
void wireDecode(const Block &block)
Definition: rib-entry.cpp:288
Route & setOrigin(RouteOrigin origin)
Definition: rib-entry.cpp:59
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
represents a route in a RibEntry
Definition: rib-entry.hpp:42
void wireDecode(const Block &block)
Definition: rib-entry.cpp:140
Route & setCost(uint64_t cost)
Definition: rib-entry.cpp:67
bool hasExpirationPeriod() const
Definition: rib-entry.hpp:93
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
const Name & getName() const
Definition: rib-entry.hpp:168
#define NDN_THROW(e)
Definition: exception.hpp:61
const Block & wireEncode() const
Definition: rib-entry.cpp:272
time::milliseconds getExpirationPeriod() const
Definition: rib-entry.hpp:99
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:249
const std::vector< Route > & getRoutes() const
Definition: rib-entry.hpp:177
uint64_t getFlags() const
Definition: rib-entry.hpp:84
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
RibEntry & addRoute(const Route &route)
Definition: rib-entry.cpp:238
size_t size() const
Get number of components.
Definition: name.hpp:147
const Block & wireEncode() const
Definition: rib-entry.cpp:124
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
Represents an absolute name.
Definition: name.hpp:43
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
Route & unsetExpirationPeriod()
Definition: rib-entry.cpp:94
Route & setFaceId(uint64_t faceId)
Definition: rib-entry.cpp:51
RouteOrigin getOrigin() const
Definition: rib-entry.hpp:66
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
Route & setExpirationPeriod(time::milliseconds expirationPeriod)
Definition: rib-entry.cpp:83
RibEntry & setName(const Name &prefix)
Definition: rib-entry.cpp:230
represents an item in NFD RIB dataset
Definition: rib-entry.hpp:153
EncodingImpl< EncoderTag > EncodingBuffer
const nullopt_t nullopt((nullopt_t::init()))
const uint64_t INVALID_FACE_ID
EncodingImpl< EstimatorTag > EncodingEstimator
RibEntry & clearRoutes()
Definition: rib-entry.cpp:246