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