Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_INTEREST_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 23 | #define NDN_INTEREST_HPP |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/delegation-list.hpp" |
| 26 | #include "ndn-cxx/name.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "ndn-cxx/selectors.hpp" |
Junxiao Shi | 1e36ceb | 2018-12-17 13:20:26 -0700 | [diff] [blame^] | 28 | #include "ndn-cxx/detail/packet-base.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "ndn-cxx/util/time.hpp" |
| 30 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 31 | #include <boost/logic/tribool.hpp> |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 35 | class Data; |
| 36 | |
Junxiao Shi | 7007a3c | 2014-11-20 22:37:55 -0700 | [diff] [blame] | 37 | /** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME; |
| 38 | * @brief default value for InterestLifetime |
| 39 | */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 40 | const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 41 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 42 | /** @brief Represents an Interest packet. |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 43 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 44 | class Interest : public PacketBase, public std::enable_shared_from_this<Interest> |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 45 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 46 | public: |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 47 | class Error : public tlv::Error |
| 48 | { |
| 49 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 50 | using tlv::Error::Error; |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 53 | /** @brief Construct an Interest with given @p name and @p lifetime. |
| 54 | * @throw std::invalid_argument @p lifetime is negative |
| 55 | * @warning In certain contexts that use `Interest::shared_from_this()`, Interest must be created |
| 56 | * using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior. |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 57 | */ |
Junxiao Shi | 909ffef | 2017-07-07 22:12:27 +0000 | [diff] [blame] | 58 | explicit |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 59 | Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 61 | /** @brief Construct an Interest by decoding from @p wire. |
| 62 | * @warning In certain contexts that use `Interest::shared_from_this()`, Interest must be created |
| 63 | * using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 64 | */ |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 65 | explicit |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 66 | Interest(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 67 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 68 | /** @brief Prepend wire encoding to @p encoder. |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 69 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 70 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 71 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 72 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 73 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 74 | /** @brief Encode to a @c Block. |
| 75 | * |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 76 | * Encodes into NDN Packet Format v0.3 if Parameters element is present. In this case, Selectors |
| 77 | * are not encoded. Otherwise, encodes into NDN Packet Format v0.2. |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 78 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 79 | const Block& |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 80 | wireEncode() const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 81 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 82 | /** @brief Decode from @p wire in NDN Packet Format v0.2 or v0.3. |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 83 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 84 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 85 | wireDecode(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 86 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 87 | /** @brief Check if this instance has cached wire encoding. |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 88 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 89 | bool |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 90 | hasWire() const |
| 91 | { |
| 92 | return m_wire.hasWire(); |
| 93 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 94 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 95 | /** @brief Return a URI-like string that represents the Interest. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 96 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 97 | * The string starts with `getName().toUri()`. |
| 98 | * If the Interest contains selectors, they are included as a query string. |
| 99 | * Example: "/test/name?ndn.MustBeFresh=1" |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 100 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 101 | std::string |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 102 | toUri() const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 103 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 104 | public: // matching |
| 105 | /** @brief Check if Interest, including selectors, matches the given @p name |
| 106 | * @param name The name to be matched. If this is a Data name, it shall contain the |
| 107 | * implicit digest component |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 108 | */ |
| 109 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 110 | matchesName(const Name& name) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 111 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 112 | /** @brief Check if Interest can be satisfied by @p data. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 113 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 114 | * This method considers Name, MinSuffixComponents, MaxSuffixComponents, |
| 115 | * PublisherPublicKeyLocator, and Exclude. |
| 116 | * This method does not consider ChildSelector and MustBeFresh. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 117 | */ |
| 118 | bool |
| 119 | matchesData(const Data& data) const; |
| 120 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 121 | /** @brief Check if Interest matches @p other interest |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 122 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 123 | * Interest matches @p other if both have the same name, selectors, and link. Other fields |
| 124 | * (e.g., Nonce) may be different. |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 125 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 126 | * @todo Implement distinguishing Interests by forwarding hint. The current implementation |
| 127 | * checks only name+selectors (Issue #3162). |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 128 | */ |
| 129 | bool |
| 130 | matchesInterest(const Interest& other) const; |
| 131 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 132 | public: // element access |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 133 | const Name& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 134 | getName() const |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 135 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 136 | return m_name; |
| 137 | } |
| 138 | |
| 139 | Interest& |
| 140 | setName(const Name& name) |
| 141 | { |
| 142 | m_name = name; |
| 143 | m_wire.reset(); |
| 144 | return *this; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 145 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 147 | /** @brief Declare the default CanBePrefix setting of the application. |
| 148 | * |
| 149 | * As part of transitioning to NDN Packet Format v0.3, the default setting for CanBePrefix |
| 150 | * will be changed from "true" to "false". Application developers are advised to review all |
| 151 | * Interests expressed by their application and decide what CanBePrefix setting is appropriate |
| 152 | * for each Interest, to avoid breaking changes when the transition occurs. Application may |
| 153 | * either set CanBePrefix on a per-Interest basis, or declare a default CanBePrefix setting for |
| 154 | * all Interests expressed by the application using this function. If an application neither |
| 155 | * declares a default nor sets CanBePrefix on every Interest, Interest::wireEncode will print a |
| 156 | * one-time warning message. |
| 157 | * |
| 158 | * @note This function should not be used in libraries or in ndn-cxx unit tests. |
| 159 | * @sa https://redmine.named-data.net/projects/nfd/wiki/Packet03Transition |
| 160 | */ |
| 161 | static void |
| 162 | setDefaultCanBePrefix(bool canBePrefix) |
| 163 | { |
| 164 | s_defaultCanBePrefix = canBePrefix; |
| 165 | } |
| 166 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 167 | /** @brief Check whether the CanBePrefix element is present. |
| 168 | * |
| 169 | * This is a getter for the CanBePrefix element as defined in NDN Packet Format v0.3. |
| 170 | * In this implementation, it is mapped to the closest v0.2 semantics: |
| 171 | * MaxSuffixComponents=1 means CanBePrefix is absent. |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 172 | */ |
| 173 | bool |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 174 | getCanBePrefix() const |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 175 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 176 | return m_selectors.getMaxSuffixComponents() != 1; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 179 | /** @brief Add or remove CanBePrefix element. |
| 180 | * @param canBePrefix whether CanBePrefix element should be present. |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 181 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 182 | * This is a setter for the CanBePrefix element as defined in NDN Packet Format v0.3. |
| 183 | * In this implementation, it is mapped to the closest v0.2 semantics: |
| 184 | * MaxSuffixComponents=1 means CanBePrefix is absent. |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 185 | */ |
| 186 | Interest& |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 187 | setCanBePrefix(bool canBePrefix) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 188 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 189 | m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1); |
| 190 | m_wire.reset(); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 191 | m_isCanBePrefixSet = true; |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 192 | return *this; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 195 | /** @brief Check whether the MustBeFresh element is present. |
| 196 | * |
| 197 | * This is a getter for the MustBeFresh element as defined in NDN Packet Format v0.3. |
| 198 | * In this implementation, it is mapped to the closest v0.2 semantics and appears as |
| 199 | * MustBeFresh element under Selectors. |
| 200 | */ |
| 201 | bool |
| 202 | getMustBeFresh() const |
| 203 | { |
| 204 | return m_selectors.getMustBeFresh(); |
| 205 | } |
| 206 | |
| 207 | /** @brief Add or remove MustBeFresh element. |
| 208 | * @param mustBeFresh whether MustBeFresh element should be present. |
| 209 | * |
| 210 | * This is a setter for the MustBeFresh element as defined in NDN Packet Format v0.3. |
| 211 | * In this implementation, it is mapped to the closest v0.2 semantics and appears as |
| 212 | * MustBeFresh element under Selectors. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 213 | */ |
| 214 | Interest& |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 215 | setMustBeFresh(bool mustBeFresh) |
| 216 | { |
| 217 | m_selectors.setMustBeFresh(mustBeFresh); |
| 218 | m_wire.reset(); |
| 219 | return *this; |
| 220 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 221 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 222 | const DelegationList& |
| 223 | getForwardingHint() const |
| 224 | { |
| 225 | return m_forwardingHint; |
| 226 | } |
| 227 | |
| 228 | Interest& |
| 229 | setForwardingHint(const DelegationList& value); |
| 230 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 231 | /** @brief Modify ForwardingHint in-place. |
Junxiao Shi | b2a7033 | 2017-07-07 22:15:03 +0000 | [diff] [blame] | 232 | * @tparam Modifier a unary function that accepts DelegationList& |
| 233 | * |
| 234 | * This is equivalent to, but more efficient (avoids copying) than: |
| 235 | * @code |
| 236 | * auto fh = interest.getForwardingHint(); |
| 237 | * modifier(fh); |
| 238 | * interest.setForwardingHint(fh); |
| 239 | * @endcode |
| 240 | */ |
| 241 | template<typename Modifier> |
| 242 | Interest& |
| 243 | modifyForwardingHint(const Modifier& modifier) |
| 244 | { |
| 245 | modifier(m_forwardingHint); |
| 246 | m_wire.reset(); |
| 247 | return *this; |
| 248 | } |
| 249 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 250 | /** @brief Check if the Nonce element is present. |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 251 | */ |
| 252 | bool |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 253 | hasNonce() const |
| 254 | { |
| 255 | return static_cast<bool>(m_nonce); |
| 256 | } |
| 257 | |
| 258 | /** @brief Get nonce value. |
| 259 | * |
| 260 | * If nonce was not present, it is added and assigned a random value. |
| 261 | */ |
| 262 | uint32_t |
| 263 | getNonce() const; |
| 264 | |
| 265 | /** @brief Set nonce value. |
| 266 | */ |
| 267 | Interest& |
| 268 | setNonce(uint32_t nonce); |
| 269 | |
| 270 | /** @brief Change nonce value. |
| 271 | * |
| 272 | * If the Nonce element is present, the new nonce value will differ from the old value. |
| 273 | * If the Nonce element is not present, this method does nothing. |
| 274 | */ |
| 275 | void |
| 276 | refreshNonce(); |
| 277 | |
| 278 | time::milliseconds |
| 279 | getInterestLifetime() const |
| 280 | { |
| 281 | return m_interestLifetime; |
| 282 | } |
| 283 | |
| 284 | /** @brief Set Interest's lifetime |
| 285 | * @throw std::invalid_argument @p lifetime is negative |
| 286 | */ |
| 287 | Interest& |
| 288 | setInterestLifetime(time::milliseconds lifetime); |
| 289 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 290 | bool |
| 291 | hasParameters() const |
| 292 | { |
| 293 | return !m_parameters.empty(); |
| 294 | } |
| 295 | |
| 296 | const Block& |
| 297 | getParameters() const |
| 298 | { |
| 299 | return m_parameters; |
| 300 | } |
| 301 | |
| 302 | /** @brief Set parameters from a Block |
| 303 | * |
| 304 | * If the block's TLV-TYPE is Parameters, it will be used directly as this Interest's Parameters element. |
| 305 | * If the block's TLV-TYPE is not Parameters, it will be nested into a Parameters element. |
| 306 | * @return a reference to this Interest |
| 307 | */ |
| 308 | Interest& |
| 309 | setParameters(const Block& parameters); |
| 310 | |
| 311 | /** @brief Copy parameters from raw buffer |
| 312 | * |
| 313 | * @param buffer pointer to the first octet of parameters |
| 314 | * @param bufferSize size of the raw buffer |
| 315 | * @return a reference to this Interest |
| 316 | */ |
| 317 | Interest& |
| 318 | setParameters(const uint8_t* buffer, size_t bufferSize); |
| 319 | |
| 320 | /** @brief Set parameters from a wire buffer |
| 321 | * |
| 322 | * @param buffer containing the Interest parameters |
| 323 | * @return a reference to this Interest |
| 324 | */ |
| 325 | Interest& |
| 326 | setParameters(ConstBufferPtr buffer); |
| 327 | |
| 328 | /** @brief Remove the Parameters element from this Interest |
| 329 | * |
| 330 | * @post hasParameters() == false |
| 331 | */ |
| 332 | Interest& |
| 333 | unsetParameters(); |
| 334 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 335 | public: // Selectors (deprecated) |
| 336 | /** @brief Check if Interest has any selector present. |
| 337 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 338 | [[deprecated]] |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 339 | bool |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 340 | hasSelectors() const |
| 341 | { |
| 342 | return !m_selectors.empty(); |
| 343 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 344 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 345 | [[deprecated]] |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 346 | const Selectors& |
| 347 | getSelectors() const |
| 348 | { |
| 349 | return m_selectors; |
| 350 | } |
| 351 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 352 | [[deprecated]] |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 353 | Interest& |
| 354 | setSelectors(const Selectors& selectors) |
| 355 | { |
| 356 | m_selectors = selectors; |
| 357 | m_wire.reset(); |
| 358 | return *this; |
| 359 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 360 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 361 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 362 | int |
| 363 | getMinSuffixComponents() const |
| 364 | { |
| 365 | return m_selectors.getMinSuffixComponents(); |
| 366 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 367 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 368 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 369 | Interest& |
| 370 | setMinSuffixComponents(int minSuffixComponents) |
| 371 | { |
| 372 | m_selectors.setMinSuffixComponents(minSuffixComponents); |
| 373 | m_wire.reset(); |
| 374 | return *this; |
| 375 | } |
| 376 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 377 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 378 | int |
| 379 | getMaxSuffixComponents() const |
| 380 | { |
| 381 | return m_selectors.getMaxSuffixComponents(); |
| 382 | } |
| 383 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 384 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 385 | Interest& |
| 386 | setMaxSuffixComponents(int maxSuffixComponents) |
| 387 | { |
| 388 | m_selectors.setMaxSuffixComponents(maxSuffixComponents); |
| 389 | m_wire.reset(); |
| 390 | return *this; |
| 391 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 392 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 393 | [[deprecated]] |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 394 | const KeyLocator& |
| 395 | getPublisherPublicKeyLocator() const |
| 396 | { |
| 397 | return m_selectors.getPublisherPublicKeyLocator(); |
| 398 | } |
| 399 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 400 | [[deprecated]] |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 401 | Interest& |
| 402 | setPublisherPublicKeyLocator(const KeyLocator& keyLocator) |
| 403 | { |
| 404 | m_selectors.setPublisherPublicKeyLocator(keyLocator); |
| 405 | m_wire.reset(); |
| 406 | return *this; |
| 407 | } |
| 408 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 409 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 410 | const Exclude& |
| 411 | getExclude() const |
| 412 | { |
| 413 | return m_selectors.getExclude(); |
| 414 | } |
| 415 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 416 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 417 | Interest& |
| 418 | setExclude(const Exclude& exclude) |
| 419 | { |
| 420 | m_selectors.setExclude(exclude); |
| 421 | m_wire.reset(); |
| 422 | return *this; |
| 423 | } |
| 424 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 425 | [[deprecated]] |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 426 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 427 | getChildSelector() const |
| 428 | { |
| 429 | return m_selectors.getChildSelector(); |
| 430 | } |
| 431 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 432 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 433 | Interest& |
| 434 | setChildSelector(int childSelector) |
| 435 | { |
| 436 | m_selectors.setChildSelector(childSelector); |
| 437 | m_wire.reset(); |
| 438 | return *this; |
| 439 | } |
| 440 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 441 | private: |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 442 | /** @brief Prepend wire encoding to @p encoder in NDN Packet Format v0.2. |
| 443 | */ |
| 444 | template<encoding::Tag TAG> |
| 445 | size_t |
| 446 | encode02(EncodingImpl<TAG>& encoder) const; |
| 447 | |
| 448 | /** @brief Prepend wire encoding to @p encoder in NDN Packet Format v0.3. |
| 449 | */ |
| 450 | template<encoding::Tag TAG> |
| 451 | size_t |
| 452 | encode03(EncodingImpl<TAG>& encoder) const; |
| 453 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 454 | /** @brief Decode @c m_wire as NDN Packet Format v0.2. |
| 455 | * @retval true decoding successful. |
| 456 | * @retval false decoding failed due to structural error. |
| 457 | * @throw tlv::Error decoding error within a sub-element. |
| 458 | */ |
| 459 | bool |
| 460 | decode02(); |
| 461 | |
| 462 | /** @brief Decode @c m_wire as NDN Packet Format v0.3. |
| 463 | * @throw tlv::Error decoding error. |
| 464 | */ |
| 465 | void |
| 466 | decode03(); |
| 467 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 468 | #ifdef NDN_CXX_HAVE_TESTS |
| 469 | public: |
| 470 | /** @brief If true, not setting CanBePrefix results in an error in wireEncode(). |
| 471 | */ |
| 472 | static bool s_errorIfCanBePrefixUnset; |
| 473 | #endif // NDN_CXX_HAVE_TESTS |
| 474 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 475 | private: |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 476 | static boost::logic::tribool s_defaultCanBePrefix; |
| 477 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 478 | Name m_name; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 479 | Selectors m_selectors; // NDN Packet Format v0.2 only |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 480 | mutable bool m_isCanBePrefixSet; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 481 | mutable optional<uint32_t> m_nonce; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 482 | time::milliseconds m_interestLifetime; |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 483 | DelegationList m_forwardingHint; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 484 | Block m_parameters; // NDN Packet Format v0.3 only |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 485 | |
| 486 | mutable Block m_wire; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 487 | |
| 488 | friend bool operator==(const Interest& lhs, const Interest& rhs); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 489 | }; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 490 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 491 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest); |
| 492 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 493 | std::ostream& |
| 494 | operator<<(std::ostream& os, const Interest& interest); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 495 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 496 | bool |
| 497 | operator==(const Interest& lhs, const Interest& rhs); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 498 | |
| 499 | inline bool |
| 500 | operator!=(const Interest& lhs, const Interest& rhs) |
| 501 | { |
| 502 | return !(lhs == rhs); |
| 503 | } |
| 504 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 505 | } // namespace ndn |
| 506 | |
| 507 | #endif // NDN_INTEREST_HPP |