interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #ifndef NDN_INTEREST_HPP
23 #define NDN_INTEREST_HPP
24 
26 #include "ndn-cxx/name.hpp"
27 #include "ndn-cxx/selectors.hpp"
29 #include "ndn-cxx/util/time.hpp"
30 
31 #include <boost/logic/tribool.hpp>
32 
33 namespace ndn {
34 
35 class Data;
36 
40 const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
41 
44 class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
45 {
46 public:
47  class Error : public tlv::Error
48  {
49  public:
50  using tlv::Error::Error;
51  };
52 
58  explicit
59  Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
60 
65  explicit
66  Interest(const Block& wire);
67 
70  template<encoding::Tag TAG>
71  size_t
72  wireEncode(EncodingImpl<TAG>& encoder) const;
73 
79  const Block&
80  wireEncode() const;
81 
84  void
85  wireDecode(const Block& wire);
86 
89  bool
90  hasWire() const
91  {
92  return m_wire.hasWire();
93  }
94 
101  std::string
102  toUri() const;
103 
104 public: // matching
109  bool
110  matchesName(const Name& name) const;
111 
118  bool
119  matchesData(const Data& data) const;
120 
129  bool
130  matchesInterest(const Interest& other) const;
131 
132 public: // element access
133  const Name&
134  getName() const
135  {
136  return m_name;
137  }
138 
139  Interest&
140  setName(const Name& name)
141  {
142  m_name = name;
143  m_wire.reset();
144  return *this;
145  }
146 
161  static void
162  setDefaultCanBePrefix(bool canBePrefix)
163  {
164  s_defaultCanBePrefix = canBePrefix;
165  }
166 
173  bool
175  {
176  return m_selectors.getMaxSuffixComponents() != 1;
177  }
178 
186  Interest&
187  setCanBePrefix(bool canBePrefix)
188  {
189  m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1);
190  m_wire.reset();
191  m_isCanBePrefixSet = true;
192  return *this;
193  }
194 
201  bool
203  {
204  return m_selectors.getMustBeFresh();
205  }
206 
214  Interest&
215  setMustBeFresh(bool mustBeFresh)
216  {
217  m_selectors.setMustBeFresh(mustBeFresh);
218  m_wire.reset();
219  return *this;
220  }
221 
222  const DelegationList&
224  {
225  return m_forwardingHint;
226  }
227 
228  Interest&
229  setForwardingHint(const DelegationList& value);
230 
241  template<typename Modifier>
242  Interest&
243  modifyForwardingHint(const Modifier& modifier)
244  {
245  modifier(m_forwardingHint);
246  m_wire.reset();
247  return *this;
248  }
249 
252  bool
253  hasNonce() const
254  {
255  return m_nonce.has_value();
256  }
257 
262  uint32_t
263  getNonce() const;
264 
267  Interest&
268  setNonce(uint32_t nonce);
269 
275  void
276  refreshNonce();
277 
278  time::milliseconds
280  {
281  return m_interestLifetime;
282  }
283 
287  Interest&
288  setInterestLifetime(time::milliseconds lifetime);
289 
290  bool
292  {
293  return !m_parameters.empty();
294  }
295 
296  const Block&
298  {
299  return m_parameters;
300  }
301 
311  Interest&
312  setApplicationParameters(const Block& parameters);
313 
320  Interest&
321  setApplicationParameters(const uint8_t* buffer, size_t bufferSize);
322 
328  Interest&
330 
335  Interest&
337 
338 public: // Selectors (deprecated)
341  [[deprecated]]
342  bool
343  hasSelectors() const
344  {
345  return !m_selectors.empty();
346  }
347 
348  [[deprecated]]
349  const Selectors&
350  getSelectors() const
351  {
352  return m_selectors;
353  }
354 
355  [[deprecated]]
356  Interest&
357  setSelectors(const Selectors& selectors)
358  {
359  m_selectors = selectors;
360  m_wire.reset();
361  return *this;
362  }
363 
364  [[deprecated]]
365  int
367  {
368  return m_selectors.getMinSuffixComponents();
369  }
370 
371  [[deprecated]]
372  Interest&
373  setMinSuffixComponents(int minSuffixComponents)
374  {
375  m_selectors.setMinSuffixComponents(minSuffixComponents);
376  m_wire.reset();
377  return *this;
378  }
379 
380  [[deprecated]]
381  int
383  {
384  return m_selectors.getMaxSuffixComponents();
385  }
386 
387  [[deprecated]]
388  Interest&
389  setMaxSuffixComponents(int maxSuffixComponents)
390  {
391  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
392  m_wire.reset();
393  return *this;
394  }
395 
396  [[deprecated]]
397  const KeyLocator&
399  {
400  return m_selectors.getPublisherPublicKeyLocator();
401  }
402 
403  [[deprecated]]
404  Interest&
406  {
407  m_selectors.setPublisherPublicKeyLocator(keyLocator);
408  m_wire.reset();
409  return *this;
410  }
411 
412  [[deprecated]]
413  const Exclude&
414  getExclude() const
415  {
416  return m_selectors.getExclude();
417  }
418 
419  [[deprecated]]
420  Interest&
421  setExclude(const Exclude& exclude)
422  {
423  m_selectors.setExclude(exclude);
424  m_wire.reset();
425  return *this;
426  }
427 
428  [[deprecated]]
429  int
431  {
432  return m_selectors.getChildSelector();
433  }
434 
435  [[deprecated]]
436  Interest&
437  setChildSelector(int childSelector)
438  {
439  m_selectors.setChildSelector(childSelector);
440  m_wire.reset();
441  return *this;
442  }
443 
444 private:
447  template<encoding::Tag TAG>
448  size_t
449  encode02(EncodingImpl<TAG>& encoder) const;
450 
453  template<encoding::Tag TAG>
454  size_t
455  encode03(EncodingImpl<TAG>& encoder) const;
456 
462  bool
463  decode02();
464 
468  void
469  decode03();
470 
471 #ifdef NDN_CXX_HAVE_TESTS
472 public:
475  static bool s_errorIfCanBePrefixUnset;
476 #endif // NDN_CXX_HAVE_TESTS
477 
478 private:
479  static boost::logic::tribool s_defaultCanBePrefix;
480 
481  Name m_name;
482  Selectors m_selectors; // NDN Packet Format v0.2 only
483  mutable bool m_isCanBePrefixSet;
484  mutable optional<uint32_t> m_nonce;
485  time::milliseconds m_interestLifetime;
486  DelegationList m_forwardingHint;
487  Block m_parameters; // NDN Packet Format v0.3 only
488 
489  mutable Block m_wire;
490 
491  friend bool operator==(const Interest& lhs, const Interest& rhs);
492 };
493 
495 
496 std::ostream&
497 operator<<(std::ostream& os, const Interest& interest);
498 
499 bool
500 operator==(const Interest& lhs, const Interest& rhs);
501 
502 inline bool
503 operator!=(const Interest& lhs, const Interest& rhs)
504 {
505  return !(lhs == rhs);
506 }
507 
508 } // namespace ndn
509 
510 #endif // NDN_INTEREST_HPP
int getMinSuffixComponents() const
Definition: interest.hpp:366
int getMaxSuffixComponents() const
Definition: interest.hpp:382
const Name & getName() const
Definition: interest.hpp:134
Definition: data.cpp:26
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
Definition: interest.cpp:430
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:243
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:215
const Block & getApplicationParameters() const
Definition: interest.hpp:297
void refreshNonce()
Change nonce value.
Definition: interest.cpp:566
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:321
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:50
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: interest.hpp:90
bool hasSelectors() const
Check if Interest has any selector present.
Definition: interest.hpp:343
const Block & wireEncode() const
Encode to a Block.
Definition: interest.cpp:215
Interest & setApplicationParameters(const Block &parameters)
Set ApplicationParameters from a Block.
Definition: interest.cpp:599
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:44
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:262
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:420
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
int getChildSelector() const
Definition: interest.hpp:430
uint32_t getNonce() const
Get nonce value.
Definition: interest.cpp:549
bool getCanBePrefix() const
Check whether the CanBePrefix element is present.
Definition: interest.hpp:174
const Selectors & getSelectors() const
Definition: interest.hpp:350
Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:421
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:437
Interest & setName(const Name &name)
Definition: interest.hpp:140
Interest & setNonce(uint32_t nonce)
Set nonce value.
Definition: interest.cpp:558
Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:405
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
const Exclude & getExclude() const
Definition: interest.hpp:414
Abstraction implementing Interest selectors.
Definition: selectors.hpp:35
Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:373
friend bool operator==(const Interest &lhs, const Interest &rhs)
Definition: interest.cpp:647
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:389
static void setDefaultCanBePrefix(bool canBePrefix)
Declare the default CanBePrefix setting of the application.
Definition: interest.hpp:162
Represents an absolute name.
Definition: name.hpp:43
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:457
Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:357
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: interest.cpp:231
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
Definition: interest.cpp:539
const DelegationList & getForwardingHint() const
Definition: interest.hpp:223
represents a list of Delegations
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:591
Interest & setInterestLifetime(time::milliseconds lifetime)
Set Interest&#39;s lifetime.
Definition: interest.cpp:580
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:398
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:40
time::milliseconds getInterestLifetime() const
Definition: interest.hpp:279
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:637
Represents a Data packet.
Definition: data.hpp:35
util::scheduler::EventId deprecated
Definition: scheduler.hpp:228
bool getMustBeFresh() const
Check whether the MustBeFresh element is present.
Definition: interest.hpp:202
bool hasApplicationParameters() const
Definition: interest.hpp:291
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:43
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:187
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
bool hasNonce() const
Check if the Nonce element is present.
Definition: interest.hpp:253