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