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