blob: a98e8072d4566d2f65aaffe2fa121f189d6f4fc2 [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
Yukai Tud93c5fc2015-08-25 11:37:16 +080054 if (this->hasFacePersistency()) {
55 totalLength += prependNonNegativeIntegerBlock(encoder,
56 tlv::nfd::FacePersistency, m_facePersistency);
57 }
Junxiao Shi70911652014-08-12 10:14:24 -070058 if (this->hasExpirationPeriod()) {
59 totalLength += prependNonNegativeIntegerBlock(encoder,
60 tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
61 }
62 if (this->hasStrategy()) {
63 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
64 }
Eric Newberryda916d62016-08-11 23:04:34 -070065 if (this->hasMask()) {
66 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
67 }
Junxiao Shi70911652014-08-12 10:14:24 -070068 if (this->hasFlags()) {
69 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
70 }
Junxiao Shi22f85682018-01-22 19:23:22 +000071 if (this->hasCapacity()) {
72 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Capacity, m_capacity);
73 }
Junxiao Shi70911652014-08-12 10:14:24 -070074 if (this->hasCost()) {
75 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost);
76 }
77 if (this->hasOrigin()) {
78 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
79 }
Eric Newberryd7f5b282017-03-28 19:55:20 -070080 if (this->hasLocalUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000081 totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
Eric Newberryd7f5b282017-03-28 19:55:20 -070082 }
Junxiao Shi70911652014-08-12 10:14:24 -070083 if (this->hasUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000084 totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri);
Junxiao Shi70911652014-08-12 10:14:24 -070085 }
86 if (this->hasFaceId()) {
87 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
88 }
89 if (this->hasName()) {
90 totalLength += m_name.wireEncode(encoder);
91 }
92
93 totalLength += encoder.prependVarNumber(totalLength);
94 totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
95 return totalLength;
96}
97
Davide Pesavento88a0d812017-08-19 21:31:42 -040098NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ControlParameters);
Junxiao Shi70911652014-08-12 10:14:24 -070099
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700100Block
Junxiao Shi70911652014-08-12 10:14:24 -0700101ControlParameters::wireEncode() const
102{
103 if (m_wire.hasWire())
104 return m_wire;
105
106 EncodingEstimator estimator;
107 size_t estimatedSize = wireEncode(estimator);
108
109 EncodingBuffer buffer(estimatedSize, 0);
110 wireEncode(buffer);
111
112 m_wire = buffer.block();
113 return m_wire;
114}
115
116void
117ControlParameters::wireDecode(const Block& block)
118{
119 if (block.type() != tlv::nfd::ControlParameters) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters"));
Junxiao Shi70911652014-08-12 10:14:24 -0700121 }
122 m_wire = block;
123 m_wire.parse();
124 Block::element_const_iterator val;
125
126 val = m_wire.find(tlv::Name);
127 m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
128 if (this->hasName()) {
129 m_name.wireDecode(*val);
130 }
131
132 val = m_wire.find(tlv::nfd::FaceId);
133 m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
134 if (this->hasFaceId()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000135 m_faceId = readNonNegativeInteger(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700136 }
137
138 val = m_wire.find(tlv::nfd::Uri);
139 m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
140 if (this->hasUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000141 m_uri = readString(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700142 }
143
Eric Newberryd7f5b282017-03-28 19:55:20 -0700144 val = m_wire.find(tlv::nfd::LocalUri);
145 m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
146 if (this->hasLocalUri()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000147 m_localUri = readString(*val);
Eric Newberryd7f5b282017-03-28 19:55:20 -0700148 }
149
Junxiao Shi70911652014-08-12 10:14:24 -0700150 val = m_wire.find(tlv::nfd::Origin);
151 m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
152 if (this->hasOrigin()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000153 m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700154 }
155
156 val = m_wire.find(tlv::nfd::Cost);
157 m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
158 if (this->hasCost()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000159 m_cost = readNonNegativeInteger(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700160 }
161
Junxiao Shi22f85682018-01-22 19:23:22 +0000162 val = m_wire.find(tlv::nfd::Capacity);
163 m_hasFields[CONTROL_PARAMETER_CAPACITY] = val != m_wire.elements_end();
164 if (this->hasCapacity()) {
165 m_capacity = readNonNegativeInteger(*val);
166 }
167
Junxiao Shi70911652014-08-12 10:14:24 -0700168 val = m_wire.find(tlv::nfd::Flags);
169 m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
170 if (this->hasFlags()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000171 m_flags = readNonNegativeInteger(*val);
Junxiao Shi70911652014-08-12 10:14:24 -0700172 }
173
Eric Newberryda916d62016-08-11 23:04:34 -0700174 val = m_wire.find(tlv::nfd::Mask);
175 m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
176 if (this->hasMask()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000177 m_mask = readNonNegativeInteger(*val);
Eric Newberryda916d62016-08-11 23:04:34 -0700178 }
179
Junxiao Shi70911652014-08-12 10:14:24 -0700180 val = m_wire.find(tlv::nfd::Strategy);
181 m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
182 if (this->hasStrategy()) {
183 val->parse();
184 if (val->elements().empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700185 BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name"));
Junxiao Shi70911652014-08-12 10:14:24 -0700186 }
187 else {
188 m_strategy.wireDecode(*val->elements_begin());
189 }
190 }
191
192 val = m_wire.find(tlv::nfd::ExpirationPeriod);
193 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
194 if (this->hasExpirationPeriod()) {
195 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
196 }
Yukai Tud93c5fc2015-08-25 11:37:16 +0800197
198 val = m_wire.find(tlv::nfd::FacePersistency);
199 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
200 if (this->hasFacePersistency()) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000201 m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800202 }
Junxiao Shi70911652014-08-12 10:14:24 -0700203}
204
Eric Newberryda916d62016-08-11 23:04:34 -0700205bool
206ControlParameters::hasFlagBit(size_t bit) const
207{
208 if (bit >= 64) {
209 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
210 }
211
212 if (!hasMask()) {
213 return false;
214 }
215
216 return getMask() & (1 << bit);
217}
218
219bool
220ControlParameters::getFlagBit(size_t bit) const
221{
222 if (bit >= 64) {
223 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
224 }
225
226 if (!hasFlags()) {
227 return false;
228 }
229
230 return getFlags() & (1 << bit);
231}
232
233ControlParameters&
234ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
235{
236 if (bit >= 64) {
237 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
238 }
239
240 uint64_t flags = hasFlags() ? getFlags() : 0;
241 if (value) {
242 flags |= (1 << bit);
243 }
244 else {
245 flags &= ~(1 << bit);
246 }
247 setFlags(flags);
248
249 if (wantMask) {
250 uint64_t mask = hasMask() ? getMask() : 0;
251 mask |= (1 << bit);
252 setMask(mask);
253 }
254
255 return *this;
256}
257
258ControlParameters&
259ControlParameters::unsetFlagBit(size_t bit)
260{
261 if (bit >= 64) {
262 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
263 }
264
265 uint64_t mask = hasMask() ? getMask() : 0;
266 mask &= ~(1 << bit);
267 if (mask == 0) {
268 unsetMask();
269 unsetFlags();
270 }
271 else {
272 setMask(mask);
273 }
274
275 return *this;
276}
277
Junxiao Shi70911652014-08-12 10:14:24 -0700278std::ostream&
279operator<<(std::ostream& os, const ControlParameters& parameters)
280{
281 os << "ControlParameters(";
282
283 if (parameters.hasName()) {
284 os << "Name: " << parameters.getName() << ", ";
285 }
286
287 if (parameters.hasFaceId()) {
288 os << "FaceId: " << parameters.getFaceId() << ", ";
289 }
290
291 if (parameters.hasUri()) {
292 os << "Uri: " << parameters.getUri() << ", ";
293 }
294
Eric Newberryd7f5b282017-03-28 19:55:20 -0700295 if (parameters.hasLocalUri()) {
296 os << "LocalUri: " << parameters.getLocalUri() << ", ";
297 }
298
Junxiao Shi70911652014-08-12 10:14:24 -0700299 if (parameters.hasOrigin()) {
300 os << "Origin: " << parameters.getOrigin() << ", ";
301 }
302
303 if (parameters.hasCost()) {
304 os << "Cost: " << parameters.getCost() << ", ";
305 }
306
Junxiao Shi22f85682018-01-22 19:23:22 +0000307 if (parameters.hasCapacity()) {
308 os << "Capacity: " << parameters.getCapacity() << ", ";
309 }
310
Junxiao Shi70911652014-08-12 10:14:24 -0700311 if (parameters.hasFlags()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500312 os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
Eric Newberryda916d62016-08-11 23:04:34 -0700313 }
314
315 if (parameters.hasMask()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500316 os << "Mask: " << AsHex{parameters.getMask()} << ", ";
Junxiao Shi70911652014-08-12 10:14:24 -0700317 }
318
319 if (parameters.hasStrategy()) {
320 os << "Strategy: " << parameters.getStrategy() << ", ";
321 }
322
323 if (parameters.hasExpirationPeriod()) {
324 os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
325 }
326
Yanbiao Licbdacb22016-08-02 16:02:35 +0800327 if (parameters.hasFacePersistency()) {
328 os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
329 }
330
Junxiao Shi70911652014-08-12 10:14:24 -0700331 os << ")";
332 return os;
333}
334
335} // namespace nfd
336} // namespace ndn