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