blob: bc18f13d797c3a3a929ff3e15f096baa110d34cd [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 }
Eric Newberryd7f5b282017-03-28 19:55:20 -070077 if (this->hasLocalUri()) {
78 size_t valLength = encoder.prependByteArray(
79 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
80 totalLength += valLength;
81 totalLength += encoder.prependVarNumber(valLength);
82 totalLength += encoder.prependVarNumber(tlv::nfd::LocalUri);
83 }
Junxiao Shi70911652014-08-12 10:14:24 -070084 if (this->hasUri()) {
85 size_t valLength = encoder.prependByteArray(
86 reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
87 totalLength += valLength;
88 totalLength += encoder.prependVarNumber(valLength);
89 totalLength += encoder.prependVarNumber(tlv::nfd::Uri);
90 }
91 if (this->hasFaceId()) {
92 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
93 }
94 if (this->hasName()) {
95 totalLength += m_name.wireEncode(encoder);
96 }
97
98 totalLength += encoder.prependVarNumber(totalLength);
99 totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters);
100 return totalLength;
101}
102
103template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800104ControlParameters::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi70911652014-08-12 10:14:24 -0700105
106template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800107ControlParameters::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi70911652014-08-12 10:14:24 -0700108
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700109Block
Junxiao Shi70911652014-08-12 10:14:24 -0700110ControlParameters::wireEncode() const
111{
112 if (m_wire.hasWire())
113 return m_wire;
114
115 EncodingEstimator estimator;
116 size_t estimatedSize = wireEncode(estimator);
117
118 EncodingBuffer buffer(estimatedSize, 0);
119 wireEncode(buffer);
120
121 m_wire = buffer.block();
122 return m_wire;
123}
124
125void
126ControlParameters::wireDecode(const Block& block)
127{
128 if (block.type() != tlv::nfd::ControlParameters) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700129 BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters"));
Junxiao Shi70911652014-08-12 10:14:24 -0700130 }
131 m_wire = block;
132 m_wire.parse();
133 Block::element_const_iterator val;
134
135 val = m_wire.find(tlv::Name);
136 m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
137 if (this->hasName()) {
138 m_name.wireDecode(*val);
139 }
140
141 val = m_wire.find(tlv::nfd::FaceId);
142 m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
143 if (this->hasFaceId()) {
144 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
145 }
146
147 val = m_wire.find(tlv::nfd::Uri);
148 m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
149 if (this->hasUri()) {
150 m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
151 }
152
Eric Newberryd7f5b282017-03-28 19:55:20 -0700153 val = m_wire.find(tlv::nfd::LocalUri);
154 m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end();
155 if (this->hasLocalUri()) {
156 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
157 }
158
Junxiao Shi70911652014-08-12 10:14:24 -0700159 val = m_wire.find(tlv::nfd::Origin);
160 m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
161 if (this->hasOrigin()) {
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400162 m_origin = static_cast<RouteOrigin>(readNonNegativeInteger(*val));
Junxiao Shi70911652014-08-12 10:14:24 -0700163 }
164
165 val = m_wire.find(tlv::nfd::Cost);
166 m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
167 if (this->hasCost()) {
168 m_cost = static_cast<uint64_t>(readNonNegativeInteger(*val));
169 }
170
171 val = m_wire.find(tlv::nfd::Flags);
172 m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
173 if (this->hasFlags()) {
174 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
175 }
176
Eric Newberryda916d62016-08-11 23:04:34 -0700177 val = m_wire.find(tlv::nfd::Mask);
178 m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
179 if (this->hasMask()) {
180 m_mask = static_cast<uint64_t>(readNonNegativeInteger(*val));
181 }
182
Junxiao Shi70911652014-08-12 10:14:24 -0700183 val = m_wire.find(tlv::nfd::Strategy);
184 m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
185 if (this->hasStrategy()) {
186 val->parse();
187 if (val->elements().empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700188 BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name"));
Junxiao Shi70911652014-08-12 10:14:24 -0700189 }
190 else {
191 m_strategy.wireDecode(*val->elements_begin());
192 }
193 }
194
195 val = m_wire.find(tlv::nfd::ExpirationPeriod);
196 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
197 if (this->hasExpirationPeriod()) {
198 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
199 }
Yukai Tud93c5fc2015-08-25 11:37:16 +0800200
201 val = m_wire.find(tlv::nfd::FacePersistency);
202 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end();
203 if (this->hasFacePersistency()) {
204 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
205 }
Junxiao Shi70911652014-08-12 10:14:24 -0700206}
207
Eric Newberryda916d62016-08-11 23:04:34 -0700208bool
209ControlParameters::hasFlagBit(size_t bit) const
210{
211 if (bit >= 64) {
212 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
213 }
214
215 if (!hasMask()) {
216 return false;
217 }
218
219 return getMask() & (1 << bit);
220}
221
222bool
223ControlParameters::getFlagBit(size_t bit) const
224{
225 if (bit >= 64) {
226 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
227 }
228
229 if (!hasFlags()) {
230 return false;
231 }
232
233 return getFlags() & (1 << bit);
234}
235
236ControlParameters&
237ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
238{
239 if (bit >= 64) {
240 BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
241 }
242
243 uint64_t flags = hasFlags() ? getFlags() : 0;
244 if (value) {
245 flags |= (1 << bit);
246 }
247 else {
248 flags &= ~(1 << bit);
249 }
250 setFlags(flags);
251
252 if (wantMask) {
253 uint64_t mask = hasMask() ? getMask() : 0;
254 mask |= (1 << bit);
255 setMask(mask);
256 }
257
258 return *this;
259}
260
261ControlParameters&
262ControlParameters::unsetFlagBit(size_t bit)
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 mask = hasMask() ? getMask() : 0;
269 mask &= ~(1 << bit);
270 if (mask == 0) {
271 unsetMask();
272 unsetFlags();
273 }
274 else {
275 setMask(mask);
276 }
277
278 return *this;
279}
280
Junxiao Shi70911652014-08-12 10:14:24 -0700281std::ostream&
282operator<<(std::ostream& os, const ControlParameters& parameters)
283{
284 os << "ControlParameters(";
285
286 if (parameters.hasName()) {
287 os << "Name: " << parameters.getName() << ", ";
288 }
289
290 if (parameters.hasFaceId()) {
291 os << "FaceId: " << parameters.getFaceId() << ", ";
292 }
293
294 if (parameters.hasUri()) {
295 os << "Uri: " << parameters.getUri() << ", ";
296 }
297
Eric Newberryd7f5b282017-03-28 19:55:20 -0700298 if (parameters.hasLocalUri()) {
299 os << "LocalUri: " << parameters.getLocalUri() << ", ";
300 }
301
Junxiao Shi70911652014-08-12 10:14:24 -0700302 if (parameters.hasOrigin()) {
303 os << "Origin: " << parameters.getOrigin() << ", ";
304 }
305
306 if (parameters.hasCost()) {
307 os << "Cost: " << parameters.getCost() << ", ";
308 }
309
310 if (parameters.hasFlags()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500311 os << "Flags: " << AsHex{parameters.getFlags()} << ", ";
Eric Newberryda916d62016-08-11 23:04:34 -0700312 }
313
314 if (parameters.hasMask()) {
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500315 os << "Mask: " << AsHex{parameters.getMask()} << ", ";
Junxiao Shi70911652014-08-12 10:14:24 -0700316 }
317
318 if (parameters.hasStrategy()) {
319 os << "Strategy: " << parameters.getStrategy() << ", ";
320 }
321
322 if (parameters.hasExpirationPeriod()) {
323 os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", ";
324 }
325
Yanbiao Licbdacb22016-08-02 16:02:35 +0800326 if (parameters.hasFacePersistency()) {
327 os << "FacePersistency: " << parameters.getFacePersistency() << ", ";
328 }
329
Junxiao Shi70911652014-08-12 10:14:24 -0700330 os << ")";
331 return os;
332}
333
334} // namespace nfd
335} // namespace ndn