blob: 121541bd9bbaef969e523d7b55c1d1647c0db327 [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"
Jeff Thompson53412192013-08-06 13:35:50 -070026#include "name.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080027#include "selectors.hpp"
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080028#include "tag-host.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 */
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 the given name and interest lifetime
Junxiao Shi899277a2017-07-07 22:12:12 +000056 * @throw std::invalid_argument InterestLifetime is negative
Junxiao Shi2af905b2014-11-27 13:10:54 -070057 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
Junxiao Shi899277a2017-07-07 22:12:12 +000058 * 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 Shi899277a2017-07-07 22:12:12 +000061 Interest(const Name& name = Name(), time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062
Junxiao Shi2af905b2014-11-27 13:10:54 -070063 /** @brief Create from wire encoding
64 * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created
Junxiao Shi899277a2017-07-07 22:12:12 +000065 * 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
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080070 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080071 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080072 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080073 template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -070074 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070075 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080076
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080077 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080078 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080079 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070080 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080081 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080082
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080083 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080084 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080085 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070086 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080088
89 /**
90 * @brief Check if already has wire
91 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070092 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -070093 hasWire() const
94 {
95 return m_wire.hasWire();
96 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097
Jeff Thompsond9e278c2013-07-08 15:20:13 -070098 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070099 * @brief Encode the name according to the NDN URI Scheme
100 *
101 * If there are interest selectors, this method will append "?" and add the selectors as
102 * a query string. For example, "/test/name?ndn.ChildSelector=1"
Jeff Thompson13e280b2013-12-03 13:12:23 -0800103 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700104 std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800105 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700106
Junxiao Shi2af905b2014-11-27 13:10:54 -0700107public: // matching
108 /** @brief Check if Interest, including selectors, matches the given @p name
109 * @param name The name to be matched. If this is a Data name, it shall contain the
110 * implicit digest component
Alexander Afanasyev84681982014-01-03 13:26:09 -0800111 */
112 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700113 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800114
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700115 /**
116 * @brief Check if Interest can be satisfied by @p data.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700117 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700118 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
119 * PublisherPublicKeyLocator, and Exclude.
120 * This method does not consider ChildSelector and MustBeFresh.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700121 */
122 bool
123 matchesData(const Data& data) const;
124
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800125 /**
126 * @brief Check if Interest matches @p other interest
127 *
128 * Interest matches @p other if both have the same name, selectors, and link. Other fields
129 * (e.g., Nonce) may be different.
130 *
131 * @todo Implement distinguishing interests by link. The current implementation checks only
132 * name+selectors (Issue #3162).
133 */
134 bool
135 matchesInterest(const Interest& other) const;
136
Junxiao Shi899277a2017-07-07 22:12:12 +0000137public: // Name, Nonce, and Guiders
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800139 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700140 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800141 return m_name;
142 }
143
144 Interest&
145 setName(const Name& name)
146 {
147 m_name = name;
148 m_wire.reset();
149 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700150 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700151
Junxiao Shi2af905b2014-11-27 13:10:54 -0700152 /** @brief Check if Nonce set
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300153 */
154 bool
155 hasNonce() const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800156 {
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000157 return static_cast<bool>(m_nonce);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800158 }
159
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000160 /** @brief Get nonce
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300161 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700162 * If nonce was not set before this call, it will be automatically assigned to a random value
163 */
164 uint32_t
165 getNonce() const;
166
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000167 /** @brief Set nonce
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300168 */
169 Interest&
170 setNonce(uint32_t nonce);
171
Junxiao Shi2af905b2014-11-27 13:10:54 -0700172 /** @brief Refresh nonce
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700173 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700174 * It's guaranteed that new nonce value differs from the existing one.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700175 *
Junxiao Shi2af905b2014-11-27 13:10:54 -0700176 * If nonce is already set, it will be updated to a different random value.
177 * If nonce is not set, this method does nothing.
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700178 */
179 void
180 refreshNonce();
181
Junxiao Shi899277a2017-07-07 22:12:12 +0000182 time::milliseconds
183 getInterestLifetime() const
184 {
185 return m_interestLifetime;
186 }
187
188 /**
189 * @brief Set Interest's lifetime
190 * @throw std::invalid_argument specified lifetime is < 0
191 */
192 Interest&
193 setInterestLifetime(time::milliseconds interestLifetime);
194
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000195 const DelegationList&
196 getForwardingHint() const
197 {
198 return m_forwardingHint;
199 }
200
201 Interest&
202 setForwardingHint(const DelegationList& value);
203
Junxiao Shib2a70332017-07-07 22:15:03 +0000204 /** @brief modify ForwardingHint in-place
205 * @tparam Modifier a unary function that accepts DelegationList&
206 *
207 * This is equivalent to, but more efficient (avoids copying) than:
208 * @code
209 * auto fh = interest.getForwardingHint();
210 * modifier(fh);
211 * interest.setForwardingHint(fh);
212 * @endcode
213 */
214 template<typename Modifier>
215 Interest&
216 modifyForwardingHint(const Modifier& modifier)
217 {
218 modifier(m_forwardingHint);
219 m_wire.reset();
220 return *this;
221 }
222
Junxiao Shi2af905b2014-11-27 13:10:54 -0700223public: // Selectors
224 /**
225 * @return true if Interest has any selector present
226 */
227 bool
228 hasSelectors() const
229 {
230 return !m_selectors.empty();
231 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700232
Junxiao Shi2af905b2014-11-27 13:10:54 -0700233 const Selectors&
234 getSelectors() const
235 {
236 return m_selectors;
237 }
238
239 Interest&
240 setSelectors(const Selectors& selectors)
241 {
242 m_selectors = selectors;
243 m_wire.reset();
244 return *this;
245 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700246
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800247 int
248 getMinSuffixComponents() const
249 {
250 return m_selectors.getMinSuffixComponents();
251 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700252
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800253 Interest&
254 setMinSuffixComponents(int minSuffixComponents)
255 {
256 m_selectors.setMinSuffixComponents(minSuffixComponents);
257 m_wire.reset();
258 return *this;
259 }
260
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800261 int
262 getMaxSuffixComponents() const
263 {
264 return m_selectors.getMaxSuffixComponents();
265 }
266
267 Interest&
268 setMaxSuffixComponents(int maxSuffixComponents)
269 {
270 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
271 m_wire.reset();
272 return *this;
273 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700274
Junxiao Shib332e782014-03-31 14:23:46 -0700275 const KeyLocator&
276 getPublisherPublicKeyLocator() const
277 {
278 return m_selectors.getPublisherPublicKeyLocator();
279 }
280
281 Interest&
282 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
283 {
284 m_selectors.setPublisherPublicKeyLocator(keyLocator);
285 m_wire.reset();
286 return *this;
287 }
288
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800289 const Exclude&
290 getExclude() const
291 {
292 return m_selectors.getExclude();
293 }
294
295 Interest&
296 setExclude(const Exclude& exclude)
297 {
298 m_selectors.setExclude(exclude);
299 m_wire.reset();
300 return *this;
301 }
302
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700303 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800304 getChildSelector() const
305 {
306 return m_selectors.getChildSelector();
307 }
308
309 Interest&
310 setChildSelector(int childSelector)
311 {
312 m_selectors.setChildSelector(childSelector);
313 m_wire.reset();
314 return *this;
315 }
316
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700317 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800318 getMustBeFresh() const
319 {
320 return m_selectors.getMustBeFresh();
321 }
322
323 Interest&
324 setMustBeFresh(bool mustBeFresh)
325 {
326 m_selectors.setMustBeFresh(mustBeFresh);
327 m_wire.reset();
328 return *this;
329 }
330
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800331private:
332 Name m_name;
333 Selectors m_selectors;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000334 mutable optional<uint32_t> m_nonce;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700335 time::milliseconds m_interestLifetime;
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000336 DelegationList m_forwardingHint;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800337
338 mutable Block m_wire;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700339};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800340
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700341std::ostream&
342operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800343
Junxiao Shi899277a2017-07-07 22:12:12 +0000344inline bool
345operator==(const Interest& lhs, const Interest& rhs)
346{
347 return lhs.wireEncode() == rhs.wireEncode();
348}
349
350inline bool
351operator!=(const Interest& lhs, const Interest& rhs)
352{
353 return !(lhs == rhs);
354}
355
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800356} // namespace ndn
357
358#endif // NDN_INTEREST_HPP