blob: d547daab770e5bc3949dcd9d87d2bc7054922294 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi899277a2017-07-07 22:12:12 +00002/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Thompsonb7f95562013-07-03 18:36:42 -070020 */
21
22#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070023#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -070024
Junxiao Shi9c154cb2017-07-07 22:14:54 +000025#include "delegation-list.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070026#include "name.hpp"
Eric Newberryc3a46792017-09-24 14:54:24 -070027#include "packet-base.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080028#include "selectors.hpp"
Junxiao Shi899277a2017-07-07 22:12:12 +000029#include "util/time.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070030
31namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080032
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070033class Data;
34
Junxiao Shi7007a3c2014-11-20 22:37:55 -070035/** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME;
36 * @brief default value for InterestLifetime
37 */
Davide Pesavento0f830802018-01-16 23:58:58 -050038const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080039
Junxiao Shi8d3f8342018-04-04 12:46:37 +000040/** @brief Represents an Interest packet.
Jeff Thompson8238d002013-07-10 11:56:49 -070041 */
Eric Newberryc3a46792017-09-24 14:54:24 -070042class Interest : public PacketBase, public enable_shared_from_this<Interest>
Alexander Afanasyevc348f832014-02-17 16:35:17 -080043{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044public:
Junxiao Shic2b8d242014-11-04 08:35:29 -070045 class Error : public tlv::Error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : tlv::Error(what)
51 {
52 }
53 };
54
Junxiao Shi8d3f8342018-04-04 12:46:37 +000055 /** @brief Construct an Interest with given @p name and @p lifetime.
56 * @throw std::invalid_argument @p lifetime is negative
57 * @warning In certain contexts that use `Interest::shared_from_this()`, Interest must be created
58 * using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080059 */
Junxiao Shi909ffef2017-07-07 22:12:27 +000060 explicit
Junxiao Shi8d3f8342018-04-04 12:46:37 +000061 Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062
Junxiao Shi8d3f8342018-04-04 12:46:37 +000063 /** @brief Construct an Interest by decoding from @p wire.
64 * @warning In certain contexts that use `Interest::shared_from_this()`, Interest must be created
65 * using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080067 explicit
Junxiao Shi2af905b2014-11-27 13:10:54 -070068 Interest(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080069
Junxiao Shi8d3f8342018-04-04 12:46:37 +000070 /** @brief Prepend wire encoding to @p encoder in NDN Packet Format v0.2.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080071 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080072 template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -070073 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070074 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080075
Junxiao Shi8d3f8342018-04-04 12:46:37 +000076 /** @brief Encode to a @c Block in NDN Packet Format v0.2.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080077 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070078 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080079 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080080
Junxiao Shi8d3f8342018-04-04 12:46:37 +000081 /** @brief Decode from @p wire in NDN Packet Format v0.2.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080082 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070083 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070084 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080085
Junxiao Shi8d3f8342018-04-04 12:46:37 +000086 /** @brief Check if this instance has cached wire encoding.
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080087 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070088 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -070089 hasWire() const
90 {
91 return m_wire.hasWire();
92 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093
Junxiao Shi8d3f8342018-04-04 12:46:37 +000094 /** @brief Return a URI-like string that represents the Interest.
Alexander Afanasyev770827c2014-05-13 17:42:55 -070095 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +000096 * The string starts with `getName().toUri()`.
97 * If the Interest contains selectors, they are included as a query string.
98 * Example: "/test/name?ndn.MustBeFresh=1"
Jeff Thompson13e280b2013-12-03 13:12:23 -080099 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700100 std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800101 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700102
Junxiao Shi2af905b2014-11-27 13:10:54 -0700103public: // matching
104 /** @brief Check if Interest, including selectors, matches the given @p name
105 * @param name The name to be matched. If this is a Data name, it shall contain the
106 * implicit digest component
Alexander Afanasyev84681982014-01-03 13:26:09 -0800107 */
108 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700109 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800110
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000111 /** @brief Check if Interest can be satisfied by @p data.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700112 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000113 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
114 * PublisherPublicKeyLocator, and Exclude.
115 * This method does not consider ChildSelector and MustBeFresh.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700116 */
117 bool
118 matchesData(const Data& data) const;
119
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000120 /** @brief Check if Interest matches @p other interest
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800121 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000122 * Interest matches @p other if both have the same name, selectors, and link. Other fields
123 * (e.g., Nonce) may be different.
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800124 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000125 * @todo Implement distinguishing Interests by forwarding hint. The current implementation
126 * checks only name+selectors (Issue #3162).
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800127 */
128 bool
129 matchesInterest(const Interest& other) const;
130
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000131public: // element access
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800133 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700134 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800135 return m_name;
136 }
137
138 Interest&
139 setName(const Name& name)
140 {
141 m_name = name;
142 m_wire.reset();
143 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700144 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700145
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000146 /** @brief Check whether the CanBePrefix element is present.
147 *
148 * This is a getter for the CanBePrefix element as defined in NDN Packet Format v0.3.
149 * In this implementation, it is mapped to the closest v0.2 semantics:
150 * MaxSuffixComponents=1 means CanBePrefix is absent.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300151 */
152 bool
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000153 getCanBePrefix() const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800154 {
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000155 return m_selectors.getMaxSuffixComponents() != 1;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800156 }
157
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000158 /** @brief Add or remove CanBePrefix element.
159 * @param canBePrefix whether CanBePrefix element should be present.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300160 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000161 * This is a setter for the CanBePrefix element as defined in NDN Packet Format v0.3.
162 * In this implementation, it is mapped to the closest v0.2 semantics:
163 * MaxSuffixComponents=1 means CanBePrefix is absent.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300164 */
165 Interest&
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000166 setCanBePrefix(bool canBePrefix)
Junxiao Shi899277a2017-07-07 22:12:12 +0000167 {
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000168 m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1);
169 m_wire.reset();
170 return *this;
Junxiao Shi899277a2017-07-07 22:12:12 +0000171 }
172
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000173 /** @brief Check whether the MustBeFresh element is present.
174 *
175 * This is a getter for the MustBeFresh element as defined in NDN Packet Format v0.3.
176 * In this implementation, it is mapped to the closest v0.2 semantics and appears as
177 * MustBeFresh element under Selectors.
178 */
179 bool
180 getMustBeFresh() const
181 {
182 return m_selectors.getMustBeFresh();
183 }
184
185 /** @brief Add or remove MustBeFresh element.
186 * @param mustBeFresh whether MustBeFresh element should be present.
187 *
188 * This is a setter for the MustBeFresh element as defined in NDN Packet Format v0.3.
189 * In this implementation, it is mapped to the closest v0.2 semantics and appears as
190 * MustBeFresh element under Selectors.
Junxiao Shi899277a2017-07-07 22:12:12 +0000191 */
192 Interest&
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000193 setMustBeFresh(bool mustBeFresh)
194 {
195 m_selectors.setMustBeFresh(mustBeFresh);
196 m_wire.reset();
197 return *this;
198 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000199
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000200 const DelegationList&
201 getForwardingHint() const
202 {
203 return m_forwardingHint;
204 }
205
206 Interest&
207 setForwardingHint(const DelegationList& value);
208
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000209 /** @brief Modify ForwardingHint in-place.
Junxiao Shib2a70332017-07-07 22:15:03 +0000210 * @tparam Modifier a unary function that accepts DelegationList&
211 *
212 * This is equivalent to, but more efficient (avoids copying) than:
213 * @code
214 * auto fh = interest.getForwardingHint();
215 * modifier(fh);
216 * interest.setForwardingHint(fh);
217 * @endcode
218 */
219 template<typename Modifier>
220 Interest&
221 modifyForwardingHint(const Modifier& modifier)
222 {
223 modifier(m_forwardingHint);
224 m_wire.reset();
225 return *this;
226 }
227
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000228 /** @brief Check if the Nonce element is present.
Junxiao Shi2af905b2014-11-27 13:10:54 -0700229 */
230 bool
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000231 hasNonce() const
232 {
233 return static_cast<bool>(m_nonce);
234 }
235
236 /** @brief Get nonce value.
237 *
238 * If nonce was not present, it is added and assigned a random value.
239 */
240 uint32_t
241 getNonce() const;
242
243 /** @brief Set nonce value.
244 */
245 Interest&
246 setNonce(uint32_t nonce);
247
248 /** @brief Change nonce value.
249 *
250 * If the Nonce element is present, the new nonce value will differ from the old value.
251 * If the Nonce element is not present, this method does nothing.
252 */
253 void
254 refreshNonce();
255
256 time::milliseconds
257 getInterestLifetime() const
258 {
259 return m_interestLifetime;
260 }
261
262 /** @brief Set Interest's lifetime
263 * @throw std::invalid_argument @p lifetime is negative
264 */
265 Interest&
266 setInterestLifetime(time::milliseconds lifetime);
267
268public: // Selectors (deprecated)
269 /** @brief Check if Interest has any selector present.
270 */
271 NDN_CXX_DEPRECATED
272 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -0700273 hasSelectors() const
274 {
275 return !m_selectors.empty();
276 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700277
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000278 NDN_CXX_DEPRECATED
Junxiao Shi2af905b2014-11-27 13:10:54 -0700279 const Selectors&
280 getSelectors() const
281 {
282 return m_selectors;
283 }
284
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000285 NDN_CXX_DEPRECATED
Junxiao Shi2af905b2014-11-27 13:10:54 -0700286 Interest&
287 setSelectors(const Selectors& selectors)
288 {
289 m_selectors = selectors;
290 m_wire.reset();
291 return *this;
292 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700293
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000294 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800295 int
296 getMinSuffixComponents() const
297 {
298 return m_selectors.getMinSuffixComponents();
299 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700300
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000301 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800302 Interest&
303 setMinSuffixComponents(int minSuffixComponents)
304 {
305 m_selectors.setMinSuffixComponents(minSuffixComponents);
306 m_wire.reset();
307 return *this;
308 }
309
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000310 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800311 int
312 getMaxSuffixComponents() const
313 {
314 return m_selectors.getMaxSuffixComponents();
315 }
316
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000317 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800318 Interest&
319 setMaxSuffixComponents(int maxSuffixComponents)
320 {
321 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
322 m_wire.reset();
323 return *this;
324 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700325
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000326 NDN_CXX_DEPRECATED
Junxiao Shib332e782014-03-31 14:23:46 -0700327 const KeyLocator&
328 getPublisherPublicKeyLocator() const
329 {
330 return m_selectors.getPublisherPublicKeyLocator();
331 }
332
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000333 NDN_CXX_DEPRECATED
Junxiao Shib332e782014-03-31 14:23:46 -0700334 Interest&
335 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
336 {
337 m_selectors.setPublisherPublicKeyLocator(keyLocator);
338 m_wire.reset();
339 return *this;
340 }
341
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000342 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800343 const Exclude&
344 getExclude() const
345 {
346 return m_selectors.getExclude();
347 }
348
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000349 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800350 Interest&
351 setExclude(const Exclude& exclude)
352 {
353 m_selectors.setExclude(exclude);
354 m_wire.reset();
355 return *this;
356 }
357
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000358 NDN_CXX_DEPRECATED
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700359 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800360 getChildSelector() const
361 {
362 return m_selectors.getChildSelector();
363 }
364
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000365 NDN_CXX_DEPRECATED
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800366 Interest&
367 setChildSelector(int childSelector)
368 {
369 m_selectors.setChildSelector(childSelector);
370 m_wire.reset();
371 return *this;
372 }
373
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800374private:
375 Name m_name;
376 Selectors m_selectors;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000377 mutable optional<uint32_t> m_nonce;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700378 time::milliseconds m_interestLifetime;
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000379 DelegationList m_forwardingHint;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800380
381 mutable Block m_wire;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700382};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800383
Davide Pesavento88a0d812017-08-19 21:31:42 -0400384NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest);
385
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700386std::ostream&
387operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800388
Junxiao Shi899277a2017-07-07 22:12:12 +0000389inline bool
390operator==(const Interest& lhs, const Interest& rhs)
391{
392 return lhs.wireEncode() == rhs.wireEncode();
393}
394
395inline bool
396operator!=(const Interest& lhs, const Interest& rhs)
397{
398 return !(lhs == rhs);
399}
400
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800401} // namespace ndn
402
403#endif // NDN_INTEREST_HPP