blob: f821e66113fe67a1fea7254aaf5488139ce00355 [file] [log] [blame]
Alexander Afanasyev0553cd52014-02-18 02:34:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
Alexander Afanasyev0553cd52014-02-18 02:34:45 -08005 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080011 */
12
13#ifndef NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP
14#define NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP
15
16#include "../encoding/encoding-buffer.hpp"
17#include "../encoding/tlv-nrd.hpp"
18#include "../name.hpp"
19
20namespace ndn {
21namespace nrd {
22
23const uint64_t INVALID_FACE_ID = std::numeric_limits<uint64_t>::max();
24const uint64_t DEFAULT_COST = 0;
25const uint64_t DEFAULT_FLAGS = tlv::nrd::NDN_FORW_CHILD_INHERIT;
26
27/**
28 * @brief Abstraction for prefix registration options for NRD Prefix registration protocol
29 *
30 * @see http://redmine.named-data.net/projects/nrd/wiki/NRD_Prefix_Registration_protocol
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070031 * @deprecated use NFD RIB Management
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080032 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033class PrefixRegOptions
34{
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080035public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036 class Error : public Tlv::Error
37 {
38 public:
39 explicit
40 Error(const std::string& what)
41 : Tlv::Error(what)
42 {
43 }
44 };
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080045
46 PrefixRegOptions()
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047 : m_faceId(INVALID_FACE_ID)
48 , m_flags(DEFAULT_FLAGS)
49 , m_cost(DEFAULT_COST)
50 , m_expirationPeriod(time::milliseconds::min())
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080051 {
52 }
53
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 explicit
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080055 PrefixRegOptions(const Block& block)
56 {
57 wireDecode(block);
58 }
59
60 template<bool T>
61 size_t
62 wireEncode(EncodingImpl<T>& block) const;
Obaid6e7f5f12014-03-11 14:46:10 -050063
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080064 const Block&
65 wireEncode() const;
Obaid6e7f5f12014-03-11 14:46:10 -050066
67 void
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080068 wireDecode(const Block& wire);
Obaid6e7f5f12014-03-11 14:46:10 -050069
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080070 ////////////////////////////////////////////////////////
Obaid6e7f5f12014-03-11 14:46:10 -050071
72 const Name&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080073 getName() const
74 {
75 return m_name;
76 }
Obaid6e7f5f12014-03-11 14:46:10 -050077
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080078 PrefixRegOptions&
79 setName(const Name& name)
80 {
81 m_name = name;
82 m_wire.reset();
83 return *this;
84 }
85
86 //
Obaid6e7f5f12014-03-11 14:46:10 -050087
Alexander Afanasyev87a7f5f2014-02-26 11:40:13 -080088 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080089 getFaceId() const
90 {
91 return m_faceId;
92 }
93
94 PrefixRegOptions&
Alexander Afanasyev87a7f5f2014-02-26 11:40:13 -080095 setFaceId(uint64_t faceId)
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080096 {
97 m_faceId = faceId;
98 m_wire.reset();
99 return *this;
100 }
101
102 //
Obaid6e7f5f12014-03-11 14:46:10 -0500103
104 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800105 getFlags() const
106 {
107 return m_flags;
108 }
109
110 PrefixRegOptions&
111 setFlags(uint64_t flags)
112 {
113 m_flags = flags;
114 m_wire.reset();
115 return *this;
116 }
117
118 //
Obaid6e7f5f12014-03-11 14:46:10 -0500119
120 uint64_t
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800121 getCost() const
122 {
123 return m_cost;
124 }
125
126 PrefixRegOptions&
127 setCost(uint64_t cost)
128 {
129 m_cost = cost;
130 m_wire.reset();
131 return *this;
132 }
133
134 //
Obaid6e7f5f12014-03-11 14:46:10 -0500135
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 const time::milliseconds&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800137 getExpirationPeriod() const
138 {
139 return m_expirationPeriod;
140 }
141
142 PrefixRegOptions&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700143 setExpirationPeriod(const time::milliseconds& expirationPeriod)
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800144 {
145 m_expirationPeriod = expirationPeriod;
146 m_wire.reset();
147 return *this;
148 }
149
150 //
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800151
Obaid6e7f5f12014-03-11 14:46:10 -0500152 const std::string&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800153 getProtocol() const
154 {
155 return m_protocol;
156 }
157
158 PrefixRegOptions&
159 setProtocol(const std::string& protocol)
160 {
161 m_protocol = protocol;
162 m_wire.reset();
163 return *this;
164 }
Obaid6e7f5f12014-03-11 14:46:10 -0500165
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800166private:
167 Name m_name;
168 uint64_t m_faceId;
169 uint64_t m_flags;
170 uint64_t m_cost;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700171 time::milliseconds m_expirationPeriod;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800172 std::string m_protocol;
Obaid6e7f5f12014-03-11 14:46:10 -0500173
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800174 mutable Block m_wire;
175};
176
177template<bool T>
178inline size_t
179PrefixRegOptions::wireEncode(EncodingImpl<T>& block) const
180{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700181 size_t totalLength = 0;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800182
183 // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
184 // Name
185 // FaceId?
186 // Flags?
Obaid6e7f5f12014-03-11 14:46:10 -0500187 // Cost?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800188 // ExpirationPeriod?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800189 // Protocol?
190
191 // (reverse encoding)
192
193 // Protocol
194 if (!m_protocol.empty())
195 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700196 totalLength += prependByteArrayBlock(block,
197 tlv::nrd::Protocol,
198 reinterpret_cast<const uint8_t*>(m_protocol.c_str()),
199 m_protocol.size());
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800200 }
201
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800202 // ExpirationPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700203 if (m_expirationPeriod > time::milliseconds::zero())
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800204 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700205 totalLength += prependNonNegativeIntegerBlock(block,
206 tlv::nrd::ExpirationPeriod,
207 m_expirationPeriod.count());
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800208 }
209
210 // Cost
211 if (m_cost != DEFAULT_COST)
212 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800214 }
215
216 // Flags
217 if (m_flags != DEFAULT_FLAGS)
218 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800220 }
221
222 // FaceId
223 if (m_faceId != INVALID_FACE_ID)
224 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700225 totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800226 }
227
228 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700229 totalLength += m_name.wireEncode(block);
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800230
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700231 totalLength += block.prependVarNumber(totalLength);
232 totalLength += block.prependVarNumber(tlv::nrd::PrefixRegOptions);
233 return totalLength;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800234}
235
236inline const Block&
237PrefixRegOptions::wireEncode() const
238{
239 if (m_wire.hasWire())
240 return m_wire;
241
242 EncodingEstimator estimator;
243 size_t estimatedSize = wireEncode(estimator);
Obaid6e7f5f12014-03-11 14:46:10 -0500244
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800245 EncodingBuffer buffer(estimatedSize, 0);
246 wireEncode(buffer);
247
248 m_wire = buffer.block();
249 return m_wire;
250}
Obaid6e7f5f12014-03-11 14:46:10 -0500251
252inline void
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800253PrefixRegOptions::wireDecode(const Block& wire)
254{
255 // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
256 // Name
257 // FaceId?
258 // Flags?
Obaid6e7f5f12014-03-11 14:46:10 -0500259 // Cost?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800260 // ExpirationPeriod?
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800261 // Protocol?
262
263 m_name.clear();
264 m_faceId = INVALID_FACE_ID;
265 m_flags = DEFAULT_FLAGS;
266 m_cost = DEFAULT_COST;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700267 m_expirationPeriod = time::milliseconds::min();
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800268 m_protocol.clear();
269
270 m_wire = wire;
271
272 if (m_wire.type() != tlv::nrd::PrefixRegOptions)
273 throw Error("Requested decoding of PrefixRegOptions, but Block is of different type");
Obaid6e7f5f12014-03-11 14:46:10 -0500274
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800275 m_wire.parse ();
276
277 // Name
278 Block::element_const_iterator val = m_wire.find(Tlv::Name);
279 if (val != m_wire.elements_end())
280 {
281 m_name.wireDecode(*val);
282 }
283
284 // FaceId
285 val = m_wire.find(tlv::nrd::FaceId);
286 if (val != m_wire.elements_end())
287 {
288 m_faceId = readNonNegativeInteger(*val);
289 }
Obaid6e7f5f12014-03-11 14:46:10 -0500290
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800291 // Flags
292 val = m_wire.find(tlv::nrd::Flags);
293 if (val != m_wire.elements_end())
294 {
295 m_flags = readNonNegativeInteger(*val);
296 }
297
298 // Cost
299 val = m_wire.find(tlv::nrd::Cost);
300 if (val != m_wire.elements_end())
301 {
302 m_cost = readNonNegativeInteger(*val);
303 }
304
305 // ExpirationPeriod
306 val = m_wire.find(tlv::nrd::ExpirationPeriod);
307 if (val != m_wire.elements_end())
308 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700309 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800310 }
311
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800312 // Protocol
313 val = m_wire.find(tlv::nrd::Protocol);
314 if (val != m_wire.elements_end())
315 {
316 m_protocol = std::string(reinterpret_cast<const char*>(val->value()),
317 val->value_size());
318 }
319}
320
Obaid6e7f5f12014-03-11 14:46:10 -0500321inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700322operator << (std::ostream& os, const PrefixRegOptions& option)
Obaid6e7f5f12014-03-11 14:46:10 -0500323{
324 os << "PrefixRegOptions(";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800325
Obaid6e7f5f12014-03-11 14:46:10 -0500326 // Name
327 os << "Prefix: " << option.getName() << ", ";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800328
Obaid6e7f5f12014-03-11 14:46:10 -0500329 // FaceID
330 os << "FaceID: " << option.getFaceId() << ", ";
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800331
Obaid6e7f5f12014-03-11 14:46:10 -0500332 // Flags
333 os << "Flags: " << option.getFlags() << ", ";
334
335 // Cost
336 os << "Cost: " << option.getCost() << ", ";
337
338 // ExpirationPeriod
339 os << "ExpirationPeriod: " << option.getExpirationPeriod() << ", ";
340
341 // Protocol
342 os << "Protocol: " << option.getProtocol();
343
344 os << ")";
345 return os;
346}
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800347
348} // namespace nrd
349} // namespace ndn
350
351#endif // NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP