ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2025 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
26
27namespace ndn::nfd {
28
30
32
34{
35 wireDecode(block);
36}
37
38template<encoding::Tag TAG>
39size_t
41{
42 size_t totalLength = 0;
43
44 if (this->hasMtu()) {
45 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mtu, m_mtu);
46 }
48 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::DefaultCongestionThreshold,
49 m_defaultCongestionThreshold);
50 }
52 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::BaseCongestionMarkingInterval,
53 m_baseCongestionMarkingInterval.count());
54 }
55 if (this->hasFacePersistency()) {
56 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
57 }
58 if (this->hasExpirationPeriod()) {
59 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::ExpirationPeriod,
60 m_expirationPeriod.count());
61 }
62 if (this->hasStrategy()) {
63 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
64 }
65 if (this->hasMask()) {
66 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
67 }
68 if (this->hasFlags()) {
69 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
70 }
71 if (this->hasCount()) {
72 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Count, m_count);
73 }
74 if (this->hasCapacity()) {
75 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
76 }
77 if (this->hasCost()) {
78 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost);
79 }
80 if (this->hasOrigin()) {
81 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
82 }
83 if (this->hasLocalUri()) {
84 totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
85 }
86 if (this->hasUri()) {
87 totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
88 }
89 if (this->hasFaceId()) {
90 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
91 }
92 if (this->hasName()) {
93 totalLength += m_name.wireEncode(encoder);
94 }
95
96 totalLength += encoder.prependVarNumber(totalLength);
97 totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
98 return totalLength;
99}
100
102
103Block
105{
106 if (m_wire.hasWire())
107 return m_wire;
108
109 EncodingEstimator estimator;
110 size_t estimatedSize = wireEncode(estimator);
111
112 EncodingBuffer buffer(estimatedSize, 0);
113 wireEncode(buffer);
114
115 m_wire = buffer.block();
116 return m_wire;
117}
118
119void
121{
122 if (block.type() != tlv::nfd::ControlParameters) {
123 NDN_THROW(Error("ControlParameters", block.type()));
124 }
125
126 *this = {};
127 m_wire = block;
128 m_wire.parse();
129
130 for (const auto& e : m_wire.elements()) {
131 switch (e.type()) {
132 case tlv::Name:
133 m_hasFields.set(CONTROL_PARAMETER_NAME);
134 m_name.wireDecode(e);
135 break;
136 case tlv::nfd::FaceId:
137 m_hasFields.set(CONTROL_PARAMETER_FACE_ID);
138 m_faceId = readNonNegativeInteger(e);
139 break;
140 case tlv::nfd::Uri:
141 m_hasFields.set(CONTROL_PARAMETER_URI);
142 m_uri = readString(e);
143 break;
145 m_hasFields.set(CONTROL_PARAMETER_LOCAL_URI);
146 m_localUri = readString(e);
147 break;
148 case tlv::nfd::Origin:
149 m_hasFields.set(CONTROL_PARAMETER_ORIGIN);
150 m_origin = readNonNegativeIntegerAs<RouteOrigin>(e);
151 break;
152 case tlv::nfd::Cost:
153 m_hasFields.set(CONTROL_PARAMETER_COST);
154 m_cost = readNonNegativeInteger(e);
155 break;
157 m_hasFields.set(CONTROL_PARAMETER_CAPACITY);
158 m_capacity = readNonNegativeInteger(e);
159 break;
160 case tlv::nfd::Count:
161 m_hasFields.set(CONTROL_PARAMETER_COUNT);
162 m_count = readNonNegativeInteger(e);
163 break;
164 case tlv::nfd::Flags:
165 m_hasFields.set(CONTROL_PARAMETER_FLAGS);
166 m_flags = readNonNegativeInteger(e);
167 break;
168 case tlv::nfd::Mask:
169 m_hasFields.set(CONTROL_PARAMETER_MASK);
170 m_mask = readNonNegativeInteger(e);
171 break;
173 m_hasFields.set(CONTROL_PARAMETER_STRATEGY);
174 e.parse();
175 if (e.elements().empty()) {
176 NDN_THROW(Error("Expecting Strategy.Name"));
177 }
178 m_strategy.wireDecode(e.elements().front());
179 break;
182 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(e));
183 break;
185 m_hasFields.set(CONTROL_PARAMETER_FACE_PERSISTENCY);
186 m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(e);
187 break;
190 m_baseCongestionMarkingInterval = time::nanoseconds(readNonNegativeInteger(e));
191 break;
194 m_defaultCongestionThreshold = readNonNegativeInteger(e);
195 break;
196 case tlv::nfd::Mtu:
197 m_hasFields.set(CONTROL_PARAMETER_MTU);
198 m_mtu = readNonNegativeInteger(e);
199 break;
200 default:
201 // ignore unrecognized elements
202 break;
203 }
204 }
205}
206
207bool
209{
210 if (bit >= 64) {
211 NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
212 }
213
214 if (!hasMask()) {
215 return false;
216 }
217
218 return getMask() & (1 << bit);
219}
220
221bool
223{
224 if (bit >= 64) {
225 NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
226 }
227
228 if (!hasFlags()) {
229 return false;
230 }
231
232 return getFlags() & (1 << bit);
233}
234
236ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask)
237{
238 if (bit >= 64) {
239 NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
240 }
241
242 uint64_t flags = hasFlags() ? getFlags() : 0;
243 if (value) {
244 flags |= (1 << bit);
245 }
246 else {
247 flags &= ~(1 << bit);
248 }
249 setFlags(flags);
250
251 if (wantMask) {
252 uint64_t mask = hasMask() ? getMask() : 0;
253 mask |= (1 << bit);
254 setMask(mask);
255 }
256
257 return *this;
258}
259
262{
263 if (bit >= 64) {
264 NDN_THROW(std::out_of_range("bit must be within range [0, 64)"));
265 }
266
267 uint64_t mask = hasMask() ? getMask() : 0;
268 mask &= ~(1 << bit);
269 if (mask == 0) {
270 unsetMask();
271 unsetFlags();
272 }
273 else {
274 setMask(mask);
275 }
276
277 return *this;
278}
279
280std::ostream&
281operator<<(std::ostream& os, const ControlParameters& parameters)
282{
283 os << "ControlParameters(";
284
285 if (parameters.hasName()) {
286 os << "Name: " << parameters.getName() << ", ";
287 }
288
289 if (parameters.hasFaceId()) {
290 os << "FaceId: " << parameters.getFaceId() << ", ";
291 }
292
293 if (parameters.hasUri()) {
294 os << "Uri: " << parameters.getUri() << ", ";
295 }
296
297 if (parameters.hasLocalUri()) {
298 os << "LocalUri: " << parameters.getLocalUri() << ", ";
299 }
300
301 if (parameters.hasOrigin()) {
302 os << "Origin: " << parameters.getOrigin() << ", ";
303 }
304
305 if (parameters.hasCost()) {
306 os << "Cost: " << parameters.getCost() << ", ";
307 }
308
309 if (parameters.hasCapacity()) {
310 os << "Capacity: " << parameters.getCapacity() << ", ";
311 }
312
313 if (parameters.hasCount()) {
314 os << "Count: " << parameters.getCount() << ", ";
315 }
316
317 if (parameters.hasFlags()) {
318 os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
319 }
320
321 if (parameters.hasMask()) {
322 os << "Mask: " << AsHex{parameters.getMask()} << ", ";
323 }
324
325 if (parameters.hasStrategy()) {
326 os << "Strategy: " << parameters.getStrategy() << ", ";
327 }
328
329 if (parameters.hasExpirationPeriod()) {
330 os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
331 }
332
333 if (parameters.hasFacePersistency()) {
334 os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
335 }
336
337 if (parameters.hasBaseCongestionMarkingInterval()) {
338 os << "BaseCongestionMarkingInterval: " << parameters.getBaseCongestionMarkingInterval() << ", ";
339 }
340
341 if (parameters.hasDefaultCongestionThreshold()) {
342 os << "DefaultCongestionThreshold: " << parameters.getDefaultCongestionThreshold() << ", ";
343 }
344
345 if (parameters.hasMtu()) {
346 os << "Mtu: " << parameters.getMtu() << ", ";
347 }
348
349 os << ")";
350 return os;
351}
352
353} // namespace ndn::nfd
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
const element_container & elements() const noexcept
Get container of sub-elements.
Definition block.hpp:424
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition name.cpp:92
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition name.cpp:125
Represents parameters in a ControlCommand request or response.
void wireDecode(const Block &wire) final
const std::string & getUri() const
ControlParameters & setMask(uint64_t mask)
ControlParameters & unsetFlagBit(size_t bit)
Disable a bit in Mask.
uint64_t getDefaultCongestionThreshold() const
Get default congestion threshold (measured in bytes).
const time::milliseconds & getExpirationPeriod() const
FacePersistency getFacePersistency() const
ControlParameters & setFlagBit(size_t bit, bool value, bool wantMask=true)
Set a bit in Flags.
bool hasFlagBit(size_t bit) const
ControlParameters & setFlags(uint64_t flags)
const std::string & getLocalUri() const
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:56
@ CONTROL_PARAMETER_FACE_PERSISTENCY
@ CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD
@ CONTROL_PARAMETER_EXPIRATION_PERIOD
@ CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL
Contains classes and functions related to the NFD Management protocol.
constexpr std::array CONTROL_PARAMETER_FIELD
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
::boost::chrono::milliseconds milliseconds
Definition time.hpp:52
::boost::chrono::nanoseconds nanoseconds
Definition time.hpp:54
@ BaseCongestionMarkingInterval
Definition tlv-nfd.hpp:70
@ DefaultCongestionThreshold
Definition tlv-nfd.hpp:71
@ Name
Definition tlv.hpp:71