NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nfd-rib-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "nfd-rib-entry.hpp"
23 #include "encoding/tlv-nfd.hpp"
25 #include "util/concepts.hpp"
26 
27 namespace ndn {
28 namespace nfd {
29 
30 //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Route>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<Route>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<Route>));
33 static_assert(std::is_base_of<tlv::Error, Route::Error>::value,
34  "Route::Error must inherit from tlv::Error");
35 
36 //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<RibEntry>));
37 BOOST_CONCEPT_ASSERT((WireEncodable<RibEntry>));
38 BOOST_CONCEPT_ASSERT((WireDecodable<RibEntry>));
39 static_assert(std::is_base_of<tlv::Error, RibEntry::Error>::value,
40  "RibEntry::Error must inherit from tlv::Error");
41 
42 const time::milliseconds Route::INFINITE_EXPIRATION_PERIOD(time::milliseconds::max());
43 
45  : m_faceId(0)
46  , m_origin(0)
47  , m_cost(0)
48  , m_flags(ROUTE_FLAG_CHILD_INHERIT)
49  , m_expirationPeriod(INFINITE_EXPIRATION_PERIOD)
50  , m_hasInfiniteExpirationPeriod(true)
51 {
52 }
53 
54 Route::Route(const Block& block)
55 {
56  wireDecode(block);
57 }
58 
59 template<encoding::Tag TAG>
60 size_t
61 Route::wireEncode(EncodingImpl<TAG>& block) const
62 {
63  size_t totalLength = 0;
64 
65  // Absence of an ExpirationPeriod signifies non-expiration
66  if (!m_hasInfiniteExpirationPeriod) {
67  totalLength += prependNonNegativeIntegerBlock(block,
69  m_expirationPeriod.count());
70  }
71 
72  totalLength += prependNonNegativeIntegerBlock(block,
74  m_flags);
75 
76  totalLength += prependNonNegativeIntegerBlock(block,
78  m_cost);
79 
80  totalLength += prependNonNegativeIntegerBlock(block,
82  m_origin);
83 
84  totalLength += prependNonNegativeIntegerBlock(block,
86  m_faceId);
87 
88  totalLength += block.prependVarNumber(totalLength);
89  totalLength += block.prependVarNumber(ndn::tlv::nfd::Route);
90 
91  return totalLength;
92 }
93 
94 template size_t
95 Route::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
96 
97 template size_t
98 Route::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
99 
100 const Block&
102 {
103  if (m_wire.hasWire()) {
104  return m_wire;
105  }
106 
107  EncodingEstimator estimator;
108  size_t estimatedSize = wireEncode(estimator);
109 
110  EncodingBuffer buffer(estimatedSize, 0);
111  wireEncode(buffer);
112 
113  m_wire = buffer.block();
114 
115  return m_wire;
116 }
117 
118 void
120 {
121  m_faceId = 0;
122  m_origin = 0;
123  m_cost = 0;
124  m_flags = 0;
125  m_expirationPeriod = time::milliseconds::min();
126 
127  m_wire = wire;
128 
129  if (m_wire.type() != tlv::nfd::Route) {
130  std::stringstream error;
131  error << "Expected Route Block, but Block is of a different type: #"
132  << m_wire.type();
133  BOOST_THROW_EXCEPTION(Error(error.str()));
134  }
135 
136  m_wire.parse();
137 
139 
140  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
141  m_faceId = readNonNegativeInteger(*val);
142  ++val;
143  }
144  else {
145  BOOST_THROW_EXCEPTION(Error("Missing required FaceId field"));
146  }
147 
148  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) {
149  m_origin = readNonNegativeInteger(*val);
150  ++val;
151  }
152  else {
153  BOOST_THROW_EXCEPTION(Error("Missing required Origin field"));
154  }
155 
156  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) {
157  m_cost = readNonNegativeInteger(*val);
158  ++val;
159  }
160  else {
161  BOOST_THROW_EXCEPTION(Error("Missing required Cost field"));
162  }
163 
164  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
165  m_flags = readNonNegativeInteger(*val);
166  ++val;
167  }
168  else {
169  BOOST_THROW_EXCEPTION(Error("Missing required Flags field"));
170  }
171 
172  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
173  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
174  m_hasInfiniteExpirationPeriod = false;
175  }
176  else {
177  m_expirationPeriod = INFINITE_EXPIRATION_PERIOD;
178  m_hasInfiniteExpirationPeriod = true;
179  }
180 }
181 
182 std::ostream&
183 operator<<(std::ostream& os, const Route& route)
184 {
185  os << "Route("
186  << "FaceId: " << route.getFaceId() << ", "
187  << "Origin: " << route.getOrigin() << ", "
188  << "Cost: " << route.getCost() << ", "
189  << "Flags: " << route.getFlags() << ", ";
190 
191  if (!route.hasInfiniteExpirationPeriod()) {
192  os << "ExpirationPeriod: " << route.getExpirationPeriod();
193  }
194  else {
195  os << "ExpirationPeriod: Infinity";
196  }
197 
198  os << ")";
199 
200  return os;
201 }
202 
203 
207 
208 
210 {
211 }
212 
214 {
215  wireDecode(block);
216 }
217 
218 
219 template<encoding::Tag TAG>
220 size_t
221 RibEntry::wireEncode(EncodingImpl<TAG>& block) const
222 {
223  size_t totalLength = 0;
224 
225  for (std::list<Route>::const_reverse_iterator it = m_routes.rbegin();
226  it != m_routes.rend(); ++it)
227  {
228  totalLength += it->wireEncode(block);
229  }
230 
231  totalLength += m_prefix.wireEncode(block);
232 
233  totalLength += block.prependVarNumber(totalLength);
234  totalLength += block.prependVarNumber(tlv::nfd::RibEntry);
235 
236  return totalLength;
237 }
238 
239 template size_t
240 RibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
241 
242 template size_t
243 RibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
244 
245 const Block&
247 {
248  if (m_wire.hasWire()) {
249  return m_wire;
250  }
251 
252  EncodingEstimator estimator;
253  size_t estimatedSize = wireEncode(estimator);
254 
255  EncodingBuffer buffer(estimatedSize, 0);
256  wireEncode(buffer);
257 
258  m_wire = buffer.block();
259 
260  return m_wire;
261 }
262 
263 void
265 {
266  m_prefix.clear();
267  m_routes.clear();
268 
269  m_wire = wire;
270 
271  if (m_wire.type() != tlv::nfd::RibEntry) {
272  std::stringstream error;
273  error << "Expected RibEntry Block, but Block is of a different type: #"
274  << m_wire.type();
275  BOOST_THROW_EXCEPTION(Error(error.str()));
276  }
277 
278  m_wire.parse();
279 
281 
282  if (val != m_wire.elements_end() && val->type() == tlv::Name) {
283  m_prefix.wireDecode(*val);
284  ++val;
285  }
286  else {
287  BOOST_THROW_EXCEPTION(Error("Missing required Name field"));
288  }
289 
290  for (; val != m_wire.elements_end(); ++val) {
291 
292  if (val->type() == tlv::nfd::Route) {
293  m_routes.push_back(Route(*val));
294  }
295  else {
296  std::stringstream error;
297  error << "Expected Route Block, but Block is of a different type: #"
298  << m_wire.type();
299  BOOST_THROW_EXCEPTION(Error(error.str()));
300  }
301  }
302 }
303 
304 std::ostream&
305 operator<<(std::ostream& os, const RibEntry& entry)
306 {
307  os << "RibEntry{\n"
308  << " Name: " << entry.getName() << "\n";
309 
310  for (RibEntry::iterator it = entry.begin(); it != entry.end(); ++it) {
311  os << " " << *it << "\n";
312  }
313 
314  os << "}";
315 
316  return os;
317 }
318 
319 } // namespace nfd
320 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
uint64_t getCost() const
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
RouteList::const_iterator iterator
EncodingImpl< EstimatorTag > EncodingEstimator
uint64_t getFaceId() const
uint64_t getOrigin() const
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
Data abstraction for Route.
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
const Name & getName() const
iterator end() const
element_const_iterator elements_end() const
Definition: block.cpp:595
static const time::milliseconds INFINITE_EXPIRATION_PERIOD
element_const_iterator elements_begin() const
Definition: block.cpp:589
const Block & wireEncode() const
const time::milliseconds & getExpirationPeriod() const
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
uint64_t getFlags() const
bool hasInfiniteExpirationPeriod() const
void wireDecode(const Block &wire)
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
const Block & wireEncode() const
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
iterator begin() const
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:346
void wireDecode(const Block &wire)
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
Data abstraction for RIB entry.