NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018 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 #include "control-parameters.hpp"
24 #include "encoding/tlv-nfd.hpp"
25 #include "util/concepts.hpp"
26 #include "util/string-helper.hpp"
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->hasDefaultCongestionThreshold()) {
56  m_defaultCongestionThreshold);
57  }
60  m_baseCongestionMarkingInterval.count());
61  }
62  if (this->hasFacePersistency()) {
63  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
64  }
65  if (this->hasExpirationPeriod()) {
67  m_expirationPeriod.count());
68  }
69  if (this->hasStrategy()) {
70  totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
71  }
72  if (this->hasMask()) {
73  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
74  }
75  if (this->hasFlags()) {
76  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
77  }
78  if (this->hasCount()) {
79  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Count, m_count);
80  }
81  if (this->hasCapacity()) {
82  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
83  }
84  if (this->hasCost()) {
85  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost);
86  }
87  if (this->hasOrigin()) {
88  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
89  }
90  if (this->hasLocalUri()) {
91  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
92  }
93  if (this->hasUri()) {
94  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
95  }
96  if (this->hasFaceId()) {
97  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
98  }
99  if (this->hasName()) {
100  totalLength += m_name.wireEncode(encoder);
101  }
102 
103  totalLength += encoder.prependVarNumber(totalLength);
104  totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
105  return totalLength;
106 }
107 
109 
110 Block
112 {
113  if (m_wire.hasWire())
114  return m_wire;
115 
116  EncodingEstimator estimator;
117  size_t estimatedSize = wireEncode(estimator);
118 
119  EncodingBuffer buffer(estimatedSize, 0);
120  wireEncode(buffer);
121 
122  m_wire = buffer.block();
123  return m_wire;
124 }
125 
126 void
128 {
129  if (block.type() != tlv::nfd::ControlParameters) {
130  BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters"));
131  }
132  m_wire = block;
133  m_wire.parse();
135 
136  val = m_wire.find(tlv::Name);
137  m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
138  if (this->hasName()) {
139  m_name.wireDecode(*val);
140  }
141 
142  val = m_wire.find(tlv::nfd::FaceId);
143  m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
144  if (this->hasFaceId()) {
145  m_faceId = readNonNegativeInteger(*val);
146  }
147 
148  val = m_wire.find(tlv::nfd::Uri);
149  m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
150  if (this->hasUri()) {
151  m_uri = readString(*val);
152  }
153 
154  val = m_wire.find(tlv::nfd::LocalUri);
155  m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
156  if (this->hasLocalUri()) {
157  m_localUri = readString(*val);
158  }
159 
160  val = m_wire.find(tlv::nfd::Origin);
161  m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
162  if (this->hasOrigin()) {
163  m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
164  }
165 
166  val = m_wire.find(tlv::nfd::Cost);
167  m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
168  if (this->hasCost()) {
169  m_cost = readNonNegativeInteger(*val);
170  }
171 
172  val = m_wire.find(tlv::nfd::Capacity);
173  m_hasFields[CONTROL_PARAMETER_CAPACITY] = val != m_wire.elements_end();
174  if (this->hasCapacity()) {
175  m_capacity = readNonNegativeInteger(*val);
176  }
177 
178  val = m_wire.find(tlv::nfd::Count);
179  m_hasFields[CONTROL_PARAMETER_COUNT] = val != m_wire.elements_end();
180  if (this->hasCount()) {
181  m_count = readNonNegativeInteger(*val);
182  }
183 
184  val = m_wire.find(tlv::nfd::Flags);
185  m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
186  if (this->hasFlags()) {
187  m_flags = readNonNegativeInteger(*val);
188  }
189 
190  val = m_wire.find(tlv::nfd::Mask);
191  m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
192  if (this->hasMask()) {
193  m_mask = readNonNegativeInteger(*val);
194  }
195 
196  val = m_wire.find(tlv::nfd::Strategy);
197  m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
198  if (this->hasStrategy()) {
199  val->parse();
200  if (val->elements().empty()) {
201  BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name"));
202  }
203  else {
204  m_strategy.wireDecode(*val->elements_begin());
205  }
206  }
207 
208  val = m_wire.find(tlv::nfd::ExpirationPeriod);
209  m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
210  if (this->hasExpirationPeriod()) {
211  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
212  }
213 
214  val = m_wire.find(tlv::nfd::FacePersistency);
215  m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
216  if (this->hasFacePersistency()) {
217  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
218  }
219 
222  if (this->hasBaseCongestionMarkingInterval()) {
223  m_baseCongestionMarkingInterval = time::nanoseconds(readNonNegativeInteger(*val));
224  }
225 
227  m_hasFields[CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD] = val != m_wire.elements_end();
228  if (this->hasDefaultCongestionThreshold()) {
229  m_defaultCongestionThreshold = readNonNegativeInteger(*val);
230  }
231 }
232 
233 bool
235 {
236  if (bit >= 64) {
237  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
238  }
239 
240  if (!hasMask()) {
241  return false;
242  }
243 
244  return getMask() & (1 << bit);
245 }
246 
247 bool
249 {
250  if (bit >= 64) {
251  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
252  }
253 
254  if (!hasFlags()) {
255  return false;
256  }
257 
258  return getFlags() & (1 << bit);
259 }
260 
262 ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
263 {
264  if (bit >= 64) {
265  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
266  }
267 
268  uint64_t flags = hasFlags() ? getFlags() : 0;
269  if (value) {
270  flags |= (1 << bit);
271  }
272  else {
273  flags &= ~(1 << bit);
274  }
275  setFlags(flags);
276 
277  if (wantMask) {
278  uint64_t mask = hasMask() ? getMask() : 0;
279  mask |= (1 << bit);
280  setMask(mask);
281  }
282 
283  return *this;
284 }
285 
288 {
289  if (bit >= 64) {
290  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
291  }
292 
293  uint64_t mask = hasMask() ? getMask() : 0;
294  mask &= ~(1 << bit);
295  if (mask == 0) {
296  unsetMask();
297  unsetFlags();
298  }
299  else {
300  setMask(mask);
301  }
302 
303  return *this;
304 }
305 
306 std::ostream&
307 operator<<(std::ostream& os, const ControlParameters& parameters)
308 {
309  os << "ControlParameters(";
310 
311  if (parameters.hasName()) {
312  os << "Name: " << parameters.getName() << ", ";
313  }
314 
315  if (parameters.hasFaceId()) {
316  os << "FaceId: " << parameters.getFaceId() << ", ";
317  }
318 
319  if (parameters.hasUri()) {
320  os << "Uri: " << parameters.getUri() << ", ";
321  }
322 
323  if (parameters.hasLocalUri()) {
324  os << "LocalUri: " << parameters.getLocalUri() << ", ";
325  }
326 
327  if (parameters.hasOrigin()) {
328  os << "Origin: " << parameters.getOrigin() << ", ";
329  }
330 
331  if (parameters.hasCost()) {
332  os << "Cost: " << parameters.getCost() << ", ";
333  }
334 
335  if (parameters.hasCapacity()) {
336  os << "Capacity: " << parameters.getCapacity() << ", ";
337  }
338 
339  if (parameters.hasCount()) {
340  os << "Count: " << parameters.getCount() << ", ";
341  }
342 
343  if (parameters.hasFlags()) {
344  os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
345  }
346 
347  if (parameters.hasMask()) {
348  os << "Mask: " << AsHex{parameters.getMask()} << ", ";
349  }
350 
351  if (parameters.hasStrategy()) {
352  os << "Strategy: " << parameters.getStrategy() << ", ";
353  }
354 
355  if (parameters.hasExpirationPeriod()) {
356  os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
357  }
358 
359  if (parameters.hasFacePersistency()) {
360  os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
361  }
362 
363  if (parameters.hasBaseCongestionMarkingInterval()) {
364  os << "BaseCongestionMarkingInterval: " << parameters.getBaseCongestionMarkingInterval() << ", ";
365  }
366 
367  if (parameters.hasDefaultCongestionThreshold()) {
368  os << "DefaultCongestionThreshold: " << parameters.getDefaultCongestionThreshold() << ", ";
369  }
370 
371  os << ")";
372  return os;
373 }
374 
375 } // namespace nfd
376 } // namespace ndn
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:252
void wireDecode(const Block &wire) final
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
represents parameters in a ControlCommand request or response
time::nanoseconds getBaseCongestionMarkingInterval() const
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:336
ControlParameters & setFlags(uint64_t flags)
ControlParameters & unsetMask()
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
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.
ControlParameters & unsetFlags()
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
ControlParameters & unsetFlagBit(size_t bit)
disable a bit in Mask
FacePersistency getFacePersistency() const
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Definition: block.cpp:437
const std::string & getLocalUri() const
uint64_t getDefaultCongestionThreshold() const
get default congestion threshold (measured in bytes)
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool hasFlagBit(size_t bit) const
const std::string & getUri() const
const time::milliseconds & getExpirationPeriod() const
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:126
bool getFlagBit(size_t bit) const
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:159
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
const Name & getStrategy() const
ControlParameters & setMask(uint64_t mask)
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
ControlParameters & setFlagBit(size_t bit, bool value, bool wantMask=true)
set a bit in Flags
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
EncodingImpl< EstimatorTag > EncodingEstimator