blob: d57c75d656decf2f0f4e2fa07c652965850f4260 [file] [log] [blame]
Junxiao Shi7357ef22016-09-07 02:39:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_MGMT_NFD_CONTROL_PARAMETERS_HPP
23#define NDN_MGMT_NFD_CONTROL_PARAMETERS_HPP
24
25#include "../../encoding/nfd-constants.hpp"
26#include "../../name.hpp"
27#include "../../util/time.hpp"
28#include "../control-parameters.hpp"
29
30namespace ndn {
31namespace nfd {
32
33/**
34 * \ingroup management
35 */
36enum ControlParameterField {
37 CONTROL_PARAMETER_NAME,
38 CONTROL_PARAMETER_FACE_ID,
39 CONTROL_PARAMETER_URI,
40 CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE,
41 CONTROL_PARAMETER_ORIGIN,
42 CONTROL_PARAMETER_COST,
43 CONTROL_PARAMETER_FLAGS,
44 CONTROL_PARAMETER_MASK,
45 CONTROL_PARAMETER_STRATEGY,
46 CONTROL_PARAMETER_EXPIRATION_PERIOD,
47 CONTROL_PARAMETER_FACE_PERSISTENCY,
48 CONTROL_PARAMETER_UBOUND
49};
50
51const std::string CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND] = {
52 "Name",
53 "FaceId",
54 "Uri",
55 "LocalControlFeature",
56 "Origin",
57 "Cost",
58 "Flags",
59 "Mask",
60 "Strategy",
61 "ExpirationPeriod",
62 "FacePersistency"
63};
64
65/**
66 * \ingroup management
67 * \deprecated use Flags+Mask fields instead
68 */
69enum LocalControlFeature {
70 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID = 1,
71 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID = 2
72};
73
74/**
75 * \ingroup management
76 * \brief represents parameters in a ControlCommand request or response
77 * \sa http://redmine.named-data.net/projects/nfd/wiki/ControlCommand#ControlParameters
78 * \details This type is copyable because it's an abstraction of a TLV type.
79 */
80class ControlParameters : public ndn::mgmt::ControlParameters
81{
82public:
83 class Error : public tlv::Error
84 {
85 public:
86 explicit
87 Error(const std::string& what)
88 : tlv::Error(what)
89 {
90 }
91 };
92
93 ControlParameters();
94
95 explicit
96 ControlParameters(const Block& block);
97
98 template<encoding::Tag TAG>
99 size_t
100 wireEncode(EncodingImpl<TAG>& encoder) const;
101
102 virtual Block
103 wireEncode() const final;
104
105 virtual void
106 wireDecode(const Block& wire) final;
107
108public: // getters & setters
109 bool
110 hasName() const
111 {
112 return m_hasFields[CONTROL_PARAMETER_NAME];
113 }
114
115 const Name&
116 getName() const
117 {
118 BOOST_ASSERT(this->hasName());
119 return m_name;
120 }
121
122 ControlParameters&
123 setName(const Name& name)
124 {
125 m_wire.reset();
126 m_name = name;
127 m_hasFields[CONTROL_PARAMETER_NAME] = true;
128 return *this;
129 }
130
131 ControlParameters&
132 unsetName()
133 {
134 m_wire.reset();
135 m_hasFields[CONTROL_PARAMETER_NAME] = false;
136 return *this;
137 }
138
139 bool
140 hasFaceId() const
141 {
142 return m_hasFields[CONTROL_PARAMETER_FACE_ID];
143 }
144
145 uint64_t
146 getFaceId() const
147 {
148 BOOST_ASSERT(this->hasFaceId());
149 return m_faceId;
150 }
151
152 ControlParameters&
153 setFaceId(uint64_t faceId)
154 {
155 m_wire.reset();
156 m_faceId = faceId;
157 m_hasFields[CONTROL_PARAMETER_FACE_ID] = true;
158 return *this;
159 }
160
161 ControlParameters&
162 unsetFaceId()
163 {
164 m_wire.reset();
165 m_hasFields[CONTROL_PARAMETER_FACE_ID] = false;
166 return *this;
167 }
168
169 bool
170 hasUri() const
171 {
172 return m_hasFields[CONTROL_PARAMETER_URI];
173 }
174
175 const std::string&
176 getUri() const
177 {
178 BOOST_ASSERT(this->hasUri());
179 return m_uri;
180 }
181
182 ControlParameters&
183 setUri(const std::string& uri)
184 {
185 m_wire.reset();
186 m_uri = uri;
187 m_hasFields[CONTROL_PARAMETER_URI] = true;
188 return *this;
189 }
190
191 ControlParameters&
192 unsetUri()
193 {
194 m_wire.reset();
195 m_hasFields[CONTROL_PARAMETER_URI] = false;
196 return *this;
197 }
198
199 /**
200 * \deprecated use Flags+Mask fields instead
201 */
202 bool
203 hasLocalControlFeature() const
204 {
205 return m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE];
206 }
207
208 /**
209 * \deprecated use Flags+Mask fields instead
210 */
211 LocalControlFeature
212 getLocalControlFeature() const
213 {
214 BOOST_ASSERT(this->hasLocalControlFeature());
215 return m_localControlFeature;
216 }
217
218 /**
219 * \deprecated use Flags+Mask fields instead
220 */
221 ControlParameters&
222 setLocalControlFeature(LocalControlFeature localControlFeature)
223 {
224 m_wire.reset();
225 m_localControlFeature = localControlFeature;
226 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = true;
227 return *this;
228 }
229
230 /**
231 * \deprecated use Flags+Mask fields instead
232 */
233 ControlParameters&
234 unsetLocalControlFeature()
235 {
236 m_wire.reset();
237 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = false;
238 return *this;
239 }
240
241 bool
242 hasOrigin() const
243 {
244 return m_hasFields[CONTROL_PARAMETER_ORIGIN];
245 }
246
247 uint64_t
248 getOrigin() const
249 {
250 BOOST_ASSERT(this->hasOrigin());
251 return m_origin;
252 }
253
254 ControlParameters&
255 setOrigin(uint64_t origin)
256 {
257 m_wire.reset();
258 m_origin = origin;
259 m_hasFields[CONTROL_PARAMETER_ORIGIN] = true;
260 return *this;
261 }
262
263 ControlParameters&
264 unsetOrigin()
265 {
266 m_wire.reset();
267 m_hasFields[CONTROL_PARAMETER_ORIGIN] = false;
268 return *this;
269 }
270
271 bool
272 hasCost() const
273 {
274 return m_hasFields[CONTROL_PARAMETER_COST];
275 }
276
277 uint64_t
278 getCost() const
279 {
280 BOOST_ASSERT(this->hasCost());
281 return m_cost;
282 }
283
284 ControlParameters&
285 setCost(uint64_t cost)
286 {
287 m_wire.reset();
288 m_cost = cost;
289 m_hasFields[CONTROL_PARAMETER_COST] = true;
290 return *this;
291 }
292
293 ControlParameters&
294 unsetCost()
295 {
296 m_wire.reset();
297 m_hasFields[CONTROL_PARAMETER_COST] = false;
298 return *this;
299 }
300
301 bool
302 hasFlags() const
303 {
304 return m_hasFields[CONTROL_PARAMETER_FLAGS];
305 }
306
307 uint64_t
308 getFlags() const
309 {
310 BOOST_ASSERT(this->hasFlags());
311 return m_flags;
312 }
313
314 ControlParameters&
315 setFlags(uint64_t flags)
316 {
317 m_wire.reset();
318 m_flags = flags;
319 m_hasFields[CONTROL_PARAMETER_FLAGS] = true;
320 return *this;
321 }
322
323 ControlParameters&
324 unsetFlags()
325 {
326 m_wire.reset();
327 m_hasFields[CONTROL_PARAMETER_FLAGS] = false;
328 return *this;
329 }
330
331 bool
332 hasMask() const
333 {
334 return m_hasFields[CONTROL_PARAMETER_MASK];
335 }
336
337 uint64_t
338 getMask() const
339 {
340 BOOST_ASSERT(this->hasMask());
341 return m_mask;
342 }
343
344 ControlParameters&
345 setMask(uint64_t mask)
346 {
347 m_wire.reset();
348 m_mask = mask;
349 m_hasFields[CONTROL_PARAMETER_MASK] = true;
350 return *this;
351 }
352
353 ControlParameters&
354 unsetMask()
355 {
356 m_wire.reset();
357 m_hasFields[CONTROL_PARAMETER_MASK] = false;
358 return *this;
359 }
360
361 bool
362 hasStrategy() const
363 {
364 return m_hasFields[CONTROL_PARAMETER_STRATEGY];
365 }
366
367 const Name&
368 getStrategy() const
369 {
370 BOOST_ASSERT(this->hasStrategy());
371 return m_strategy;
372 }
373
374 ControlParameters&
375 setStrategy(const Name& strategy)
376 {
377 m_wire.reset();
378 m_strategy = strategy;
379 m_hasFields[CONTROL_PARAMETER_STRATEGY] = true;
380 return *this;
381 }
382
383 ControlParameters&
384 unsetStrategy()
385 {
386 m_wire.reset();
387 m_hasFields[CONTROL_PARAMETER_STRATEGY] = false;
388 return *this;
389 }
390
391 bool
392 hasExpirationPeriod() const
393 {
394 return m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD];
395 }
396
397 const time::milliseconds&
398 getExpirationPeriod() const
399 {
400 BOOST_ASSERT(this->hasExpirationPeriod());
401 return m_expirationPeriod;
402 }
403
404 ControlParameters&
405 setExpirationPeriod(const time::milliseconds& expirationPeriod)
406 {
407 m_wire.reset();
408 m_expirationPeriod = expirationPeriod;
409 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = true;
410 return *this;
411 }
412
413 ControlParameters&
414 unsetExpirationPeriod()
415 {
416 m_wire.reset();
417 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = false;
418 return *this;
419 }
420
421 bool
422 hasFacePersistency() const
423 {
424 return m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY];
425 }
426
427 FacePersistency
428 getFacePersistency() const
429 {
430 BOOST_ASSERT(this->hasFacePersistency());
431 return m_facePersistency;
432 }
433
434 ControlParameters&
435 setFacePersistency(FacePersistency persistency)
436 {
437 m_wire.reset();
438 m_facePersistency = persistency;
439 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = true;
440 return *this;
441 }
442
443 ControlParameters&
444 unsetFacePersistency()
445 {
446 m_wire.reset();
447 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = false;
448 return *this;
449 }
450
451 const std::vector<bool>&
452 getPresentFields() const
453 {
454 return m_hasFields;
455 }
456
457public: // Flags and Mask helpers
458 /**
459 * \return whether bit is enabled in Mask
460 * \param bit bit position within range [0, 64) (least significant bit is 0)
461 */
462 bool
463 hasFlagBit(size_t bit) const;
464
465 /**
466 * \return bit at a position in Flags
467 * \param bit bit position within range [0, 64) (least significant bit is 0)
468 */
469 bool
470 getFlagBit(size_t bit) const;
471
472 /**
473 * \brief set a bit in Flags
474 * \param bit bit position within range [0, 64) (least significant bit is 0)
475 * \param value new value in Flags
476 * \param wantMask if true, enable the bit in Mask
477 */
478 ControlParameters&
479 setFlagBit(size_t bit, bool value, bool wantMask = true);
480
481 /**
482 * \brief disable a bit in Mask
483 * \param bit bit position within range [0, 64) (least significant bit is 0)
484 * \post If all bits are disabled, Flags and Mask fields are deleted.
485 */
486 ControlParameters&
487 unsetFlagBit(size_t bit);
488
489private: // fields
490 std::vector<bool> m_hasFields;
491
492 Name m_name;
493 uint64_t m_faceId;
494 std::string m_uri;
495 LocalControlFeature m_localControlFeature;
496 uint64_t m_origin;
497 uint64_t m_cost;
498 uint64_t m_flags;
499 uint64_t m_mask;
500 Name m_strategy;
501 time::milliseconds m_expirationPeriod;
502 FacePersistency m_facePersistency;
503
504private:
505 mutable Block m_wire;
506};
507
508std::ostream&
509operator<<(std::ostream& os, const ControlParameters& parameters);
510
511} // namespace nfd
512} // namespace ndn
513
514#endif // NDN_MGMT_NFD_CONTROL_PARAMETERS_HPP