All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nrd-prefix-reg-options.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #ifndef NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP
9 #define NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP
10 
11 #include "../encoding/encoding-buffer.hpp"
12 #include "../encoding/tlv-nrd.hpp"
13 #include "../name.hpp"
14 
15 namespace ndn {
16 namespace nrd {
17 
18 const uint64_t INVALID_FACE_ID = std::numeric_limits<uint64_t>::max();
19 const uint64_t DEFAULT_COST = 0;
21 
29 {
30 public:
31  class Error : public Tlv::Error
32  {
33  public:
34  explicit
35  Error(const std::string& what)
36  : Tlv::Error(what)
37  {
38  }
39  };
40 
42  : m_faceId(INVALID_FACE_ID)
43  , m_flags(DEFAULT_FLAGS)
44  , m_cost(DEFAULT_COST)
45  , m_expirationPeriod(time::milliseconds::min())
46  {
47  }
48 
49  explicit
50  PrefixRegOptions(const Block& block)
51  {
52  wireDecode(block);
53  }
54 
55  template<bool T>
56  size_t
57  wireEncode(EncodingImpl<T>& block) const;
58 
59  const Block&
60  wireEncode() const;
61 
62  void
63  wireDecode(const Block& wire);
64 
66 
67  const Name&
68  getName() const
69  {
70  return m_name;
71  }
72 
74  setName(const Name& name)
75  {
76  m_name = name;
77  m_wire.reset();
78  return *this;
79  }
80 
81  //
82 
83  uint64_t
84  getFaceId() const
85  {
86  return m_faceId;
87  }
88 
90  setFaceId(uint64_t faceId)
91  {
92  m_faceId = faceId;
93  m_wire.reset();
94  return *this;
95  }
96 
97  //
98 
99  uint64_t
100  getFlags() const
101  {
102  return m_flags;
103  }
104 
106  setFlags(uint64_t flags)
107  {
108  m_flags = flags;
109  m_wire.reset();
110  return *this;
111  }
112 
113  //
114 
115  uint64_t
116  getCost() const
117  {
118  return m_cost;
119  }
120 
122  setCost(uint64_t cost)
123  {
124  m_cost = cost;
125  m_wire.reset();
126  return *this;
127  }
128 
129  //
130 
131  const time::milliseconds&
133  {
134  return m_expirationPeriod;
135  }
136 
138  setExpirationPeriod(const time::milliseconds& expirationPeriod)
139  {
140  m_expirationPeriod = expirationPeriod;
141  m_wire.reset();
142  return *this;
143  }
144 
145  //
146 
147  const std::string&
148  getProtocol() const
149  {
150  return m_protocol;
151  }
152 
154  setProtocol(const std::string& protocol)
155  {
156  m_protocol = protocol;
157  m_wire.reset();
158  return *this;
159  }
160 
161 private:
162  Name m_name;
163  uint64_t m_faceId;
164  uint64_t m_flags;
165  uint64_t m_cost;
166  time::milliseconds m_expirationPeriod;
167  std::string m_protocol;
168 
169  mutable Block m_wire;
170 };
171 
172 template<bool T>
173 inline size_t
175 {
176  size_t totalLength = 0;
177 
178  // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
179  // Name
180  // FaceId?
181  // Flags?
182  // Cost?
183  // ExpirationPeriod?
184  // Protocol?
185 
186  // (reverse encoding)
187 
188  // Protocol
189  if (!m_protocol.empty())
190  {
191  totalLength += prependByteArrayBlock(block,
193  reinterpret_cast<const uint8_t*>(m_protocol.c_str()),
194  m_protocol.size());
195  }
196 
197  // ExpirationPeriod
198  if (m_expirationPeriod > time::milliseconds::zero())
199  {
200  totalLength += prependNonNegativeIntegerBlock(block,
202  m_expirationPeriod.count());
203  }
204 
205  // Cost
206  if (m_cost != DEFAULT_COST)
207  {
208  totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost);
209  }
210 
211  // Flags
212  if (m_flags != DEFAULT_FLAGS)
213  {
214  totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags);
215  }
216 
217  // FaceId
218  if (m_faceId != INVALID_FACE_ID)
219  {
220  totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId);
221  }
222 
223  // Name
224  totalLength += m_name.wireEncode(block);
225 
226  totalLength += block.prependVarNumber(totalLength);
227  totalLength += block.prependVarNumber(tlv::nrd::PrefixRegOptions);
228  return totalLength;
229 }
230 
231 inline const Block&
233 {
234  if (m_wire.hasWire())
235  return m_wire;
236 
237  EncodingEstimator estimator;
238  size_t estimatedSize = wireEncode(estimator);
239 
240  EncodingBuffer buffer(estimatedSize, 0);
241  wireEncode(buffer);
242 
243  m_wire = buffer.block();
244  return m_wire;
245 }
246 
247 inline void
249 {
250  // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
251  // Name
252  // FaceId?
253  // Flags?
254  // Cost?
255  // ExpirationPeriod?
256  // Protocol?
257 
258  m_name.clear();
259  m_faceId = INVALID_FACE_ID;
260  m_flags = DEFAULT_FLAGS;
261  m_cost = DEFAULT_COST;
262  m_expirationPeriod = time::milliseconds::min();
263  m_protocol.clear();
264 
265  m_wire = wire;
266 
267  if (m_wire.type() != tlv::nrd::PrefixRegOptions)
268  throw Error("Requested decoding of PrefixRegOptions, but Block is of different type");
269 
270  m_wire.parse ();
271 
272  // Name
274  if (val != m_wire.elements_end())
275  {
276  m_name.wireDecode(*val);
277  }
278 
279  // FaceId
280  val = m_wire.find(tlv::nrd::FaceId);
281  if (val != m_wire.elements_end())
282  {
283  m_faceId = readNonNegativeInteger(*val);
284  }
285 
286  // Flags
287  val = m_wire.find(tlv::nrd::Flags);
288  if (val != m_wire.elements_end())
289  {
290  m_flags = readNonNegativeInteger(*val);
291  }
292 
293  // Cost
294  val = m_wire.find(tlv::nrd::Cost);
295  if (val != m_wire.elements_end())
296  {
297  m_cost = readNonNegativeInteger(*val);
298  }
299 
300  // ExpirationPeriod
301  val = m_wire.find(tlv::nrd::ExpirationPeriod);
302  if (val != m_wire.elements_end())
303  {
304  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
305  }
306 
307  // Protocol
308  val = m_wire.find(tlv::nrd::Protocol);
309  if (val != m_wire.elements_end())
310  {
311  m_protocol = std::string(reinterpret_cast<const char*>(val->value()),
312  val->value_size());
313  }
314 }
315 
316 inline std::ostream&
317 operator << (std::ostream& os, const PrefixRegOptions& option)
318 {
319  os << "PrefixRegOptions(";
320 
321  // Name
322  os << "Prefix: " << option.getName() << ", ";
323 
324  // FaceID
325  os << "FaceID: " << option.getFaceId() << ", ";
326 
327  // Flags
328  os << "Flags: " << option.getFlags() << ", ";
329 
330  // Cost
331  os << "Cost: " << option.getCost() << ", ";
332 
333  // ExpirationPeriod
334  os << "ExpirationPeriod: " << option.getExpirationPeriod() << ", ";
335 
336  // Protocol
337  os << "Protocol: " << option.getProtocol();
338 
339  os << ")";
340  return os;
341 }
342 
343 } // namespace nrd
344 } // namespace ndn
345 
346 #endif // NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP
PrefixRegOptions & setFaceId(uint64_t faceId)
PrefixRegOptions & setCost(uint64_t cost)
const uint64_t DEFAULT_FLAGS
element_const_iterator find(uint32_t type) const
Definition: block.hpp:343
std::ostream & operator<<(std::ostream &os, const PrefixRegOptions &option)
const std::string & getProtocol() const
Class representing wire element of the NDN packet.
Definition: block.hpp:26
void wireDecode(const Block &wire)
uint64_t readNonNegativeInteger(const Block &block)
const uint64_t DEFAULT_COST
PrefixRegOptions & setExpirationPeriod(const time::milliseconds &expirationPeriod)
element_const_iterator elements_end() const
Definition: block.hpp:476
const uint64_t INVALID_FACE_ID
PrefixRegOptions & setName(const Name &name)
PrefixRegOptions & setFlags(uint64_t flags)
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
PrefixRegOptions & setProtocol(const std::string &protocol)
const time::milliseconds & getExpirationPeriod() const
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
Abstraction for prefix registration options for NRD Prefix registration protocol. ...
size_t prependNonNegativeIntegerBlock(EncodingImpl< P > &encoder, uint32_t type, uint64_t number)
helper methods
void wireDecode(const Block &wire)
Definition: name.hpp:746
void clear()
Clear all the components.
Definition: name.hpp:211
size_t prependByteArrayBlock(EncodingImpl< P > &encoder, uint32_t type, const uint8_t *array, size_t arraySize)
Class representing wire element of the NDN packet.