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