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