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