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