NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018 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 static_cast<bool>(m_nonce);
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 
308  Interest&
309  setParameters(const Block& parameters);
310 
317  Interest&
318  setParameters(const uint8_t* buffer, size_t bufferSize);
319 
325  Interest&
327 
332  Interest&
333  unsetParameters();
334 
335 public: // Selectors (deprecated)
338  [[deprecated]]
339  bool
340  hasSelectors() const
341  {
342  return !m_selectors.empty();
343  }
344 
345  [[deprecated]]
346  const Selectors&
347  getSelectors() const
348  {
349  return m_selectors;
350  }
351 
352  [[deprecated]]
353  Interest&
354  setSelectors(const Selectors& selectors)
355  {
356  m_selectors = selectors;
357  m_wire.reset();
358  return *this;
359  }
360 
361  [[deprecated]]
362  int
364  {
365  return m_selectors.getMinSuffixComponents();
366  }
367 
368  [[deprecated]]
369  Interest&
370  setMinSuffixComponents(int minSuffixComponents)
371  {
372  m_selectors.setMinSuffixComponents(minSuffixComponents);
373  m_wire.reset();
374  return *this;
375  }
376 
377  [[deprecated]]
378  int
380  {
381  return m_selectors.getMaxSuffixComponents();
382  }
383 
384  [[deprecated]]
385  Interest&
386  setMaxSuffixComponents(int maxSuffixComponents)
387  {
388  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
389  m_wire.reset();
390  return *this;
391  }
392 
393  [[deprecated]]
394  const KeyLocator&
396  {
397  return m_selectors.getPublisherPublicKeyLocator();
398  }
399 
400  [[deprecated]]
401  Interest&
403  {
404  m_selectors.setPublisherPublicKeyLocator(keyLocator);
405  m_wire.reset();
406  return *this;
407  }
408 
409  [[deprecated]]
410  const Exclude&
411  getExclude() const
412  {
413  return m_selectors.getExclude();
414  }
415 
416  [[deprecated]]
417  Interest&
418  setExclude(const Exclude& exclude)
419  {
420  m_selectors.setExclude(exclude);
421  m_wire.reset();
422  return *this;
423  }
424 
425  [[deprecated]]
426  int
428  {
429  return m_selectors.getChildSelector();
430  }
431 
432  [[deprecated]]
433  Interest&
434  setChildSelector(int childSelector)
435  {
436  m_selectors.setChildSelector(childSelector);
437  m_wire.reset();
438  return *this;
439  }
440 
441 private:
444  template<encoding::Tag TAG>
445  size_t
446  encode02(EncodingImpl<TAG>& encoder) const;
447 
450  template<encoding::Tag TAG>
451  size_t
452  encode03(EncodingImpl<TAG>& encoder) const;
453 
459  bool
460  decode02();
461 
465  void
466  decode03();
467 
468 #ifdef NDN_CXX_HAVE_TESTS
469 public:
472  static bool s_errorIfCanBePrefixUnset;
473 #endif // NDN_CXX_HAVE_TESTS
474 
475 private:
476  static boost::logic::tribool s_defaultCanBePrefix;
477 
478  Name m_name;
479  Selectors m_selectors; // NDN Packet Format v0.2 only
480  mutable bool m_isCanBePrefixSet;
481  mutable optional<uint32_t> m_nonce;
482  time::milliseconds m_interestLifetime;
483  DelegationList m_forwardingHint;
484  Block m_parameters; // NDN Packet Format v0.3 only
485 
486  mutable Block m_wire;
487 
488  friend bool operator==(const Interest& lhs, const Interest& rhs);
489 };
490 
492 
493 std::ostream&
494 operator<<(std::ostream& os, const Interest& interest);
495 
496 bool
497 operator==(const Interest& lhs, const Interest& rhs);
498 
499 inline bool
500 operator!=(const Interest& lhs, const Interest& rhs)
501 {
502  return !(lhs == rhs);
503 }
504 
505 } // namespace ndn
506 
507 #endif // NDN_INTEREST_HPP
int getMinSuffixComponents() const
Definition: selectors.hpp:76
const Block & wireEncode() const
Encode to a Block.
Definition: interest.cpp:202
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:249
Copyright (c) 2011-2015 Regents of the University of California.
int getChildSelector() const
Definition: interest.hpp:427
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:243
const Selectors & getSelectors() const
Definition: interest.hpp:347
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:215
Selectors & setMustBeFresh(bool mustBeFresh)
Definition: selectors.cpp:225
bool empty() const
Definition: selectors.cpp:49
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:408
void refreshNonce()
Change nonce value.
Definition: interest.cpp:556
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:46
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
Definition: interest.cpp:529
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
Definition: interest.cpp:418
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
bool getMustBeFresh() const
Definition: selectors.hpp:125
int getChildSelector() const
Definition: selectors.hpp:112
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.cpp:190
bool hasNonce() const
Check if the Nonce element is present.
Definition: interest.hpp:253
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.cpp:206
Selectors & setChildSelector(int childSelector)
set ChildSelector
Definition: selectors.cpp:214
int getMaxSuffixComponents() const
Definition: interest.hpp:379
bool empty() const
Check if the Block is empty.
Definition: block.hpp:187
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.cpp:182
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:395
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:94
Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:418
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:434
Interest & setName(const Name &name)
Definition: interest.hpp:140
uint32_t getNonce() const
Get nonce value.
Definition: interest.cpp:539
const Block & getParameters() const
Definition: interest.hpp:297
Interest & setNonce(uint32_t nonce)
Set nonce value.
Definition: interest.cpp:548
Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:402
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
Abstraction implementing Interest selectors.
Definition: selectors.hpp:35
Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:370
friend bool operator==(const Interest &lhs, const Interest &rhs)
Definition: interest.cpp:628
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:445
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:386
void reset()
Reset wire buffer of the element.
Definition: block.cpp:255
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 getCanBePrefix() const
Check whether the CanBePrefix element is present.
Definition: interest.hpp:174
bool hasParameters() const
Definition: interest.hpp:291
const Exclude & getExclude() const
Definition: interest.hpp:411
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: interest.hpp:90
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:354
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: interest.cpp:218
int getMaxSuffixComponents() const
Definition: selectors.hpp:85
int getMinSuffixComponents() const
Definition: interest.hpp:363
const Exclude & getExclude() const
Definition: selectors.hpp:103
time::milliseconds getInterestLifetime() const
Definition: interest.hpp:279
const DelegationList & getForwardingHint() const
Definition: interest.hpp:223
represents a list of Delegations
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:313
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:581
bool getMustBeFresh() const
Check whether the MustBeFresh element is present.
Definition: interest.hpp:202
Interest & setInterestLifetime(time::milliseconds lifetime)
Set Interest's lifetime.
Definition: interest.cpp:570
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:40
Interest & unsetParameters()
Remove the Parameters element from this Interest.
Definition: interest.cpp:618
Represents a Data packet.
Definition: data.hpp:35
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:43
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.cpp:198
bool hasSelectors() const
Check if Interest has any selector present.
Definition: interest.hpp:340
const Name & getName() const
Definition: interest.hpp:134
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:187
Interest & setParameters(const Block &parameters)
Set parameters from a Block.
Definition: interest.cpp:589
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126