blob: cd432ca4346add671cad36a9412e7f76233a7c1b [file] [log] [blame]
Junxiao Shi7357ef22016-09-07 02:39:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento7f20d6e2017-01-16 14:43:58 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Junxiao Shi7357ef22016-09-07 02:39:37 +00004 *
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,
Eric Newberryd7f5b282017-03-28 19:55:20 -070040 CONTROL_PARAMETER_LOCAL_URI,
Junxiao Shi7357ef22016-09-07 02:39:37 +000041 CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE,
42 CONTROL_PARAMETER_ORIGIN,
43 CONTROL_PARAMETER_COST,
44 CONTROL_PARAMETER_FLAGS,
45 CONTROL_PARAMETER_MASK,
46 CONTROL_PARAMETER_STRATEGY,
47 CONTROL_PARAMETER_EXPIRATION_PERIOD,
48 CONTROL_PARAMETER_FACE_PERSISTENCY,
49 CONTROL_PARAMETER_UBOUND
50};
51
52const std::string CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND] = {
53 "Name",
54 "FaceId",
55 "Uri",
Eric Newberryd7f5b282017-03-28 19:55:20 -070056 "LocalUri",
Junxiao Shi7357ef22016-09-07 02:39:37 +000057 "LocalControlFeature",
58 "Origin",
59 "Cost",
60 "Flags",
61 "Mask",
62 "Strategy",
63 "ExpirationPeriod",
64 "FacePersistency"
65};
66
67/**
68 * \ingroup management
69 * \deprecated use Flags+Mask fields instead
70 */
71enum LocalControlFeature {
72 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID = 1,
73 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID = 2
74};
75
76/**
77 * \ingroup management
78 * \brief represents parameters in a ControlCommand request or response
Eric Newberryd7f5b282017-03-28 19:55:20 -070079 * \sa https://redmine.named-data.net/projects/nfd/wiki/ControlCommand#ControlParameters
Junxiao Shi7357ef22016-09-07 02:39:37 +000080 * \details This type is copyable because it's an abstraction of a TLV type.
81 */
Davide Pesavento7f20d6e2017-01-16 14:43:58 -050082class ControlParameters : public mgmt::ControlParameters
Junxiao Shi7357ef22016-09-07 02:39:37 +000083{
84public:
85 class Error : public tlv::Error
86 {
87 public:
88 explicit
89 Error(const std::string& what)
90 : tlv::Error(what)
91 {
92 }
93 };
94
95 ControlParameters();
96
97 explicit
98 ControlParameters(const Block& block);
99
100 template<encoding::Tag TAG>
101 size_t
102 wireEncode(EncodingImpl<TAG>& encoder) const;
103
Davide Pesavento57c07df2016-12-11 18:41:45 -0500104 Block
Junxiao Shi7357ef22016-09-07 02:39:37 +0000105 wireEncode() const final;
106
Davide Pesavento57c07df2016-12-11 18:41:45 -0500107 void
Junxiao Shi7357ef22016-09-07 02:39:37 +0000108 wireDecode(const Block& wire) final;
109
110public: // getters & setters
111 bool
112 hasName() const
113 {
114 return m_hasFields[CONTROL_PARAMETER_NAME];
115 }
116
117 const Name&
118 getName() const
119 {
120 BOOST_ASSERT(this->hasName());
121 return m_name;
122 }
123
124 ControlParameters&
125 setName(const Name& name)
126 {
127 m_wire.reset();
128 m_name = name;
129 m_hasFields[CONTROL_PARAMETER_NAME] = true;
130 return *this;
131 }
132
133 ControlParameters&
134 unsetName()
135 {
136 m_wire.reset();
137 m_hasFields[CONTROL_PARAMETER_NAME] = false;
138 return *this;
139 }
140
141 bool
142 hasFaceId() const
143 {
144 return m_hasFields[CONTROL_PARAMETER_FACE_ID];
145 }
146
147 uint64_t
148 getFaceId() const
149 {
150 BOOST_ASSERT(this->hasFaceId());
151 return m_faceId;
152 }
153
154 ControlParameters&
155 setFaceId(uint64_t faceId)
156 {
157 m_wire.reset();
158 m_faceId = faceId;
159 m_hasFields[CONTROL_PARAMETER_FACE_ID] = true;
160 return *this;
161 }
162
163 ControlParameters&
164 unsetFaceId()
165 {
166 m_wire.reset();
167 m_hasFields[CONTROL_PARAMETER_FACE_ID] = false;
168 return *this;
169 }
170
171 bool
172 hasUri() const
173 {
174 return m_hasFields[CONTROL_PARAMETER_URI];
175 }
176
177 const std::string&
178 getUri() const
179 {
180 BOOST_ASSERT(this->hasUri());
181 return m_uri;
182 }
183
184 ControlParameters&
185 setUri(const std::string& uri)
186 {
187 m_wire.reset();
188 m_uri = uri;
189 m_hasFields[CONTROL_PARAMETER_URI] = true;
190 return *this;
191 }
192
193 ControlParameters&
194 unsetUri()
195 {
196 m_wire.reset();
197 m_hasFields[CONTROL_PARAMETER_URI] = false;
198 return *this;
199 }
200
Eric Newberryd7f5b282017-03-28 19:55:20 -0700201 bool
202 hasLocalUri() const
203 {
204 return m_hasFields[CONTROL_PARAMETER_LOCAL_URI];
205 }
206
207 const std::string&
208 getLocalUri() const
209 {
210 BOOST_ASSERT(this->hasLocalUri());
211 return m_localUri;
212 }
213
214 ControlParameters&
215 setLocalUri(const std::string& localUri)
216 {
217 m_wire.reset();
218 m_localUri = localUri;
219 m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = true;
220 return *this;
221 }
222
223 ControlParameters&
224 unsetLocalUri()
225 {
226 m_wire.reset();
227 m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = false;
228 return *this;
229 }
230
Junxiao Shi7357ef22016-09-07 02:39:37 +0000231 /**
232 * \deprecated use Flags+Mask fields instead
233 */
234 bool
235 hasLocalControlFeature() const
236 {
237 return m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE];
238 }
239
240 /**
241 * \deprecated use Flags+Mask fields instead
242 */
243 LocalControlFeature
244 getLocalControlFeature() const
245 {
246 BOOST_ASSERT(this->hasLocalControlFeature());
247 return m_localControlFeature;
248 }
249
250 /**
251 * \deprecated use Flags+Mask fields instead
252 */
253 ControlParameters&
254 setLocalControlFeature(LocalControlFeature localControlFeature)
255 {
256 m_wire.reset();
257 m_localControlFeature = localControlFeature;
258 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = true;
259 return *this;
260 }
261
262 /**
263 * \deprecated use Flags+Mask fields instead
264 */
265 ControlParameters&
266 unsetLocalControlFeature()
267 {
268 m_wire.reset();
269 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = false;
270 return *this;
271 }
272
273 bool
274 hasOrigin() const
275 {
276 return m_hasFields[CONTROL_PARAMETER_ORIGIN];
277 }
278
279 uint64_t
280 getOrigin() const
281 {
282 BOOST_ASSERT(this->hasOrigin());
283 return m_origin;
284 }
285
286 ControlParameters&
287 setOrigin(uint64_t origin)
288 {
289 m_wire.reset();
290 m_origin = origin;
291 m_hasFields[CONTROL_PARAMETER_ORIGIN] = true;
292 return *this;
293 }
294
295 ControlParameters&
296 unsetOrigin()
297 {
298 m_wire.reset();
299 m_hasFields[CONTROL_PARAMETER_ORIGIN] = false;
300 return *this;
301 }
302
303 bool
304 hasCost() const
305 {
306 return m_hasFields[CONTROL_PARAMETER_COST];
307 }
308
309 uint64_t
310 getCost() const
311 {
312 BOOST_ASSERT(this->hasCost());
313 return m_cost;
314 }
315
316 ControlParameters&
317 setCost(uint64_t cost)
318 {
319 m_wire.reset();
320 m_cost = cost;
321 m_hasFields[CONTROL_PARAMETER_COST] = true;
322 return *this;
323 }
324
325 ControlParameters&
326 unsetCost()
327 {
328 m_wire.reset();
329 m_hasFields[CONTROL_PARAMETER_COST] = false;
330 return *this;
331 }
332
333 bool
334 hasFlags() const
335 {
336 return m_hasFields[CONTROL_PARAMETER_FLAGS];
337 }
338
339 uint64_t
340 getFlags() const
341 {
342 BOOST_ASSERT(this->hasFlags());
343 return m_flags;
344 }
345
346 ControlParameters&
347 setFlags(uint64_t flags)
348 {
349 m_wire.reset();
350 m_flags = flags;
351 m_hasFields[CONTROL_PARAMETER_FLAGS] = true;
352 return *this;
353 }
354
355 ControlParameters&
356 unsetFlags()
357 {
358 m_wire.reset();
359 m_hasFields[CONTROL_PARAMETER_FLAGS] = false;
360 return *this;
361 }
362
363 bool
364 hasMask() const
365 {
366 return m_hasFields[CONTROL_PARAMETER_MASK];
367 }
368
369 uint64_t
370 getMask() const
371 {
372 BOOST_ASSERT(this->hasMask());
373 return m_mask;
374 }
375
376 ControlParameters&
377 setMask(uint64_t mask)
378 {
379 m_wire.reset();
380 m_mask = mask;
381 m_hasFields[CONTROL_PARAMETER_MASK] = true;
382 return *this;
383 }
384
385 ControlParameters&
386 unsetMask()
387 {
388 m_wire.reset();
389 m_hasFields[CONTROL_PARAMETER_MASK] = false;
390 return *this;
391 }
392
393 bool
394 hasStrategy() const
395 {
396 return m_hasFields[CONTROL_PARAMETER_STRATEGY];
397 }
398
399 const Name&
400 getStrategy() const
401 {
402 BOOST_ASSERT(this->hasStrategy());
403 return m_strategy;
404 }
405
406 ControlParameters&
407 setStrategy(const Name& strategy)
408 {
409 m_wire.reset();
410 m_strategy = strategy;
411 m_hasFields[CONTROL_PARAMETER_STRATEGY] = true;
412 return *this;
413 }
414
415 ControlParameters&
416 unsetStrategy()
417 {
418 m_wire.reset();
419 m_hasFields[CONTROL_PARAMETER_STRATEGY] = false;
420 return *this;
421 }
422
423 bool
424 hasExpirationPeriod() const
425 {
426 return m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD];
427 }
428
429 const time::milliseconds&
430 getExpirationPeriod() const
431 {
432 BOOST_ASSERT(this->hasExpirationPeriod());
433 return m_expirationPeriod;
434 }
435
436 ControlParameters&
437 setExpirationPeriod(const time::milliseconds& expirationPeriod)
438 {
439 m_wire.reset();
440 m_expirationPeriod = expirationPeriod;
441 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = true;
442 return *this;
443 }
444
445 ControlParameters&
446 unsetExpirationPeriod()
447 {
448 m_wire.reset();
449 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = false;
450 return *this;
451 }
452
453 bool
454 hasFacePersistency() const
455 {
456 return m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY];
457 }
458
459 FacePersistency
460 getFacePersistency() const
461 {
462 BOOST_ASSERT(this->hasFacePersistency());
463 return m_facePersistency;
464 }
465
466 ControlParameters&
467 setFacePersistency(FacePersistency persistency)
468 {
469 m_wire.reset();
470 m_facePersistency = persistency;
471 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = true;
472 return *this;
473 }
474
475 ControlParameters&
476 unsetFacePersistency()
477 {
478 m_wire.reset();
479 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = false;
480 return *this;
481 }
482
483 const std::vector<bool>&
484 getPresentFields() const
485 {
486 return m_hasFields;
487 }
488
489public: // Flags and Mask helpers
490 /**
491 * \return whether bit is enabled in Mask
492 * \param bit bit position within range [0, 64) (least significant bit is 0)
493 */
494 bool
495 hasFlagBit(size_t bit) const;
496
497 /**
498 * \return bit at a position in Flags
499 * \param bit bit position within range [0, 64) (least significant bit is 0)
500 */
501 bool
502 getFlagBit(size_t bit) const;
503
504 /**
505 * \brief set a bit in Flags
506 * \param bit bit position within range [0, 64) (least significant bit is 0)
507 * \param value new value in Flags
508 * \param wantMask if true, enable the bit in Mask
509 */
510 ControlParameters&
511 setFlagBit(size_t bit, bool value, bool wantMask = true);
512
513 /**
514 * \brief disable a bit in Mask
515 * \param bit bit position within range [0, 64) (least significant bit is 0)
516 * \post If all bits are disabled, Flags and Mask fields are deleted.
517 */
518 ControlParameters&
519 unsetFlagBit(size_t bit);
520
521private: // fields
522 std::vector<bool> m_hasFields;
523
524 Name m_name;
525 uint64_t m_faceId;
526 std::string m_uri;
Eric Newberryd7f5b282017-03-28 19:55:20 -0700527 std::string m_localUri;
Junxiao Shi7357ef22016-09-07 02:39:37 +0000528 LocalControlFeature m_localControlFeature;
529 uint64_t m_origin;
530 uint64_t m_cost;
531 uint64_t m_flags;
532 uint64_t m_mask;
533 Name m_strategy;
534 time::milliseconds m_expirationPeriod;
535 FacePersistency m_facePersistency;
536
537private:
538 mutable Block m_wire;
539};
540
541std::ostream&
542operator<<(std::ostream& os, const ControlParameters& parameters);
543
544} // namespace nfd
545} // namespace ndn
546
547#endif // NDN_MGMT_NFD_CONTROL_PARAMETERS_HPP