Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [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. |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 5 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 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. |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP |
| 14 | #define NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP |
| 15 | |
| 16 | #include "../encoding/encoding-buffer.hpp" |
| 17 | #include "../encoding/tlv-nrd.hpp" |
| 18 | #include "../name.hpp" |
| 19 | |
| 20 | namespace ndn { |
| 21 | namespace nrd { |
| 22 | |
| 23 | const uint64_t INVALID_FACE_ID = std::numeric_limits<uint64_t>::max(); |
| 24 | const uint64_t DEFAULT_COST = 0; |
| 25 | const uint64_t DEFAULT_FLAGS = tlv::nrd::NDN_FORW_CHILD_INHERIT; |
| 26 | |
| 27 | /** |
| 28 | * @brief Abstraction for prefix registration options for NRD Prefix registration protocol |
| 29 | * |
| 30 | * @see http://redmine.named-data.net/projects/nrd/wiki/NRD_Prefix_Registration_protocol |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 31 | * @deprecated use NFD RIB Management |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 32 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 33 | class PrefixRegOptions |
| 34 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 35 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | class Error : public Tlv::Error |
| 37 | { |
| 38 | public: |
| 39 | explicit |
| 40 | Error(const std::string& what) |
| 41 | : Tlv::Error(what) |
| 42 | { |
| 43 | } |
| 44 | }; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 45 | |
| 46 | PrefixRegOptions() |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 47 | : m_faceId(INVALID_FACE_ID) |
| 48 | , m_flags(DEFAULT_FLAGS) |
| 49 | , m_cost(DEFAULT_COST) |
| 50 | , m_expirationPeriod(time::milliseconds::min()) |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 54 | explicit |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 55 | PrefixRegOptions(const Block& block) |
| 56 | { |
| 57 | wireDecode(block); |
| 58 | } |
| 59 | |
| 60 | template<bool T> |
| 61 | size_t |
| 62 | wireEncode(EncodingImpl<T>& block) const; |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 63 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 64 | const Block& |
| 65 | wireEncode() const; |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 66 | |
| 67 | void |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 68 | wireDecode(const Block& wire); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 69 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 70 | //////////////////////////////////////////////////////// |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 71 | |
| 72 | const Name& |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 73 | getName() const |
| 74 | { |
| 75 | return m_name; |
| 76 | } |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 77 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 78 | PrefixRegOptions& |
| 79 | setName(const Name& name) |
| 80 | { |
| 81 | m_name = name; |
| 82 | m_wire.reset(); |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | // |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 87 | |
Alexander Afanasyev | 87a7f5f | 2014-02-26 11:40:13 -0800 | [diff] [blame] | 88 | uint64_t |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 89 | getFaceId() const |
| 90 | { |
| 91 | return m_faceId; |
| 92 | } |
| 93 | |
| 94 | PrefixRegOptions& |
Alexander Afanasyev | 87a7f5f | 2014-02-26 11:40:13 -0800 | [diff] [blame] | 95 | setFaceId(uint64_t faceId) |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 96 | { |
| 97 | m_faceId = faceId; |
| 98 | m_wire.reset(); |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | // |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 103 | |
| 104 | uint64_t |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 105 | getFlags() const |
| 106 | { |
| 107 | return m_flags; |
| 108 | } |
| 109 | |
| 110 | PrefixRegOptions& |
| 111 | setFlags(uint64_t flags) |
| 112 | { |
| 113 | m_flags = flags; |
| 114 | m_wire.reset(); |
| 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | // |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 119 | |
| 120 | uint64_t |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 121 | getCost() const |
| 122 | { |
| 123 | return m_cost; |
| 124 | } |
| 125 | |
| 126 | PrefixRegOptions& |
| 127 | setCost(uint64_t cost) |
| 128 | { |
| 129 | m_cost = cost; |
| 130 | m_wire.reset(); |
| 131 | return *this; |
| 132 | } |
| 133 | |
| 134 | // |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 135 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 136 | const time::milliseconds& |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 137 | getExpirationPeriod() const |
| 138 | { |
| 139 | return m_expirationPeriod; |
| 140 | } |
| 141 | |
| 142 | PrefixRegOptions& |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 143 | setExpirationPeriod(const time::milliseconds& expirationPeriod) |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 144 | { |
| 145 | m_expirationPeriod = expirationPeriod; |
| 146 | m_wire.reset(); |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | // |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 151 | |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 152 | const std::string& |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 153 | getProtocol() const |
| 154 | { |
| 155 | return m_protocol; |
| 156 | } |
| 157 | |
| 158 | PrefixRegOptions& |
| 159 | setProtocol(const std::string& protocol) |
| 160 | { |
| 161 | m_protocol = protocol; |
| 162 | m_wire.reset(); |
| 163 | return *this; |
| 164 | } |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 165 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 166 | private: |
| 167 | Name m_name; |
| 168 | uint64_t m_faceId; |
| 169 | uint64_t m_flags; |
| 170 | uint64_t m_cost; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 171 | time::milliseconds m_expirationPeriod; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 172 | std::string m_protocol; |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 173 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 174 | mutable Block m_wire; |
| 175 | }; |
| 176 | |
| 177 | template<bool T> |
| 178 | inline size_t |
| 179 | PrefixRegOptions::wireEncode(EncodingImpl<T>& block) const |
| 180 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 181 | size_t totalLength = 0; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 182 | |
| 183 | // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH |
| 184 | // Name |
| 185 | // FaceId? |
| 186 | // Flags? |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 187 | // Cost? |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 188 | // ExpirationPeriod? |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 189 | // Protocol? |
| 190 | |
| 191 | // (reverse encoding) |
| 192 | |
| 193 | // Protocol |
| 194 | if (!m_protocol.empty()) |
| 195 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 196 | totalLength += prependByteArrayBlock(block, |
| 197 | tlv::nrd::Protocol, |
| 198 | reinterpret_cast<const uint8_t*>(m_protocol.c_str()), |
| 199 | m_protocol.size()); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 202 | // ExpirationPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 203 | if (m_expirationPeriod > time::milliseconds::zero()) |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 204 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 205 | totalLength += prependNonNegativeIntegerBlock(block, |
| 206 | tlv::nrd::ExpirationPeriod, |
| 207 | m_expirationPeriod.count()); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Cost |
| 211 | if (m_cost != DEFAULT_COST) |
| 212 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 213 | totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Flags |
| 217 | if (m_flags != DEFAULT_FLAGS) |
| 218 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 219 | totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // FaceId |
| 223 | if (m_faceId != INVALID_FACE_ID) |
| 224 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 225 | totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Name |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 229 | totalLength += m_name.wireEncode(block); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 230 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 231 | totalLength += block.prependVarNumber(totalLength); |
| 232 | totalLength += block.prependVarNumber(tlv::nrd::PrefixRegOptions); |
| 233 | return totalLength; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | inline const Block& |
| 237 | PrefixRegOptions::wireEncode() const |
| 238 | { |
| 239 | if (m_wire.hasWire()) |
| 240 | return m_wire; |
| 241 | |
| 242 | EncodingEstimator estimator; |
| 243 | size_t estimatedSize = wireEncode(estimator); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 244 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 245 | EncodingBuffer buffer(estimatedSize, 0); |
| 246 | wireEncode(buffer); |
| 247 | |
| 248 | m_wire = buffer.block(); |
| 249 | return m_wire; |
| 250 | } |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 251 | |
| 252 | inline void |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 253 | PrefixRegOptions::wireDecode(const Block& wire) |
| 254 | { |
| 255 | // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH |
| 256 | // Name |
| 257 | // FaceId? |
| 258 | // Flags? |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 259 | // Cost? |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 260 | // ExpirationPeriod? |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 261 | // Protocol? |
| 262 | |
| 263 | m_name.clear(); |
| 264 | m_faceId = INVALID_FACE_ID; |
| 265 | m_flags = DEFAULT_FLAGS; |
| 266 | m_cost = DEFAULT_COST; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 267 | m_expirationPeriod = time::milliseconds::min(); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 268 | m_protocol.clear(); |
| 269 | |
| 270 | m_wire = wire; |
| 271 | |
| 272 | if (m_wire.type() != tlv::nrd::PrefixRegOptions) |
| 273 | throw Error("Requested decoding of PrefixRegOptions, but Block is of different type"); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 274 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 275 | m_wire.parse (); |
| 276 | |
| 277 | // Name |
| 278 | Block::element_const_iterator val = m_wire.find(Tlv::Name); |
| 279 | if (val != m_wire.elements_end()) |
| 280 | { |
| 281 | m_name.wireDecode(*val); |
| 282 | } |
| 283 | |
| 284 | // FaceId |
| 285 | val = m_wire.find(tlv::nrd::FaceId); |
| 286 | if (val != m_wire.elements_end()) |
| 287 | { |
| 288 | m_faceId = readNonNegativeInteger(*val); |
| 289 | } |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 290 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 291 | // Flags |
| 292 | val = m_wire.find(tlv::nrd::Flags); |
| 293 | if (val != m_wire.elements_end()) |
| 294 | { |
| 295 | m_flags = readNonNegativeInteger(*val); |
| 296 | } |
| 297 | |
| 298 | // Cost |
| 299 | val = m_wire.find(tlv::nrd::Cost); |
| 300 | if (val != m_wire.elements_end()) |
| 301 | { |
| 302 | m_cost = readNonNegativeInteger(*val); |
| 303 | } |
| 304 | |
| 305 | // ExpirationPeriod |
| 306 | val = m_wire.find(tlv::nrd::ExpirationPeriod); |
| 307 | if (val != m_wire.elements_end()) |
| 308 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 309 | m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 312 | // Protocol |
| 313 | val = m_wire.find(tlv::nrd::Protocol); |
| 314 | if (val != m_wire.elements_end()) |
| 315 | { |
| 316 | m_protocol = std::string(reinterpret_cast<const char*>(val->value()), |
| 317 | val->value_size()); |
| 318 | } |
| 319 | } |
| 320 | |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 321 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 322 | operator << (std::ostream& os, const PrefixRegOptions& option) |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 323 | { |
| 324 | os << "PrefixRegOptions("; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 325 | |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 326 | // Name |
| 327 | os << "Prefix: " << option.getName() << ", "; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 328 | |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 329 | // FaceID |
| 330 | os << "FaceID: " << option.getFaceId() << ", "; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 331 | |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 332 | // Flags |
| 333 | os << "Flags: " << option.getFlags() << ", "; |
| 334 | |
| 335 | // Cost |
| 336 | os << "Cost: " << option.getCost() << ", "; |
| 337 | |
| 338 | // ExpirationPeriod |
| 339 | os << "ExpirationPeriod: " << option.getExpirationPeriod() << ", "; |
| 340 | |
| 341 | // Protocol |
| 342 | os << "Protocol: " << option.getProtocol(); |
| 343 | |
| 344 | os << ")"; |
| 345 | return os; |
| 346 | } |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 347 | |
| 348 | } // namespace nrd |
| 349 | } // namespace ndn |
| 350 | |
| 351 | #endif // NDN_MANAGEMENT_NRD_PREFIX_REG_OPTIONS_HPP |