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 
34 namespace ndn {
35 
36 class LpPacket;
37 class Data;
38 
42 class Interest {
43 public:
49  Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
50  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
51  {
52  construct();
53  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
54  }
55 
60  Interest(const Name& name)
61  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
62  {
63  construct();
64  }
65 
66  Interest(const Interest& interest)
67  : name_(interest.name_), minSuffixComponents_(interest.minSuffixComponents_),
68  maxSuffixComponents_(interest.maxSuffixComponents_),
69  keyLocator_(interest.keyLocator_), exclude_(interest.exclude_),
70  childSelector_(interest.childSelector_),
71  mustBeFresh_(interest.mustBeFresh_),
72  interestLifetimeMilliseconds_(interest.interestLifetimeMilliseconds_),
73  nonce_(interest.nonce_), getNonceChangeCount_(0),
74  linkWireEncoding_(interest.linkWireEncoding_),
75  linkWireEncodingFormat_(interest.linkWireEncodingFormat_),
76  selectedDelegationIndex_(interest.selectedDelegationIndex_),
77  changeCount_(0)
78  {
79  if (interest.link_.get())
80  link_.set(ptr_lib::make_shared<Link>(*interest.link_.get()));
81 
82  setDefaultWireEncoding
83  (interest.getDefaultWireEncoding(), interest.defaultWireEncodingFormat_);
84  }
85 
90  : getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
91  {
92  construct();
93  }
94 
95  Interest& operator=(const Interest& interest);
96 
106  SignedBlob
108 
117  void
118  wireDecode
119  (const Blob& input,
121 
131  void
132  wireDecode
133  (const uint8_t *input, size_t inputLength,
135 
144  void
145  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
146  {
147  wireDecode(&input[0], input.size(), wireFormat);
148  }
149 
158  std::string
159  toUri() const;
160 
171  void
172  get(InterestLite& interestLite, WireFormat& wireFormat) const;
173 
180  void
181  set(const InterestLite& interestLite, WireFormat& wireFormat);
182 
183  Name&
184  getName() { return name_.get(); }
185 
186  const Name&
187  getName() const { return name_.get(); }
188 
189  int
190  getMinSuffixComponents() const { return minSuffixComponents_; }
191 
192  int
193  getMaxSuffixComponents() const { return maxSuffixComponents_; }
194 
195  const KeyLocator&
196  getKeyLocator() const { return keyLocator_.get(); }
197 
198  KeyLocator&
199  getKeyLocator() { return keyLocator_.get(); }
200 
201  Exclude&
202  getExclude() { return exclude_.get(); }
203 
204  const Exclude&
205  getExclude() const { return exclude_.get(); }
206 
207  int
208  getChildSelector() const { return childSelector_; }
209 
214  bool
215  getMustBeFresh() const { return mustBeFresh_; }
216 
218  getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
219 
225  const Blob&
226  getNonce() const
227  {
228  if (getNonceChangeCount_ != getChangeCount()) {
229  // The values have changed, so the existing nonce is invalidated.
230  // This method can be called on a const object, but we want to be able to update the default cached value.
231  const_cast<Interest*>(this)->nonce_ = Blob();
232  const_cast<Interest*>(this)->getNonceChangeCount_ = getChangeCount();
233  }
234 
235  return nonce_;
236  }
237 
243  bool
244  hasLink() const
245  {
246  return link_.get() || !linkWireEncoding_.isNull();
247  }
248 
249 
256  Link*
257  getLink();
258 
259  const Link*
260  getLink() const { return const_cast<Interest*>(this)->getLink(); }
261 
271  Blob
273  (WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
274 
279  int
280  getSelectedDelegationIndex() const { return selectedDelegationIndex_; }
281 
286  uint64_t
287  getIncomingFaceId() const;
288 
295  Interest&
296  setName(const Name& name)
297  {
298  name_.set(name);
299  ++changeCount_;
300  return *this;
301  }
302 
309  Interest&
310  setMinSuffixComponents(int minSuffixComponents)
311  {
312  minSuffixComponents_ = minSuffixComponents;
313  ++changeCount_;
314  return *this;
315  }
316 
323  Interest&
324  setMaxSuffixComponents(int maxSuffixComponents)
325  {
326  maxSuffixComponents_ = maxSuffixComponents;
327  ++changeCount_;
328  return *this;
329  }
330 
336  Interest&
337  setChildSelector(int childSelector)
338  {
339  childSelector_ = childSelector;
340  ++changeCount_;
341  return *this;
342  }
343 
350  Interest&
351  setMustBeFresh(bool mustBeFresh)
352  {
353  mustBeFresh_ = mustBeFresh;
354  ++changeCount_;
355  return *this;
356  }
357 
364  Interest&
365  setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
366  {
367  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
368  ++changeCount_;
369  return *this;
370  }
371 
375  Interest&
376  DEPRECATED_IN_NDN_CPP setNonce(const Blob& nonce)
377  {
378  nonce_ = nonce;
379  // Set getNonceChangeCount_ so that the next call to getNonce() won't clear nonce_.
380  ++changeCount_;
381  getNonceChangeCount_ = getChangeCount();
382  return *this;
383  }
384 
393  Interest&
394  setKeyLocator(const KeyLocator& keyLocator)
395  {
396  keyLocator_ = keyLocator;
397  ++changeCount_;
398  return *this;
399  }
400 
409  Interest&
410  setExclude(const Exclude& exclude)
411  {
412  exclude_ = exclude;
413  ++changeCount_;
414  return *this;
415  }
416 
428  Interest&
430  (Blob encoding,
432  {
433  linkWireEncoding_ = encoding;
434  linkWireEncodingFormat_ = &wireFormat;
435 
436  // Clear the link object, assuming that it has a different encoding.
437  link_.set(ptr_lib::shared_ptr<Link>());
438 
439  ++changeCount_;
440  return *this;
441  }
442 
447  Interest&
449  {
450  WireFormat* wireFormat = 0;
451  return setLinkWireEncoding(Blob(), *wireFormat);
452  }
453 
460  Interest&
461  setSelectedDelegationIndex(int selectedDelegationIndex)
462  {
463  selectedDelegationIndex_ = selectedDelegationIndex;
464  ++changeCount_;
465  return *this;
466  }
467 
475  Interest&
476  setLpPacket(const ptr_lib::shared_ptr<LpPacket>& lpPacket)
477  {
478  lpPacket_ = lpPacket;
479  // Don't update changeCount_ since this doesn't affect the wire encoding.
480  return *this;
481  }
482 
488  void
489  refreshNonce();
490 
497  bool
498  matchesName(const Name& name) const;
499 
511  bool
513  (const Data& data,
514  WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
515 
520  const SignedBlob&
522  {
523  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
524  // The values have changed, so the default wire encoding is invalidated.
525  // This method can be called on a const object, but we want to be able to update the default cached value.
526  const_cast<Interest*>(this)->defaultWireEncoding_ = SignedBlob();
527  const_cast<Interest*>(this)->defaultWireEncodingFormat_ = 0;
528  const_cast<Interest*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
529  }
530 
531  return defaultWireEncoding_;
532  }
533 
539  WireFormat*
540  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
541 
546  uint64_t
548  {
549  // Make sure each of the checkChanged is called.
550  bool changed = name_.checkChanged();
551  changed = keyLocator_.checkChanged() || changed;
552  changed = exclude_.checkChanged() || changed;
553  changed = link_.checkChanged() || changed;
554  if (changed)
555  // A child object has changed, so update the change count.
556  // This method can be called on a const object, but we want to be able to update the changeCount_.
557  ++const_cast<Interest*>(this)->changeCount_;
558 
559  return changeCount_;
560  }
561 
562 private:
563  void
564  construct()
565  {
566  minSuffixComponents_ = -1;
567  maxSuffixComponents_ = -1;
568  childSelector_ = -1;
569  mustBeFresh_ = true;
570  interestLifetimeMilliseconds_ = -1.0;
571  linkWireEncodingFormat_ = 0;
572  selectedDelegationIndex_ = -1;
573  }
574 
575  void
576  setDefaultWireEncoding
577  (const SignedBlob& defaultWireEncoding,
578  WireFormat *defaultWireEncodingFormat)
579  {
580  defaultWireEncoding_ = defaultWireEncoding;
581  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
582  // Set getDefaultWireEncodingChangeCount_ so that the next call to
583  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
584  getDefaultWireEncodingChangeCount_ = getChangeCount();
585  }
586 
587  ChangeCounter<Name> name_;
588  int minSuffixComponents_;
589  int maxSuffixComponents_;
590  ChangeCounter<KeyLocator> keyLocator_;
591  ChangeCounter<Exclude> exclude_;
592  int childSelector_;
593  bool mustBeFresh_;
594  Milliseconds interestLifetimeMilliseconds_;
595  Blob nonce_;
596  uint64_t getNonceChangeCount_;
597  Blob linkWireEncoding_;
598  WireFormat* linkWireEncodingFormat_;
599  SharedPointerChangeCounter<Link> link_;
600  int selectedDelegationIndex_;
601  SignedBlob defaultWireEncoding_;
602  WireFormat *defaultWireEncodingFormat_;
603  uint64_t getDefaultWireEncodingChangeCount_;
604  ptr_lib::shared_ptr<LpPacket> lpPacket_;
605  uint64_t changeCount_;
606 };
607 
608 }
609 
610 #endif
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: interest.hpp:521
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:112
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
Interest & setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
Set the interest lifetime.
Definition: interest.hpp:365
Interest & setKeyLocator(const KeyLocator &keyLocator)
Set this interest to use a copy of the given KeyLocator object.
Definition: interest.hpp:394
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:90
Blob getLinkWireEncoding(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Get the wire encoding of the link object.
Definition: interest.cpp:317
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: interest.hpp:547
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:351
Interest & setMinSuffixComponents(int minSuffixComponents)
Set the min suffix components count.
Definition: interest.hpp:310
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:60
uint64_t getIncomingFaceId() const
Get the incoming face ID according to the incoming packet header.
Definition: interest.cpp:59
Interest & setLinkWireEncoding(Blob encoding, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Set the link wire encoding bytes, without decoding them.
Definition: interest.hpp:430
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:49
Interest & unsetLink()
Clear the link wire encoding and link object so that getLink() returns null.
Definition: interest.hpp:448
bool 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:244
int getSelectedDelegationIndex() const
Get the selected delegation index.
Definition: interest.hpp:280
const Blob & getNonce() const
Return the nonce value from the incoming interest.
Definition: interest.hpp:226
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:136
Interest & setChildSelector(int childSelector)
Set the child selector.
Definition: interest.hpp:337
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:42
std::string toUri() const
Encode the name according to the "NDN URI Scheme".
Definition: interest.cpp:171
Interest & setExclude(const Exclude &exclude)
Set this interest to use a copy of the given Exclude object.
Definition: interest.hpp:410
Interest()
Create a new Interest with an empty name and "none" for all values.
Definition: interest.hpp:89
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:145
Interest & setName(const Name &name)
Set the interest name.
Definition: interest.hpp:296
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Set the max suffix components count.
Definition: interest.hpp:324
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 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:205
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:376
Link * getLink()
Get the link object.
Definition: interest.cpp:296
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: interest.hpp:540
Definition: key-locator.hpp:35
bool getMustBeFresh() const
Return true if the content must be fresh.
Definition: interest.hpp:215
void refreshNonce()
Update the bytes of the nonce with new random values.
Definition: interest.cpp:330
Interest & setLpPacket(const ptr_lib::shared_ptr< LpPacket > &lpPacket)
An internal library method to set the LpPacket for an incoming packet.
Definition: interest.hpp:476
Interest & setSelectedDelegationIndex(int selectedDelegationIndex)
Set the selected delegation index.
Definition: interest.hpp:461
SignedBlob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Interest for a particular wire format.
Definition: interest.cpp:115
bool matchesData(const Data &data, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Check if the given Data packet can satisfy this Interest.
Definition: interest.cpp:226