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 | |
| 16 | class ControlParameters; |
| 17 | typedef ControlParameters FaceManagementOptions; |
| 18 | typedef ControlParameters FibManagementOptions; |
| 19 | typedef ControlParameters StrategyChoiceOptions; |
| 20 | |
| 21 | enum LocalControlFeature { |
| 22 | LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID = 1, |
| 23 | LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID = 2 |
| 24 | }; |
| 25 | |
| 26 | class ControlParameters { |
| 27 | public: |
| 28 | class Error : public Tlv::Error |
| 29 | { |
| 30 | public: |
| 31 | explicit |
| 32 | Error(const std::string& what) |
| 33 | : Tlv::Error(what) |
| 34 | { |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | ControlParameters(); |
| 39 | |
| 40 | explicit |
| 41 | ControlParameters(const Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
| 45 | |
| 46 | template<bool T> |
| 47 | size_t |
| 48 | wireEncode(EncodingImpl<T>& encoder) const; |
| 49 | |
| 50 | const Block& |
| 51 | wireEncode() const; |
| 52 | |
| 53 | void |
| 54 | wireDecode(const Block& wire); |
| 55 | |
| 56 | public: // getters & setters |
| 57 | bool |
| 58 | hasName() const |
| 59 | { |
| 60 | return m_hasName; |
| 61 | } |
| 62 | |
| 63 | const Name& |
| 64 | getName() const |
| 65 | { |
| 66 | BOOST_ASSERT(m_hasName); |
| 67 | return m_name; |
| 68 | } |
| 69 | |
| 70 | ControlParameters& |
| 71 | setName(const Name& name) |
| 72 | { |
| 73 | m_wire.reset(); |
| 74 | m_name = name; |
| 75 | m_hasName = true; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | ControlParameters& |
| 80 | unsetName() |
| 81 | { |
| 82 | m_wire.reset(); |
| 83 | m_hasName = false; |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | hasFaceId() const |
| 89 | { |
| 90 | return m_hasFaceId; |
| 91 | } |
| 92 | |
| 93 | uint64_t |
| 94 | getFaceId() const |
| 95 | { |
| 96 | BOOST_ASSERT(m_hasFaceId); |
| 97 | return m_faceId; |
| 98 | } |
| 99 | |
| 100 | ControlParameters& |
| 101 | setFaceId(uint64_t faceId) |
| 102 | { |
| 103 | m_wire.reset(); |
| 104 | m_faceId = faceId; |
| 105 | m_hasFaceId = true; |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | ControlParameters& |
| 110 | unsetFaceId() |
| 111 | { |
| 112 | m_wire.reset(); |
| 113 | m_hasFaceId = false; |
| 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | bool |
| 118 | hasUri() const |
| 119 | { |
| 120 | return m_hasUri; |
| 121 | } |
| 122 | |
| 123 | const std::string& |
| 124 | getUri() const |
| 125 | { |
| 126 | BOOST_ASSERT(m_hasUri); |
| 127 | return m_uri; |
| 128 | } |
| 129 | |
| 130 | ControlParameters& |
| 131 | setUri(const std::string& uri) |
| 132 | { |
| 133 | m_wire.reset(); |
| 134 | m_uri = uri; |
| 135 | m_hasUri = true; |
| 136 | return *this; |
| 137 | } |
| 138 | |
| 139 | ControlParameters& |
| 140 | unsetUri() |
| 141 | { |
| 142 | m_wire.reset(); |
| 143 | m_hasUri = false; |
| 144 | return *this; |
| 145 | } |
| 146 | |
| 147 | bool |
| 148 | hasLocalControlFeature() const |
| 149 | { |
| 150 | return m_hasLocalControlFeature; |
| 151 | } |
| 152 | |
| 153 | LocalControlFeature |
| 154 | getLocalControlFeature() const |
| 155 | { |
| 156 | BOOST_ASSERT(m_hasLocalControlFeature); |
| 157 | return m_localControlFeature; |
| 158 | } |
| 159 | |
| 160 | ControlParameters& |
| 161 | setLocalControlFeature(LocalControlFeature localControlFeature) |
| 162 | { |
| 163 | m_wire.reset(); |
| 164 | m_localControlFeature = localControlFeature; |
| 165 | m_hasLocalControlFeature = true; |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | ControlParameters& |
| 170 | unsetLocalControlFeature() |
| 171 | { |
| 172 | m_wire.reset(); |
| 173 | m_hasLocalControlFeature = false; |
| 174 | return *this; |
| 175 | } |
| 176 | |
| 177 | bool |
| 178 | hasCost() const |
| 179 | { |
| 180 | return m_hasCost; |
| 181 | } |
| 182 | |
| 183 | uint64_t |
| 184 | getCost() const |
| 185 | { |
| 186 | BOOST_ASSERT(m_hasCost); |
| 187 | return m_cost; |
| 188 | } |
| 189 | |
| 190 | ControlParameters& |
| 191 | setCost(uint64_t cost) |
| 192 | { |
| 193 | m_wire.reset(); |
| 194 | m_cost = cost; |
| 195 | m_hasCost = true; |
| 196 | return *this; |
| 197 | } |
| 198 | |
| 199 | ControlParameters& |
| 200 | unsetCost() |
| 201 | { |
| 202 | m_wire.reset(); |
| 203 | m_hasCost = false; |
| 204 | return *this; |
| 205 | } |
| 206 | |
| 207 | bool |
| 208 | hasStrategy() const |
| 209 | { |
| 210 | return m_hasStrategy; |
| 211 | } |
| 212 | |
| 213 | const Name& |
| 214 | getStrategy() const |
| 215 | { |
| 216 | BOOST_ASSERT(m_hasStrategy); |
| 217 | return m_strategy; |
| 218 | } |
| 219 | |
| 220 | ControlParameters& |
| 221 | setStrategy(const Name& strategy) |
| 222 | { |
| 223 | m_wire.reset(); |
| 224 | m_strategy = strategy; |
| 225 | m_hasStrategy = true; |
| 226 | return *this; |
| 227 | } |
| 228 | |
| 229 | ControlParameters& |
| 230 | unsetStrategy() |
| 231 | { |
| 232 | m_wire.reset(); |
| 233 | m_hasStrategy = false; |
| 234 | return *this; |
| 235 | } |
| 236 | |
| 237 | private: // fields |
| 238 | Name m_name; |
| 239 | uint64_t m_faceId; |
| 240 | std::string m_uri; |
| 241 | LocalControlFeature m_localControlFeature; |
| 242 | uint64_t m_cost; |
| 243 | Name m_strategy; |
| 244 | |
| 245 | bool m_hasName; |
| 246 | bool m_hasFaceId; |
| 247 | bool m_hasUri; |
| 248 | bool m_hasLocalControlFeature; |
| 249 | bool m_hasCost; |
| 250 | bool m_hasStrategy; |
| 251 | |
| 252 | private: |
| 253 | mutable Block m_wire; |
| 254 | }; |
| 255 | |
| 256 | |
| 257 | inline |
| 258 | ControlParameters::ControlParameters() |
| 259 | : m_hasName(false) |
| 260 | , m_hasFaceId(false) |
| 261 | , m_hasUri(false) |
| 262 | , m_hasLocalControlFeature(false) |
| 263 | , m_hasCost(false) |
| 264 | , m_hasStrategy(false) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | template<bool T> |
| 269 | inline size_t |
| 270 | ControlParameters::wireEncode(EncodingImpl<T>& encoder) const |
| 271 | { |
| 272 | size_t totalLength = 0; |
| 273 | |
| 274 | if (m_hasStrategy) { |
| 275 | totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy); |
| 276 | } |
| 277 | if (m_hasCost) { |
| 278 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Cost, m_cost); |
| 279 | } |
| 280 | if (m_hasLocalControlFeature) { |
| 281 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 282 | tlv::nfd::LocalControlFeature, m_localControlFeature); |
| 283 | } |
| 284 | if (m_hasUri) { |
| 285 | size_t valLength = encoder.prependByteArray( |
| 286 | reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size()); |
| 287 | totalLength += valLength; |
| 288 | totalLength += encoder.prependVarNumber(valLength); |
| 289 | totalLength += encoder.prependVarNumber(tlv::nfd::Uri); |
| 290 | } |
| 291 | if (m_hasFaceId) { |
| 292 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId); |
| 293 | } |
| 294 | if (m_hasName) { |
| 295 | totalLength += m_name.wireEncode(encoder); |
| 296 | } |
| 297 | |
| 298 | totalLength += encoder.prependVarNumber(totalLength); |
| 299 | totalLength += encoder.prependVarNumber(tlv::nfd::ControlParameters); |
| 300 | return totalLength; |
| 301 | } |
| 302 | |
| 303 | inline const Block& |
| 304 | ControlParameters::wireEncode() const |
| 305 | { |
| 306 | if (m_wire.hasWire()) |
| 307 | return m_wire; |
| 308 | |
| 309 | EncodingEstimator estimator; |
| 310 | size_t estimatedSize = wireEncode(estimator); |
| 311 | |
| 312 | EncodingBuffer buffer(estimatedSize, 0); |
| 313 | wireEncode(buffer); |
| 314 | |
| 315 | m_wire = buffer.block(); |
| 316 | return m_wire; |
| 317 | } |
| 318 | |
| 319 | inline void |
| 320 | ControlParameters::wireDecode(const Block& block) |
| 321 | { |
| 322 | if (block.type() != tlv::nfd::ControlParameters) { |
| 323 | throw Error("expecting TLV-TYPE ControlParameters"); |
| 324 | } |
| 325 | m_wire = block; |
| 326 | m_wire.parse(); |
| 327 | Block::element_const_iterator val; |
| 328 | |
| 329 | val = m_wire.find(Tlv::Name); |
| 330 | m_hasName = val != m_wire.elements_end(); |
| 331 | if (m_hasName) { |
| 332 | m_name.wireDecode(*val); |
| 333 | } |
| 334 | |
| 335 | val = m_wire.find(tlv::nfd::FaceId); |
| 336 | m_hasFaceId = val != m_wire.elements_end(); |
| 337 | if (m_hasFaceId) { |
| 338 | m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 339 | } |
| 340 | |
| 341 | val = m_wire.find(tlv::nfd::Uri); |
| 342 | m_hasUri = val != m_wire.elements_end(); |
| 343 | if (m_hasUri) { |
| 344 | m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 345 | } |
| 346 | |
| 347 | val = m_wire.find(tlv::nfd::LocalControlFeature); |
| 348 | m_hasLocalControlFeature = val != m_wire.elements_end(); |
| 349 | if (m_hasLocalControlFeature) { |
| 350 | m_localControlFeature = static_cast<LocalControlFeature>(readNonNegativeInteger(*val)); |
| 351 | } |
| 352 | |
| 353 | val = m_wire.find(tlv::nfd::Cost); |
| 354 | m_hasCost = val != m_wire.elements_end(); |
| 355 | if (m_hasCost) { |
| 356 | m_cost = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 357 | } |
| 358 | |
| 359 | val = m_wire.find(tlv::nfd::Strategy); |
| 360 | m_hasStrategy = val != m_wire.elements_end(); |
| 361 | if (m_hasStrategy) { |
| 362 | val->parse(); |
| 363 | if (val->elements().empty()) { |
| 364 | throw Error("expecting Strategy/Name"); |
| 365 | } |
| 366 | else { |
| 367 | m_strategy.wireDecode(*val->elements_begin()); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | inline std::ostream& |
| 373 | operator<<(std::ostream& os, const ControlParameters& parameters) |
| 374 | { |
| 375 | os << "ControlParameters("; |
| 376 | |
| 377 | if (parameters.hasName()) { |
| 378 | os << "Name: " << parameters.getName() << ", "; |
| 379 | } |
| 380 | |
| 381 | if (parameters.hasFaceId()) { |
| 382 | os << "FaceId: " << parameters.getFaceId() << ", "; |
| 383 | } |
| 384 | |
| 385 | if (parameters.hasUri()) { |
| 386 | os << "Uri: " << parameters.getUri() << ", "; |
| 387 | } |
| 388 | |
| 389 | if (parameters.hasLocalControlFeature()) { |
| 390 | os << "LocalControlFeature: " << parameters.getLocalControlFeature() << ", "; |
| 391 | } |
| 392 | |
| 393 | if (parameters.hasCost()) { |
| 394 | os << "Cost: " << parameters.getCost() << ", "; |
| 395 | } |
| 396 | |
| 397 | if (parameters.hasStrategy()) { |
| 398 | os << "Strategy: " << parameters.getStrategy() << ", "; |
| 399 | } |
| 400 | |
| 401 | os << ")"; |
| 402 | return os; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | } // namespace nfd |
| 407 | } // namespace ndn |
| 408 | |
| 409 | #endif // NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP |