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