Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 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/block-helpers.hpp" |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 24 | #include "encoding/tlv-nfd.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 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 | } |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 77 | if (this->hasLocalUri()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 78 | totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri); |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 79 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 80 | if (this->hasUri()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 81 | totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_uri); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 82 | } |
| 83 | if (this->hasFaceId()) { |
| 84 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId); |
| 85 | } |
| 86 | if (this->hasName()) { |
| 87 | totalLength += m_name.wireEncode(encoder); |
| 88 | } |
| 89 | |
| 90 | totalLength += encoder.prependVarNumber(totalLength); |
| 91 | totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters); |
| 92 | return totalLength; |
| 93 | } |
| 94 | |
| 95 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 96 | ControlParameters::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 97 | |
| 98 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 99 | ControlParameters::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 100 | |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 101 | Block |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 102 | ControlParameters::wireEncode() const |
| 103 | { |
| 104 | if (m_wire.hasWire()) |
| 105 | return m_wire; |
| 106 | |
| 107 | EncodingEstimator estimator; |
| 108 | size_t estimatedSize = wireEncode(estimator); |
| 109 | |
| 110 | EncodingBuffer buffer(estimatedSize, 0); |
| 111 | wireEncode(buffer); |
| 112 | |
| 113 | m_wire = buffer.block(); |
| 114 | return m_wire; |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | ControlParameters::wireDecode(const Block& block) |
| 119 | { |
| 120 | if (block.type() != tlv::nfd::ControlParameters) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 121 | BOOST_THROW_EXCEPTION(Error("Expecting TLV-TYPE ControlParameters")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 122 | } |
| 123 | m_wire = block; |
| 124 | m_wire.parse(); |
| 125 | Block::element_const_iterator val; |
| 126 | |
| 127 | val = m_wire.find(tlv::Name); |
| 128 | m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end(); |
| 129 | if (this->hasName()) { |
| 130 | m_name.wireDecode(*val); |
| 131 | } |
| 132 | |
| 133 | val = m_wire.find(tlv::nfd::FaceId); |
| 134 | m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end(); |
| 135 | if (this->hasFaceId()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 136 | m_faceId = readNonNegativeInteger(*val); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | val = m_wire.find(tlv::nfd::Uri); |
| 140 | m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end(); |
| 141 | if (this->hasUri()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 142 | m_uri = readString(*val); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 145 | val = m_wire.find(tlv::nfd::LocalUri); |
| 146 | m_hasFields[CONTROL_PARAMETER_LOCAL_URI] = val != m_wire.elements_end(); |
| 147 | if (this->hasLocalUri()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 148 | m_localUri = readString(*val); |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 151 | val = m_wire.find(tlv::nfd::Origin); |
| 152 | m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end(); |
| 153 | if (this->hasOrigin()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 154 | m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | val = m_wire.find(tlv::nfd::Cost); |
| 158 | m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end(); |
| 159 | if (this->hasCost()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 160 | m_cost = readNonNegativeInteger(*val); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | val = m_wire.find(tlv::nfd::Flags); |
| 164 | m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end(); |
| 165 | if (this->hasFlags()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 166 | m_flags = readNonNegativeInteger(*val); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 169 | val = m_wire.find(tlv::nfd::Mask); |
| 170 | m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end(); |
| 171 | if (this->hasMask()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 172 | m_mask = readNonNegativeInteger(*val); |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 175 | val = m_wire.find(tlv::nfd::Strategy); |
| 176 | m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end(); |
| 177 | if (this->hasStrategy()) { |
| 178 | val->parse(); |
| 179 | if (val->elements().empty()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 180 | BOOST_THROW_EXCEPTION(Error("Expecting Strategy/Name")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 181 | } |
| 182 | else { |
| 183 | m_strategy.wireDecode(*val->elements_begin()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | val = m_wire.find(tlv::nfd::ExpirationPeriod); |
| 188 | m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end(); |
| 189 | if (this->hasExpirationPeriod()) { |
| 190 | m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
| 191 | } |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 192 | |
| 193 | val = m_wire.find(tlv::nfd::FacePersistency); |
| 194 | m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = val != m_wire.elements_end(); |
| 195 | if (this->hasFacePersistency()) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 196 | m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val); |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 197 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 200 | bool |
| 201 | ControlParameters::hasFlagBit(size_t bit) const |
| 202 | { |
| 203 | if (bit >= 64) { |
| 204 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 205 | } |
| 206 | |
| 207 | if (!hasMask()) { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | return getMask() & (1 << bit); |
| 212 | } |
| 213 | |
| 214 | bool |
| 215 | ControlParameters::getFlagBit(size_t bit) const |
| 216 | { |
| 217 | if (bit >= 64) { |
| 218 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 219 | } |
| 220 | |
| 221 | if (!hasFlags()) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | return getFlags() & (1 << bit); |
| 226 | } |
| 227 | |
| 228 | ControlParameters& |
| 229 | ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/) |
| 230 | { |
| 231 | if (bit >= 64) { |
| 232 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 233 | } |
| 234 | |
| 235 | uint64_t flags = hasFlags() ? getFlags() : 0; |
| 236 | if (value) { |
| 237 | flags |= (1 << bit); |
| 238 | } |
| 239 | else { |
| 240 | flags &= ~(1 << bit); |
| 241 | } |
| 242 | setFlags(flags); |
| 243 | |
| 244 | if (wantMask) { |
| 245 | uint64_t mask = hasMask() ? getMask() : 0; |
| 246 | mask |= (1 << bit); |
| 247 | setMask(mask); |
| 248 | } |
| 249 | |
| 250 | return *this; |
| 251 | } |
| 252 | |
| 253 | ControlParameters& |
| 254 | ControlParameters::unsetFlagBit(size_t bit) |
| 255 | { |
| 256 | if (bit >= 64) { |
| 257 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 258 | } |
| 259 | |
| 260 | uint64_t mask = hasMask() ? getMask() : 0; |
| 261 | mask &= ~(1 << bit); |
| 262 | if (mask == 0) { |
| 263 | unsetMask(); |
| 264 | unsetFlags(); |
| 265 | } |
| 266 | else { |
| 267 | setMask(mask); |
| 268 | } |
| 269 | |
| 270 | return *this; |
| 271 | } |
| 272 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 273 | std::ostream& |
| 274 | operator<<(std::ostream& os, const ControlParameters& parameters) |
| 275 | { |
| 276 | os << "ControlParameters("; |
| 277 | |
| 278 | if (parameters.hasName()) { |
| 279 | os << "Name: " << parameters.getName() << ", "; |
| 280 | } |
| 281 | |
| 282 | if (parameters.hasFaceId()) { |
| 283 | os << "FaceId: " << parameters.getFaceId() << ", "; |
| 284 | } |
| 285 | |
| 286 | if (parameters.hasUri()) { |
| 287 | os << "Uri: " << parameters.getUri() << ", "; |
| 288 | } |
| 289 | |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 290 | if (parameters.hasLocalUri()) { |
| 291 | os << "LocalUri: " << parameters.getLocalUri() << ", "; |
| 292 | } |
| 293 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 294 | if (parameters.hasOrigin()) { |
| 295 | os << "Origin: " << parameters.getOrigin() << ", "; |
| 296 | } |
| 297 | |
| 298 | if (parameters.hasCost()) { |
| 299 | os << "Cost: " << parameters.getCost() << ", "; |
| 300 | } |
| 301 | |
| 302 | if (parameters.hasFlags()) { |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 303 | os << "Flags: " << AsHex{parameters.getFlags()} << ", "; |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (parameters.hasMask()) { |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 307 | os << "Mask: " << AsHex{parameters.getMask()} << ", "; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (parameters.hasStrategy()) { |
| 311 | os << "Strategy: " << parameters.getStrategy() << ", "; |
| 312 | } |
| 313 | |
| 314 | if (parameters.hasExpirationPeriod()) { |
| 315 | os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", "; |
| 316 | } |
| 317 | |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 318 | if (parameters.hasFacePersistency()) { |
| 319 | os << "FacePersistency: " << parameters.getFacePersistency() << ", "; |
| 320 | } |
| 321 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 322 | os << ")"; |
| 323 | return os; |
| 324 | } |
| 325 | |
| 326 | } // namespace nfd |
| 327 | } // namespace ndn |