control-parameters.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
27 
28 namespace ndn {
29 namespace nfd {
30 
31 //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlParameters>));
32 BOOST_CONCEPT_ASSERT((WireEncodable<ControlParameters>));
33 BOOST_CONCEPT_ASSERT((WireDecodable<ControlParameters>));
34 static_assert(std::is_base_of<tlv::Error, ControlParameters::Error>::value,
35  "ControlParameters::Error must inherit from tlv::Error");
36 
38  : m_hasFields(CONTROL_PARAMETER_UBOUND)
39 {
40 }
41 
43  : m_hasFields(CONTROL_PARAMETER_UBOUND)
44 {
45  wireDecode(block);
46 }
47 
48 template<encoding::Tag TAG>
49 size_t
51 {
52  size_t totalLength = 0;
53 
54  if (this->hasMtu()) {
55  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mtu, m_mtu);
56  }
57  if (this->hasDefaultCongestionThreshold()) {
59  m_defaultCongestionThreshold);
60  }
63  m_baseCongestionMarkingInterval.count());
64  }
65  if (this->hasFacePersistency()) {
66  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
67  }
68  if (this->hasExpirationPeriod()) {
70  m_expirationPeriod.count());
71  }
72  if (this->hasStrategy()) {
73  totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
74  }
75  if (this->hasMask()) {
76  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
77  }
78  if (this->hasFlags()) {
79  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
80  }
81  if (this->hasCount()) {
82  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Count, m_count);
83  }
84  if (this->hasCapacity()) {
85  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
86  }
87  if (this->hasCost()) {
88  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost);
89  }
90  if (this->hasOrigin()) {
91  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
92  }
93  if (this->hasLocalUri()) {
94  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
95  }
96  if (this->hasUri()) {
97  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
98  }
99  if (this->hasFaceId()) {
100  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
101  }
102  if (this->hasName()) {
103  totalLength += m_name.wireEncode(encoder);
104  }
105 
106  totalLength += encoder.prependVarNumber(totalLength);
107  totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
108  return totalLength;
109 }
110 
112 
113 Block
115 {
116  if (m_wire.hasWire())
117  return m_wire;
118 
119  EncodingEstimator estimator;
120  size_t estimatedSize = wireEncode(estimator);
121 
122  EncodingBuffer buffer(estimatedSize, 0);
123  wireEncode(buffer);
124 
125  m_wire = buffer.block();
126  return m_wire;
127 }
128 
129 void
131 {
132  if (block.type() != tlv::nfd::ControlParameters) {
133  NDN_THROW(Error("ControlParameters", block.type()));
134  }
135 
136  m_wire = block;
137  m_wire.parse();
139 
140  val = m_wire.find(tlv::Name);
141  m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
142  if (this->hasName()) {
143  m_name.wireDecode(*val);
144  }
145 
146  val = m_wire.find(tlv::nfd::FaceId);
147  m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
148  if (this->hasFaceId()) {
149  m_faceId = readNonNegativeInteger(*val);
150  }
151 
152  val = m_wire.find(tlv::nfd::Uri);
153  m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
154  if (this->hasUri()) {
155  m_uri = readString(*val);
156  }
157 
158  val = m_wire.find(tlv::nfd::LocalUri);
159  m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
160  if (this->hasLocalUri()) {
161  m_localUri = readString(*val);
162  }
163 
164  val = m_wire.find(tlv::nfd::Origin);
165  m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
166  if (this->hasOrigin()) {
167  m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
168  }
169 
170  val = m_wire.find(tlv::nfd::Cost);
171  m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
172  if (this->hasCost()) {
173  m_cost = readNonNegativeInteger(*val);
174  }
175 
176  val = m_wire.find(tlv::nfd::Capacity);
177  m_hasFields[CONTROL_PARAMETER_CAPACITY] = val != m_wire.elements_end();
178  if (this->hasCapacity()) {
179  m_capacity = readNonNegativeInteger(*val);
180  }
181 
182  val = m_wire.find(tlv::nfd::Count);
183  m_hasFields[CONTROL_PARAMETER_COUNT] = val != m_wire.elements_end();
184  if (this->hasCount()) {
185  m_count = readNonNegativeInteger(*val);
186  }
187 
188  val = m_wire.find(tlv::nfd::Flags);
189  m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
190  if (this->hasFlags()) {
191  m_flags = readNonNegativeInteger(*val);
192  }
193 
194  val = m_wire.find(tlv::nfd::Mask);
195  m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
196  if (this->hasMask()) {
197  m_mask = readNonNegativeInteger(*val);
198  }
199 
200  val = m_wire.find(tlv::nfd::Strategy);
201  m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
202  if (this->hasStrategy()) {
203  val->parse();
204  if (val->elements().empty()) {
205  NDN_THROW(Error("Expecting Strategy/Name"));
206  }
207  else {
208  m_strategy.wireDecode(*val->elements_begin());
209  }
210  }
211 
212  val = m_wire.find(tlv::nfd::ExpirationPeriod);
213  m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
214  if (this->hasExpirationPeriod()) {
215  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
216  }
217 
218  val = m_wire.find(tlv::nfd::FacePersistency);
219  m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
220  if (this->hasFacePersistency()) {
221  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
222  }
223 
226  if (this->hasBaseCongestionMarkingInterval()) {
227  m_baseCongestionMarkingInterval = time::nanoseconds(readNonNegativeInteger(*val));
228  }
229 
231  m_hasFields[CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD] = val != m_wire.elements_end();
232  if (this->hasDefaultCongestionThreshold()) {
233  m_defaultCongestionThreshold = readNonNegativeInteger(*val);
234  }
235 
236  val = m_wire.find(tlv::nfd::Mtu);
237  m_hasFields[CONTROL_PARAMETER_MTU] = val != m_wire.elements_end();
238  if (this->hasMtu()) {
239  m_mtu = readNonNegativeInteger(*val);
240  }
241 }
242 
243 bool
245 {
246  if (bit >= 64) {
247  NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
248  }
249 
250  if (!hasMask()) {
251  return false;
252  }
253 
254  return getMask() & (1 << bit);
255 }
256 
257 bool
259 {
260  if (bit >= 64) {
261  NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
262  }
263 
264  if (!hasFlags()) {
265  return false;
266  }
267 
268  return getFlags() & (1 << bit);
269 }
270 
272 ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
273 {
274  if (bit >= 64) {
275  NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
276  }
277 
278  uint64_t flags = hasFlags() ? getFlags() : 0;
279  if (value) {
280  flags |= (1 << bit);
281  }
282  else {
283  flags &= ~(1 << bit);
284  }
285  setFlags(flags);
286 
287  if (wantMask) {
288  uint64_t mask = hasMask() ? getMask() : 0;
289  mask |= (1 << bit);
290  setMask(mask);
291  }
292 
293  return *this;
294 }
295 
298 {
299  if (bit >= 64) {
300  NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
301  }
302 
303  uint64_t mask = hasMask() ? getMask() : 0;
304  mask &= ~(1 << bit);
305  if (mask == 0) {
306  unsetMask();
307  unsetFlags();
308  }
309  else {
310  setMask(mask);
311  }
312 
313  return *this;
314 }
315 
316 std::ostream&
317 operator<<(std::ostream& os, const ControlParameters& parameters)
318 {
319  os << "ControlParameters(";
320 
321  if (parameters.hasName()) {
322  os << "Name: " << parameters.getName() << ", ";
323  }
324 
325  if (parameters.hasFaceId()) {
326  os << "FaceId: " << parameters.getFaceId() << ", ";
327  }
328 
329  if (parameters.hasUri()) {
330  os << "Uri: " << parameters.getUri() << ", ";
331  }
332 
333  if (parameters.hasLocalUri()) {
334  os << "LocalUri: " << parameters.getLocalUri() << ", ";
335  }
336 
337  if (parameters.hasOrigin()) {
338  os << "Origin: " << parameters.getOrigin() << ", ";
339  }
340 
341  if (parameters.hasCost()) {
342  os << "Cost: " << parameters.getCost() << ", ";
343  }
344 
345  if (parameters.hasCapacity()) {
346  os << "Capacity: " << parameters.getCapacity() << ", ";
347  }
348 
349  if (parameters.hasCount()) {
350  os << "Count: " << parameters.getCount() << ", ";
351  }
352 
353  if (parameters.hasFlags()) {
354  os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
355  }
356 
357  if (parameters.hasMask()) {
358  os << "Mask: " << AsHex{parameters.getMask()} << ", ";
359  }
360 
361  if (parameters.hasStrategy()) {
362  os << "Strategy: " << parameters.getStrategy() << ", ";
363  }
364 
365  if (parameters.hasExpirationPeriod()) {
366  os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
367  }
368 
369  if (parameters.hasFacePersistency()) {
370  os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
371  }
372 
373  if (parameters.hasBaseCongestionMarkingInterval()) {
374  os << "BaseCongestionMarkingInterval: " << parameters.getBaseCongestionMarkingInterval() << ", ";
375  }
376 
377  if (parameters.hasDefaultCongestionThreshold()) {
378  os << "DefaultCongestionThreshold: " << parameters.getDefaultCongestionThreshold() << ", ";
379  }
380 
381  if (parameters.hasMtu()) {
382  os << "Mtu: " << parameters.getMtu() << ", ";
383  }
384 
385  os << ")";
386  return os;
387 }
388 
389 } // namespace nfd
390 } // namespace ndn
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
element_container::const_iterator element_const_iterator
Definition: block.hpp:49
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:441
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:418
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
represents parameters in a ControlCommand request or response
void wireDecode(const Block &wire) final
ControlParameters & unsetFlags()
ControlParameters & unsetMask()
ControlParameters & unsetFlagBit(size_t bit)
disable a bit in Mask
uint64_t getDefaultCongestionThreshold() const
get default congestion threshold (measured in bytes)
FacePersistency getFacePersistency() const
const std::string & getUri() const
ControlParameters & setFlagBit(size_t bit, bool value, bool wantMask=true)
set a bit in Flags
ControlParameters & setFlags(uint64_t flags)
bool hasFlagBit(size_t bit) const
const std::string & getLocalUri() const
const Name & getStrategy() const
const time::milliseconds & getExpirationPeriod() const
ControlParameters & setMask(uint64_t mask)
bool getFlagBit(size_t bit) const
time::nanoseconds getBaseCongestionMarkingInterval() const
uint64_t getMtu() const
get MTU (measured in bytes)
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
@ CONTROL_PARAMETER_FACE_PERSISTENCY
@ CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD
@ CONTROL_PARAMETER_EXPIRATION_PERIOD
@ CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
@ BaseCongestionMarkingInterval
Definition: tlv-nfd.hpp:70
@ ControlParameters
Definition: tlv-nfd.hpp:35
@ DefaultCongestionThreshold
Definition: tlv-nfd.hpp:71
@ ExpirationPeriod
Definition: tlv-nfd.hpp:45
@ Name
Definition: tlv.hpp:67
Definition: data.cpp:25