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 |
| 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 | 1eb961a | 2014-01-03 13:51:49 -0800 | [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 |
| 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 | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 410 | private: |
| 411 | Name m_name; |
| 412 | Selectors m_selectors; |
| 413 | mutable uint32_t m_nonce; |
| 414 | int m_scope; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 415 | time::milliseconds m_interestLifetime; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 416 | |
| 417 | mutable Block m_wire; |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 418 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 419 | nfd::LocalControlHeader m_localControlHeader; |
| 420 | friend class nfd::LocalControlHeader; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 421 | }; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 422 | |
| 423 | std::ostream & |
| 424 | operator << (std::ostream &os, const Interest &interest); |
| 425 | |
| 426 | inline std::string |
| 427 | Interest::toUri() const |
| 428 | { |
| 429 | std::ostringstream os; |
| 430 | os << *this; |
| 431 | return os.str(); |
| 432 | } |
| 433 | |
| 434 | inline bool |
| 435 | Interest::hasSelectors() const |
| 436 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 437 | return !m_selectors.empty(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | inline bool |
| 441 | Interest::hasGuiders() const |
| 442 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 443 | return m_scope >= 0 || |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 444 | m_interestLifetime >= time::milliseconds::zero() || |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 445 | m_nonce > 0; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 448 | template<bool T> |
| 449 | inline size_t |
| 450 | Interest::wireEncode(EncodingImpl<T> &block) const |
| 451 | { |
| 452 | size_t total_len = 0; |
| 453 | |
| 454 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 455 | // Name |
| 456 | // Selectors? |
| 457 | // Nonce |
| 458 | // Scope? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 459 | // InterestLifetime? |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 460 | |
| 461 | // (reverse encoding) |
| 462 | |
| 463 | // InterestLifetime |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 464 | if (getInterestLifetime() >= time::milliseconds::zero() |
| 465 | && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 466 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 467 | total_len += prependNonNegativeIntegerBlock(block, Tlv::InterestLifetime, getInterestLifetime().count()); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Scope |
| 471 | if (getScope() >= 0) |
| 472 | { |
| 473 | total_len += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope()); |
| 474 | } |
| 475 | |
| 476 | // Nonce |
| 477 | total_len += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce()); |
| 478 | |
| 479 | // Selectors |
| 480 | if (!getSelectors().empty()) |
| 481 | { |
| 482 | total_len += getSelectors().wireEncode(block); |
| 483 | } |
| 484 | |
| 485 | // Name |
| 486 | total_len += getName().wireEncode(block); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 487 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 488 | total_len += block.prependVarNumber (total_len); |
| 489 | total_len += block.prependVarNumber (Tlv::Interest); |
| 490 | return total_len; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 493 | inline const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 494 | Interest::wireEncode() const |
| 495 | { |
| 496 | if (m_wire.hasWire()) |
| 497 | return m_wire; |
| 498 | |
| 499 | EncodingEstimator estimator; |
| 500 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 501 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 502 | EncodingBuffer buffer(estimatedSize, 0); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 503 | wireEncode(buffer); |
| 504 | |
| 505 | m_wire = buffer.block(); |
| 506 | return m_wire; |
| 507 | } |
| 508 | |
| 509 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 510 | Interest::wireDecode(const Block &wire) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 511 | { |
| 512 | m_wire = wire; |
| 513 | m_wire.parse(); |
| 514 | |
| 515 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 516 | // Name |
| 517 | // Selectors? |
| 518 | // Nonce |
| 519 | // Scope? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 520 | // InterestLifetime? |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 521 | |
| 522 | if (m_wire.type() != Tlv::Interest) |
| 523 | throw Tlv::Error("Unexpected TLV number when decoding Interest"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 524 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 525 | // Name |
| 526 | m_name.wireDecode(m_wire.get(Tlv::Name)); |
| 527 | |
| 528 | // Selectors |
| 529 | Block::element_const_iterator val = m_wire.find(Tlv::Selectors); |
| 530 | if (val != m_wire.elements_end()) |
| 531 | { |
| 532 | m_selectors.wireDecode(*val); |
| 533 | } |
| 534 | else |
| 535 | m_selectors = Selectors(); |
| 536 | |
| 537 | // Nonce |
| 538 | val = m_wire.find(Tlv::Nonce); |
| 539 | if (val != m_wire.elements_end()) |
| 540 | { |
| 541 | m_nonce = readNonNegativeInteger(*val); |
| 542 | } |
| 543 | else |
| 544 | m_nonce = 0; |
| 545 | |
| 546 | // Scope |
| 547 | val = m_wire.find(Tlv::Scope); |
| 548 | if (val != m_wire.elements_end()) |
| 549 | { |
| 550 | m_scope = readNonNegativeInteger(*val); |
| 551 | } |
| 552 | else |
| 553 | m_scope = -1; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 554 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 555 | // InterestLifetime |
| 556 | val = m_wire.find(Tlv::InterestLifetime); |
| 557 | if (val != m_wire.elements_end()) |
| 558 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 559 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 560 | } |
| 561 | else |
| 562 | { |
| 563 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 564 | } |
| 565 | } |
| 566 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 567 | inline bool |
| 568 | Interest::hasWire() const |
| 569 | { |
| 570 | return m_wire.hasWire(); |
| 571 | } |
| 572 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 573 | |
| 574 | } // namespace ndn |
| 575 | |
| 576 | #endif // NDN_INTEREST_HPP |