Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef NDN_INTEREST_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_INTEREST_HPP |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 9 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 10 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "name.hpp" |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 12 | #include "selectors.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 13 | #include "management/nfd-local-control-header.hpp" |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 16 | |
| 17 | const Milliseconds DEFAULT_INTEREST_LIFETIME = 4000; |
| 18 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 19 | /** |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 20 | * An Interest holds a Name and other fields for an interest. |
| 21 | */ |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 22 | class Interest : public enable_shared_from_this<Interest> |
| 23 | { |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 24 | public: |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 25 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 26 | * @brief Create a new Interest with an empty name and "none" for all values. |
| 27 | */ |
| 28 | Interest() |
| 29 | : m_nonce(0) |
| 30 | , m_scope(-1) |
| 31 | , m_interestLifetime(-1.0) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @brief Create a new Interest with the given name and "none" for other values. |
| 37 | * |
| 38 | * @param name The name for the interest. |
| 39 | */ |
| 40 | Interest(const Name& name) |
| 41 | : m_name(name) |
| 42 | , m_nonce(0) |
| 43 | , m_scope(-1) |
| 44 | , m_interestLifetime(-1.0) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create a new Interest with the given name and interest lifetime and "none" for other values. |
| 50 | * @param name The name for the interest. |
| 51 | * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none. |
| 52 | */ |
| 53 | Interest(const Name& name, Milliseconds interestLifetime) |
| 54 | : m_name(name) |
| 55 | , m_nonce(0) |
| 56 | , m_scope(-1) |
| 57 | , m_interestLifetime(interestLifetime) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | Interest(const Name& name, |
| 62 | const Selectors& selectors, |
| 63 | int scope, |
| 64 | Milliseconds interestLifetime, |
| 65 | uint32_t nonce = 0) |
| 66 | : m_name(name) |
| 67 | , m_selectors(selectors) |
| 68 | , m_nonce(nonce) |
| 69 | , m_scope(scope) |
| 70 | , m_interestLifetime(interestLifetime) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | /** |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 75 | * Create a new Interest for the given name and values. |
| 76 | * @param name |
| 77 | * @param minSuffixComponents |
| 78 | * @param maxSuffixComponents |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 79 | * @param exclude |
| 80 | * @param childSelector |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 81 | * @param mustBeFresh |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 82 | * @param scope |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 83 | * @param interestLifetime |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 84 | * @param nonce |
| 85 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 86 | Interest(const Name& name, |
| 87 | int minSuffixComponents, int maxSuffixComponents, |
| 88 | const Exclude& exclude, |
| 89 | int childSelector, |
| 90 | bool mustBeFresh, |
| 91 | int scope, |
| 92 | Milliseconds interestLifetime, |
| 93 | uint32_t nonce = 0) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 94 | : m_name(name) |
| 95 | , m_selectors(minSuffixComponents, maxSuffixComponents, exclude, childSelector, mustBeFresh) |
| 96 | , m_nonce(nonce) |
| 97 | , m_scope(scope) |
| 98 | , m_interestLifetime(interestLifetime) |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 102 | explicit |
| 103 | Interest(const Block& wire) |
| 104 | { |
| 105 | wireDecode(wire); |
| 106 | } |
| 107 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 108 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 109 | * @brief Fast encoding or block size estimation |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 110 | */ |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 111 | template<bool T> |
| 112 | inline size_t |
| 113 | wireEncode(EncodingImpl<T> &block) const; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 114 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 115 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 116 | * @brief Encode to a wire format |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 117 | */ |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 118 | inline const Block& |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 119 | wireEncode() const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 120 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 121 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 122 | * @brief Decode from the wire format |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 123 | */ |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 124 | inline void |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 125 | wireDecode(const Block &wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * @brief Check if already has wire |
| 129 | */ |
| 130 | inline bool |
| 131 | hasWire() const; |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 134 | * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and |
| 135 | * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1". |
| 136 | * @return The URI string. |
| 137 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 138 | inline std::string |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 139 | toUri() const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 141 | inline bool |
| 142 | hasSelectors() const; |
| 143 | |
| 144 | inline bool |
| 145 | hasGuiders() const; |
| 146 | |
| 147 | /** |
| 148 | * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the |
| 149 | * interest selectors. |
| 150 | * @param self A pointer to the ndn_Interest struct. |
| 151 | * @param name A pointer to the name to check. |
| 152 | * @return 1 if the name and interest selectors match, 0 otherwise. |
| 153 | */ |
| 154 | bool |
| 155 | matchesName(const Name &name) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 156 | |
| 157 | /////////////////////////////////////////////////////////////////////////////// |
| 158 | /////////////////////////////////////////////////////////////////////////////// |
| 159 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame^] | 160 | // Getters/setters |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 162 | const Name& |
| 163 | getName() const |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 164 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 165 | return m_name; |
| 166 | } |
| 167 | |
| 168 | Interest& |
| 169 | setName(const Name& name) |
| 170 | { |
| 171 | m_name = name; |
| 172 | m_wire.reset(); |
| 173 | return *this; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 174 | } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 176 | // |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 177 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 178 | const Selectors& |
| 179 | getSelectors() const |
| 180 | { |
| 181 | return m_selectors; |
| 182 | } |
| 183 | |
| 184 | Interest& |
| 185 | setSelectors(const Selectors& selectors) |
| 186 | { |
| 187 | m_selectors = selectors; |
| 188 | m_wire.reset(); |
| 189 | return *this; |
| 190 | } |
| 191 | |
| 192 | // |
| 193 | |
| 194 | int |
| 195 | getScope() const |
| 196 | { |
| 197 | return m_scope; |
| 198 | } |
| 199 | |
| 200 | Interest& |
| 201 | setScope(int scope) |
| 202 | { |
| 203 | m_scope = scope; |
| 204 | m_wire.reset(); |
| 205 | return *this; |
| 206 | } |
| 207 | |
| 208 | // |
| 209 | |
| 210 | Milliseconds |
| 211 | getInterestLifetime() const |
| 212 | { |
| 213 | return m_interestLifetime; |
| 214 | } |
| 215 | |
| 216 | Interest& |
| 217 | setInterestLifetime(Milliseconds interestLifetime) |
| 218 | { |
| 219 | m_interestLifetime = interestLifetime; |
| 220 | m_wire.reset(); |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | // |
| 225 | |
| 226 | /** |
| 227 | * @brief Get Interest's nonce |
| 228 | * |
| 229 | * If nonce was not set before this call, it will be automatically assigned to a random value |
| 230 | * |
| 231 | * Const reference needed for C decoding |
| 232 | */ |
| 233 | const uint32_t& |
| 234 | getNonce() const; |
| 235 | |
| 236 | Interest& |
| 237 | setNonce(uint32_t nonce) |
| 238 | { |
| 239 | m_nonce = nonce; |
| 240 | m_wire.reset(); |
| 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | // |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 245 | |
| 246 | nfd::LocalControlHeader& |
| 247 | getLocalControlHeader() |
| 248 | { |
| 249 | return m_localControlHeader; |
| 250 | } |
| 251 | |
| 252 | const nfd::LocalControlHeader& |
| 253 | getLocalControlHeader() const |
| 254 | { |
| 255 | return m_localControlHeader; |
| 256 | } |
| 257 | |
| 258 | // helper methods for LocalControlHeader |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 259 | |
| 260 | uint64_t |
| 261 | getIncomingFaceId() const |
| 262 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 263 | return getLocalControlHeader().getIncomingFaceId(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 264 | } |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 265 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 266 | Interest& |
| 267 | setIncomingFaceId(uint64_t incomingFaceId) |
| 268 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 269 | getLocalControlHeader().setIncomingFaceId(incomingFaceId); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame^] | 270 | // ! do not reset Interest's wire ! |
| 271 | return *this; |
| 272 | } |
| 273 | |
| 274 | // |
| 275 | |
| 276 | // NextHopFaceId helpers make sense only for Interests |
| 277 | |
| 278 | uint64_t |
| 279 | getNextHopFaceId() const |
| 280 | { |
| 281 | return getLocalControlHeader().getNextHopFaceId(); |
| 282 | } |
| 283 | |
| 284 | Interest& |
| 285 | setNextHopFaceId(uint64_t nextHopFaceId) |
| 286 | { |
| 287 | getLocalControlHeader().setNextHopFaceId(nextHopFaceId); |
| 288 | // ! do not reset Interest's wire ! |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 289 | return *this; |
| 290 | } |
| 291 | |
| 292 | // |
| 293 | |
| 294 | /////////////////////////////////////////////////////////////////////////////// |
| 295 | /////////////////////////////////////////////////////////////////////////////// |
| 296 | /////////////////////////////////////////////////////////////////////////////// |
| 297 | // Wrappers for Selectors |
| 298 | // |
| 299 | |
| 300 | int |
| 301 | getMinSuffixComponents() const |
| 302 | { |
| 303 | return m_selectors.getMinSuffixComponents(); |
| 304 | } |
| 305 | |
| 306 | Interest& |
| 307 | setMinSuffixComponents(int minSuffixComponents) |
| 308 | { |
| 309 | m_selectors.setMinSuffixComponents(minSuffixComponents); |
| 310 | m_wire.reset(); |
| 311 | return *this; |
| 312 | } |
| 313 | |
| 314 | // |
| 315 | |
| 316 | int |
| 317 | getMaxSuffixComponents() const |
| 318 | { |
| 319 | return m_selectors.getMaxSuffixComponents(); |
| 320 | } |
| 321 | |
| 322 | Interest& |
| 323 | setMaxSuffixComponents(int maxSuffixComponents) |
| 324 | { |
| 325 | m_selectors.setMaxSuffixComponents(maxSuffixComponents); |
| 326 | m_wire.reset(); |
| 327 | return *this; |
| 328 | } |
| 329 | |
| 330 | // |
| 331 | |
| 332 | const Exclude& |
| 333 | getExclude() const |
| 334 | { |
| 335 | return m_selectors.getExclude(); |
| 336 | } |
| 337 | |
| 338 | Interest& |
| 339 | setExclude(const Exclude& exclude) |
| 340 | { |
| 341 | m_selectors.setExclude(exclude); |
| 342 | m_wire.reset(); |
| 343 | return *this; |
| 344 | } |
| 345 | |
| 346 | // |
| 347 | |
| 348 | int |
| 349 | getChildSelector() const |
| 350 | { |
| 351 | return m_selectors.getChildSelector(); |
| 352 | } |
| 353 | |
| 354 | Interest& |
| 355 | setChildSelector(int childSelector) |
| 356 | { |
| 357 | m_selectors.setChildSelector(childSelector); |
| 358 | m_wire.reset(); |
| 359 | return *this; |
| 360 | } |
| 361 | |
| 362 | // |
| 363 | |
| 364 | int |
| 365 | getMustBeFresh() const |
| 366 | { |
| 367 | return m_selectors.getMustBeFresh(); |
| 368 | } |
| 369 | |
| 370 | Interest& |
| 371 | setMustBeFresh(bool mustBeFresh) |
| 372 | { |
| 373 | m_selectors.setMustBeFresh(mustBeFresh); |
| 374 | m_wire.reset(); |
| 375 | return *this; |
| 376 | } |
| 377 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 378 | private: |
| 379 | Name m_name; |
| 380 | Selectors m_selectors; |
| 381 | mutable uint32_t m_nonce; |
| 382 | int m_scope; |
| 383 | Milliseconds m_interestLifetime; |
| 384 | |
| 385 | mutable Block m_wire; |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 386 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 387 | nfd::LocalControlHeader m_localControlHeader; |
| 388 | friend class nfd::LocalControlHeader; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 389 | }; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 390 | |
| 391 | std::ostream & |
| 392 | operator << (std::ostream &os, const Interest &interest); |
| 393 | |
| 394 | inline std::string |
| 395 | Interest::toUri() const |
| 396 | { |
| 397 | std::ostringstream os; |
| 398 | os << *this; |
| 399 | return os.str(); |
| 400 | } |
| 401 | |
| 402 | inline bool |
| 403 | Interest::hasSelectors() const |
| 404 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 405 | return !m_selectors.empty(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | inline bool |
| 409 | Interest::hasGuiders() const |
| 410 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 411 | return m_scope >= 0 || |
| 412 | m_interestLifetime >= 0 || |
| 413 | m_nonce > 0; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 416 | template<bool T> |
| 417 | inline size_t |
| 418 | Interest::wireEncode(EncodingImpl<T> &block) const |
| 419 | { |
| 420 | size_t total_len = 0; |
| 421 | |
| 422 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 423 | // Name |
| 424 | // Selectors? |
| 425 | // Nonce |
| 426 | // Scope? |
| 427 | // InterestLifetime? |
| 428 | |
| 429 | // (reverse encoding) |
| 430 | |
| 431 | // InterestLifetime |
| 432 | if (getInterestLifetime() >= 0 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) |
| 433 | { |
| 434 | total_len += prependNonNegativeIntegerBlock(block, Tlv::InterestLifetime, getInterestLifetime()); |
| 435 | } |
| 436 | |
| 437 | // Scope |
| 438 | if (getScope() >= 0) |
| 439 | { |
| 440 | total_len += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope()); |
| 441 | } |
| 442 | |
| 443 | // Nonce |
| 444 | total_len += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce()); |
| 445 | |
| 446 | // Selectors |
| 447 | if (!getSelectors().empty()) |
| 448 | { |
| 449 | total_len += getSelectors().wireEncode(block); |
| 450 | } |
| 451 | |
| 452 | // Name |
| 453 | total_len += getName().wireEncode(block); |
| 454 | |
| 455 | total_len += block.prependVarNumber (total_len); |
| 456 | total_len += block.prependVarNumber (Tlv::Interest); |
| 457 | return total_len; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 460 | inline const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 461 | Interest::wireEncode() const |
| 462 | { |
| 463 | if (m_wire.hasWire()) |
| 464 | return m_wire; |
| 465 | |
| 466 | EncodingEstimator estimator; |
| 467 | size_t estimatedSize = wireEncode(estimator); |
| 468 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame^] | 469 | EncodingBuffer buffer(estimatedSize, 0); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 470 | wireEncode(buffer); |
| 471 | |
| 472 | m_wire = buffer.block(); |
| 473 | return m_wire; |
| 474 | } |
| 475 | |
| 476 | inline void |
| 477 | Interest::wireDecode(const Block &wire) |
| 478 | { |
| 479 | m_wire = wire; |
| 480 | m_wire.parse(); |
| 481 | |
| 482 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 483 | // Name |
| 484 | // Selectors? |
| 485 | // Nonce |
| 486 | // Scope? |
| 487 | // InterestLifetime? |
| 488 | |
| 489 | if (m_wire.type() != Tlv::Interest) |
| 490 | throw Tlv::Error("Unexpected TLV number when decoding Interest"); |
| 491 | |
| 492 | // Name |
| 493 | m_name.wireDecode(m_wire.get(Tlv::Name)); |
| 494 | |
| 495 | // Selectors |
| 496 | Block::element_const_iterator val = m_wire.find(Tlv::Selectors); |
| 497 | if (val != m_wire.elements_end()) |
| 498 | { |
| 499 | m_selectors.wireDecode(*val); |
| 500 | } |
| 501 | else |
| 502 | m_selectors = Selectors(); |
| 503 | |
| 504 | // Nonce |
| 505 | val = m_wire.find(Tlv::Nonce); |
| 506 | if (val != m_wire.elements_end()) |
| 507 | { |
| 508 | m_nonce = readNonNegativeInteger(*val); |
| 509 | } |
| 510 | else |
| 511 | m_nonce = 0; |
| 512 | |
| 513 | // Scope |
| 514 | val = m_wire.find(Tlv::Scope); |
| 515 | if (val != m_wire.elements_end()) |
| 516 | { |
| 517 | m_scope = readNonNegativeInteger(*val); |
| 518 | } |
| 519 | else |
| 520 | m_scope = -1; |
| 521 | |
| 522 | // InterestLifetime |
| 523 | val = m_wire.find(Tlv::InterestLifetime); |
| 524 | if (val != m_wire.elements_end()) |
| 525 | { |
| 526 | m_interestLifetime = readNonNegativeInteger(*val); |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 531 | } |
| 532 | } |
| 533 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 534 | inline bool |
| 535 | Interest::hasWire() const |
| 536 | { |
| 537 | return m_wire.hasWire(); |
| 538 | } |
| 539 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 540 | |
| 541 | } // namespace ndn |
| 542 | |
| 543 | #endif // NDN_INTEREST_HPP |