blob: f772defc5820dc3af8430b0dcedb4b18fda80d21 [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 {
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300158 return m_nonce.hasWire();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800159 }
160
Junxiao Shi2af905b2014-11-27 13:10:54 -0700161 /** @brief Get Interest's 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
168 /** @brief Set Interest's nonce
169 *
170 * If wire format already exists, this call simply replaces nonce in the
171 * existing wire format, without resetting and recreating it.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300172 */
173 Interest&
174 setNonce(uint32_t nonce);
175
Junxiao Shi2af905b2014-11-27 13:10:54 -0700176 /** @brief Refresh nonce
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700177 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700178 * It's guaranteed that new nonce value differs from the existing one.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700179 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700180 * If nonce is already set, it will be updated to a different random value.
181 * If nonce is not set, this method does nothing.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700182 */
183 void
184 refreshNonce();
185
Junxiao Shi899277a2017-07-07 22:12:12 +0000186 time::milliseconds
187 getInterestLifetime() const
188 {
189 return m_interestLifetime;
190 }
191
192 /**
193 * @brief Set Interest's lifetime
194 * @throw std::invalid_argument specified lifetime is < 0
195 */
196 Interest&
197 setInterestLifetime(time::milliseconds interestLifetime);
198
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000199 const DelegationList&
200 getForwardingHint() const
201 {
202 return m_forwardingHint;
203 }
204
205 Interest&
206 setForwardingHint(const DelegationList& value);
207
Junxiao Shi2af905b2014-11-27 13:10:54 -0700208public: // Selectors
209 /**
210 * @return true if Interest has any selector present
211 */
212 bool
213 hasSelectors() const
214 {
215 return !m_selectors.empty();
216 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700217
Junxiao Shi2af905b2014-11-27 13:10:54 -0700218 const Selectors&
219 getSelectors() const
220 {
221 return m_selectors;
222 }
223
224 Interest&
225 setSelectors(const Selectors& selectors)
226 {
227 m_selectors = selectors;
228 m_wire.reset();
229 return *this;
230 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700231
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800232 int
233 getMinSuffixComponents() const
234 {
235 return m_selectors.getMinSuffixComponents();
236 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700237
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800238 Interest&
239 setMinSuffixComponents(int minSuffixComponents)
240 {
241 m_selectors.setMinSuffixComponents(minSuffixComponents);
242 m_wire.reset();
243 return *this;
244 }
245
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800246 int
247 getMaxSuffixComponents() const
248 {
249 return m_selectors.getMaxSuffixComponents();
250 }
251
252 Interest&
253 setMaxSuffixComponents(int maxSuffixComponents)
254 {
255 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
256 m_wire.reset();
257 return *this;
258 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700259
Junxiao Shib332e782014-03-31 14:23:46 -0700260 const KeyLocator&
261 getPublisherPublicKeyLocator() const
262 {
263 return m_selectors.getPublisherPublicKeyLocator();
264 }
265
266 Interest&
267 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
268 {
269 m_selectors.setPublisherPublicKeyLocator(keyLocator);
270 m_wire.reset();
271 return *this;
272 }
273
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800274 const Exclude&
275 getExclude() const
276 {
277 return m_selectors.getExclude();
278 }
279
280 Interest&
281 setExclude(const Exclude& exclude)
282 {
283 m_selectors.setExclude(exclude);
284 m_wire.reset();
285 return *this;
286 }
287
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700288 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800289 getChildSelector() const
290 {
291 return m_selectors.getChildSelector();
292 }
293
294 Interest&
295 setChildSelector(int childSelector)
296 {
297 m_selectors.setChildSelector(childSelector);
298 m_wire.reset();
299 return *this;
300 }
301
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700302 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800303 getMustBeFresh() const
304 {
305 return m_selectors.getMustBeFresh();
306 }
307
308 Interest&
309 setMustBeFresh(bool mustBeFresh)
310 {
311 m_selectors.setMustBeFresh(mustBeFresh);
312 m_wire.reset();
313 return *this;
314 }
315
Junxiao Shi899277a2017-07-07 22:12:12 +0000316public: // Link and SelectedDelegation
317 /**
318 * @brief Check whether the Interest contains a Link object
319 * @return True if there is a link object, otherwise false
320 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700321 bool
Junxiao Shi899277a2017-07-07 22:12:12 +0000322 hasLink() const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700323
Junxiao Shi899277a2017-07-07 22:12:12 +0000324 /**
325 * @brief Get the link object for this interest
326 * @return The link object if there is one contained in this interest
327 * @throws Interest::Error if there is no link object contained in the interest
328 * @throws tlv::Error if the incorporated link object is malformed
329 */
330 const Link&
331 getLink() const;
332
333 /**
334 * @brief Set the link object for this interest
335 * @param link The link object that will be included in this interest (in wire format)
336 * @post !hasSelectedDelegation()
337 */
338 void
339 setLink(const Block& link);
340
341 /**
342 * @brief Delete the link object for this interest
343 * @post !hasLink()
344 */
345 void
346 unsetLink();
347
348 /**
349 * @brief Check whether the Interest includes a selected delegation
350 * @return True if there is a selected delegation, otherwise false
351 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700352 bool
Junxiao Shi899277a2017-07-07 22:12:12 +0000353 hasSelectedDelegation() const;
354
355 /**
356 * @brief Get the name of the selected delegation
357 * @return The name of the selected delegation
358 * @throw Error SelectedDelegation is not set.
359 */
360 Name
361 getSelectedDelegation() const;
362
363 /**
364 * @brief Set the selected delegation
365 * @param delegationName The name of the selected delegation
366 * @throw Error Link is not set.
367 * @throw std::invalid_argument @p delegationName does not exist in Link.
368 */
369 void
370 setSelectedDelegation(const Name& delegationName);
371
372 /**
373 * @brief Set the selected delegation
374 * @param delegationIndex The index of the selected delegation
375 * @throw Error Link is not set.
376 * @throw std::out_of_range @p delegationIndex is out of bound in Link.
377 */
378 void
379 setSelectedDelegation(size_t delegationIndex);
380
381 /**
382 * @brief Unset the selected delegation
383 */
384 void
385 unsetSelectedDelegation();
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700386
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800387private:
388 Name m_name;
389 Selectors m_selectors;
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300390 mutable Block m_nonce;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700391 time::milliseconds m_interestLifetime;
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000392 DelegationList m_forwardingHint;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800393
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700394 mutable Block m_link;
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700395 mutable shared_ptr<Link> m_linkCached;
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700396 size_t m_selectedDelegationIndex;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800397 mutable Block m_wire;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700398};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800399
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700400std::ostream&
401operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800402
403inline std::string
404Interest::toUri() const
405{
406 std::ostringstream os;
407 os << *this;
408 return os.str();
409}
410
Junxiao Shi899277a2017-07-07 22:12:12 +0000411inline bool
412operator==(const Interest& lhs, const Interest& rhs)
413{
414 return lhs.wireEncode() == rhs.wireEncode();
415}
416
417inline bool
418operator!=(const Interest& lhs, const Interest& rhs)
419{
420 return !(lhs == rhs);
421}
422
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800423} // namespace ndn
424
425#endif // NDN_INTEREST_HPP