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 
25 #include "delegation-list.hpp"
26 #include "name.hpp"
27 #include "packet-base.hpp"
28 #include "selectors.hpp"
29 #include "util/time.hpp"
30 
31 namespace ndn {
32 
33 class Data;
34 
38 const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
39 
42 class Interest : public PacketBase, public enable_shared_from_this<Interest>
43 {
44 public:
45  class Error : public tlv::Error
46  {
47  public:
48  explicit
49  Error(const std::string& what)
50  : tlv::Error(what)
51  {
52  }
53  };
54 
60  explicit
61  Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
62 
67  explicit
68  Interest(const Block& wire);
69 
72  template<encoding::Tag TAG>
73  size_t
74  wireEncode(EncodingImpl<TAG>& encoder) const;
75 
82  const Block&
83  wireEncode() const;
84 
87  void
88  wireDecode(const Block& wire);
89 
92  bool
93  hasWire() const
94  {
95  return m_wire.hasWire();
96  }
97 
104  std::string
105  toUri() const;
106 
107 public: // matching
112  bool
113  matchesName(const Name& name) const;
114 
121  bool
122  matchesData(const Data& data) const;
123 
132  bool
133  matchesInterest(const Interest& other) const;
134 
135 public: // element access
136  const Name&
137  getName() const
138  {
139  return m_name;
140  }
141 
142  Interest&
143  setName(const Name& name)
144  {
145  m_name = name;
146  m_wire.reset();
147  return *this;
148  }
149 
156  bool
158  {
159  return m_selectors.getMaxSuffixComponents() != 1;
160  }
161 
169  Interest&
170  setCanBePrefix(bool canBePrefix)
171  {
172  m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1);
173  m_wire.reset();
174  return *this;
175  }
176 
183  bool
185  {
186  return m_selectors.getMustBeFresh();
187  }
188 
196  Interest&
197  setMustBeFresh(bool mustBeFresh)
198  {
199  m_selectors.setMustBeFresh(mustBeFresh);
200  m_wire.reset();
201  return *this;
202  }
203 
204  const DelegationList&
206  {
207  return m_forwardingHint;
208  }
209 
210  Interest&
211  setForwardingHint(const DelegationList& value);
212 
223  template<typename Modifier>
224  Interest&
225  modifyForwardingHint(const Modifier& modifier)
226  {
227  modifier(m_forwardingHint);
228  m_wire.reset();
229  return *this;
230  }
231 
234  bool
235  hasNonce() const
236  {
237  return static_cast<bool>(m_nonce);
238  }
239 
244  uint32_t
245  getNonce() const;
246 
249  Interest&
250  setNonce(uint32_t nonce);
251 
257  void
258  refreshNonce();
259 
260  time::milliseconds
262  {
263  return m_interestLifetime;
264  }
265 
269  Interest&
270  setInterestLifetime(time::milliseconds lifetime);
271 
272 public: // Selectors (deprecated)
276  bool
277  hasSelectors() const
278  {
279  return !m_selectors.empty();
280  }
281 
283  const Selectors&
284  getSelectors() const
285  {
286  return m_selectors;
287  }
288 
290  Interest&
291  setSelectors(const Selectors& selectors)
292  {
293  m_selectors = selectors;
294  m_wire.reset();
295  return *this;
296  }
297 
299  int
301  {
302  return m_selectors.getMinSuffixComponents();
303  }
304 
306  Interest&
307  setMinSuffixComponents(int minSuffixComponents)
308  {
309  m_selectors.setMinSuffixComponents(minSuffixComponents);
310  m_wire.reset();
311  return *this;
312  }
313 
315  int
317  {
318  return m_selectors.getMaxSuffixComponents();
319  }
320 
322  Interest&
323  setMaxSuffixComponents(int maxSuffixComponents)
324  {
325  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
326  m_wire.reset();
327  return *this;
328  }
329 
331  const KeyLocator&
333  {
334  return m_selectors.getPublisherPublicKeyLocator();
335  }
336 
338  Interest&
340  {
341  m_selectors.setPublisherPublicKeyLocator(keyLocator);
342  m_wire.reset();
343  return *this;
344  }
345 
347  const Exclude&
348  getExclude() const
349  {
350  return m_selectors.getExclude();
351  }
352 
354  Interest&
355  setExclude(const Exclude& exclude)
356  {
357  m_selectors.setExclude(exclude);
358  m_wire.reset();
359  return *this;
360  }
361 
363  int
365  {
366  return m_selectors.getChildSelector();
367  }
368 
370  Interest&
371  setChildSelector(int childSelector)
372  {
373  m_selectors.setChildSelector(childSelector);
374  m_wire.reset();
375  return *this;
376  }
377 
378 private:
384  bool
385  decode02();
386 
390  void
391  decode03();
392 
393 private:
394  Name m_name;
395  Selectors m_selectors;
396  mutable optional<uint32_t> m_nonce;
397  time::milliseconds m_interestLifetime;
398  DelegationList m_forwardingHint;
399 
400  mutable Block m_wire;
401 };
402 
404 
405 std::ostream&
406 operator<<(std::ostream& os, const Interest& interest);
407 
408 inline bool
409 operator==(const Interest& lhs, const Interest& rhs)
410 {
411  return lhs.wireEncode() == rhs.wireEncode();
412 }
413 
414 inline bool
415 operator!=(const Interest& lhs, const Interest& rhs)
416 {
417  return !(lhs == rhs);
418 }
419 
420 } // namespace ndn
421 
422 #endif // NDN_INTEREST_HPP
NDN_CXX_DEPRECATED Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:355
int getMinSuffixComponents() const
Definition: selectors.hpp:81
const Block & wireEncode() const
Encode to a Block.
Definition: interest.cpp:103
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:252
Copyright (c) 2011-2015 Regents of the University of California.
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:225
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:197
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:314
void refreshNonce()
Change nonce value.
Definition: interest.cpp:462
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:38
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
Definition: interest.cpp:435
NDN_CXX_DEPRECATED Interest & setChildSelector(int childSelector)
Definition: interest.hpp:371
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
Definition: interest.cpp:324
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:42
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:280
NDN_CXX_DEPRECATED Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:291
bool getMustBeFresh() const
Definition: selectors.hpp:130
int getChildSelector() const
Definition: selectors.hpp:117
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.cpp:190
bool hasNonce() const
Check if the Nonce element is present.
Definition: interest.hpp:235
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.cpp:206
Selectors & setChildSelector(int childSelector)
set ChildSelector
Definition: selectors.cpp:214
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.cpp:182
NDN_CXX_DEPRECATED Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:307
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:99
NDN_CXX_DEPRECATED int getMinSuffixComponents() const
Definition: interest.hpp:300
Error(const std::string &what)
Definition: interest.hpp:49
Interest & setName(const Name &name)
Definition: interest.hpp:143
uint32_t getNonce() const
Get nonce value.
Definition: interest.cpp:445
NDN_CXX_DEPRECATED const Selectors & getSelectors() const
Definition: interest.hpp:284
NDN_CXX_DEPRECATED Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:339
Interest & setNonce(uint32_t nonce)
Set nonce value.
Definition: interest.cpp:454
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
Abstraction implementing Interest selectors.
Definition: selectors.hpp:36
NDN_CXX_DEPRECATED const Exclude & getExclude() const
Definition: interest.hpp:348
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:351
void reset()
Reset wire buffer of the element.
Definition: block.cpp:258
Represents an absolute name.
Definition: name.hpp:42
bool getCanBePrefix() const
Check whether the CanBePrefix element is present.
Definition: interest.hpp:157
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: interest.hpp:93
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
NDN_CXX_DEPRECATED Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:323
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: interest.cpp:119
int getMaxSuffixComponents() const
Definition: selectors.hpp:90
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
Definition: interest.cpp:56
const Exclude & getExclude() const
Definition: selectors.hpp:108
NDN_CXX_DEPRECATED int getChildSelector() const
Definition: interest.hpp:364
time::milliseconds getInterestLifetime() const
Definition: interest.hpp:261
NDN_CXX_DEPRECATED bool hasSelectors() const
Check if Interest has any selector present.
Definition: interest.hpp:277
const DelegationList & getForwardingHint() const
Definition: interest.hpp:205
represents a list of Delegations
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:328
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:487
bool getMustBeFresh() const
Check whether the MustBeFresh element is present.
Definition: interest.hpp:184
Interest & setInterestLifetime(time::milliseconds lifetime)
Set Interest&#39;s lifetime.
Definition: interest.cpp:476
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:38
#define NDN_CXX_DEPRECATED
Mark a type, variable, or function as deprecated.
Definition: backports.hpp:75
NDN_CXX_DEPRECATED const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:332
Represents a Data packet.
Definition: data.hpp:35
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:42
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.cpp:198
const Name & getName() const
Definition: interest.hpp:137
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:170
NDN_CXX_DEPRECATED int getMaxSuffixComponents() const
Definition: interest.hpp:316