Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP |
| 24 | |
| 25 | #include "../name.hpp" |
| 26 | #include "../encoding/tlv-nfd.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame^] | 27 | #include "../encoding/block-helpers.hpp" |
| 28 | #include "../util/time.hpp" |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace nfd { |
| 32 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 33 | /** |
| 34 | * \ingroup management |
| 35 | */ |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 36 | enum ControlParameterField { |
| 37 | CONTROL_PARAMETER_NAME, |
| 38 | CONTROL_PARAMETER_FACE_ID, |
| 39 | CONTROL_PARAMETER_URI, |
| 40 | CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE, |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 41 | CONTROL_PARAMETER_ORIGIN, |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 42 | CONTROL_PARAMETER_COST, |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 43 | CONTROL_PARAMETER_FLAGS, |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 44 | CONTROL_PARAMETER_STRATEGY, |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 45 | CONTROL_PARAMETER_EXPIRATION_PERIOD, |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 46 | CONTROL_PARAMETER_UBOUND |
| 47 | }; |
| 48 | |
| 49 | const std::string CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND] = { |
| 50 | "Name", |
| 51 | "FaceId", |
| 52 | "Uri", |
| 53 | "LocalControlFeature", |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 54 | "Origin", |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 55 | "Cost", |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 56 | "Flags", |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 57 | "Strategy", |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 58 | "ExpirationPeriod", |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 61 | /** |
| 62 | * \ingroup management |
| 63 | */ |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 64 | enum LocalControlFeature { |
| 65 | LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID = 1, |
| 66 | LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID = 2 |
| 67 | }; |
| 68 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 69 | /** |
| 70 | * \ingroup management |
| 71 | * \brief represents parameters in a ControlCommand request or response |
| 72 | * \sa http://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 73 | */ |
| 74 | class ControlParameters |
| 75 | { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 76 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 77 | class Error : public tlv::Error |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 78 | { |
| 79 | public: |
| 80 | explicit |
| 81 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 82 | : tlv::Error(what) |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 83 | { |
| 84 | } |
| 85 | }; |
| 86 | |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 87 | ControlParameters() |
| 88 | : m_hasFields(CONTROL_PARAMETER_UBOUND) |
| 89 | { |
| 90 | } |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 91 | |
| 92 | explicit |
| 93 | ControlParameters(const Block& block) |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 94 | : m_hasFields(CONTROL_PARAMETER_UBOUND) |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 95 | { |
| 96 | wireDecode(block); |
| 97 | } |
| 98 | |
| 99 | template<bool T> |
| 100 | size_t |
| 101 | wireEncode(EncodingImpl<T>& encoder) const; |
| 102 | |
| 103 | const Block& |
| 104 | wireEncode() const; |
| 105 | |
| 106 | void |
| 107 | wireDecode(const Block& wire); |
| 108 | |
| 109 | public: // getters & setters |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 111 | bool |
| 112 | hasName() const |
| 113 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 114 | return m_hasFields[CONTROL_PARAMETER_NAME]; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | const Name& |
| 118 | getName() const |
| 119 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 120 | BOOST_ASSERT(this->hasName()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 121 | return m_name; |
| 122 | } |
| 123 | |
| 124 | ControlParameters& |
| 125 | setName(const Name& name) |
| 126 | { |
| 127 | m_wire.reset(); |
| 128 | m_name = name; |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 129 | m_hasFields[CONTROL_PARAMETER_NAME] = true; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | ControlParameters& |
| 134 | unsetName() |
| 135 | { |
| 136 | m_wire.reset(); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 137 | m_hasFields[CONTROL_PARAMETER_NAME] = false; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 138 | return *this; |
| 139 | } |
| 140 | |
| 141 | bool |
| 142 | hasFaceId() const |
| 143 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 144 | return m_hasFields[CONTROL_PARAMETER_FACE_ID]; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | uint64_t |
| 148 | getFaceId() const |
| 149 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 150 | BOOST_ASSERT(this->hasFaceId()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 151 | return m_faceId; |
| 152 | } |
| 153 | |
| 154 | ControlParameters& |
| 155 | setFaceId(uint64_t faceId) |
| 156 | { |
| 157 | m_wire.reset(); |
| 158 | m_faceId = faceId; |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 159 | m_hasFields[CONTROL_PARAMETER_FACE_ID] = true; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 160 | return *this; |
| 161 | } |
| 162 | |
| 163 | ControlParameters& |
| 164 | unsetFaceId() |
| 165 | { |
| 166 | m_wire.reset(); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 167 | m_hasFields[CONTROL_PARAMETER_FACE_ID] = false; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 168 | return *this; |
| 169 | } |
| 170 | |
| 171 | bool |
| 172 | hasUri() const |
| 173 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 174 | return m_hasFields[CONTROL_PARAMETER_URI]; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | const std::string& |
| 178 | getUri() const |
| 179 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 180 | BOOST_ASSERT(this->hasUri()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 181 | return m_uri; |
| 182 | } |
| 183 | |
| 184 | ControlParameters& |
| 185 | setUri(const std::string& uri) |
| 186 | { |
| 187 | m_wire.reset(); |
| 188 | m_uri = uri; |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 189 | m_hasFields[CONTROL_PARAMETER_URI] = true; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 190 | return *this; |
| 191 | } |
| 192 | |
| 193 | ControlParameters& |
| 194 | unsetUri() |
| 195 | { |
| 196 | m_wire.reset(); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 197 | m_hasFields[CONTROL_PARAMETER_URI] = false; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 198 | return *this; |
| 199 | } |
| 200 | |
| 201 | bool |
| 202 | hasLocalControlFeature() const |
| 203 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 204 | return m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE]; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | LocalControlFeature |
| 208 | getLocalControlFeature() const |
| 209 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 210 | BOOST_ASSERT(this->hasLocalControlFeature()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 211 | return m_localControlFeature; |
| 212 | } |
| 213 | |
| 214 | ControlParameters& |
| 215 | setLocalControlFeature(LocalControlFeature localControlFeature) |
| 216 | { |
| 217 | m_wire.reset(); |
| 218 | m_localControlFeature = localControlFeature; |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 219 | m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = true; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 220 | return *this; |
| 221 | } |
| 222 | |
| 223 | ControlParameters& |
| 224 | unsetLocalControlFeature() |
| 225 | { |
| 226 | m_wire.reset(); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 227 | m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = false; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 228 | return *this; |
| 229 | } |
| 230 | |
| 231 | bool |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 232 | hasOrigin() const |
| 233 | { |
| 234 | return m_hasFields[CONTROL_PARAMETER_ORIGIN]; |
| 235 | } |
| 236 | |
| 237 | uint64_t |
| 238 | getOrigin() const |
| 239 | { |
| 240 | BOOST_ASSERT(this->hasOrigin()); |
| 241 | return m_origin; |
| 242 | } |
| 243 | |
| 244 | ControlParameters& |
| 245 | setOrigin(uint64_t origin) |
| 246 | { |
| 247 | m_wire.reset(); |
| 248 | m_origin = origin; |
| 249 | m_hasFields[CONTROL_PARAMETER_ORIGIN] = true; |
| 250 | return *this; |
| 251 | } |
| 252 | |
| 253 | ControlParameters& |
| 254 | unsetOrigin() |
| 255 | { |
| 256 | m_wire.reset(); |
| 257 | m_hasFields[CONTROL_PARAMETER_ORIGIN] = false; |
| 258 | return *this; |
| 259 | } |
| 260 | |
| 261 | bool |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 262 | hasCost() const |
| 263 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 264 | return m_hasFields[CONTROL_PARAMETER_COST]; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | uint64_t |
| 268 | getCost() const |
| 269 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 270 | BOOST_ASSERT(this->hasCost()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 271 | return m_cost; |
| 272 | } |
| 273 | |
| 274 | ControlParameters& |
| 275 | setCost(uint64_t cost) |
| 276 | { |
| 277 | m_wire.reset(); |
| 278 | m_cost = cost; |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 279 | m_hasFields[CONTROL_PARAMETER_COST] = true; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 280 | return *this; |
| 281 | } |
| 282 | |
| 283 | ControlParameters& |
| 284 | unsetCost() |
| 285 | { |
| 286 | m_wire.reset(); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 287 | m_hasFields[CONTROL_PARAMETER_COST] = false; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 288 | return *this; |
| 289 | } |
| 290 | |
| 291 | bool |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 292 | hasFlags() const |
| 293 | { |
| 294 | return m_hasFields[CONTROL_PARAMETER_FLAGS]; |
| 295 | } |
| 296 | |
| 297 | uint64_t |
| 298 | getFlags() const |
| 299 | { |
| 300 | BOOST_ASSERT(this->hasFlags()); |
| 301 | return m_flags; |
| 302 | } |
| 303 | |
| 304 | ControlParameters& |
| 305 | setFlags(uint64_t flags) |
| 306 | { |
| 307 | m_wire.reset(); |
| 308 | m_flags = flags; |
| 309 | m_hasFields[CONTROL_PARAMETER_FLAGS] = true; |
| 310 | return *this; |
| 311 | } |
| 312 | |
| 313 | ControlParameters& |
| 314 | unsetFlags() |
| 315 | { |
| 316 | m_wire.reset(); |
| 317 | m_hasFields[CONTROL_PARAMETER_FLAGS] = false; |
| 318 | return *this; |
| 319 | } |
| 320 | |
| 321 | bool |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 322 | hasStrategy() const |
| 323 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 324 | return m_hasFields[CONTROL_PARAMETER_STRATEGY]; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | const Name& |
| 328 | getStrategy() const |
| 329 | { |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 330 | BOOST_ASSERT(this->hasStrategy()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 331 | return m_strategy; |
| 332 | } |
| 333 | |
| 334 | ControlParameters& |
| 335 | setStrategy(const Name& strategy) |
| 336 | { |
| 337 | m_wire.reset(); |
| 338 | m_strategy = strategy; |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 339 | m_hasFields[CONTROL_PARAMETER_STRATEGY] = true; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 340 | return *this; |
| 341 | } |
| 342 | |
| 343 | ControlParameters& |
| 344 | unsetStrategy() |
| 345 | { |
| 346 | m_wire.reset(); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 347 | m_hasFields[CONTROL_PARAMETER_STRATEGY] = false; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 348 | return *this; |
| 349 | } |
| 350 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 351 | bool |
| 352 | hasExpirationPeriod() const |
| 353 | { |
| 354 | return m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD]; |
| 355 | } |
| 356 | |
| 357 | const time::milliseconds& |
| 358 | getExpirationPeriod() const |
| 359 | { |
| 360 | BOOST_ASSERT(this->hasExpirationPeriod()); |
| 361 | return m_expirationPeriod; |
| 362 | } |
| 363 | |
| 364 | ControlParameters& |
| 365 | setExpirationPeriod(const time::milliseconds& expirationPeriod) |
| 366 | { |
| 367 | m_wire.reset(); |
| 368 | m_expirationPeriod = expirationPeriod; |
| 369 | m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = true; |
| 370 | return *this; |
| 371 | } |
| 372 | |
| 373 | ControlParameters& |
| 374 | unsetExpirationPeriod() |
| 375 | { |
| 376 | m_wire.reset(); |
| 377 | m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = false; |
| 378 | return *this; |
| 379 | } |
| 380 | |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 381 | const std::vector<bool>& |
| 382 | getPresentFields() const |
| 383 | { |
| 384 | return m_hasFields; |
| 385 | } |
| 386 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 387 | private: // fields |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 388 | std::vector<bool> m_hasFields; |
| 389 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 390 | Name m_name; |
| 391 | uint64_t m_faceId; |
| 392 | std::string m_uri; |
| 393 | LocalControlFeature m_localControlFeature; |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 394 | uint64_t m_origin; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 395 | uint64_t m_cost; |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 396 | uint64_t m_flags; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 397 | Name m_strategy; |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 398 | time::milliseconds m_expirationPeriod; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 399 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 400 | private: |
| 401 | mutable Block m_wire; |
| 402 | }; |
| 403 | |
| 404 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 405 | template<bool T> |
| 406 | inline size_t |
| 407 | ControlParameters::wireEncode(EncodingImpl<T>& encoder) const |
| 408 | { |
| 409 | size_t totalLength = 0; |
| 410 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 411 | if (this->hasExpirationPeriod()) { |
| 412 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 413 | tlv::nfd::ExpirationPeriod, m_expirationPeriod.count()); |
| 414 | } |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 415 | if (this->hasStrategy()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 416 | totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy); |
| 417 | } |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 418 | if (this->hasFlags()) { |
| 419 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags); |
| 420 | } |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 421 | if (this->hasCost()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 422 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost); |
| 423 | } |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 424 | if (this->hasOrigin()) { |
| 425 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Origin, m_origin); |
| 426 | } |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 427 | if (this->hasLocalControlFeature()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 428 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 429 | tlv::nfd::LocalControlFeature, m_localControlFeature); |
| 430 | } |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 431 | if (this->hasUri()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 432 | size_t valLength = encoder.prependByteArray( |
| 433 | reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size()); |
| 434 | totalLength += valLength; |
| 435 | totalLength += encoder.prependVarNumber(valLength); |
| 436 | totalLength += encoder.prependVarNumber(tlv::nfd::Uri); |
| 437 | } |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 438 | if (this->hasFaceId()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 439 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId); |
| 440 | } |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 441 | if (this->hasName()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 442 | totalLength += m_name.wireEncode(encoder); |
| 443 | } |
| 444 | |
| 445 | totalLength += encoder.prependVarNumber(totalLength); |
| 446 | totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters); |
| 447 | return totalLength; |
| 448 | } |
| 449 | |
| 450 | inline const Block& |
| 451 | ControlParameters::wireEncode() const |
| 452 | { |
| 453 | if (m_wire.hasWire()) |
| 454 | return m_wire; |
| 455 | |
| 456 | EncodingEstimator estimator; |
| 457 | size_t estimatedSize = wireEncode(estimator); |
| 458 | |
| 459 | EncodingBuffer buffer(estimatedSize, 0); |
| 460 | wireEncode(buffer); |
| 461 | |
| 462 | m_wire = buffer.block(); |
| 463 | return m_wire; |
| 464 | } |
| 465 | |
| 466 | inline void |
| 467 | ControlParameters::wireDecode(const Block& block) |
| 468 | { |
| 469 | if (block.type() != tlv::nfd::ControlParameters) { |
| 470 | throw Error("expecting TLV-TYPE ControlParameters"); |
| 471 | } |
| 472 | m_wire = block; |
| 473 | m_wire.parse(); |
| 474 | Block::element_const_iterator val; |
| 475 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 476 | val = m_wire.find(tlv::Name); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 477 | m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end(); |
| 478 | if (this->hasName()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 479 | m_name.wireDecode(*val); |
| 480 | } |
| 481 | |
| 482 | val = m_wire.find(tlv::nfd::FaceId); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 483 | m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end(); |
| 484 | if (this->hasFaceId()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 485 | m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 486 | } |
| 487 | |
| 488 | val = m_wire.find(tlv::nfd::Uri); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 489 | m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end(); |
| 490 | if (this->hasUri()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 491 | m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 492 | } |
| 493 | |
| 494 | val = m_wire.find(tlv::nfd::LocalControlFeature); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 495 | m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = val != m_wire.elements_end(); |
| 496 | if (this->hasLocalControlFeature()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 497 | m_localControlFeature = static_cast<LocalControlFeature>(readNonNegativeInteger(*val)); |
| 498 | } |
| 499 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 500 | val = m_wire.find(tlv::nfd::Origin); |
| 501 | m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end(); |
| 502 | if (this->hasOrigin()) { |
| 503 | m_origin = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 504 | } |
| 505 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 506 | val = m_wire.find(tlv::nfd::Cost); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 507 | m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end(); |
| 508 | if (this->hasCost()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 509 | m_cost = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 510 | } |
| 511 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 512 | val = m_wire.find(tlv::nfd::Flags); |
| 513 | m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end(); |
| 514 | if (this->hasFlags()) { |
| 515 | m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 516 | } |
| 517 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 518 | val = m_wire.find(tlv::nfd::Strategy); |
Junxiao Shi | 5ec8022 | 2014-03-25 20:08:05 -0700 | [diff] [blame] | 519 | m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end(); |
| 520 | if (this->hasStrategy()) { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 521 | val->parse(); |
| 522 | if (val->elements().empty()) { |
| 523 | throw Error("expecting Strategy/Name"); |
| 524 | } |
| 525 | else { |
| 526 | m_strategy.wireDecode(*val->elements_begin()); |
| 527 | } |
| 528 | } |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 529 | |
| 530 | val = m_wire.find(tlv::nfd::ExpirationPeriod); |
| 531 | m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end(); |
| 532 | if (this->hasExpirationPeriod()) { |
| 533 | m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
| 534 | } |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | inline std::ostream& |
| 538 | operator<<(std::ostream& os, const ControlParameters& parameters) |
| 539 | { |
| 540 | os << "ControlParameters("; |
| 541 | |
| 542 | if (parameters.hasName()) { |
| 543 | os << "Name: " << parameters.getName() << ", "; |
| 544 | } |
| 545 | |
| 546 | if (parameters.hasFaceId()) { |
| 547 | os << "FaceId: " << parameters.getFaceId() << ", "; |
| 548 | } |
| 549 | |
| 550 | if (parameters.hasUri()) { |
| 551 | os << "Uri: " << parameters.getUri() << ", "; |
| 552 | } |
| 553 | |
| 554 | if (parameters.hasLocalControlFeature()) { |
| 555 | os << "LocalControlFeature: " << parameters.getLocalControlFeature() << ", "; |
| 556 | } |
| 557 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 558 | if (parameters.hasOrigin()) { |
| 559 | os << "Origin: " << parameters.getOrigin() << ", "; |
| 560 | } |
| 561 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 562 | if (parameters.hasCost()) { |
| 563 | os << "Cost: " << parameters.getCost() << ", "; |
| 564 | } |
| 565 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 566 | if (parameters.hasFlags()) { |
| 567 | os << "Flags: " << parameters.getFlags() << ", "; |
| 568 | } |
| 569 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 570 | if (parameters.hasStrategy()) { |
| 571 | os << "Strategy: " << parameters.getStrategy() << ", "; |
| 572 | } |
| 573 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 574 | if (parameters.hasExpirationPeriod()) { |
| 575 | os << "ExpirationPeriod: " << parameters.getExpirationPeriod() << ", "; |
| 576 | } |
| 577 | |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 578 | os << ")"; |
| 579 | return os; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | } // namespace nfd |
| 584 | } // namespace ndn |
| 585 | |
| 586 | #endif // NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP |