Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "control-parameters.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 23 | #include "encoding/tlv-nfd.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/concepts.hpp" |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 26 | #include "util/string-helper.hpp" |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace nfd { |
| 30 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 31 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlParameters>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodable<ControlParameters>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireDecodable<ControlParameters>)); |
| 34 | static_assert(std::is_base_of<tlv::Error, ControlParameters::Error>::value, |
| 35 | "ControlParameters::Error must inherit from tlv::Error"); |
| 36 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 37 | ControlParameters::ControlParameters() |
| 38 | : m_hasFields(CONTROL_PARAMETER_UBOUND) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | ControlParameters::ControlParameters(const Block& block) |
| 43 | : m_hasFields(CONTROL_PARAMETER_UBOUND) |
| 44 | { |
| 45 | wireDecode(block); |
| 46 | } |
| 47 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 48 | template<encoding::Tag TAG> |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 49 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 50 | ControlParameters::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 51 | { |
| 52 | size_t totalLength = 0; |
| 53 | |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 54 | if (this->hasFacePersistency()) { |
| 55 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 56 | tlv::nfd::FacePersistency, m_facePersistency); |
| 57 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 58 | 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 Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 65 | if (this->hasMask()) { |
| 66 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask); |
| 67 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 68 | 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 Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 81 | 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 Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 88 | 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 | |
| 107 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 108 | ControlParameters::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 109 | |
| 110 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 111 | ControlParameters::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 112 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 113 | Block |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 114 | ControlParameters::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 | |
| 129 | void |
| 130 | ControlParameters::wireDecode(const Block& block) |
| 131 | { |
| 132 | if (block.type() != tlv::nfd::ControlParameters) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 133 | BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 134 | } |
| 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 Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 157 | 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 Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 163 | 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 Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 172 | m_origin = static_cast<RouteOrigin>(readNonNegativeInteger(*val)); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 173 | } |
| 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 Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 187 | 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 Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 193 | 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 198 | BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 199 | } |
| 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 Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 210 | |
| 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 Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 218 | bool |
| 219 | ControlParameters::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 | |
| 232 | bool |
| 233 | ControlParameters::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 | |
| 246 | ControlParameters& |
| 247 | ControlParameters::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 | |
| 271 | ControlParameters& |
| 272 | ControlParameters::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 Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 291 | std::ostream& |
| 292 | operator<<(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 Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 308 | if (parameters.hasLocalUri()) { |
| 309 | os << "LocalUri: " << parameters.getLocalUri() << ", "; |
| 310 | } |
| 311 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 312 | 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 Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 325 | os << "Flags: " << AsHex{parameters.getFlags()} << ", "; |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (parameters.hasMask()) { |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 329 | os << "Mask: " << AsHex{parameters.getMask()} << ", "; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (parameters.hasStrategy()) { |
| 333 | os << "Strategy: " << parameters.getStrategy() << ", "; |
| 334 | } |
| 335 | |
| 336 | if (parameters.hasExpirationPeriod()) { |
| 337 | os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", "; |
| 338 | } |
| 339 | |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 340 | if (parameters.hasFacePersistency()) { |
| 341 | os << "FacePersistency: " << parameters.getFacePersistency() << ", "; |
| 342 | } |
| 343 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 344 | os << ")"; |
| 345 | return os; |
| 346 | } |
| 347 | |
| 348 | } // namespace nfd |
| 349 | } // namespace ndn |