blob: 65360a5e39da475727ced011838f7abe0ba8c5bf [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
Junxiao Shi22f85682018-01-22 19:23:22 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi70911652014-08-12 10:14:24 -07004 *
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
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "control-parameters.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/block-helpers.hpp"
Junxiao Shi5d75fd92017-08-08 18:09:20 +000024#include "encoding/tlv-nfd.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070025#include "util/concepts.hpp"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050026#include "util/string-helper.hpp"
Junxiao Shi70911652014-08-12 10:14:24 -070027
28namespace ndn {
29namespace nfd {
30
Junxiao Shi65f1a712014-11-20 14:59:36 -070031//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlParameters>));
32BOOST_CONCEPT_ASSERT((WireEncodable<ControlParameters>));
33BOOST_CONCEPT_ASSERT((WireDecodable<ControlParameters>));
34static_assert(std::is_base_of<tlv::Error, ControlParameters::Error>::value,
35 "ControlParameters::Error must inherit from tlv::Error");
36
Junxiao Shi70911652014-08-12 10:14:24 -070037ControlParameters::ControlParameters()
38 : m_hasFields(CONTROL_PARAMETER_UBOUND)
39{
40}
41
42ControlParameters::ControlParameters(const Block& block)
43 : m_hasFields(CONTROL_PARAMETER_UBOUND)
44{
45 wireDecode(block);
46}
47
Alexander Afanasyev74633892015-02-08 18:08:46 -080048template<encoding::Tag TAG>
Junxiao Shi70911652014-08-12 10:14:24 -070049size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080050ControlParameters::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi70911652014-08-12 10:14:24 -070051{
52 size_t totalLength = 0;
53
Eric Newberry07d05c92018-01-22 16:08:01 -070054 if (this->hasDefaultCongestionThreshold()) {
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050055 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::DefaultCongestionThreshold,
56 m_defaultCongestionThreshold);
Eric Newberry07d05c92018-01-22 16:08:01 -070057 }
58 if (this->hasBaseCongestionMarkingInterval()) {
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050059 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::BaseCongestionMarkingInterval,
60 m_baseCongestionMarkingInterval.count());
Eric Newberry07d05c92018-01-22 16:08:01 -070061 }
Yukai Tud93c5fc2015-08-25 11:37:16 +080062 if (this->hasFacePersistency()) {
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050063 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
Yukai Tud93c5fc2015-08-25 11:37:16 +080064 }
Junxiao Shi70911652014-08-12 10:14:24 -070065 if (this->hasExpirationPeriod()) {
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050066 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::ExpirationPeriod,
67 m_expirationPeriod.count());
Junxiao Shi70911652014-08-12 10:14:24 -070068 }
69 if (this->hasStrategy()) {
70 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
71 }
Eric Newberryda916d62016-08-11 23:04:34 -070072 if (this->hasMask()) {
73 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
74 }
Junxiao Shi70911652014-08-12 10:14:24 -070075 if (this->hasFlags()) {
76 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
77 }
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050078 if (this->hasCount()) {
79 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Count, m_count);
Junxiao Shidf505382018-03-04 13:40:44 +000080 }
Junxiao Shi22f85682018-01-22 19:23:22 +000081 if (this->hasCapacity()) {
82 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
83 }
Junxiao Shi70911652014-08-12 10:14:24 -070084 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 }
Eric Newberryd7f5b282017-03-28 19:55:20 -070090 if (this->hasLocalUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000091 totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
Eric Newberryd7f5b282017-03-28 19:55:20 -070092 }
Junxiao Shi70911652014-08-12 10:14:24 -070093 if (this->hasUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000094 totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
Junxiao Shi70911652014-08-12 10:14:24 -070095 }
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
Davide Pesavento88a0d812017-08-19 21:31:42 -0400108NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ControlParameters);
Junxiao Shi70911652014-08-12 10:14:24 -0700109
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700110Block
Junxiao Shi70911652014-08-12 10:14:24 -0700111ControlParameters::wireEncode() const
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
126void
127ControlParameters::wireDecode(const Block& block)
128{
129 if (block.type() != tlv::nfd::ControlParameters) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700130 BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters"));
Junxiao Shi70911652014-08-12 10:14:24 -0700131 }
132 m_wire = block;
133 m_wire.parse();
134 Block::element_const_iterator val;
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()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000145 m_faceId = readNonNegativeInteger(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700146 }
147
148 val = m_wire.find(tlv::nfd::Uri);
149 m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
150 if (this->hasUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000151 m_uri = readString(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700152 }
153
Eric Newberryd7f5b282017-03-28 19:55:20 -0700154 val = m_wire.find(tlv::nfd::LocalUri);
155 m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
156 if (this->hasLocalUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000157 m_localUri = readString(*val);
Eric Newberryd7f5b282017-03-28 19:55:20 -0700158 }
159
Junxiao Shi70911652014-08-12 10:14:24 -0700160 val = m_wire.find(tlv::nfd::Origin);
161 m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
162 if (this->hasOrigin()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000163 m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700164 }
165
166 val = m_wire.find(tlv::nfd::Cost);
167 m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
168 if (this->hasCost()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000169 m_cost = readNonNegativeInteger(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700170 }
171
Junxiao Shi22f85682018-01-22 19:23:22 +0000172 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
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500178 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);
Junxiao Shidf505382018-03-04 13:40:44 +0000182 }
183
Junxiao Shi70911652014-08-12 10:14:24 -0700184 val = m_wire.find(tlv::nfd::Flags);
185 m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
186 if (this->hasFlags()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000187 m_flags = readNonNegativeInteger(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700188 }
189
Eric Newberryda916d62016-08-11 23:04:34 -0700190 val = m_wire.find(tlv::nfd::Mask);
191 m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
192 if (this->hasMask()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000193 m_mask = readNonNegativeInteger(*val);
Eric Newberryda916d62016-08-11 23:04:34 -0700194 }
195
Junxiao Shi70911652014-08-12 10:14:24 -0700196 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()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700201 BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name"));
Junxiao Shi70911652014-08-12 10:14:24 -0700202 }
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 }
Yukai Tud93c5fc2015-08-25 11:37:16 +0800213
214 val = m_wire.find(tlv::nfd::FacePersistency);
215 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
216 if (this->hasFacePersistency()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000217 m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800218 }
Eric Newberry07d05c92018-01-22 16:08:01 -0700219
220 val = m_wire.find(tlv::nfd::BaseCongestionMarkingInterval);
221 m_hasFields[CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL] = val != m_wire.elements_end();
222 if (this->hasBaseCongestionMarkingInterval()) {
223 m_baseCongestionMarkingInterval = time::nanoseconds(readNonNegativeInteger(*val));
224 }
225
226 val = m_wire.find(tlv::nfd::DefaultCongestionThreshold);
227 m_hasFields[CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD] = val != m_wire.elements_end();
228 if (this->hasDefaultCongestionThreshold()) {
229 m_defaultCongestionThreshold = readNonNegativeInteger(*val);
230 }
Junxiao Shi70911652014-08-12 10:14:24 -0700231}
232
Eric Newberryda916d62016-08-11 23:04:34 -0700233bool
234ControlParameters::hasFlagBit(size_t bit) const
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
247bool
248ControlParameters::getFlagBit(size_t bit) const
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
261ControlParameters&
262ControlParameters::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
286ControlParameters&
287ControlParameters::unsetFlagBit(size_t bit)
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
Junxiao Shi70911652014-08-12 10:14:24 -0700306std::ostream&
307operator<<(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
Eric Newberryd7f5b282017-03-28 19:55:20 -0700323 if (parameters.hasLocalUri()) {
324 os << "LocalUri: " << parameters.getLocalUri() << ", ";
325 }
326
Junxiao Shi70911652014-08-12 10:14:24 -0700327 if (parameters.hasOrigin()) {
328 os << "Origin: " << parameters.getOrigin() << ", ";
329 }
330
331 if (parameters.hasCost()) {
332 os << "Cost: " << parameters.getCost() << ", ";
333 }
334
Junxiao Shi22f85682018-01-22 19:23:22 +0000335 if (parameters.hasCapacity()) {
336 os << "Capacity: " << parameters.getCapacity() << ", ";
337 }
338
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500339 if (parameters.hasCount()) {
340 os << "Count: " << parameters.getCount() << ", ";
Junxiao Shidf505382018-03-04 13:40:44 +0000341 }
342
Junxiao Shi70911652014-08-12 10:14:24 -0700343 if (parameters.hasFlags()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500344 os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
Eric Newberryda916d62016-08-11 23:04:34 -0700345 }
346
347 if (parameters.hasMask()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500348 os << "Mask: " << AsHex{parameters.getMask()} << ", ";
Junxiao Shi70911652014-08-12 10:14:24 -0700349 }
350
351 if (parameters.hasStrategy()) {
352 os << "Strategy: " << parameters.getStrategy() << ", ";
353 }
354
355 if (parameters.hasExpirationPeriod()) {
356 os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
357 }
358
Yanbiao Licbdacb22016-08-02 16:02:35 +0800359 if (parameters.hasFacePersistency()) {
360 os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
361 }
362
Eric Newberry07d05c92018-01-22 16:08:01 -0700363 if (parameters.hasBaseCongestionMarkingInterval()) {
364 os << "BaseCongestionMarkingInterval: " << parameters.getBaseCongestionMarkingInterval() << ", ";
365 }
366
367 if (parameters.hasDefaultCongestionThreshold()) {
368 os << "DefaultCongestionThreshold: " << parameters.getDefaultCongestionThreshold() << ", ";
369 }
370
Junxiao Shi70911652014-08-12 10:14:24 -0700371 os << ")";
372 return os;
373}
374
375} // namespace nfd
376} // namespace ndn