blob: 8d3f29b456e4c08526974caba58d407be0e95d90 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
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
Jeff Thompson53412192013-08-06 13:35:50 -070025#include "name.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080026#include "selectors.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070027#include "util/time.hpp"
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080028#include "tag-host.hpp"
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070029#include "link.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 */
38const time::milliseconds DEFAULT_INTEREST_LIFETIME = time::milliseconds(4000);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080039
Junxiao Shic2b8d242014-11-04 08:35:29 -070040/** @brief represents an Interest packet
Jeff Thompson8238d002013-07-10 11:56:49 -070041 */
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080042class Interest : public TagHost, 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 Shi2af905b2014-11-27 13:10:54 -070055 /** @brief Create a new Interest with an empty name (`ndn:/`)
56 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
57 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080058 */
Junxiao Shi2af905b2014-11-27 13:10:54 -070059 Interest();
Alexander Afanasyevc348f832014-02-17 16:35:17 -080060
Junxiao Shi2af905b2014-11-27 13:10:54 -070061 /** @brief Create a new Interest with the given name
62 * @param name The name for the interest.
63 * @note This constructor allows implicit conversion from Name.
64 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
65 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080066 */
Junxiao Shi2af905b2014-11-27 13:10:54 -070067 Interest(const Name& name);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080068
Junxiao Shi2af905b2014-11-27 13:10:54 -070069 /** @brief Create a new Interest with the given name and interest lifetime
70 * @param name The name for the interest.
71 * @param interestLifetime The interest lifetime in time::milliseconds, or -1 for none.
72 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
73 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080074 */
Junxiao Shi2af905b2014-11-27 13:10:54 -070075 Interest(const Name& name, const time::milliseconds& interestLifetime);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
Junxiao Shi2af905b2014-11-27 13:10:54 -070077 /** @brief Create from wire encoding
78 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
79 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080081 explicit
Junxiao Shi2af905b2014-11-27 13:10:54 -070082 Interest(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080083
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080084 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080085 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080086 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080087 template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -070088 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070089 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080090
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080091 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080092 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080093 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070094 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080095 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080096
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080097 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080098 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080099 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700100 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700101 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800102
103 /**
104 * @brief Check if already has wire
105 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700106 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -0700107 hasWire() const
108 {
109 return m_wire.hasWire();
110 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700111
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700112 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700113 * @brief Encode the name according to the NDN URI Scheme
114 *
115 * If there are interest selectors, this method will append "?" and add the selectors as
116 * a query string. For example, "/test/name?ndn.ChildSelector=1"
Jeff Thompson13e280b2013-12-03 13:12:23 -0800117 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700118 std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800119 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700120
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700121public: // Link and forwarding hint
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800122 /**
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700123 * @brief Check whether the Interest contains a Link object
124 * @return True if there is a link object, otherwise false
125 */
126 bool
127 hasLink() const;
128
129 /**
130 * @brief Get the link object for this interest
131 * @return The link object if there is one contained in this interest
132 * @throws Interest::Error if there is no link object contained in the interest
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700133 * @throws tlv::Error if the incorporated link object is malformed
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700134 */
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700135 const Link&
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700136 getLink() const;
137
138 /**
139 * @brief Set the link object for this interest
140 * @param link The link object that will be included in this interest (in wire format)
141 * @post !hasSelectedDelegation()
142 */
143 void
144 setLink(const Block& link);
145
146 /**
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700147 * @brief Delete the link object for this interest
148 * @post !hasLink()
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700149 */
150 void
151 unsetLink();
152
153 /**
154 * @brief Check whether the Interest includes a selected delegation
155 * @return True if there is a selected delegation, otherwise false
156 */
157 bool
158 hasSelectedDelegation() const;
159
160 /**
161 * @brief Get the name of the selected delegation
162 * @return The name of the selected delegation
163 * @throw Error SelectedDelegation is not set.
164 */
165 Name
166 getSelectedDelegation() const;
167
168 /**
169 * @brief Set the selected delegation
170 * @param delegationName The name of the selected delegation
171 * @throw Error Link is not set.
172 * @throw std::invalid_argument @p delegationName does not exist in Link.
173 */
174 void
175 setSelectedDelegation(const Name& delegationName);
176
177 /**
178 * @brief Set the selected delegation
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200179 * @param delegationIndex The index of the selected delegation
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700180 * @throw Error Link is not set.
181 * @throw std::out_of_range @p delegationIndex is out of bound in Link.
182 */
183 void
184 setSelectedDelegation(size_t delegationIndex);
185
186 /**
187 * @brief Unset the selected delegation
188 */
189 void
190 unsetSelectedDelegation();
191
Junxiao Shi2af905b2014-11-27 13:10:54 -0700192public: // matching
193 /** @brief Check if Interest, including selectors, matches the given @p name
194 * @param name The name to be matched. If this is a Data name, it shall contain the
195 * implicit digest component
Alexander Afanasyev84681982014-01-03 13:26:09 -0800196 */
197 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700198 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800199
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700200 /**
201 * @brief Check if Interest can be satisfied by @p data.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700202 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700203 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
204 * PublisherPublicKeyLocator, and Exclude.
205 * This method does not consider ChildSelector and MustBeFresh.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700206 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700207 * @todo recognize implicit digest component
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700208 */
209 bool
210 matchesData(const Data& data) const;
211
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800212 /**
213 * @brief Check if Interest matches @p other interest
214 *
215 * Interest matches @p other if both have the same name, selectors, and link. Other fields
216 * (e.g., Nonce) may be different.
217 *
218 * @todo Implement distinguishing interests by link. The current implementation checks only
219 * name+selectors (Issue #3162).
220 */
221 bool
222 matchesInterest(const Interest& other) const;
223
Junxiao Shi2af905b2014-11-27 13:10:54 -0700224public: // Name and guiders
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700225 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800226 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700227 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800228 return m_name;
229 }
230
231 Interest&
232 setName(const Name& name)
233 {
234 m_name = name;
235 m_wire.reset();
236 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700237 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700238
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700239 const time::milliseconds&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800240 getInterestLifetime() const
241 {
242 return m_interestLifetime;
243 }
244
245 Interest&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700246 setInterestLifetime(const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800247 {
248 m_interestLifetime = interestLifetime;
249 m_wire.reset();
250 return *this;
251 }
252
Junxiao Shi2af905b2014-11-27 13:10:54 -0700253 /** @brief Check if Nonce set
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300254 */
255 bool
256 hasNonce() const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800257 {
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300258 return m_nonce.hasWire();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800259 }
260
Junxiao Shi2af905b2014-11-27 13:10:54 -0700261 /** @brief Get Interest's nonce
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300262 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700263 * If nonce was not set before this call, it will be automatically assigned to a random value
264 */
265 uint32_t
266 getNonce() const;
267
268 /** @brief Set Interest's nonce
269 *
270 * If wire format already exists, this call simply replaces nonce in the
271 * existing wire format, without resetting and recreating it.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300272 */
273 Interest&
274 setNonce(uint32_t nonce);
275
Junxiao Shi2af905b2014-11-27 13:10:54 -0700276 /** @brief Refresh nonce
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700277 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700278 * It's guaranteed that new nonce value differs from the existing one.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700279 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700280 * If nonce is already set, it will be updated to a different random value.
281 * If nonce is not set, this method does nothing.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700282 */
283 void
284 refreshNonce();
285
Junxiao Shi2af905b2014-11-27 13:10:54 -0700286public: // Selectors
287 /**
288 * @return true if Interest has any selector present
289 */
290 bool
291 hasSelectors() const
292 {
293 return !m_selectors.empty();
294 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700295
Junxiao Shi2af905b2014-11-27 13:10:54 -0700296 const Selectors&
297 getSelectors() const
298 {
299 return m_selectors;
300 }
301
302 Interest&
303 setSelectors(const Selectors& selectors)
304 {
305 m_selectors = selectors;
306 m_wire.reset();
307 return *this;
308 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700309
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800310 int
311 getMinSuffixComponents() const
312 {
313 return m_selectors.getMinSuffixComponents();
314 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700315
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800316 Interest&
317 setMinSuffixComponents(int minSuffixComponents)
318 {
319 m_selectors.setMinSuffixComponents(minSuffixComponents);
320 m_wire.reset();
321 return *this;
322 }
323
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800324 int
325 getMaxSuffixComponents() const
326 {
327 return m_selectors.getMaxSuffixComponents();
328 }
329
330 Interest&
331 setMaxSuffixComponents(int maxSuffixComponents)
332 {
333 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
334 m_wire.reset();
335 return *this;
336 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700337
Junxiao Shib332e782014-03-31 14:23:46 -0700338 const KeyLocator&
339 getPublisherPublicKeyLocator() const
340 {
341 return m_selectors.getPublisherPublicKeyLocator();
342 }
343
344 Interest&
345 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
346 {
347 m_selectors.setPublisherPublicKeyLocator(keyLocator);
348 m_wire.reset();
349 return *this;
350 }
351
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800352 const Exclude&
353 getExclude() const
354 {
355 return m_selectors.getExclude();
356 }
357
358 Interest&
359 setExclude(const Exclude& exclude)
360 {
361 m_selectors.setExclude(exclude);
362 m_wire.reset();
363 return *this;
364 }
365
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700366 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800367 getChildSelector() const
368 {
369 return m_selectors.getChildSelector();
370 }
371
372 Interest&
373 setChildSelector(int childSelector)
374 {
375 m_selectors.setChildSelector(childSelector);
376 m_wire.reset();
377 return *this;
378 }
379
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700380 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800381 getMustBeFresh() const
382 {
383 return m_selectors.getMustBeFresh();
384 }
385
386 Interest&
387 setMustBeFresh(bool mustBeFresh)
388 {
389 m_selectors.setMustBeFresh(mustBeFresh);
390 m_wire.reset();
391 return *this;
392 }
393
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700394public: // EqualityComparable concept
395 bool
396 operator==(const Interest& other) const
397 {
398 return wireEncode() == other.wireEncode();
399 }
400
401 bool
402 operator!=(const Interest& other) const
403 {
404 return !(*this == other);
405 }
406
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800407private:
408 Name m_name;
409 Selectors m_selectors;
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300410 mutable Block m_nonce;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700411 time::milliseconds m_interestLifetime;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800412
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700413 mutable Block m_link;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700414 mutable shared_ptr<Link> m_linkCached;
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700415 size_t m_selectedDelegationIndex;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800416 mutable Block m_wire;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700417};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800418
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700419std::ostream&
420operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800421
422inline std::string
423Interest::toUri() const
424{
425 std::ostringstream os;
426 os << *this;
427 return os.str();
428}
429
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800430} // namespace ndn
431
432#endif // NDN_INTEREST_HPP