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