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