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