blob: 1e064fb54b1480fb05393ae7d1947b0c57d87d5d [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 Afanasyev74633892015-02-08 18:08:46 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025#include "common.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026
Jeff Thompson53412192013-08-06 13:35:50 -070027#include "name.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080028#include "selectors.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070029#include "util/time.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080030#include "management/nfd-local-control-header.hpp"
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080031#include "tag-host.hpp"
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070032#include "link.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070033
34namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080035
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070036class Data;
37
Junxiao Shi7007a3c2014-11-20 22:37:55 -070038/** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME;
39 * @brief default value for InterestLifetime
40 */
41const time::milliseconds DEFAULT_INTEREST_LIFETIME = time::milliseconds(4000);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080042
Junxiao Shic2b8d242014-11-04 08:35:29 -070043/** @brief represents an Interest packet
Jeff Thompson8238d002013-07-10 11:56:49 -070044 */
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080045class Interest : public TagHost, public enable_shared_from_this<Interest>
Alexander Afanasyevc348f832014-02-17 16:35:17 -080046{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047public:
Junxiao Shic2b8d242014-11-04 08:35:29 -070048 class Error : public tlv::Error
49 {
50 public:
51 explicit
52 Error(const std::string& what)
53 : tlv::Error(what)
54 {
55 }
56 };
57
Junxiao Shi2af905b2014-11-27 13:10:54 -070058 /** @brief Create a new Interest with an empty name (`ndn:/`)
59 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
60 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080061 */
Junxiao Shi2af905b2014-11-27 13:10:54 -070062 Interest();
Alexander Afanasyevc348f832014-02-17 16:35:17 -080063
Junxiao Shi2af905b2014-11-27 13:10:54 -070064 /** @brief Create a new Interest with the given name
65 * @param name The name for the interest.
66 * @note This constructor allows implicit conversion from Name.
67 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
68 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080069 */
Junxiao Shi2af905b2014-11-27 13:10:54 -070070 Interest(const Name& name);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080071
Junxiao Shi2af905b2014-11-27 13:10:54 -070072 /** @brief Create a new Interest with the given name and interest lifetime
73 * @param name The name for the interest.
74 * @param interestLifetime The interest lifetime in time::milliseconds, or -1 for none.
75 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
76 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080077 */
Junxiao Shi2af905b2014-11-27 13:10:54 -070078 Interest(const Name& name, const time::milliseconds& interestLifetime);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079
Junxiao Shi2af905b2014-11-27 13:10:54 -070080 /** @brief Create a new Interest for the given name, selectors, and guiders
81 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
82 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyev770827c2014-05-13 17:42:55 -070083 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -080084 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085 const Selectors& selectors,
Alexander Afanasyevc348f832014-02-17 16:35:17 -080086 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087 const time::milliseconds& interestLifetime,
Junxiao Shi2af905b2014-11-27 13:10:54 -070088 uint32_t nonce = 0);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089
Junxiao Shi2af905b2014-11-27 13:10:54 -070090 /** @brief Create from wire encoding
91 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
92 * using `make_shared`. Otherwise, .shared_from_this() will throw an exception.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080094 explicit
Junxiao Shi2af905b2014-11-27 13:10:54 -070095 Interest(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080096
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080097 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080098 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080099 */
Alexander Afanasyev74633892015-02-08 18:08:46 -0800100 template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700101 size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800102 wireEncode(EncodingImpl<TAG>& block) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800103
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800104 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800105 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800106 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700107 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800108 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800109
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800110 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800111 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800112 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700113 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700114 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800115
116 /**
117 * @brief Check if already has wire
118 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700119 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -0700120 hasWire() const
121 {
122 return m_wire.hasWire();
123 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700125 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700126 * @brief Encode the name according to the NDN URI Scheme
127 *
128 * If there are interest selectors, this method will append "?" and add the selectors as
129 * a query string. For example, "/test/name?ndn.ChildSelector=1"
Jeff Thompson13e280b2013-12-03 13:12:23 -0800130 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700131 std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800132 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700133
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700134public: // Link and forwarding hint
135
136 /**
137 * @brief Check whether the Interest contains a Link object
138 * @return True if there is a link object, otherwise false
139 */
140 bool
141 hasLink() const;
142
143 /**
144 * @brief Get the link object for this interest
145 * @return The link object if there is one contained in this interest
146 * @throws Interest::Error if there is no link object contained in the interest
147 */
148 Link
149 getLink() const;
150
151 /**
152 * @brief Set the link object for this interest
153 * @param link The link object that will be included in this interest (in wire format)
154 * @post !hasSelectedDelegation()
155 */
156 void
157 setLink(const Block& link);
158
159 /**
160 *@brief Reset the wire format of the given interest and the contained link
161 */
162 void
163 unsetLink();
164
165 /**
166 * @brief Check whether the Interest includes a selected delegation
167 * @return True if there is a selected delegation, otherwise false
168 */
169 bool
170 hasSelectedDelegation() const;
171
172 /**
173 * @brief Get the name of the selected delegation
174 * @return The name of the selected delegation
175 * @throw Error SelectedDelegation is not set.
176 */
177 Name
178 getSelectedDelegation() const;
179
180 /**
181 * @brief Set the selected delegation
182 * @param delegationName The name of the selected delegation
183 * @throw Error Link is not set.
184 * @throw std::invalid_argument @p delegationName does not exist in Link.
185 */
186 void
187 setSelectedDelegation(const Name& delegationName);
188
189 /**
190 * @brief Set the selected delegation
191 * @param delegation The index of the selected delegation
192 * @throw Error Link is not set.
193 * @throw std::out_of_range @p delegationIndex is out of bound in Link.
194 */
195 void
196 setSelectedDelegation(size_t delegationIndex);
197
198 /**
199 * @brief Unset the selected delegation
200 */
201 void
202 unsetSelectedDelegation();
203
Junxiao Shi2af905b2014-11-27 13:10:54 -0700204public: // matching
205 /** @brief Check if Interest, including selectors, matches the given @p name
206 * @param name The name to be matched. If this is a Data name, it shall contain the
207 * implicit digest component
Alexander Afanasyev84681982014-01-03 13:26:09 -0800208 */
209 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700210 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800211
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700212 /**
213 * @brief Check if Interest can be satisfied by @p data.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700214 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700215 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
216 * PublisherPublicKeyLocator, and Exclude.
217 * This method does not consider ChildSelector and MustBeFresh.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700218 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700219 * @todo recognize implicit digest component
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700220 */
221 bool
222 matchesData(const Data& data) 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 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800240 getScope() const
241 {
242 return m_scope;
243 }
244
245 Interest&
246 setScope(int scope)
247 {
248 m_scope = scope;
249 m_wire.reset();
250 return *this;
251 }
252
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700253 const time::milliseconds&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800254 getInterestLifetime() const
255 {
256 return m_interestLifetime;
257 }
258
259 Interest&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700260 setInterestLifetime(const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800261 {
262 m_interestLifetime = interestLifetime;
263 m_wire.reset();
264 return *this;
265 }
266
Junxiao Shi2af905b2014-11-27 13:10:54 -0700267 /** @brief Check if Nonce set
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300268 */
269 bool
270 hasNonce() const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800271 {
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300272 return m_nonce.hasWire();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800273 }
274
Junxiao Shi2af905b2014-11-27 13:10:54 -0700275 /** @brief Get Interest's nonce
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300276 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700277 * If nonce was not set before this call, it will be automatically assigned to a random value
278 */
279 uint32_t
280 getNonce() const;
281
282 /** @brief Set Interest's nonce
283 *
284 * If wire format already exists, this call simply replaces nonce in the
285 * existing wire format, without resetting and recreating it.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300286 */
287 Interest&
288 setNonce(uint32_t nonce);
289
Junxiao Shi2af905b2014-11-27 13:10:54 -0700290 /** @brief Refresh nonce
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700291 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700292 * It's guaranteed that new nonce value differs from the existing one.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700293 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700294 * If nonce is already set, it will be updated to a different random value.
295 * If nonce is not set, this method does nothing.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700296 */
297 void
298 refreshNonce();
299
Junxiao Shi2af905b2014-11-27 13:10:54 -0700300public: // local control header
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800301 nfd::LocalControlHeader&
302 getLocalControlHeader()
303 {
304 return m_localControlHeader;
305 }
306
307 const nfd::LocalControlHeader&
308 getLocalControlHeader() const
309 {
310 return m_localControlHeader;
311 }
312
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800313 uint64_t
314 getIncomingFaceId() const
315 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800316 return getLocalControlHeader().getIncomingFaceId();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800317 }
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800318
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800319 Interest&
320 setIncomingFaceId(uint64_t incomingFaceId)
321 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800322 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800323 // ! do not reset Interest's wire !
324 return *this;
325 }
326
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800327 uint64_t
328 getNextHopFaceId() const
329 {
330 return getLocalControlHeader().getNextHopFaceId();
331 }
332
333 Interest&
334 setNextHopFaceId(uint64_t nextHopFaceId)
335 {
336 getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
337 // ! do not reset Interest's wire !
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800338 return *this;
339 }
340
Junxiao Shi2af905b2014-11-27 13:10:54 -0700341public: // Selectors
342 /**
343 * @return true if Interest has any selector present
344 */
345 bool
346 hasSelectors() const
347 {
348 return !m_selectors.empty();
349 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700350
Junxiao Shi2af905b2014-11-27 13:10:54 -0700351 const Selectors&
352 getSelectors() const
353 {
354 return m_selectors;
355 }
356
357 Interest&
358 setSelectors(const Selectors& selectors)
359 {
360 m_selectors = selectors;
361 m_wire.reset();
362 return *this;
363 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700364
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800365 int
366 getMinSuffixComponents() const
367 {
368 return m_selectors.getMinSuffixComponents();
369 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700370
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800371 Interest&
372 setMinSuffixComponents(int minSuffixComponents)
373 {
374 m_selectors.setMinSuffixComponents(minSuffixComponents);
375 m_wire.reset();
376 return *this;
377 }
378
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800379 int
380 getMaxSuffixComponents() const
381 {
382 return m_selectors.getMaxSuffixComponents();
383 }
384
385 Interest&
386 setMaxSuffixComponents(int maxSuffixComponents)
387 {
388 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
389 m_wire.reset();
390 return *this;
391 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700392
Junxiao Shib332e782014-03-31 14:23:46 -0700393 const KeyLocator&
394 getPublisherPublicKeyLocator() const
395 {
396 return m_selectors.getPublisherPublicKeyLocator();
397 }
398
399 Interest&
400 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
401 {
402 m_selectors.setPublisherPublicKeyLocator(keyLocator);
403 m_wire.reset();
404 return *this;
405 }
406
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800407 const Exclude&
408 getExclude() const
409 {
410 return m_selectors.getExclude();
411 }
412
413 Interest&
414 setExclude(const Exclude& exclude)
415 {
416 m_selectors.setExclude(exclude);
417 m_wire.reset();
418 return *this;
419 }
420
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700421 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800422 getChildSelector() const
423 {
424 return m_selectors.getChildSelector();
425 }
426
427 Interest&
428 setChildSelector(int childSelector)
429 {
430 m_selectors.setChildSelector(childSelector);
431 m_wire.reset();
432 return *this;
433 }
434
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700435 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800436 getMustBeFresh() const
437 {
438 return m_selectors.getMustBeFresh();
439 }
440
441 Interest&
442 setMustBeFresh(bool mustBeFresh)
443 {
444 m_selectors.setMustBeFresh(mustBeFresh);
445 m_wire.reset();
446 return *this;
447 }
448
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700449public: // EqualityComparable concept
450 bool
451 operator==(const Interest& other) const
452 {
453 return wireEncode() == other.wireEncode();
454 }
455
456 bool
457 operator!=(const Interest& other) const
458 {
459 return !(*this == other);
460 }
461
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800462private:
463 Name m_name;
464 Selectors m_selectors;
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300465 mutable Block m_nonce;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800466 int m_scope;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700467 time::milliseconds m_interestLifetime;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800468
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700469 mutable Block m_link;
470 size_t m_selectedDelegationIndex;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800471 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800472
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800473 nfd::LocalControlHeader m_localControlHeader;
474 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700475};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800476
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700477std::ostream&
478operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800479
480inline std::string
481Interest::toUri() const
482{
483 std::ostringstream os;
484 os << *this;
485 return os.str();
486}
487
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800488} // namespace ndn
489
490#endif // NDN_INTEREST_HPP