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 
25 #include "delegation-list.hpp"
26 #include "name.hpp"
27 #include "packet-base.hpp"
28 #include "selectors.hpp"
29 #include "util/time.hpp"
30 #include <boost/logic/tribool.hpp>
31 
32 namespace ndn {
33 
34 class Data;
35 
39 const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
40 
43 class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
44 {
45 public:
46  class Error : public tlv::Error
47  {
48  public:
49  using tlv::Error::Error;
50  };
51 
57  explicit
58  Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
59 
64  explicit
65  Interest(const Block& wire);
66 
69  template<encoding::Tag TAG>
70  size_t
71  wireEncode(EncodingImpl<TAG>& encoder) const;
72 
78  const Block&
79  wireEncode() const;
80 
83  void
84  wireDecode(const Block& wire);
85 
88  bool
89  hasWire() const
90  {
91  return m_wire.hasWire();
92  }
93 
100  std::string
101  toUri() const;
102 
103 public: // matching
108  bool
109  matchesName(const Name& name) const;
110 
117  bool
118  matchesData(const Data& data) const;
119 
128  bool
129  matchesInterest(const Interest& other) const;
130 
131 public: // element access
132  const Name&
133  getName() const
134  {
135  return m_name;
136  }
137 
138  Interest&
139  setName(const Name& name)
140  {
141  m_name = name;
142  m_wire.reset();
143  return *this;
144  }
145 
160  static void
161  setDefaultCanBePrefix(bool canBePrefix)
162  {
163  s_defaultCanBePrefix = canBePrefix;
164  }
165 
172  bool
174  {
175  return m_selectors.getMaxSuffixComponents() != 1;
176  }
177 
185  Interest&
186  setCanBePrefix(bool canBePrefix)
187  {
188  m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1);
189  m_wire.reset();
190  m_isCanBePrefixSet = true;
191  return *this;
192  }
193 
200  bool
202  {
203  return m_selectors.getMustBeFresh();
204  }
205 
213  Interest&
214  setMustBeFresh(bool mustBeFresh)
215  {
216  m_selectors.setMustBeFresh(mustBeFresh);
217  m_wire.reset();
218  return *this;
219  }
220 
221  const DelegationList&
223  {
224  return m_forwardingHint;
225  }
226 
227  Interest&
228  setForwardingHint(const DelegationList& value);
229 
240  template<typename Modifier>
241  Interest&
242  modifyForwardingHint(const Modifier& modifier)
243  {
244  modifier(m_forwardingHint);
245  m_wire.reset();
246  return *this;
247  }
248 
251  bool
252  hasNonce() const
253  {
254  return static_cast<bool>(m_nonce);
255  }
256 
261  uint32_t
262  getNonce() const;
263 
266  Interest&
267  setNonce(uint32_t nonce);
268 
274  void
275  refreshNonce();
276 
277  time::milliseconds
279  {
280  return m_interestLifetime;
281  }
282 
286  Interest&
287  setInterestLifetime(time::milliseconds lifetime);
288 
289  bool
291  {
292  return !m_parameters.empty();
293  }
294 
295  const Block&
297  {
298  return m_parameters;
299  }
300 
307  Interest&
308  setParameters(const Block& parameters);
309 
316  Interest&
317  setParameters(const uint8_t* buffer, size_t bufferSize);
318 
324  Interest&
326 
331  Interest&
332  unsetParameters();
333 
334 public: // Selectors (deprecated)
337  [[deprecated]]
338  bool
339  hasSelectors() const
340  {
341  return !m_selectors.empty();
342  }
343 
344  [[deprecated]]
345  const Selectors&
346  getSelectors() const
347  {
348  return m_selectors;
349  }
350 
351  [[deprecated]]
352  Interest&
353  setSelectors(const Selectors& selectors)
354  {
355  m_selectors = selectors;
356  m_wire.reset();
357  return *this;
358  }
359 
360  [[deprecated]]
361  int
363  {
364  return m_selectors.getMinSuffixComponents();
365  }
366 
367  [[deprecated]]
368  Interest&
369  setMinSuffixComponents(int minSuffixComponents)
370  {
371  m_selectors.setMinSuffixComponents(minSuffixComponents);
372  m_wire.reset();
373  return *this;
374  }
375 
376  [[deprecated]]
377  int
379  {
380  return m_selectors.getMaxSuffixComponents();
381  }
382 
383  [[deprecated]]
384  Interest&
385  setMaxSuffixComponents(int maxSuffixComponents)
386  {
387  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
388  m_wire.reset();
389  return *this;
390  }
391 
392  [[deprecated]]
393  const KeyLocator&
395  {
396  return m_selectors.getPublisherPublicKeyLocator();
397  }
398 
399  [[deprecated]]
400  Interest&
402  {
403  m_selectors.setPublisherPublicKeyLocator(keyLocator);
404  m_wire.reset();
405  return *this;
406  }
407 
408  [[deprecated]]
409  const Exclude&
410  getExclude() const
411  {
412  return m_selectors.getExclude();
413  }
414 
415  [[deprecated]]
416  Interest&
417  setExclude(const Exclude& exclude)
418  {
419  m_selectors.setExclude(exclude);
420  m_wire.reset();
421  return *this;
422  }
423 
424  [[deprecated]]
425  int
427  {
428  return m_selectors.getChildSelector();
429  }
430 
431  [[deprecated]]
432  Interest&
433  setChildSelector(int childSelector)
434  {
435  m_selectors.setChildSelector(childSelector);
436  m_wire.reset();
437  return *this;
438  }
439 
440 private:
443  template<encoding::Tag TAG>
444  size_t
445  encode02(EncodingImpl<TAG>& encoder) const;
446 
449  template<encoding::Tag TAG>
450  size_t
451  encode03(EncodingImpl<TAG>& encoder) const;
452 
458  bool
459  decode02();
460 
464  void
465  decode03();
466 
467 #ifdef NDN_CXX_HAVE_TESTS
468 public:
471  static bool s_errorIfCanBePrefixUnset;
472 #endif // NDN_CXX_HAVE_TESTS
473 
474 private:
475  static boost::logic::tribool s_defaultCanBePrefix;
476 
477  Name m_name;
478  Selectors m_selectors; // NDN Packet Format v0.2 only
479  mutable bool m_isCanBePrefixSet;
480  mutable optional<uint32_t> m_nonce;
481  time::milliseconds m_interestLifetime;
482  DelegationList m_forwardingHint;
483  Block m_parameters; // NDN Packet Format v0.3 only
484 
485  mutable Block m_wire;
486 
487  friend bool operator==(const Interest& lhs, const Interest& rhs);
488 };
489 
491 
492 std::ostream&
493 operator<<(std::ostream& os, const Interest& interest);
494 
495 bool
496 operator==(const Interest& lhs, const Interest& rhs);
497 
498 inline bool
499 operator!=(const Interest& lhs, const Interest& rhs)
500 {
501  return !(lhs == rhs);
502 }
503 
504 } // namespace ndn
505 
506 #endif // NDN_INTEREST_HPP
int getMinSuffixComponents() const
Definition: interest.hpp:362
int getMaxSuffixComponents() const
Definition: interest.hpp:378
const Name & getName() const
Definition: interest.hpp:133
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
Definition: interest.cpp:426
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:242
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:214
const Block & getParameters() const
Definition: interest.hpp:296
void refreshNonce()
Change nonce value.
Definition: interest.cpp:564
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:337
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:46
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: interest.hpp:89
bool hasSelectors() const
Check if Interest has any selector present.
Definition: interest.hpp:339
const Block & wireEncode() const
Encode to a Block.
Definition: interest.cpp:202
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:43
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:276
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:416
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
int getChildSelector() const
Definition: interest.hpp:426
uint32_t getNonce() const
Get nonce value.
Definition: interest.cpp:547
bool hasParameters() const
Definition: interest.hpp:290
bool getCanBePrefix() const
Check whether the CanBePrefix element is present.
Definition: interest.hpp:173
const Selectors & getSelectors() const
Definition: interest.hpp:346
Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:417
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:433
Interest & setName(const Name &name)
Definition: interest.hpp:139
Interest & setNonce(uint32_t nonce)
Set nonce value.
Definition: interest.cpp:556
Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:401
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
const Exclude & getExclude() const
Definition: interest.hpp:410
Abstraction implementing Interest selectors.
Definition: selectors.hpp:36
Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:369
friend bool operator==(const Interest &lhs, const Interest &rhs)
Definition: interest.cpp:636
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:385
static void setDefaultCanBePrefix(bool canBePrefix)
Declare the default CanBePrefix setting of the application.
Definition: interest.hpp:161
Represents an absolute name.
Definition: name.hpp:42
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:453
Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:353
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: interest.cpp:218
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
Definition: interest.cpp:537
const DelegationList & getForwardingHint() const
Definition: interest.hpp:222
represents a list of Delegations
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:589
Interest & setInterestLifetime(time::milliseconds lifetime)
Set Interest&#39;s lifetime.
Definition: interest.cpp:578
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:394
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:39
time::milliseconds getInterestLifetime() const
Definition: interest.hpp:278
Interest & unsetParameters()
Remove the Parameters element from this Interest.
Definition: interest.cpp:626
Represents a Data packet.
Definition: data.hpp:35
bool getMustBeFresh() const
Check whether the MustBeFresh element is present.
Definition: interest.hpp:201
represents an error in TLV encoding or decoding
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:43
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:186
Interest & setParameters(const Block &parameters)
Set parameters from a Block.
Definition: interest.cpp:597
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
bool hasNonce() const
Check if the Nonce element is present.
Definition: interest.hpp:252