blob: ea8ca4d9922f9c80495c4082c9b91cfac177b81f [file] [log] [blame]
Alexander Afanasyev0553cd52014-02-18 02:34:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2014 Regents of the University of California.
4 *
5 * See COPYING for copyright and distribution information.
6 */
7
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
15namespace ndn {
16namespace nrd {
17
18const uint64_t INVALID_FACE_ID = std::numeric_limits<uint64_t>::max();
19const uint64_t DEFAULT_COST = 0;
20const uint64_t DEFAULT_FLAGS = tlv::nrd::NDN_FORW_CHILD_INHERIT;
21
22/**
23 * @brief Abstraction for prefix registration options for NRD Prefix registration protocol
24 *
25 * @see http://redmine.named-data.net/projects/nrd/wiki/NRD_Prefix_Registration_protocol
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070026 * @deprecated use NFD RIB Management
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080027 */
28class PrefixRegOptions {
29public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030 class Error : public Tlv::Error
31 {
32 public:
33 explicit
34 Error(const std::string& what)
35 : Tlv::Error(what)
36 {
37 }
38 };
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080039
40 PrefixRegOptions()
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041 : m_faceId(INVALID_FACE_ID)
42 , m_flags(DEFAULT_FLAGS)
43 , m_cost(DEFAULT_COST)
44 , m_expirationPeriod(time::milliseconds::min())
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080045 {
46 }
47
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048 explicit
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080049 PrefixRegOptions(const Block& block)
50 {
51 wireDecode(block);
52 }
53
54 template<bool T>
55 size_t
56 wireEncode(EncodingImpl<T>& block) const;
Obaid6e7f5f12014-03-11 14:46:10 -050057
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080058 const Block&
59 wireEncode() const;
Obaid6e7f5f12014-03-11 14:46:10 -050060
61 void
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080062 wireDecode(const Block& wire);
Obaid6e7f5f12014-03-11 14:46:10 -050063
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080064 ////////////////////////////////////////////////////////
Obaid6e7f5f12014-03-11 14:46:10 -050065
66 const Name&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080067 getName() const
68 {
69 return m_name;
70 }
Obaid6e7f5f12014-03-11 14:46:10 -050071
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080072 PrefixRegOptions&
73 setName(const Name& name)
74 {
75 m_name = name;
76 m_wire.reset();
77 return *this;
78 }
79
80 //
Obaid6e7f5f12014-03-11 14:46:10 -050081
Alexander Afanasyev87a7f5f2014-02-26 11:40:13 -080082 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080083 getFaceId() const
84 {
85 return m_faceId;
86 }
87
88 PrefixRegOptions&
Alexander Afanasyev87a7f5f2014-02-26 11:40:13 -080089 setFaceId(uint64_t faceId)
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080090 {
91 m_faceId = faceId;
92 m_wire.reset();
93 return *this;
94 }
95
96 //
Obaid6e7f5f12014-03-11 14:46:10 -050097
98 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080099 getFlags() const
100 {
101 return m_flags;
102 }
103
104 PrefixRegOptions&
105 setFlags(uint64_t flags)
106 {
107 m_flags = flags;
108 m_wire.reset();
109 return *this;
110 }
111
112 //
Obaid6e7f5f12014-03-11 14:46:10 -0500113
114 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800115 getCost() const
116 {
117 return m_cost;
118 }
119
120 PrefixRegOptions&
121 setCost(uint64_t cost)
122 {
123 m_cost = cost;
124 m_wire.reset();
125 return *this;
126 }
127
128 //
Obaid6e7f5f12014-03-11 14:46:10 -0500129
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 const time::milliseconds&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800131 getExpirationPeriod() const
132 {
133 return m_expirationPeriod;
134 }
135
136 PrefixRegOptions&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700137 setExpirationPeriod(const time::milliseconds& expirationPeriod)
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800138 {
139 m_expirationPeriod = expirationPeriod;
140 m_wire.reset();
141 return *this;
142 }
143
144 //
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800145
Obaid6e7f5f12014-03-11 14:46:10 -0500146 const std::string&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800147 getProtocol() const
148 {
149 return m_protocol;
150 }
151
152 PrefixRegOptions&
153 setProtocol(const std::string& protocol)
154 {
155 m_protocol = protocol;
156 m_wire.reset();
157 return *this;
158 }
Obaid6e7f5f12014-03-11 14:46:10 -0500159
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800160private:
161 Name m_name;
162 uint64_t m_faceId;
163 uint64_t m_flags;
164 uint64_t m_cost;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700165 time::milliseconds m_expirationPeriod;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800166 std::string m_protocol;
Obaid6e7f5f12014-03-11 14:46:10 -0500167
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800168 mutable Block m_wire;
169};
170
171template<bool T>
172inline size_t
173PrefixRegOptions::wireEncode(EncodingImpl<T>& block) const
174{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700175 size_t totalLength = 0;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800176
177 // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
178 // Name
179 // FaceId?
180 // Flags?
Obaid6e7f5f12014-03-11 14:46:10 -0500181 // Cost?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800182 // ExpirationPeriod?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800183 // Protocol?
184
185 // (reverse encoding)
186
187 // Protocol
188 if (!m_protocol.empty())
189 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190 totalLength += prependByteArrayBlock(block,
191 tlv::nrd::Protocol,
192 reinterpret_cast<const uint8_t*>(m_protocol.c_str()),
193 m_protocol.size());
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800194 }
195
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800196 // ExpirationPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700197 if (m_expirationPeriod > time::milliseconds::zero())
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800198 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 totalLength += prependNonNegativeIntegerBlock(block,
200 tlv::nrd::ExpirationPeriod,
201 m_expirationPeriod.count());
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800202 }
203
204 // Cost
205 if (m_cost != DEFAULT_COST)
206 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800208 }
209
210 // Flags
211 if (m_flags != DEFAULT_FLAGS)
212 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800214 }
215
216 // FaceId
217 if (m_faceId != INVALID_FACE_ID)
218 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800220 }
221
222 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700223 totalLength += m_name.wireEncode(block);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800224
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700225 totalLength += block.prependVarNumber(totalLength);
226 totalLength += block.prependVarNumber(tlv::nrd::PrefixRegOptions);
227 return totalLength;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800228}
229
230inline const Block&
231PrefixRegOptions::wireEncode() const
232{
233 if (m_wire.hasWire())
234 return m_wire;
235
236 EncodingEstimator estimator;
237 size_t estimatedSize = wireEncode(estimator);
Obaid6e7f5f12014-03-11 14:46:10 -0500238
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800239 EncodingBuffer buffer(estimatedSize, 0);
240 wireEncode(buffer);
241
242 m_wire = buffer.block();
243 return m_wire;
244}
Obaid6e7f5f12014-03-11 14:46:10 -0500245
246inline void
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800247PrefixRegOptions::wireDecode(const Block& wire)
248{
249 // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
250 // Name
251 // FaceId?
252 // Flags?
Obaid6e7f5f12014-03-11 14:46:10 -0500253 // Cost?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800254 // ExpirationPeriod?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800255 // Protocol?
256
257 m_name.clear();
258 m_faceId = INVALID_FACE_ID;
259 m_flags = DEFAULT_FLAGS;
260 m_cost = DEFAULT_COST;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700261 m_expirationPeriod = time::milliseconds::min();
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800262 m_protocol.clear();
263
264 m_wire = wire;
265
266 if (m_wire.type() != tlv::nrd::PrefixRegOptions)
267 throw Error("Requested decoding of PrefixRegOptions, but Block is of different type");
Obaid6e7f5f12014-03-11 14:46:10 -0500268
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800269 m_wire.parse ();
270
271 // Name
272 Block::element_const_iterator val = m_wire.find(Tlv::Name);
273 if (val != m_wire.elements_end())
274 {
275 m_name.wireDecode(*val);
276 }
277
278 // FaceId
279 val = m_wire.find(tlv::nrd::FaceId);
280 if (val != m_wire.elements_end())
281 {
282 m_faceId = readNonNegativeInteger(*val);
283 }
Obaid6e7f5f12014-03-11 14:46:10 -0500284
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800285 // Flags
286 val = m_wire.find(tlv::nrd::Flags);
287 if (val != m_wire.elements_end())
288 {
289 m_flags = readNonNegativeInteger(*val);
290 }
291
292 // Cost
293 val = m_wire.find(tlv::nrd::Cost);
294 if (val != m_wire.elements_end())
295 {
296 m_cost = readNonNegativeInteger(*val);
297 }
298
299 // ExpirationPeriod
300 val = m_wire.find(tlv::nrd::ExpirationPeriod);
301 if (val != m_wire.elements_end())
302 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700303 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800304 }
305
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800306 // Protocol
307 val = m_wire.find(tlv::nrd::Protocol);
308 if (val != m_wire.elements_end())
309 {
310 m_protocol = std::string(reinterpret_cast<const char*>(val->value()),
311 val->value_size());
312 }
313}
314
Obaid6e7f5f12014-03-11 14:46:10 -0500315inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700316operator << (std::ostream& os, const PrefixRegOptions& option)
Obaid6e7f5f12014-03-11 14:46:10 -0500317{
318 os << "PrefixRegOptions(";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800319
Obaid6e7f5f12014-03-11 14:46:10 -0500320 // Name
321 os << "Prefix: " << option.getName() << ", ";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800322
Obaid6e7f5f12014-03-11 14:46:10 -0500323 // FaceID
324 os << "FaceID: " << option.getFaceId() << ", ";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800325
Obaid6e7f5f12014-03-11 14:46:10 -0500326 // Flags
327 os << "Flags: " << option.getFlags() << ", ";
328
329 // Cost
330 os << "Cost: " << option.getCost() << ", ";
331
332 // ExpirationPeriod
333 os << "ExpirationPeriod: " << option.getExpirationPeriod() << ", ";
334
335 // Protocol
336 os << "Protocol: " << option.getProtocol();
337
338 os << ")";
339 return os;
340}
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800341
342} // namespace nrd
343} // namespace ndn
344
345#endif // NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP