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