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