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