blob: 8f2a0e537e1ad32272b67da1a5a90839dc00920a [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 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070028class PrefixRegOptions
29{
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080030public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 class Error : public Tlv::Error
32 {
33 public:
34 explicit
35 Error(const std::string& what)
36 : Tlv::Error(what)
37 {
38 }
39 };
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080040
41 PrefixRegOptions()
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 : m_faceId(INVALID_FACE_ID)
43 , m_flags(DEFAULT_FLAGS)
44 , m_cost(DEFAULT_COST)
45 , m_expirationPeriod(time::milliseconds::min())
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080046 {
47 }
48
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 explicit
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080050 PrefixRegOptions(const Block& block)
51 {
52 wireDecode(block);
53 }
54
55 template<bool T>
56 size_t
57 wireEncode(EncodingImpl<T>& block) const;
Obaid6e7f5f12014-03-11 14:46:10 -050058
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080059 const Block&
60 wireEncode() const;
Obaid6e7f5f12014-03-11 14:46:10 -050061
62 void
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080063 wireDecode(const Block& wire);
Obaid6e7f5f12014-03-11 14:46:10 -050064
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080065 ////////////////////////////////////////////////////////
Obaid6e7f5f12014-03-11 14:46:10 -050066
67 const Name&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080068 getName() const
69 {
70 return m_name;
71 }
Obaid6e7f5f12014-03-11 14:46:10 -050072
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080073 PrefixRegOptions&
74 setName(const Name& name)
75 {
76 m_name = name;
77 m_wire.reset();
78 return *this;
79 }
80
81 //
Obaid6e7f5f12014-03-11 14:46:10 -050082
Alexander Afanasyev87a7f5f2014-02-26 11:40:13 -080083 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080084 getFaceId() const
85 {
86 return m_faceId;
87 }
88
89 PrefixRegOptions&
Alexander Afanasyev87a7f5f2014-02-26 11:40:13 -080090 setFaceId(uint64_t faceId)
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080091 {
92 m_faceId = faceId;
93 m_wire.reset();
94 return *this;
95 }
96
97 //
Obaid6e7f5f12014-03-11 14:46:10 -050098
99 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800100 getFlags() const
101 {
102 return m_flags;
103 }
104
105 PrefixRegOptions&
106 setFlags(uint64_t flags)
107 {
108 m_flags = flags;
109 m_wire.reset();
110 return *this;
111 }
112
113 //
Obaid6e7f5f12014-03-11 14:46:10 -0500114
115 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800116 getCost() const
117 {
118 return m_cost;
119 }
120
121 PrefixRegOptions&
122 setCost(uint64_t cost)
123 {
124 m_cost = cost;
125 m_wire.reset();
126 return *this;
127 }
128
129 //
Obaid6e7f5f12014-03-11 14:46:10 -0500130
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700131 const time::milliseconds&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800132 getExpirationPeriod() const
133 {
134 return m_expirationPeriod;
135 }
136
137 PrefixRegOptions&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138 setExpirationPeriod(const time::milliseconds& expirationPeriod)
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800139 {
140 m_expirationPeriod = expirationPeriod;
141 m_wire.reset();
142 return *this;
143 }
144
145 //
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800146
Obaid6e7f5f12014-03-11 14:46:10 -0500147 const std::string&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800148 getProtocol() const
149 {
150 return m_protocol;
151 }
152
153 PrefixRegOptions&
154 setProtocol(const std::string& protocol)
155 {
156 m_protocol = protocol;
157 m_wire.reset();
158 return *this;
159 }
Obaid6e7f5f12014-03-11 14:46:10 -0500160
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800161private:
162 Name m_name;
163 uint64_t m_faceId;
164 uint64_t m_flags;
165 uint64_t m_cost;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700166 time::milliseconds m_expirationPeriod;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800167 std::string m_protocol;
Obaid6e7f5f12014-03-11 14:46:10 -0500168
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800169 mutable Block m_wire;
170};
171
172template<bool T>
173inline size_t
174PrefixRegOptions::wireEncode(EncodingImpl<T>& block) const
175{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700176 size_t totalLength = 0;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800177
178 // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
179 // Name
180 // FaceId?
181 // Flags?
Obaid6e7f5f12014-03-11 14:46:10 -0500182 // Cost?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800183 // ExpirationPeriod?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800184 // Protocol?
185
186 // (reverse encoding)
187
188 // Protocol
189 if (!m_protocol.empty())
190 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191 totalLength += prependByteArrayBlock(block,
192 tlv::nrd::Protocol,
193 reinterpret_cast<const uint8_t*>(m_protocol.c_str()),
194 m_protocol.size());
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800195 }
196
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800197 // ExpirationPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700198 if (m_expirationPeriod > time::milliseconds::zero())
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800199 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700200 totalLength += prependNonNegativeIntegerBlock(block,
201 tlv::nrd::ExpirationPeriod,
202 m_expirationPeriod.count());
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800203 }
204
205 // Cost
206 if (m_cost != DEFAULT_COST)
207 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700208 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800209 }
210
211 // Flags
212 if (m_flags != DEFAULT_FLAGS)
213 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700214 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800215 }
216
217 // FaceId
218 if (m_faceId != INVALID_FACE_ID)
219 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800221 }
222
223 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700224 totalLength += m_name.wireEncode(block);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800225
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226 totalLength += block.prependVarNumber(totalLength);
227 totalLength += block.prependVarNumber(tlv::nrd::PrefixRegOptions);
228 return totalLength;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800229}
230
231inline const Block&
232PrefixRegOptions::wireEncode() const
233{
234 if (m_wire.hasWire())
235 return m_wire;
236
237 EncodingEstimator estimator;
238 size_t estimatedSize = wireEncode(estimator);
Obaid6e7f5f12014-03-11 14:46:10 -0500239
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800240 EncodingBuffer buffer(estimatedSize, 0);
241 wireEncode(buffer);
242
243 m_wire = buffer.block();
244 return m_wire;
245}
Obaid6e7f5f12014-03-11 14:46:10 -0500246
247inline void
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800248PrefixRegOptions::wireDecode(const Block& wire)
249{
250 // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
251 // Name
252 // FaceId?
253 // Flags?
Obaid6e7f5f12014-03-11 14:46:10 -0500254 // Cost?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800255 // ExpirationPeriod?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800256 // Protocol?
257
258 m_name.clear();
259 m_faceId = INVALID_FACE_ID;
260 m_flags = DEFAULT_FLAGS;
261 m_cost = DEFAULT_COST;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700262 m_expirationPeriod = time::milliseconds::min();
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800263 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");
Obaid6e7f5f12014-03-11 14:46:10 -0500269
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800270 m_wire.parse ();
271
272 // Name
273 Block::element_const_iterator val = m_wire.find(Tlv::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 }
Obaid6e7f5f12014-03-11 14:46:10 -0500285
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800286 // 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 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700304 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800305 }
306
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800307 // 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
Obaid6e7f5f12014-03-11 14:46:10 -0500316inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700317operator << (std::ostream& os, const PrefixRegOptions& option)
Obaid6e7f5f12014-03-11 14:46:10 -0500318{
319 os << "PrefixRegOptions(";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800320
Obaid6e7f5f12014-03-11 14:46:10 -0500321 // Name
322 os << "Prefix: " << option.getName() << ", ";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800323
Obaid6e7f5f12014-03-11 14:46:10 -0500324 // FaceID
325 os << "FaceID: " << option.getFaceId() << ", ";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800326
Obaid6e7f5f12014-03-11 14:46:10 -0500327 // 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}
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800342
343} // namespace nrd
344} // namespace ndn
345
346#endif // NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP