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 
41 class Interest {
42 public:
48  Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
49  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
50  {
51  construct();
52  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
53  }
54 
59  Interest(const Name& name)
60  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
61  {
62  construct();
63  }
64 
65  Interest(const Interest& interest)
66  : name_(interest.name_), minSuffixComponents_(interest.minSuffixComponents_),
67  maxSuffixComponents_(interest.maxSuffixComponents_),
68  keyLocator_(interest.keyLocator_), exclude_(interest.exclude_),
69  childSelector_(interest.childSelector_),
70  mustBeFresh_(interest.mustBeFresh_),
71  interestLifetimeMilliseconds_(interest.interestLifetimeMilliseconds_),
72  nonce_(interest.nonce_), getNonceChangeCount_(0),
73  linkWireEncoding_(interest.linkWireEncoding_),
74  linkWireEncodingFormat_(interest.linkWireEncodingFormat_),
75  selectedDelegationIndex_(interest.selectedDelegationIndex_),
76  changeCount_(0)
77  {
78  if (interest.link_.get())
79  link_.set(ptr_lib::make_shared<Link>(*interest.link_.get()));
80 
81  setDefaultWireEncoding
82  (interest.getDefaultWireEncoding(), interest.defaultWireEncodingFormat_);
83  }
84 
89  : getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
90  {
91  construct();
92  }
93 
94  Interest& operator=(const Interest& interest);
95 
105  SignedBlob
107 
116  void
117  wireDecode
118  (const Blob& input,
120 
130  void
131  wireDecode
132  (const uint8_t *input, size_t inputLength,
134 
143  void
144  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
145  {
146  wireDecode(&input[0], input.size(), wireFormat);
147  }
148 
157  std::string
158  toUri() const;
159 
170  void
171  get(InterestLite& interestLite, WireFormat& wireFormat) const;
172 
179  void
180  set(const InterestLite& interestLite, WireFormat& wireFormat);
181 
182  Name&
183  getName() { return name_.get(); }
184 
185  const Name&
186  getName() const { return name_.get(); }
187 
188  int
189  getMinSuffixComponents() const { return minSuffixComponents_; }
190 
191  int
192  getMaxSuffixComponents() const { return maxSuffixComponents_; }
193 
194  const KeyLocator&
195  getKeyLocator() const { return keyLocator_.get(); }
196 
197  KeyLocator&
198  getKeyLocator() { return keyLocator_.get(); }
199 
200  Exclude&
201  getExclude() { return exclude_.get(); }
202 
203  const Exclude&
204  getExclude() const { return exclude_.get(); }
205 
206  int
207  getChildSelector() const { return childSelector_; }
208 
213  bool
214  getMustBeFresh() const { return mustBeFresh_; }
215 
217  getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
218 
224  const Blob&
225  getNonce() const
226  {
227  if (getNonceChangeCount_ != getChangeCount()) {
228  // The values have changed, so the existing nonce is invalidated.
229  // This method can be called on a const object, but we want to be able to update the default cached value.
230  const_cast<Interest*>(this)->nonce_ = Blob();
231  const_cast<Interest*>(this)->getNonceChangeCount_ = getChangeCount();
232  }
233 
234  return nonce_;
235  }
236 
242  bool
243  hasLink() const
244  {
245  return link_.get() || !linkWireEncoding_.isNull();
246  }
247 
248 
255  Link*
256  getLink();
257 
258  const Link*
259  getLink() const { return const_cast<Interest*>(this)->getLink(); }
260 
270  Blob
272  (WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
273 
278  int
279  getSelectedDelegationIndex() const { return selectedDelegationIndex_; }
280 
285  uint64_t
286  getIncomingFaceId() const;
287 
294  Interest&
295  setName(const Name& name)
296  {
297  name_.set(name);
298  ++changeCount_;
299  return *this;
300  }
301 
308  Interest&
309  setMinSuffixComponents(int minSuffixComponents)
310  {
311  minSuffixComponents_ = minSuffixComponents;
312  ++changeCount_;
313  return *this;
314  }
315 
322  Interest&
323  setMaxSuffixComponents(int maxSuffixComponents)
324  {
325  maxSuffixComponents_ = maxSuffixComponents;
326  ++changeCount_;
327  return *this;
328  }
329 
335  Interest&
336  setChildSelector(int childSelector)
337  {
338  childSelector_ = childSelector;
339  ++changeCount_;
340  return *this;
341  }
342 
349  Interest&
350  setMustBeFresh(bool mustBeFresh)
351  {
352  mustBeFresh_ = mustBeFresh;
353  ++changeCount_;
354  return *this;
355  }
356 
363  Interest&
364  setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
365  {
366  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
367  ++changeCount_;
368  return *this;
369  }
370 
374  Interest&
375  DEPRECATED_IN_NDN_CPP setNonce(const Blob& nonce)
376  {
377  nonce_ = nonce;
378  // Set getNonceChangeCount_ so that the next call to getNonce() won't clear nonce_.
379  ++changeCount_;
380  getNonceChangeCount_ = getChangeCount();
381  return *this;
382  }
383 
392  Interest&
393  setKeyLocator(const KeyLocator& keyLocator)
394  {
395  keyLocator_ = keyLocator;
396  ++changeCount_;
397  return *this;
398  }
399 
408  Interest&
409  setExclude(const Exclude& exclude)
410  {
411  exclude_ = exclude;
412  ++changeCount_;
413  return *this;
414  }
415 
427  Interest&
429  (Blob encoding,
431  {
432  linkWireEncoding_ = encoding;
433  linkWireEncodingFormat_ = &wireFormat;
434 
435  // Clear the link object, assuming that it has a different encoding.
436  link_.set(ptr_lib::shared_ptr<Link>());
437 
438  ++changeCount_;
439  return *this;
440  }
441 
446  Interest&
448  {
449  WireFormat* wireFormat = 0;
450  return setLinkWireEncoding(Blob(), *wireFormat);
451  }
452 
459  Interest&
460  setSelectedDelegationIndex(int selectedDelegationIndex)
461  {
462  selectedDelegationIndex_ = selectedDelegationIndex;
463  ++changeCount_;
464  return *this;
465  }
466 
474  Interest&
475  setLpPacket(const ptr_lib::shared_ptr<LpPacket>& lpPacket)
476  {
477  lpPacket_ = lpPacket;
478  // Don't update changeCount_ since this doesn't affect the wire encoding.
479  return *this;
480  }
481 
487  void
488  refreshNonce();
489 
496  bool
497  matchesName(const Name& name) const;
498 
503  const SignedBlob&
505  {
506  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
507  // The values have changed, so the default wire encoding is invalidated.
508  // This method can be called on a const object, but we want to be able to update the default cached value.
509  const_cast<Interest*>(this)->defaultWireEncoding_ = SignedBlob();
510  const_cast<Interest*>(this)->defaultWireEncodingFormat_ = 0;
511  const_cast<Interest*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
512  }
513 
514  return defaultWireEncoding_;
515  }
516 
522  WireFormat*
523  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
524 
529  uint64_t
531  {
532  // Make sure each of the checkChanged is called.
533  bool changed = name_.checkChanged();
534  changed = keyLocator_.checkChanged() || changed;
535  changed = exclude_.checkChanged() || changed;
536  changed = link_.checkChanged() || changed;
537  if (changed)
538  // A child object has changed, so update the change count.
539  // This method can be called on a const object, but we want to be able to update the changeCount_.
540  ++const_cast<Interest*>(this)->changeCount_;
541 
542  return changeCount_;
543  }
544 
545 private:
546  void
547  construct()
548  {
549  minSuffixComponents_ = -1;
550  maxSuffixComponents_ = -1;
551  childSelector_ = -1;
552  mustBeFresh_ = true;
553  interestLifetimeMilliseconds_ = -1.0;
554  linkWireEncodingFormat_ = 0;
555  selectedDelegationIndex_ = -1;
556  }
557 
558  void
559  setDefaultWireEncoding
560  (const SignedBlob& defaultWireEncoding,
561  WireFormat *defaultWireEncodingFormat)
562  {
563  defaultWireEncoding_ = defaultWireEncoding;
564  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
565  // Set getDefaultWireEncodingChangeCount_ so that the next call to
566  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
567  getDefaultWireEncodingChangeCount_ = getChangeCount();
568  }
569 
570  ChangeCounter<Name> name_;
571  int minSuffixComponents_;
572  int maxSuffixComponents_;
573  ChangeCounter<KeyLocator> keyLocator_;
574  ChangeCounter<Exclude> exclude_;
575  int childSelector_;
576  bool mustBeFresh_;
577  Milliseconds interestLifetimeMilliseconds_;
578  Blob nonce_;
579  uint64_t getNonceChangeCount_;
580  Blob linkWireEncoding_;
581  WireFormat* linkWireEncodingFormat_;
582  SharedPointerChangeCounter<Link> link_;
583  int selectedDelegationIndex_;
584  SignedBlob defaultWireEncoding_;
585  WireFormat *defaultWireEncodingFormat_;
586  uint64_t getDefaultWireEncodingChangeCount_;
587  ptr_lib::shared_ptr<LpPacket> lpPacket_;
588  uint64_t changeCount_;
589 };
590 
591 }
592 
593 #endif
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: interest.hpp:504
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
Interest & setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
Set the interest lifetime.
Definition: interest.hpp:364
Interest & setKeyLocator(const KeyLocator &keyLocator)
Set this interest to use a copy of the given KeyLocator object.
Definition: interest.hpp:393
void set(const InterestLite &interestLite, WireFormat &wireFormat)
Clear this interest, and set the values by copying from interestLite.
Definition: interest.cpp:89
Blob getLinkWireEncoding(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Get the wire encoding of the link object.
Definition: interest.cpp:246
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: interest.hpp:530
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:350
Interest & setMinSuffixComponents(int minSuffixComponents)
Set the min suffix components count.
Definition: interest.hpp:309
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:59
uint64_t getIncomingFaceId() const
Get the incoming face ID according to the incoming packet header.
Definition: interest.cpp:58
Interest & setLinkWireEncoding(Blob encoding, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Set the link wire encoding bytes, without decoding them.
Definition: interest.hpp:429
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:48
Interest & unsetLink()
Clear the link wire encoding and link object so that getLink() returns null.
Definition: interest.hpp:447
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:243
int getSelectedDelegationIndex() const
Get the selected delegation index.
Definition: interest.hpp:279
const Blob & getNonce() const
Return the nonce value from the incoming interest.
Definition: interest.hpp:225
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:135
Interest & setChildSelector(int childSelector)
Set the child selector.
Definition: interest.hpp:336
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:41
std::string toUri() const
Encode the name according to the "NDN URI Scheme".
Definition: interest.cpp:170
Interest & setExclude(const Exclude &exclude)
Set this interest to use a copy of the given Exclude object.
Definition: interest.hpp:409
Interest()
Create a new Interest with an empty name and "none" for all values.
Definition: interest.hpp:88
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:144
Interest & setName(const Name &name)
Set the interest name.
Definition: interest.hpp:295
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Set the max suffix components count.
Definition: interest.hpp:323
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:204
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:375
Link * getLink()
Get the link object.
Definition: interest.cpp:225
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: interest.hpp:523
Definition: key-locator.hpp:35
bool getMustBeFresh() const
Return true if the content must be fresh.
Definition: interest.hpp:214
void refreshNonce()
Update the bytes of the nonce with new random values.
Definition: interest.cpp:259
Interest & setLpPacket(const ptr_lib::shared_ptr< LpPacket > &lpPacket)
An internal library method to set the LpPacket for an incoming packet.
Definition: interest.hpp:475
Interest & setSelectedDelegationIndex(int selectedDelegationIndex)
Set the selected delegation index.
Definition: interest.hpp:460
SignedBlob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Interest for a particular wire format.
Definition: interest.cpp:114