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()) {
55  totalLength += prependNonNegativeIntegerBlock(encoder,
56  tlv::nfd::DefaultCongestionThreshold, m_defaultCongestionThreshold);
57  }
59  totalLength += prependNonNegativeIntegerBlock(encoder,
60  tlv::nfd::BaseCongestionMarkingInterval, m_baseCongestionMarkingInterval.count());
61  }
62  if (this->hasFacePersistency()) {
63  totalLength += prependNonNegativeIntegerBlock(encoder,
64  tlv::nfd::FacePersistency, m_facePersistency);
65  }
66  if (this->hasExpirationPeriod()) {
67  totalLength += prependNonNegativeIntegerBlock(encoder,
68  tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
69  }
70  if (this->hasStrategy()) {
71  totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
72  }
73  if (this->hasMask()) {
74  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
75  }
76  if (this->hasFlags()) {
77  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
78  }
79  if (this->hasCapacity()) {
80  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
81  }
82  if (this->hasCost()) {
83  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost);
84  }
85  if (this->hasOrigin()) {
86  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
87  }
88  if (this->hasLocalUri()) {
89  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
90  }
91  if (this->hasUri()) {
92  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
93  }
94  if (this->hasFaceId()) {
95  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
96  }
97  if (this->hasName()) {
98  totalLength += m_name.wireEncode(encoder);
99  }
100 
101  totalLength += encoder.prependVarNumber(totalLength);
102  totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
103  return totalLength;
104 }
105 
107 
108 Block
110 {
111  if (m_wire.hasWire())
112  return m_wire;
113 
114  EncodingEstimator estimator;
115  size_t estimatedSize = wireEncode(estimator);
116 
117  EncodingBuffer buffer(estimatedSize, 0);
118  wireEncode(buffer);
119 
120  m_wire = buffer.block();
121  return m_wire;
122 }
123 
124 void
126 {
127  if (block.type() != tlv::nfd::ControlParameters) {
128  BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters"));
129  }
130  m_wire = block;
131  m_wire.parse();
133 
134  val = m_wire.find(tlv::Name);
135  m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
136  if (this->hasName()) {
137  m_name.wireDecode(*val);
138  }
139 
140  val = m_wire.find(tlv::nfd::FaceId);
141  m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
142  if (this->hasFaceId()) {
143  m_faceId = readNonNegativeInteger(*val);
144  }
145 
146  val = m_wire.find(tlv::nfd::Uri);
147  m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
148  if (this->hasUri()) {
149  m_uri = readString(*val);
150  }
151 
152  val = m_wire.find(tlv::nfd::LocalUri);
153  m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
154  if (this->hasLocalUri()) {
155  m_localUri = readString(*val);
156  }
157 
158  val = m_wire.find(tlv::nfd::Origin);
159  m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
160  if (this->hasOrigin()) {
161  m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
162  }
163 
164  val = m_wire.find(tlv::nfd::Cost);
165  m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
166  if (this->hasCost()) {
167  m_cost = readNonNegativeInteger(*val);
168  }
169 
170  val = m_wire.find(tlv::nfd::Capacity);
171  m_hasFields[CONTROL_PARAMETER_CAPACITY] = val != m_wire.elements_end();
172  if (this->hasCapacity()) {
173  m_capacity = readNonNegativeInteger(*val);
174  }
175 
176  val = m_wire.find(tlv::nfd::Flags);
177  m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
178  if (this->hasFlags()) {
179  m_flags = readNonNegativeInteger(*val);
180  }
181 
182  val = m_wire.find(tlv::nfd::Mask);
183  m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
184  if (this->hasMask()) {
185  m_mask = readNonNegativeInteger(*val);
186  }
187 
188  val = m_wire.find(tlv::nfd::Strategy);
189  m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
190  if (this->hasStrategy()) {
191  val->parse();
192  if (val->elements().empty()) {
193  BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name"));
194  }
195  else {
196  m_strategy.wireDecode(*val->elements_begin());
197  }
198  }
199 
200  val = m_wire.find(tlv::nfd::ExpirationPeriod);
201  m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
202  if (this->hasExpirationPeriod()) {
203  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
204  }
205 
206  val = m_wire.find(tlv::nfd::FacePersistency);
207  m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
208  if (this->hasFacePersistency()) {
209  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
210  }
211 
214  if (this->hasBaseCongestionMarkingInterval()) {
215  m_baseCongestionMarkingInterval = time::nanoseconds(readNonNegativeInteger(*val));
216  }
217 
219  m_hasFields[CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD] = val != m_wire.elements_end();
220  if (this->hasDefaultCongestionThreshold()) {
221  m_defaultCongestionThreshold = readNonNegativeInteger(*val);
222  }
223 }
224 
225 bool
227 {
228  if (bit >= 64) {
229  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
230  }
231 
232  if (!hasMask()) {
233  return false;
234  }
235 
236  return getMask() & (1 << bit);
237 }
238 
239 bool
241 {
242  if (bit >= 64) {
243  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
244  }
245 
246  if (!hasFlags()) {
247  return false;
248  }
249 
250  return getFlags() & (1 << bit);
251 }
252 
254 ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
255 {
256  if (bit >= 64) {
257  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
258  }
259 
260  uint64_t flags = hasFlags() ? getFlags() : 0;
261  if (value) {
262  flags |= (1 << bit);
263  }
264  else {
265  flags &= ~(1 << bit);
266  }
267  setFlags(flags);
268 
269  if (wantMask) {
270  uint64_t mask = hasMask() ? getMask() : 0;
271  mask |= (1 << bit);
272  setMask(mask);
273  }
274 
275  return *this;
276 }
277 
280 {
281  if (bit >= 64) {
282  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
283  }
284 
285  uint64_t mask = hasMask() ? getMask() : 0;
286  mask &= ~(1 << bit);
287  if (mask == 0) {
288  unsetMask();
289  unsetFlags();
290  }
291  else {
292  setMask(mask);
293  }
294 
295  return *this;
296 }
297 
298 std::ostream&
299 operator<<(std::ostream& os, const ControlParameters& parameters)
300 {
301  os << "ControlParameters(";
302 
303  if (parameters.hasName()) {
304  os << "Name: " << parameters.getName() << ", ";
305  }
306 
307  if (parameters.hasFaceId()) {
308  os << "FaceId: " << parameters.getFaceId() << ", ";
309  }
310 
311  if (parameters.hasUri()) {
312  os << "Uri: " << parameters.getUri() << ", ";
313  }
314 
315  if (parameters.hasLocalUri()) {
316  os << "LocalUri: " << parameters.getLocalUri() << ", ";
317  }
318 
319  if (parameters.hasOrigin()) {
320  os << "Origin: " << parameters.getOrigin() << ", ";
321  }
322 
323  if (parameters.hasCost()) {
324  os << "Cost: " << parameters.getCost() << ", ";
325  }
326 
327  if (parameters.hasCapacity()) {
328  os << "Capacity: " << parameters.getCapacity() << ", ";
329  }
330 
331  if (parameters.hasFlags()) {
332  os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
333  }
334 
335  if (parameters.hasMask()) {
336  os << "Mask: " << AsHex{parameters.getMask()} << ", ";
337  }
338 
339  if (parameters.hasStrategy()) {
340  os << "Strategy: " << parameters.getStrategy() << ", ";
341  }
342 
343  if (parameters.hasExpirationPeriod()) {
344  os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
345  }
346 
347  if (parameters.hasFacePersistency()) {
348  os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
349  }
350 
351  if (parameters.hasBaseCongestionMarkingInterval()) {
352  os << "BaseCongestionMarkingInterval: " << parameters.getBaseCongestionMarkingInterval() << ", ";
353  }
354 
355  if (parameters.hasDefaultCongestionThreshold()) {
356  os << "DefaultCongestionThreshold: " << parameters.getDefaultCongestionThreshold() << ", ";
357  }
358 
359  os << ")";
360  return os;
361 }
362 
363 } // namespace nfd
364 } // namespace ndn
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:251
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:335
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:429
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:131
bool getFlagBit(size_t bit) const
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:164
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