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