blob: a489c3fa747256f7ab46f39723005c6662b58522 [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoe78eeca2017-02-23 23:22:32 -05003 * Copyright (c) 2013-2017 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/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#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 }
71 if (this->hasCost()) {
72 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost);
73 }
74 if (this->hasOrigin()) {
75 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin);
76 }
77 if (this->hasLocalControlFeature()) {
78 totalLength += prependNonNegativeIntegerBlock(encoder,
79 tlv::nfd::LocalControlFeature, m_localControlFeature);
80 }
Eric Newberryd7f5b282017-03-28 19:55:20 -070081 if (this->hasLocalUri()) {
82 size_t valLength = encoder.prependByteArray(
83 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
84 totalLength += valLength;
85 totalLength += encoder.prependVarNumber(valLength);
86 totalLength += encoder.prependVarNumber(tlv::nfd::LocalUri);
87 }
Junxiao Shi70911652014-08-12 10:14:24 -070088 if (this->hasUri()) {
89 size_t valLength = encoder.prependByteArray(
90 reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
91 totalLength += valLength;
92 totalLength += encoder.prependVarNumber(valLength);
93 totalLength += encoder.prependVarNumber(tlv::nfd::Uri);
94 }
95 if (this->hasFaceId()) {
96 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
97 }
98 if (this->hasName()) {
99 totalLength += m_name.wireEncode(encoder);
100 }
101
102 totalLength += encoder.prependVarNumber(totalLength);
103 totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
104 return totalLength;
105}
106
107template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800108ControlParameters::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi70911652014-08-12 10:14:24 -0700109
110template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800111ControlParameters::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi70911652014-08-12 10:14:24 -0700112
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700113Block
Junxiao Shi70911652014-08-12 10:14:24 -0700114ControlParameters::wireEncode() const
115{
116 if (m_wire.hasWire())
117 return m_wire;
118
119 EncodingEstimator estimator;
120 size_t estimatedSize = wireEncode(estimator);
121
122 EncodingBuffer buffer(estimatedSize, 0);
123 wireEncode(buffer);
124
125 m_wire = buffer.block();
126 return m_wire;
127}
128
129void
130ControlParameters::wireDecode(const Block& block)
131{
132 if (block.type() != tlv::nfd::ControlParameters) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700133 BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters"));
Junxiao Shi70911652014-08-12 10:14:24 -0700134 }
135 m_wire = block;
136 m_wire.parse();
137 Block::element_const_iterator val;
138
139 val = m_wire.find(tlv::Name);
140 m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
141 if (this->hasName()) {
142 m_name.wireDecode(*val);
143 }
144
145 val = m_wire.find(tlv::nfd::FaceId);
146 m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
147 if (this->hasFaceId()) {
148 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
149 }
150
151 val = m_wire.find(tlv::nfd::Uri);
152 m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
153 if (this->hasUri()) {
154 m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
155 }
156
Eric Newberryd7f5b282017-03-28 19:55:20 -0700157 val = m_wire.find(tlv::nfd::LocalUri);
158 m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
159 if (this->hasLocalUri()) {
160 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
161 }
162
Junxiao Shi70911652014-08-12 10:14:24 -0700163 val = m_wire.find(tlv::nfd::LocalControlFeature);
164 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = val != m_wire.elements_end();
165 if (this->hasLocalControlFeature()) {
166 m_localControlFeature = static_cast<LocalControlFeature>(readNonNegativeInteger(*val));
167 }
168
169 val = m_wire.find(tlv::nfd::Origin);
170 m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
171 if (this->hasOrigin()) {
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400172 m_origin = static_cast<RouteOrigin>(readNonNegativeInteger(*val));
Junxiao Shi70911652014-08-12 10:14:24 -0700173 }
174
175 val = m_wire.find(tlv::nfd::Cost);
176 m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
177 if (this->hasCost()) {
178 m_cost = static_cast<uint64_t>(readNonNegativeInteger(*val));
179 }
180
181 val = m_wire.find(tlv::nfd::Flags);
182 m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
183 if (this->hasFlags()) {
184 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
185 }
186
Eric Newberryda916d62016-08-11 23:04:34 -0700187 val = m_wire.find(tlv::nfd::Mask);
188 m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
189 if (this->hasMask()) {
190 m_mask = static_cast<uint64_t>(readNonNegativeInteger(*val));
191 }
192
Junxiao Shi70911652014-08-12 10:14:24 -0700193 val = m_wire.find(tlv::nfd::Strategy);
194 m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
195 if (this->hasStrategy()) {
196 val->parse();
197 if (val->elements().empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700198 BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name"));
Junxiao Shi70911652014-08-12 10:14:24 -0700199 }
200 else {
201 m_strategy.wireDecode(*val->elements_begin());
202 }
203 }
204
205 val = m_wire.find(tlv::nfd::ExpirationPeriod);
206 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
207 if (this->hasExpirationPeriod()) {
208 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
209 }
Yukai Tud93c5fc2015-08-25 11:37:16 +0800210
211 val = m_wire.find(tlv::nfd::FacePersistency);
212 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
213 if (this->hasFacePersistency()) {
214 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
215 }
Junxiao Shi70911652014-08-12 10:14:24 -0700216}
217
Eric Newberryda916d62016-08-11 23:04:34 -0700218bool
219ControlParameters::hasFlagBit(size_t bit) const
220{
221 if (bit >= 64) {
222 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
223 }
224
225 if (!hasMask()) {
226 return false;
227 }
228
229 return getMask() & (1 << bit);
230}
231
232bool
233ControlParameters::getFlagBit(size_t bit) const
234{
235 if (bit >= 64) {
236 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
237 }
238
239 if (!hasFlags()) {
240 return false;
241 }
242
243 return getFlags() & (1 << bit);
244}
245
246ControlParameters&
247ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
248{
249 if (bit >= 64) {
250 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
251 }
252
253 uint64_t flags = hasFlags() ? getFlags() : 0;
254 if (value) {
255 flags |= (1 << bit);
256 }
257 else {
258 flags &= ~(1 << bit);
259 }
260 setFlags(flags);
261
262 if (wantMask) {
263 uint64_t mask = hasMask() ? getMask() : 0;
264 mask |= (1 << bit);
265 setMask(mask);
266 }
267
268 return *this;
269}
270
271ControlParameters&
272ControlParameters::unsetFlagBit(size_t bit)
273{
274 if (bit >= 64) {
275 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
276 }
277
278 uint64_t mask = hasMask() ? getMask() : 0;
279 mask &= ~(1 << bit);
280 if (mask == 0) {
281 unsetMask();
282 unsetFlags();
283 }
284 else {
285 setMask(mask);
286 }
287
288 return *this;
289}
290
Junxiao Shi70911652014-08-12 10:14:24 -0700291std::ostream&
292operator<<(std::ostream& os, const ControlParameters& parameters)
293{
294 os << "ControlParameters(";
295
296 if (parameters.hasName()) {
297 os << "Name: " << parameters.getName() << ", ";
298 }
299
300 if (parameters.hasFaceId()) {
301 os << "FaceId: " << parameters.getFaceId() << ", ";
302 }
303
304 if (parameters.hasUri()) {
305 os << "Uri: " << parameters.getUri() << ", ";
306 }
307
Eric Newberryd7f5b282017-03-28 19:55:20 -0700308 if (parameters.hasLocalUri()) {
309 os << "LocalUri: " << parameters.getLocalUri() << ", ";
310 }
311
Junxiao Shi70911652014-08-12 10:14:24 -0700312 if (parameters.hasLocalControlFeature()) {
313 os << "LocalControlFeature: " << parameters.getLocalControlFeature() << ", ";
314 }
315
316 if (parameters.hasOrigin()) {
317 os << "Origin: " << parameters.getOrigin() << ", ";
318 }
319
320 if (parameters.hasCost()) {
321 os << "Cost: " << parameters.getCost() << ", ";
322 }
323
324 if (parameters.hasFlags()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500325 os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
Eric Newberryda916d62016-08-11 23:04:34 -0700326 }
327
328 if (parameters.hasMask()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500329 os << "Mask: " << AsHex{parameters.getMask()} << ", ";
Junxiao Shi70911652014-08-12 10:14:24 -0700330 }
331
332 if (parameters.hasStrategy()) {
333 os << "Strategy: " << parameters.getStrategy() << ", ";
334 }
335
336 if (parameters.hasExpirationPeriod()) {
337 os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
338 }
339
Yanbiao Licbdacb22016-08-02 16:02:35 +0800340 if (parameters.hasFacePersistency()) {
341 os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
342 }
343
Junxiao Shi70911652014-08-12 10:14:24 -0700344 os << ")";
345 return os;
346}
347
348} // namespace nfd
349} // namespace ndn