All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nfd-fib-entry.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
7 #ifndef NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP
8 #define NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP
9 
10 #include "../encoding/block.hpp"
11 #include "../encoding/encoding-buffer.hpp"
12 #include "../encoding/tlv-nfd.hpp"
13 #include "../name.hpp"
14 
15 namespace ndn {
16 namespace nfd {
17 
18 // NextHopRecord := NEXT-HOP-RECORD TLV-LENGTH
19 // FaceId
20 // Cost
21 
23 {
24 public:
25  class Error : public Tlv::Error
26  {
27  public:
28  Error(const std::string& what) : Tlv::Error(what) {}
29  };
30 
32  : m_faceId(std::numeric_limits<uint64_t>::max())
33  , m_cost(0)
34  {
35  }
36 
37  explicit
38  NextHopRecord(const Block& block)
39  {
40  wireDecode(block);
41  }
42 
43  uint64_t
44  getFaceId() const
45  {
46  return m_faceId;
47  }
48 
50  setFaceId(uint64_t faceId)
51  {
52  m_faceId = faceId;
53  m_wire.reset();
54  return *this;
55  }
56 
57  uint64_t
58  getCost() const
59  {
60  return m_cost;
61  }
62 
64  setCost(uint64_t cost)
65  {
66  m_cost = cost;
67  m_wire.reset();
68  return *this;
69  }
70 
71  template<bool T>
72  size_t
74  {
75  size_t totalLength = 0;
76  totalLength += prependNonNegativeIntegerBlock(block,
78  m_cost);
79 
80  totalLength += prependNonNegativeIntegerBlock(block,
82  m_faceId);
83 
84  totalLength += block.prependVarNumber(totalLength);
85  totalLength += block.prependVarNumber(ndn::tlv::nfd::NextHopRecord);
86  return totalLength;
87  }
88 
89  const Block&
90  wireEncode() const
91  {
92  if (m_wire.hasWire())
93  {
94  return m_wire;
95  }
96 
97  EncodingEstimator estimator;
98  size_t estimatedSize = wireEncode(estimator);
99 
100  EncodingBuffer buffer(estimatedSize, 0);
101  wireEncode(buffer);
102 
103  m_wire = buffer.block();
104  return m_wire;
105  }
106 
107  void
108  wireDecode(const Block& wire)
109  {
110  m_faceId = std::numeric_limits<uint64_t>::max();
111  m_cost = 0;
112 
113  m_wire = wire;
114 
115  if (m_wire.type() != tlv::nfd::NextHopRecord)
116  {
117  std::stringstream error;
118  error << "Requested decoding of NextHopRecord, but Block is of a different type: #"
119  << m_wire.type();
120  throw Error(error.str());
121  }
122  m_wire.parse();
123 
125  if (val == m_wire.elements_end())
126  {
127  throw Error("Unexpected end of NextHopRecord");
128  }
129  else if (val->type() != tlv::nfd::FaceId)
130  {
131  std::stringstream error;
132  error << "Expected FaceId, but Block is of a different type: #"
133  << val->type();
134  throw Error(error.str());
135  }
136  m_faceId = readNonNegativeInteger(*val);
137  ++val;
138 
139  if (val == m_wire.elements_end())
140  {
141  throw Error("Unexpected end of NextHopRecord");
142  }
143  else if (val->type() != tlv::nfd::Cost)
144  {
145  std::stringstream error;
146  error << "Expected Cost, but Block is of a different type: #"
147  << m_wire.type();
148  throw Error(error.str());
149  }
150  m_cost = readNonNegativeInteger(*val);
151  }
152 
153 
154 private:
155  uint64_t m_faceId;
156  uint64_t m_cost;
157 
158  mutable Block m_wire;
159 };
160 
161 
162 // FibEntry := FIB-ENTRY-TYPE TLV-LENGTH
163 // Name
164 // NextHopRecord*
165 
166 class FibEntry
167 {
168 public:
169  class Error : public Tlv::Error
170  {
171  public:
172  Error(const std::string& what) : Tlv::Error(what)
173  {
174  }
175  };
176 
178  {
179  }
180 
181  explicit
182  FibEntry(const Block& block)
183  {
184  wireDecode(block);
185  }
186 
187  const Name&
188  getPrefix() const
189  {
190  return m_prefix;
191  }
192 
193  FibEntry&
194  setPrefix(const Name& prefix)
195  {
196  m_prefix = prefix;
197  m_wire.reset();
198  return *this;
199  }
200 
201  const std::list<NextHopRecord>&
203  {
204  return m_nextHopRecords;
205  }
206 
207  FibEntry&
208  addNextHopRecord(const NextHopRecord& nextHopRecord)
209  {
210  m_nextHopRecords.push_back(nextHopRecord);
211  m_wire.reset();
212  return *this;
213  }
214 
215  template<typename T>
216  FibEntry&
217  setNextHopRecords(const T& begin, const T& end)
218  {
219  m_nextHopRecords.clear();
220  m_nextHopRecords.assign(begin, end);
221  m_wire.reset();
222  return *this;
223  }
224 
225  template<bool T>
226  size_t
228  {
229  size_t totalLength = 0;
230 
231  for (std::list<NextHopRecord>::const_reverse_iterator i = m_nextHopRecords.rbegin();
232  i != m_nextHopRecords.rend();
233  ++i)
234  {
235  totalLength += i->wireEncode(block);
236  }
237 
238  totalLength += m_prefix.wireEncode(block);
239  totalLength += block.prependVarNumber(totalLength);
240  totalLength += block.prependVarNumber(tlv::nfd::FibEntry);
241 
242  return totalLength;
243  }
244 
245  const Block&
246  wireEncode() const
247  {
248  if (m_wire.hasWire())
249  {
250  return m_wire;
251  }
252 
253  EncodingEstimator estimator;
254  size_t estimatedSize = wireEncode(estimator);
255 
256  EncodingBuffer buffer(estimatedSize, 0);
257  wireEncode(buffer);
258 
259  m_wire = buffer.block();
260 
261  return m_wire;
262  }
263 
264  void
265  wireDecode(const Block& wire)
266  {
267  m_prefix.clear();
268  m_nextHopRecords.clear();
269 
270  m_wire = wire;
271 
272  if (m_wire.type() != tlv::nfd::FibEntry)
273  {
274  std::stringstream error;
275  error << "Requested decoding of FibEntry, but Block is of a different type: #"
276  << m_wire.type();
277  throw Error(error.str());
278  }
279 
280  m_wire.parse();
281 
283  if (val == m_wire.elements_end())
284  {
285  throw Error("Unexpected end of FibEntry");
286  }
287  else if (val->type() != Tlv::Name)
288  {
289  std::stringstream error;
290  error << "Expected Name, but Block is of a different type: #"
291  << val->type();
292  throw Error(error.str());
293  }
294  m_prefix.wireDecode(*val);
295  ++val;
296 
297  for (; val != m_wire.elements_end(); ++val)
298  {
299  if (val->type() != tlv::nfd::NextHopRecord)
300  {
301  std::stringstream error;
302  error << "Expected NextHopRecords, but Block is of a different type: #"
303  << val->type();
304  throw Error(error.str());
305  }
306  m_nextHopRecords.push_back(NextHopRecord(*val));
307  }
308  }
309 
310 private:
311  Name m_prefix;
312  std::list<NextHopRecord> m_nextHopRecords;
313 
314  mutable Block m_wire;
315 };
316 
317 } // namespace nfd
318 } // namespace ndn
319 
320 #endif // NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP
FibEntry & setPrefix(const Name &prefix)
void wireDecode(const Block &wire)
const Block & wireEncode() const
Class representing wire element of the NDN packet.
Definition: block.hpp:26
NextHopRecord & setCost(uint64_t cost)
size_t wireEncode(EncodingImpl< T > &block) const
size_t wireEncode(EncodingImpl< T > &block) const
uint64_t readNonNegativeInteger(const Block &block)
NextHopRecord & setFaceId(uint64_t faceId)
element_const_iterator elements_end() const
Definition: block.hpp:476
element_const_iterator elements_begin() const
Definition: block.hpp:470
const Name & getPrefix() const
void wireDecode(const Block &wire)
uint64_t getCost() const
Error(const std::string &what)
const std::list< NextHopRecord > & getNextHopRecords() const
const Block & wireEncode() const
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: name.hpp:711
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
FibEntry & addNextHopRecord(const NextHopRecord &nextHopRecord)
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
size_t prependNonNegativeIntegerBlock(EncodingImpl< P > &encoder, uint32_t type, uint64_t number)
helper methods
FibEntry(const Block &block)
Error(const std::string &what)
uint64_t getFaceId() const
void wireDecode(const Block &wire)
Definition: name.hpp:746
void clear()
Clear all the components.
Definition: name.hpp:211
FibEntry & setNextHopRecords(const T &begin, const T &end)
NextHopRecord(const Block &block)
Class representing wire element of the NDN packet.