All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
interest.hpp
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 "link.hpp"
27 #include "key-locator.hpp"
28 #include "lite/interest-lite.hpp"
29 #include "encoding/wire-format.hpp"
30 #include "util/signed-blob.hpp"
31 #include "util/change-counter.hpp"
32 #include "exclude.hpp"
33 #include "delegation-set.hpp"
34 
35 namespace ndn {
36 
37 class LpPacket;
38 class Data;
39 
43 class Interest {
44 public:
50  Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
51  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
52  {
53  construct();
54  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
55  }
56 
61  Interest(const Name& name)
62  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
63  {
64  construct();
65  }
66 
67  Interest(const Interest& interest)
68  : name_(interest.name_), minSuffixComponents_(interest.minSuffixComponents_),
69  maxSuffixComponents_(interest.maxSuffixComponents_),
70  keyLocator_(interest.keyLocator_), exclude_(interest.exclude_),
71  childSelector_(interest.childSelector_),
72  mustBeFresh_(interest.mustBeFresh_),
73  interestLifetimeMilliseconds_(interest.interestLifetimeMilliseconds_),
74  nonce_(interest.nonce_), getNonceChangeCount_(0),
75  forwardingHint_(interest.forwardingHint_),
76  linkWireEncoding_(interest.linkWireEncoding_),
77  linkWireEncodingFormat_(interest.linkWireEncodingFormat_),
78  selectedDelegationIndex_(interest.selectedDelegationIndex_),
79  changeCount_(0)
80  {
81  if (interest.link_.get())
82  link_.set(ptr_lib::make_shared<Link>(*interest.link_.get()));
83 
84  setDefaultWireEncoding
85  (interest.getDefaultWireEncoding(), interest.defaultWireEncodingFormat_);
86  }
87 
92  : getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
93  {
94  construct();
95  }
96 
97  Interest& operator=(const Interest& interest);
98 
108  SignedBlob
110 
119  void
120  wireDecode
121  (const Blob& input,
123 
133  void
134  wireDecode
135  (const uint8_t *input, size_t inputLength,
137 
146  void
147  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
148  {
149  wireDecode(&input[0], input.size(), wireFormat);
150  }
151 
160  std::string
161  toUri() const;
162 
173  void
174  get(InterestLite& interestLite, WireFormat& wireFormat) const;
175 
182  void
183  set(const InterestLite& interestLite, WireFormat& wireFormat);
184 
185  Name&
186  getName() { return name_.get(); }
187 
188  const Name&
189  getName() const { return name_.get(); }
190 
191  int
192  getMinSuffixComponents() const { return minSuffixComponents_; }
193 
194  int
195  getMaxSuffixComponents() const { return maxSuffixComponents_; }
196 
201  bool
203  {
204  // Use the closest v0.2 semantics. CanBePrefix is the opposite of exact
205  // match where MaxSuffixComponents is 1 (for the implicit digest).
206  return maxSuffixComponents_ != 1;
207  }
208 
209  const KeyLocator&
210  getKeyLocator() const { return keyLocator_.get(); }
211 
212  KeyLocator&
213  getKeyLocator() { return keyLocator_.get(); }
214 
215  Exclude&
216  getExclude() { return exclude_.get(); }
217 
218  const Exclude&
219  getExclude() const { return exclude_.get(); }
220 
221  int
222  getChildSelector() const { return childSelector_; }
223 
228  bool
229  getMustBeFresh() const { return mustBeFresh_; }
230 
232  getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
233 
239  const Blob&
240  getNonce() const
241  {
242  if (getNonceChangeCount_ != getChangeCount()) {
243  // The values have changed, so the existing nonce is invalidated.
244  // This method can be called on a const object, but we want to be able to update the default cached value.
245  const_cast<Interest*>(this)->nonce_ = Blob();
246  const_cast<Interest*>(this)->getNonceChangeCount_ = getChangeCount();
247  }
248 
249  return nonce_;
250  }
251 
258  getForwardingHint() { return forwardingHint_.get(); }
259 
260  const DelegationSet&
261  getForwardingHint() const { return forwardingHint_.get(); }
262 
269  bool
270  DEPRECATED_IN_NDN_CPP hasLink() const
271  {
272  return link_.get() || !linkWireEncoding_.isNull();
273  }
274 
275 
283  Link*
284  DEPRECATED_IN_NDN_CPP getLink();
285 
286  const Link*
287  DEPRECATED_IN_NDN_CPP getLink() const { return const_cast<Interest*>(this)->getLink(); }
288 
299  Blob
300  DEPRECATED_IN_NDN_CPP getLinkWireEncoding
301  (WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
302 
308  int
309  DEPRECATED_IN_NDN_CPP getSelectedDelegationIndex() const { return selectedDelegationIndex_; }
310 
315  uint64_t
316  getIncomingFaceId() const;
317 
324  Interest&
325  setName(const Name& name)
326  {
327  name_.set(name);
328  ++changeCount_;
329  return *this;
330  }
331 
338  Interest&
339  setMinSuffixComponents(int minSuffixComponents)
340  {
341  minSuffixComponents_ = minSuffixComponents;
342  ++changeCount_;
343  return *this;
344  }
345 
352  Interest&
353  setMaxSuffixComponents(int maxSuffixComponents)
354  {
355  maxSuffixComponents_ = maxSuffixComponents;
356  ++changeCount_;
357  return *this;
358  }
359 
366  Interest&
367  setCanBePrefix(int canBePrefix)
368  {
369  // Use the closest v0.2 semantics. CanBePrefix is the opposite of exact
370  // match where MaxSuffixComponents is 1 (for the implicit digest).
371  maxSuffixComponents_ = (canBePrefix ? -1 : 1);
372  ++changeCount_;
373  return *this;
374  }
375 
381  Interest&
382  setChildSelector(int childSelector)
383  {
384  childSelector_ = childSelector;
385  ++changeCount_;
386  return *this;
387  }
388 
395  Interest&
396  setMustBeFresh(bool mustBeFresh)
397  {
398  mustBeFresh_ = mustBeFresh;
399  ++changeCount_;
400  return *this;
401  }
402 
409  Interest&
410  setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
411  {
412  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
413  ++changeCount_;
414  return *this;
415  }
416 
420  Interest&
421  DEPRECATED_IN_NDN_CPP setNonce(const Blob& nonce)
422  {
423  nonce_ = nonce;
424  // Set getNonceChangeCount_ so that the next call to getNonce() won't clear nonce_.
425  ++changeCount_;
426  getNonceChangeCount_ = getChangeCount();
427  return *this;
428  }
429 
438  Interest&
439  setKeyLocator(const KeyLocator& keyLocator)
440  {
441  keyLocator_ = keyLocator;
442  ++changeCount_;
443  return *this;
444  }
445 
454  Interest&
455  setExclude(const Exclude& exclude)
456  {
457  exclude_ = exclude;
458  ++changeCount_;
459  return *this;
460  }
461 
472  Interest&
473  setForwardingHint(const DelegationSet& forwardingHint)
474  {
475  forwardingHint_ = forwardingHint;
476  ++changeCount_;
477  return *this;
478  }
479 
492  Interest&
493  DEPRECATED_IN_NDN_CPP setLinkWireEncoding
494  (Blob encoding,
496  {
497  linkWireEncoding_ = encoding;
498  linkWireEncodingFormat_ = &wireFormat;
499 
500  // Clear the link object, assuming that it has a different encoding.
501  link_.set(ptr_lib::shared_ptr<Link>());
502 
503  ++changeCount_;
504  return *this;
505  }
506 
512  Interest&
513  DEPRECATED_IN_NDN_CPP unsetLink()
514  {
515  WireFormat* wireFormat = 0;
516  return setLinkWireEncoding(Blob(), *wireFormat);
517  }
518 
526  Interest&
527  DEPRECATED_IN_NDN_CPP setSelectedDelegationIndex(int selectedDelegationIndex)
528  {
529  selectedDelegationIndex_ = selectedDelegationIndex;
530  ++changeCount_;
531  return *this;
532  }
533 
541  Interest&
542  setLpPacket(const ptr_lib::shared_ptr<LpPacket>& lpPacket)
543  {
544  lpPacket_ = lpPacket;
545  // Don't update changeCount_ since this doesn't affect the wire encoding.
546  return *this;
547  }
548 
554  void
555  refreshNonce();
556 
563  bool
564  matchesName(const Name& name) const;
565 
577  bool
579  (const Data& data,
580  WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
581 
586  const SignedBlob&
588  {
589  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
590  // The values have changed, so the default wire encoding is invalidated.
591  // This method can be called on a const object, but we want to be able to update the default cached value.
592  const_cast<Interest*>(this)->defaultWireEncoding_ = SignedBlob();
593  const_cast<Interest*>(this)->defaultWireEncodingFormat_ = 0;
594  const_cast<Interest*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
595  }
596 
597  return defaultWireEncoding_;
598  }
599 
605  WireFormat*
606  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
607 
612  uint64_t
614  {
615  // Make sure each of the checkChanged is called.
616  bool changed = name_.checkChanged();
617  changed = keyLocator_.checkChanged() || changed;
618  changed = exclude_.checkChanged() || changed;
619  changed = forwardingHint_.checkChanged() || changed;
620  changed = link_.checkChanged() || changed;
621  if (changed)
622  // A child object has changed, so update the change count.
623  // This method can be called on a const object, but we want to be able to update the changeCount_.
624  ++const_cast<Interest*>(this)->changeCount_;
625 
626  return changeCount_;
627  }
628 
629 private:
630  void
631  construct()
632  {
633  minSuffixComponents_ = -1;
634  maxSuffixComponents_ = -1;
635  childSelector_ = -1;
636  mustBeFresh_ = true;
637  interestLifetimeMilliseconds_ = -1.0;
638  linkWireEncodingFormat_ = 0;
639  selectedDelegationIndex_ = -1;
640  }
641 
642  void
643  setDefaultWireEncoding
644  (const SignedBlob& defaultWireEncoding,
645  WireFormat *defaultWireEncodingFormat)
646  {
647  defaultWireEncoding_ = defaultWireEncoding;
648  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
649  // Set getDefaultWireEncodingChangeCount_ so that the next call to
650  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
651  getDefaultWireEncodingChangeCount_ = getChangeCount();
652  }
653 
654  ChangeCounter<Name> name_;
655  int minSuffixComponents_;
656  int maxSuffixComponents_;
657  ChangeCounter<KeyLocator> keyLocator_;
658  ChangeCounter<Exclude> exclude_;
659  int childSelector_;
660  bool mustBeFresh_;
661  Milliseconds interestLifetimeMilliseconds_;
662  Blob nonce_;
663  uint64_t getNonceChangeCount_;
664  ChangeCounter<DelegationSet> forwardingHint_;
665  Blob linkWireEncoding_;
666  WireFormat* linkWireEncodingFormat_;
667  SharedPointerChangeCounter<Link> link_;
668  int selectedDelegationIndex_;
669  SignedBlob defaultWireEncoding_;
670  WireFormat *defaultWireEncodingFormat_;
671  uint64_t getDefaultWireEncodingChangeCount_;
672  ptr_lib::shared_ptr<LpPacket> lpPacket_;
673  uint64_t changeCount_;
674 };
675 
676 }
677 
678 #endif
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: interest.hpp:587
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:114
Interest & setCanBePrefix(int canBePrefix)
Set the CanBePrefix flag.
Definition: interest.hpp:367
DelegationSet & getForwardingHint()
Get the forwarding hint object which you can modify to add or remove forwarding hints.
Definition: interest.hpp:258
Interest & setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
Set the interest lifetime.
Definition: interest.hpp:410
Interest & setKeyLocator(const KeyLocator &keyLocator)
Set this interest to use a copy of the given KeyLocator object.
Definition: interest.hpp:439
Definition: data.hpp:37
void set(const InterestLite &interestLite, WireFormat &wireFormat)
Clear this interest, and set the values by copying from interestLite.
Definition: interest.cpp:100
Blob DEPRECATED_IN_NDN_CPP getLinkWireEncoding(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Get the wire encoding of the link object.
Definition: interest.cpp:338
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: interest.hpp:613
An InterestLite holds a NameLite and other fields for an interest.
Definition: interest-lite.hpp:35
Interest & setMustBeFresh(bool mustBeFresh)
Set the MustBeFresh flag.
Definition: interest.hpp:396
Interest & setMinSuffixComponents(int minSuffixComponents)
Set the min suffix components count.
Definition: interest.hpp:339
bool isNull() const
Check if the array pointer is null.
Definition: blob.hpp:172
An Exclude holds a vector of Exclude::Entry.
Definition: exclude.hpp:33
Interest(const Name &name)
Create a new Interest with the given name and "none" for other values.
Definition: interest.hpp:61
uint64_t getIncomingFaceId() const
Get the incoming face ID according to the incoming packet header.
Definition: interest.cpp:62
Interest(const Name &name, Milliseconds interestLifetimeMilliseconds)
Create a new Interest with the given name and interest lifetime and "none" for other values...
Definition: interest.hpp:50
int DEPRECATED_IN_NDN_CPP getSelectedDelegationIndex() const
Get the selected delegation index.
Definition: interest.hpp:309
const Blob & getNonce() const
Return the nonce value from the incoming interest.
Definition: interest.hpp:240
Interest & setForwardingHint(const DelegationSet &forwardingHint)
Set this interest to use a copy of the given DelegationSet object as the forwarding hint...
Definition: interest.hpp:473
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
void wireDecode(const Blob &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Interest.
Definition: interest.cpp:157
Interest & setChildSelector(int childSelector)
Set the child selector.
Definition: interest.hpp:382
A DelegationSet holds a list of DelegationSet::Delegation entries which is used as the content of a L...
Definition: delegation-set.hpp:38
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:43
std::string toUri() const
Encode the name according to the "NDN URI Scheme".
Definition: interest.cpp:192
Interest & setExclude(const Exclude &exclude)
Set this interest to use a copy of the given Exclude object.
Definition: interest.hpp:455
Interest()
Create a new Interest with an empty name and "none" for all values.
Definition: interest.hpp:91
Interest &DEPRECATED_IN_NDN_CPP unsetLink()
Clear the link wire encoding and link object so that getLink() returns null.
Definition: interest.hpp:513
void wireDecode(const std::vector< uint8_t > &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Interest.
Definition: interest.hpp:147
bool getCanBePrefix() const
Get the CanBePrefix flag.
Definition: interest.hpp:202
Interest &DEPRECATED_IN_NDN_CPP setLinkWireEncoding(Blob encoding, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Set the link wire encoding bytes, without decoding them.
Definition: interest.hpp:494
Interest & setName(const Name &name)
Set the interest name.
Definition: interest.hpp:325
Interest &DEPRECATED_IN_NDN_CPP setSelectedDelegationIndex(int selectedDelegationIndex)
Set the selected delegation index.
Definition: interest.hpp:527
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Set the max suffix components count.
Definition: interest.hpp:353
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:34
bool DEPRECATED_IN_NDN_CPP hasLink() const
Check if this interest has a link object (or a link wire encoding which can be decoded to make the li...
Definition: interest.hpp:270
bool matchesName(const Name &name) const
Check if this Interest's name matches the given name (using Name::match) and the given name also conf...
Definition: interest.cpp:226
A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet)...
Definition: signed-blob.hpp:34
Definition: wire-format.hpp:39
Interest &DEPRECATED_IN_NDN_CPP setNonce(const Blob &nonce)
Definition: interest.hpp:421
Link *DEPRECATED_IN_NDN_CPP getLink()
Get the link object.
Definition: interest.cpp:317
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: interest.hpp:606
Definition: key-locator.hpp:35
bool getMustBeFresh() const
Return true if the content must be fresh.
Definition: interest.hpp:229
void refreshNonce()
Update the bytes of the nonce with new random values.
Definition: interest.cpp:351
Interest & setLpPacket(const ptr_lib::shared_ptr< LpPacket > &lpPacket)
An internal library method to set the LpPacket for an incoming packet.
Definition: interest.hpp:542
SignedBlob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Interest for a particular wire format.
Definition: interest.cpp:136
bool matchesData(const Data &data, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Check if the given Data packet can satisfy this Interest.
Definition: interest.cpp:247