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