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