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