blob: 7a45bc87fff68b8f543919b9f18030c0d4a0b640 [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/*
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 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"
Junxiao Shi899277a2017-07-07 22:12:12 +000026#include "link.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070027#include "name.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080028#include "selectors.hpp"
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080029#include "tag-host.hpp"
Junxiao Shi899277a2017-07-07 22:12:12 +000030#include "util/time.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070031
32namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080033
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070034class Data;
35
Junxiao Shi7007a3c2014-11-20 22:37:55 -070036/** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME;
37 * @brief default value for InterestLifetime
38 */
39const time::milliseconds DEFAULT_INTEREST_LIFETIME = time::milliseconds(4000);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080040
Junxiao Shic2b8d242014-11-04 08:35:29 -070041/** @brief represents an Interest packet
Jeff Thompson8238d002013-07-10 11:56:49 -070042 */
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080043class Interest : public TagHost, public enable_shared_from_this<Interest>
Alexander Afanasyevc348f832014-02-17 16:35:17 -080044{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045public:
Junxiao Shic2b8d242014-11-04 08:35:29 -070046 class Error : public tlv::Error
47 {
48 public:
49 explicit
50 Error(const std::string& what)
51 : tlv::Error(what)
52 {
53 }
54 };
55
Junxiao Shi2af905b2014-11-27 13:10:54 -070056 /** @brief Create a new Interest with the given name and interest lifetime
Junxiao Shi899277a2017-07-07 22:12:12 +000057 * @throw std::invalid_argument InterestLifetime is negative
Junxiao Shi2af905b2014-11-27 13:10:54 -070058 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
Junxiao Shi899277a2017-07-07 22:12:12 +000059 * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080060 */
Junxiao Shi909ffef2017-07-07 22:12:27 +000061 explicit
Junxiao Shi899277a2017-07-07 22:12:12 +000062 Interest(const Name& name = Name(), time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
Junxiao Shi2af905b2014-11-27 13:10:54 -070064 /** @brief Create from wire encoding
65 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
Junxiao Shi899277a2017-07-07 22:12:12 +000066 * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080068 explicit
Junxiao Shi2af905b2014-11-27 13:10:54 -070069 Interest(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080070
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080071 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080072 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080073 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080074 template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -070075 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070076 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080077
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080078 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080079 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080080 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070081 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080082 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080083
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080084 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080085 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080086 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070087 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070088 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080089
90 /**
91 * @brief Check if already has wire
92 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070093 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -070094 hasWire() const
95 {
96 return m_wire.hasWire();
97 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098
Jeff Thompsond9e278c2013-07-08 15:20:13 -070099 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700100 * @brief Encode the name according to the NDN URI Scheme
101 *
102 * If there are interest selectors, this method will append "?" and add the selectors as
103 * a query string. For example, "/test/name?ndn.ChildSelector=1"
Jeff Thompson13e280b2013-12-03 13:12:23 -0800104 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700105 std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800106 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700107
Junxiao Shi2af905b2014-11-27 13:10:54 -0700108public: // matching
109 /** @brief Check if Interest, including selectors, matches the given @p name
110 * @param name The name to be matched. If this is a Data name, it shall contain the
111 * implicit digest component
Alexander Afanasyev84681982014-01-03 13:26:09 -0800112 */
113 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700114 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800115
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700116 /**
117 * @brief Check if Interest can be satisfied by @p data.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700118 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700119 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
120 * PublisherPublicKeyLocator, and Exclude.
121 * This method does not consider ChildSelector and MustBeFresh.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700122 */
123 bool
124 matchesData(const Data& data) const;
125
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800126 /**
127 * @brief Check if Interest matches @p other interest
128 *
129 * Interest matches @p other if both have the same name, selectors, and link. Other fields
130 * (e.g., Nonce) may be different.
131 *
132 * @todo Implement distinguishing interests by link. The current implementation checks only
133 * name+selectors (Issue #3162).
134 */
135 bool
136 matchesInterest(const Interest& other) const;
137
Junxiao Shi899277a2017-07-07 22:12:12 +0000138public: // Name, Nonce, and Guiders
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800140 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700141 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800142 return m_name;
143 }
144
145 Interest&
146 setName(const Name& name)
147 {
148 m_name = name;
149 m_wire.reset();
150 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700151 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700152
Junxiao Shi2af905b2014-11-27 13:10:54 -0700153 /** @brief Check if Nonce set
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300154 */
155 bool
156 hasNonce() const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800157 {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000158 return static_cast<bool>(m_nonce);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800159 }
160
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000161 /** @brief Get nonce
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300162 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700163 * If nonce was not set before this call, it will be automatically assigned to a random value
164 */
165 uint32_t
166 getNonce() const;
167
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000168 /** @brief Set nonce
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300169 */
170 Interest&
171 setNonce(uint32_t nonce);
172
Junxiao Shi2af905b2014-11-27 13:10:54 -0700173 /** @brief Refresh nonce
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700174 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700175 * It's guaranteed that new nonce value differs from the existing one.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700176 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700177 * If nonce is already set, it will be updated to a different random value.
178 * If nonce is not set, this method does nothing.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700179 */
180 void
181 refreshNonce();
182
Junxiao Shi899277a2017-07-07 22:12:12 +0000183 time::milliseconds
184 getInterestLifetime() const
185 {
186 return m_interestLifetime;
187 }
188
189 /**
190 * @brief Set Interest's lifetime
191 * @throw std::invalid_argument specified lifetime is < 0
192 */
193 Interest&
194 setInterestLifetime(time::milliseconds interestLifetime);
195
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000196 const DelegationList&
197 getForwardingHint() const
198 {
199 return m_forwardingHint;
200 }
201
202 Interest&
203 setForwardingHint(const DelegationList& value);
204
Junxiao Shib2a70332017-07-07 22:15:03 +0000205 /** @brief modify ForwardingHint in-place
206 * @tparam Modifier a unary function that accepts DelegationList&
207 *
208 * This is equivalent to, but more efficient (avoids copying) than:
209 * @code
210 * auto fh = interest.getForwardingHint();
211 * modifier(fh);
212 * interest.setForwardingHint(fh);
213 * @endcode
214 */
215 template<typename Modifier>
216 Interest&
217 modifyForwardingHint(const Modifier& modifier)
218 {
219 modifier(m_forwardingHint);
220 m_wire.reset();
221 return *this;
222 }
223
Junxiao Shi2af905b2014-11-27 13:10:54 -0700224public: // Selectors
225 /**
226 * @return true if Interest has any selector present
227 */
228 bool
229 hasSelectors() const
230 {
231 return !m_selectors.empty();
232 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700233
Junxiao Shi2af905b2014-11-27 13:10:54 -0700234 const Selectors&
235 getSelectors() const
236 {
237 return m_selectors;
238 }
239
240 Interest&
241 setSelectors(const Selectors& selectors)
242 {
243 m_selectors = selectors;
244 m_wire.reset();
245 return *this;
246 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700247
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800248 int
249 getMinSuffixComponents() const
250 {
251 return m_selectors.getMinSuffixComponents();
252 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700253
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800254 Interest&
255 setMinSuffixComponents(int minSuffixComponents)
256 {
257 m_selectors.setMinSuffixComponents(minSuffixComponents);
258 m_wire.reset();
259 return *this;
260 }
261
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800262 int
263 getMaxSuffixComponents() const
264 {
265 return m_selectors.getMaxSuffixComponents();
266 }
267
268 Interest&
269 setMaxSuffixComponents(int maxSuffixComponents)
270 {
271 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
272 m_wire.reset();
273 return *this;
274 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700275
Junxiao Shib332e782014-03-31 14:23:46 -0700276 const KeyLocator&
277 getPublisherPublicKeyLocator() const
278 {
279 return m_selectors.getPublisherPublicKeyLocator();
280 }
281
282 Interest&
283 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
284 {
285 m_selectors.setPublisherPublicKeyLocator(keyLocator);
286 m_wire.reset();
287 return *this;
288 }
289
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800290 const Exclude&
291 getExclude() const
292 {
293 return m_selectors.getExclude();
294 }
295
296 Interest&
297 setExclude(const Exclude& exclude)
298 {
299 m_selectors.setExclude(exclude);
300 m_wire.reset();
301 return *this;
302 }
303
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700304 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800305 getChildSelector() const
306 {
307 return m_selectors.getChildSelector();
308 }
309
310 Interest&
311 setChildSelector(int childSelector)
312 {
313 m_selectors.setChildSelector(childSelector);
314 m_wire.reset();
315 return *this;
316 }
317
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700318 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800319 getMustBeFresh() const
320 {
321 return m_selectors.getMustBeFresh();
322 }
323
324 Interest&
325 setMustBeFresh(bool mustBeFresh)
326 {
327 m_selectors.setMustBeFresh(mustBeFresh);
328 m_wire.reset();
329 return *this;
330 }
331
Junxiao Shi899277a2017-07-07 22:12:12 +0000332public: // Link and SelectedDelegation
333 /**
334 * @brief Check whether the Interest contains a Link object
335 * @return True if there is a link object, otherwise false
336 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700337 bool
Junxiao Shi899277a2017-07-07 22:12:12 +0000338 hasLink() const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700339
Junxiao Shi899277a2017-07-07 22:12:12 +0000340 /**
341 * @brief Get the link object for this interest
342 * @return The link object if there is one contained in this interest
343 * @throws Interest::Error if there is no link object contained in the interest
344 * @throws tlv::Error if the incorporated link object is malformed
345 */
346 const Link&
347 getLink() const;
348
349 /**
350 * @brief Set the link object for this interest
351 * @param link The link object that will be included in this interest (in wire format)
352 * @post !hasSelectedDelegation()
353 */
354 void
355 setLink(const Block& link);
356
357 /**
358 * @brief Delete the link object for this interest
359 * @post !hasLink()
360 */
361 void
362 unsetLink();
363
364 /**
365 * @brief Check whether the Interest includes a selected delegation
366 * @return True if there is a selected delegation, otherwise false
367 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700368 bool
Junxiao Shi899277a2017-07-07 22:12:12 +0000369 hasSelectedDelegation() const;
370
371 /**
372 * @brief Get the name of the selected delegation
373 * @return The name of the selected delegation
374 * @throw Error SelectedDelegation is not set.
375 */
376 Name
377 getSelectedDelegation() const;
378
379 /**
380 * @brief Set the selected delegation
381 * @param delegationName The name of the selected delegation
382 * @throw Error Link is not set.
383 * @throw std::invalid_argument @p delegationName does not exist in Link.
384 */
385 void
386 setSelectedDelegation(const Name& delegationName);
387
388 /**
389 * @brief Set the selected delegation
390 * @param delegationIndex The index of the selected delegation
391 * @throw Error Link is not set.
392 * @throw std::out_of_range @p delegationIndex is out of bound in Link.
393 */
394 void
395 setSelectedDelegation(size_t delegationIndex);
396
397 /**
398 * @brief Unset the selected delegation
399 */
400 void
401 unsetSelectedDelegation();
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700402
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800403private:
404 Name m_name;
405 Selectors m_selectors;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000406 mutable optional<uint32_t> m_nonce;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700407 time::milliseconds m_interestLifetime;
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000408 DelegationList m_forwardingHint;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800409
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700410 mutable Block m_link;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700411 mutable shared_ptr<Link> m_linkCached;
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700412 size_t m_selectedDelegationIndex;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800413 mutable Block m_wire;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700414};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800415
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700416std::ostream&
417operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800418
419inline std::string
420Interest::toUri() const
421{
422 std::ostringstream os;
423 os << *this;
424 return os.str();
425}
426
Junxiao Shi899277a2017-07-07 22:12:12 +0000427inline bool
428operator==(const Interest& lhs, const Interest& rhs)
429{
430 return lhs.wireEncode() == rhs.wireEncode();
431}
432
433inline bool
434operator!=(const Interest& lhs, const Interest& rhs)
435{
436 return !(lhs == rhs);
437}
438
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800439} // namespace ndn
440
441#endif // NDN_INTEREST_HPP