interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_INTEREST_HPP
23 #define NDN_INTEREST_HPP
24 
25 #include "name.hpp"
26 #include "selectors.hpp"
27 #include "util/time.hpp"
28 #include "tag-host.hpp"
29 #include "link.hpp"
30 
31 namespace ndn {
32 
33 class Data;
34 
38 const time::milliseconds DEFAULT_INTEREST_LIFETIME = time::milliseconds(4000);
39 
42 class Interest : public TagHost, 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 
59  Interest();
60 
67  Interest(const Name& name);
68 
75  Interest(const Name& name, const time::milliseconds& interestLifetime);
76 
81  explicit
82  Interest(const Block& wire);
83 
87  template<encoding::Tag TAG>
88  size_t
89  wireEncode(EncodingImpl<TAG>& encoder) const;
90 
94  const Block&
95  wireEncode() const;
96 
100  void
101  wireDecode(const Block& wire);
102 
106  bool
107  hasWire() const
108  {
109  return m_wire.hasWire();
110  }
111 
118  std::string
119  toUri() const;
120 
121 public: // Link and forwarding hint
126  bool
127  hasLink() const;
128 
135  const Link&
136  getLink() const;
137 
143  void
144  setLink(const Block& link);
145 
150  void
151  unsetLink();
152 
157  bool
158  hasSelectedDelegation() const;
159 
165  Name
166  getSelectedDelegation() const;
167 
174  void
175  setSelectedDelegation(const Name& delegationName);
176 
183  void
184  setSelectedDelegation(size_t delegationIndex);
185 
189  void
191 
192 public: // matching
197  bool
198  matchesName(const Name& name) const;
199 
209  bool
210  matchesData(const Data& data) const;
211 
221  bool
222  matchesInterest(const Interest& other) const;
223 
224 public: // Name and guiders
225  const Name&
226  getName() const
227  {
228  return m_name;
229  }
230 
231  Interest&
232  setName(const Name& name)
233  {
234  m_name = name;
235  m_wire.reset();
236  return *this;
237  }
238 
239  const time::milliseconds&
241  {
242  return m_interestLifetime;
243  }
244 
245  Interest&
246  setInterestLifetime(const time::milliseconds& interestLifetime)
247  {
248  m_interestLifetime = interestLifetime;
249  m_wire.reset();
250  return *this;
251  }
252 
255  bool
256  hasNonce() const
257  {
258  return m_nonce.hasWire();
259  }
260 
265  uint32_t
266  getNonce() const;
267 
273  Interest&
274  setNonce(uint32_t nonce);
275 
283  void
284  refreshNonce();
285 
286 public: // Selectors
290  bool
291  hasSelectors() const
292  {
293  return !m_selectors.empty();
294  }
295 
296  const Selectors&
297  getSelectors() const
298  {
299  return m_selectors;
300  }
301 
302  Interest&
303  setSelectors(const Selectors& selectors)
304  {
305  m_selectors = selectors;
306  m_wire.reset();
307  return *this;
308  }
309 
310  int
312  {
313  return m_selectors.getMinSuffixComponents();
314  }
315 
316  Interest&
317  setMinSuffixComponents(int minSuffixComponents)
318  {
319  m_selectors.setMinSuffixComponents(minSuffixComponents);
320  m_wire.reset();
321  return *this;
322  }
323 
324  int
326  {
327  return m_selectors.getMaxSuffixComponents();
328  }
329 
330  Interest&
331  setMaxSuffixComponents(int maxSuffixComponents)
332  {
333  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
334  m_wire.reset();
335  return *this;
336  }
337 
338  const KeyLocator&
340  {
341  return m_selectors.getPublisherPublicKeyLocator();
342  }
343 
344  Interest&
346  {
347  m_selectors.setPublisherPublicKeyLocator(keyLocator);
348  m_wire.reset();
349  return *this;
350  }
351 
352  const Exclude&
353  getExclude() const
354  {
355  return m_selectors.getExclude();
356  }
357 
358  Interest&
359  setExclude(const Exclude& exclude)
360  {
361  m_selectors.setExclude(exclude);
362  m_wire.reset();
363  return *this;
364  }
365 
366  int
368  {
369  return m_selectors.getChildSelector();
370  }
371 
372  Interest&
373  setChildSelector(int childSelector)
374  {
375  m_selectors.setChildSelector(childSelector);
376  m_wire.reset();
377  return *this;
378  }
379 
380  int
382  {
383  return m_selectors.getMustBeFresh();
384  }
385 
386  Interest&
387  setMustBeFresh(bool mustBeFresh)
388  {
389  m_selectors.setMustBeFresh(mustBeFresh);
390  m_wire.reset();
391  return *this;
392  }
393 
394 public: // EqualityComparable concept
395  bool
396  operator==(const Interest& other) const
397  {
398  return wireEncode() == other.wireEncode();
399  }
400 
401  bool
402  operator!=(const Interest& other) const
403  {
404  return !(*this == other);
405  }
406 
407 private:
408  Name m_name;
409  Selectors m_selectors;
410  mutable Block m_nonce;
411  time::milliseconds m_interestLifetime;
412 
413  mutable Block m_link;
414  mutable shared_ptr<Link> m_linkCached;
415  size_t m_selectedDelegationIndex;
416  mutable Block m_wire;
417 };
418 
419 std::ostream&
420 operator<<(std::ostream& os, const Interest& interest);
421 
422 inline std::string
424 {
425  std::ostringstream os;
426  os << *this;
427  return os.str();
428 }
429 
430 } // namespace ndn
431 
432 #endif // NDN_INTEREST_HPP
int getMinSuffixComponents() const
Definition: interest.hpp:311
bool hasSelectedDelegation() const
Check whether the Interest includes a selected delegation.
Definition: interest.cpp:411
int getMaxSuffixComponents() const
Definition: interest.hpp:325
const Name & getName() const
Definition: interest.hpp:226
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
bool operator==(const Interest &other) const
Definition: interest.hpp:396
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
Definition: interest.cpp:105
Interest & setMustBeFresh(bool mustBeFresh)
Definition: interest.hpp:387
Selectors & setMustBeFresh(bool mustBeFresh)
Definition: selectors.cpp:223
void setSelectedDelegation(const Name &delegationName)
Set the selected delegation.
Definition: interest.cpp:426
Base class to store tag information (e.g., inside Interest and Data packets)
Definition: tag-host.hpp:34
void refreshNonce()
Refresh nonce.
Definition: interest.cpp:91
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:320
bool hasWire() const
Check if already has wire.
Definition: interest.hpp:107
bool hasSelectors() const
Definition: interest.hpp:291
const Block & wireEncode() const
Encode to a wire format.
Definition: interest.cpp:285
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:42
std::string toUri() const
Encode the name according to the NDN URI Scheme.
Definition: interest.hpp:423
const time::milliseconds & getInterestLifetime() const
Definition: interest.hpp:240
int getChildSelector() const
Definition: interest.hpp:367
Name getSelectedDelegation() const
Get the name of the selected delegation.
Definition: interest.cpp:417
uint32_t getNonce() const
Get Interest's nonce.
Definition: interest.cpp:62
const Link & getLink() const
Get the link object for this interest.
Definition: interest.cpp:378
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.cpp:191
void unsetSelectedDelegation()
Unset the selected delegation.
Definition: interest.cpp:449
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.cpp:207
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:97
Selectors & setChildSelector(int childSelector)
Definition: selectors.cpp:215
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.cpp:183
Interest()
Create a new Interest with an empty name (ndn:/)
Definition: interest.cpp:36
const Exclude & getExclude() const
Definition: selectors.hpp:106
void setLink(const Block &link)
Set the link object for this interest.
Definition: interest.cpp:390
const Selectors & getSelectors() const
Definition: interest.hpp:297
int getMustBeFresh() const
Definition: interest.hpp:381
int getChildSelector() const
Definition: selectors.hpp:115
Error(const std::string &what)
Definition: interest.hpp:49
Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:359
bool empty() const
Definition: selectors.cpp:49
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:373
Interest & setName(const Name &name)
Definition: interest.hpp:232
int getMustBeFresh() const
Definition: selectors.hpp:124
Interest & setNonce(uint32_t nonce)
Set Interest's nonce.
Definition: interest.cpp:76
Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:345
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: interest.cpp:225
const Exclude & getExclude() const
Definition: interest.hpp:353
Abstraction implementing Interest selectors.
Definition: selectors.hpp:34
Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:317
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:331
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:132
void unsetLink()
Delete the link object for this interest.
Definition: interest.cpp:402
Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:303
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: interest.cpp:303
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
Definition: interest.cpp:216
bool hasLink() const
Check whether the Interest contains a Link object.
Definition: interest.cpp:372
bool operator!=(const Interest &other) const
Definition: interest.hpp:402
int getMinSuffixComponents() const
Definition: selectors.hpp:79
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:339
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:38
represents a Data packet
Definition: data.hpp:37
int getMaxSuffixComponents() const
Definition: selectors.hpp:88
Interest & setInterestLifetime(const time::milliseconds &interestLifetime)
Definition: interest.hpp:246
represents an error in TLV encoding or decoding
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:38
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.cpp:199
bool hasNonce() const
Check if Nonce set.
Definition: interest.hpp:256