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