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 "publisher-public-key-digest.hpp"
27 #include "key-locator.hpp"
28 #include "c/interest-types.h"
29 #include "encoding/wire-format.hpp"
30 #include "util/signed-blob.hpp"
31 #include "util/change-counter.hpp"
32 #include "exclude.hpp"
33 
34 struct ndn_Interest;
35 
36 namespace ndn {
37 
41 class Interest {
42 public:
50  DEPRECATED_IN_NDN_CPP Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
51  const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
52  int scope, Milliseconds interestLifetimeMilliseconds, const Blob& nonce);
53 
59  DEPRECATED_IN_NDN_CPP Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
60  const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
61  int scope, Milliseconds interestLifetimeMilliseconds);
62 
67  DEPRECATED_IN_NDN_CPP Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
68  const KeyLocator& keyLocator, const Exclude& exclude, int childSelector, int answerOriginKind,
69  int scope, Milliseconds interestLifetimeMilliseconds);
70 
76  Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
77  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
78  {
79  construct();
80  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
81  }
82 
87  Interest(const Name& name)
88  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
89  {
90  construct();
91  }
92 
93  Interest(const Interest& interest)
94  : name_(interest.name_), minSuffixComponents_(interest.minSuffixComponents_),
95  maxSuffixComponents_(interest.maxSuffixComponents_),
96  publisherPublicKeyDigest_(interest.publisherPublicKeyDigest_),
97  keyLocator_(interest.keyLocator_), exclude_(interest.exclude_),
98  childSelector_(interest.childSelector_),
99  answerOriginKind_(interest.answerOriginKind_),
100  scope_(interest.scope_),
101  interestLifetimeMilliseconds_(interest.interestLifetimeMilliseconds_),
102  nonce_(interest.nonce_), getNonceChangeCount_(0), changeCount_(0)
103  {
104  setDefaultWireEncoding
105  (interest.defaultWireEncoding_, interest.defaultWireEncodingFormat_);
106  }
107 
112  : getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
113  {
114  construct();
115  }
116 
117  Interest& operator=(const Interest& interest);
118 
128  SignedBlob
130 
139  void
140  wireDecode
141  (const Blob& input,
143 
153  void
154  wireDecode
155  (const uint8_t *input, size_t inputLength,
157 
166  void
167  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
168  {
169  wireDecode(&input[0], input.size(), wireFormat);
170  }
171 
180  std::string
181  toUri() const;
182 
188  void
189  get(struct ndn_Interest& interestStruct) const;
190 
191  Name&
192  getName() { return name_.get(); }
193 
194  const Name&
195  getName() const { return name_.get(); }
196 
197  int
198  getMinSuffixComponents() const { return minSuffixComponents_; }
199 
200  int
201  getMaxSuffixComponents() const { return maxSuffixComponents_; }
202 
207  PublisherPublicKeyDigest&
208  DEPRECATED_IN_NDN_CPP getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_.get(); }
209 
215  DEPRECATED_IN_NDN_CPP getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_.get(); }
216 
217  const KeyLocator&
218  getKeyLocator() const { return keyLocator_.get(); }
219 
220  KeyLocator&
221  getKeyLocator() { return keyLocator_.get(); }
222 
223  Exclude&
224  getExclude() { return exclude_.get(); }
225 
226  const Exclude&
227  getExclude() const { return exclude_.get(); }
228 
229  int
230  getChildSelector() const { return childSelector_; }
231 
235  int
236  DEPRECATED_IN_NDN_CPP getAnswerOriginKind() const;
237 
242  bool
244  {
245  // Imitate ndn_Interest_getMustBeFresh.
246  if (answerOriginKind_ < 0)
247  return true;
248  else
249  return (answerOriginKind_ & ndn_Interest_ANSWER_STALE) == 0;
250  }
251 
252  int
253  getScope() const { return scope_; }
254 
256  getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
257 
263  const Blob&
264  getNonce() const
265  {
266  if (getNonceChangeCount_ != getChangeCount()) {
267  // The values have changed, so the existing nonce is invalidated.
268  // This method can be called on a const object, but we want to be able to update the default cached value.
269  const_cast<Interest*>(this)->nonce_ = Blob();
270  const_cast<Interest*>(this)->getNonceChangeCount_ = getChangeCount();
271  }
272 
273  return nonce_;
274  }
275 
280  void
281  set(const struct ndn_Interest& interestStruct);
282 
289  Interest&
290  setName(const Name& name)
291  {
292  name_.set(name);
293  ++changeCount_;
294  return *this;
295  }
296 
303  Interest&
304  setMinSuffixComponents(int minSuffixComponents)
305  {
306  minSuffixComponents_ = minSuffixComponents;
307  ++changeCount_;
308  return *this;
309  }
310 
317  Interest&
318  setMaxSuffixComponents(int maxSuffixComponents)
319  {
320  maxSuffixComponents_ = maxSuffixComponents;
321  ++changeCount_;
322  return *this;
323  }
324 
330  Interest&
331  setChildSelector(int childSelector)
332  {
333  childSelector_ = childSelector;
334  ++changeCount_;
335  return *this;
336  }
337 
341  Interest&
342  DEPRECATED_IN_NDN_CPP setAnswerOriginKind(int answerOriginKind);
343 
350  Interest&
351  setMustBeFresh(bool mustBeFresh)
352  {
353  if (answerOriginKind_ < 0) {
354  // It is is already the default where MustBeFresh is true.
355  if (!mustBeFresh) {
356  // Set answerOriginKind_ so that getMustBeFresh returns false.
357  answerOriginKind_ = ndn_Interest_ANSWER_STALE;
358  ++changeCount_;
359  }
360  }
361  else {
362  if (mustBeFresh)
363  // Clear the stale bit.
364  answerOriginKind_ &= ~ndn_Interest_ANSWER_STALE;
365  else
366  // Set the stale bit.
367  answerOriginKind_ |= ndn_Interest_ANSWER_STALE;
368  ++changeCount_;
369  }
370  return *this;
371  }
372 
378  Interest&
379  setScope(int scope)
380  {
381  scope_ = scope;
382  ++changeCount_;
383  return *this;
384  }
385 
392  Interest&
393  setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
394  {
395  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
396  ++changeCount_;
397  return *this;
398  }
399 
403  Interest&
404  DEPRECATED_IN_NDN_CPP setNonce(const Blob& nonce)
405  {
406  nonce_ = nonce;
407  // Set getNonceChangeCount_ so that the next call to getNonce() won't clear nonce_.
408  ++changeCount_;
409  getNonceChangeCount_ = getChangeCount();
410  return *this;
411  }
412 
421  Interest&
422  setKeyLocator(const KeyLocator& keyLocator)
423  {
424  keyLocator_ = keyLocator;
425  ++changeCount_;
426  return *this;
427  }
428 
437  Interest&
438  setExclude(const Exclude& exclude)
439  {
440  exclude_ = exclude;
441  ++changeCount_;
442  return *this;
443  }
444 
451  bool
452  matchesName(const Name& name) const;
453 
458  const SignedBlob&
460  {
461  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
462  // The values have changed, so the default wire encoding is invalidated.
463  // This method can be called on a const object, but we want to be able to update the default cached value.
464  const_cast<Interest*>(this)->defaultWireEncoding_ = SignedBlob();
465  const_cast<Interest*>(this)->defaultWireEncodingFormat_ = 0;
466  const_cast<Interest*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
467  }
468 
469  return defaultWireEncoding_;
470  }
471 
477  WireFormat*
478  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
479 
484  uint64_t
486  {
487  // Make sure each of the checkChanged is called.
488  bool changed = name_.checkChanged();
489  changed = publisherPublicKeyDigest_.checkChanged() || changed;
490  changed = keyLocator_.checkChanged() || changed;
491  changed = exclude_.checkChanged() || changed;
492  if (changed)
493  // A child object has changed, so update the change count.
494  // This method can be called on a const object, but we want to be able to update the changeCount_.
495  ++const_cast<Interest*>(this)->changeCount_;
496 
497  return changeCount_;
498  }
499 
500 private:
501  void
502  construct()
503  {
504  minSuffixComponents_ = -1;
505  maxSuffixComponents_ = -1;
506  childSelector_ = -1;
507  answerOriginKind_ = -1;
508  scope_ = -1;
509  interestLifetimeMilliseconds_ = -1.0;
510  }
511 
512  void
513  setDefaultWireEncoding
514  (const SignedBlob& defaultWireEncoding,
515  WireFormat *defaultWireEncodingFormat)
516  {
517  defaultWireEncoding_ = defaultWireEncoding;
518  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
519  // Set getDefaultWireEncodingChangeCount_ so that the next call to
520  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
521  getDefaultWireEncodingChangeCount_ = getChangeCount();
522  }
523 
524  ChangeCounter<Name> name_;
525  int minSuffixComponents_;
526  int maxSuffixComponents_;
529  ChangeCounter<PublisherPublicKeyDigest> publisherPublicKeyDigest_;
530  ChangeCounter<KeyLocator> keyLocator_;
531  ChangeCounter<Exclude> exclude_;
532  int childSelector_;
533  int answerOriginKind_;
534  int scope_;
535  Milliseconds interestLifetimeMilliseconds_;
536  Blob nonce_;
537  uint64_t getNonceChangeCount_;
538  SignedBlob defaultWireEncoding_;
539  WireFormat *defaultWireEncodingFormat_;
540  uint64_t getDefaultWireEncodingChangeCount_;
541  uint64_t changeCount_;
542 };
543 
544 }
545 
546 #endif
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: interest.hpp:459
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
Interest & setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
Set the interest lifetime.
Definition: interest.hpp:393
Interest & setScope(int scope)
Set the interest scope.
Definition: interest.hpp:379
Interest & setKeyLocator(const KeyLocator &keyLocator)
Set this interest to use a copy of the given KeyLocator object.
Definition: interest.hpp:422
int childSelector
-1 for none
Definition: interest-types.h:79
PublisherPublicKeyDigest &DEPRECATED_IN_NDN_CPP getPublisherPublicKeyDigest()
Definition: interest.hpp:208
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: interest.hpp:485
struct ndn_Blob nonce
The blob whose value is a pointer to a pre-allocated buffer.
Definition: interest-types.h:83
A PublisherPublicKeyDigest holds the publisher public key digest value, if any.
Definition: publisher-public-key-digest.hpp:37
ndn_Milliseconds interestLifetimeMilliseconds
-1.0 for none
Definition: interest-types.h:82
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:304
An Exclude holds a vector of Exclude::Entry.
Definition: exclude.hpp:36
Interest(const Name &name)
Create a new Interest with the given name and "none" for other values.
Definition: interest.hpp:87
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:76
An ndn_Interest holds an ndn_Name and other fields for an interest.
Definition: interest-types.h:70
int scope
-1 for none
Definition: interest-types.h:81
const Blob & getNonce() const
Return the nonce value from the incoming interest.
Definition: interest.hpp:264
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:42
int minSuffixComponents
-1 for none
Definition: interest-types.h:72
void wireDecode(const Blob &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Interest.
Definition: interest.cpp:149
Interest & setChildSelector(int childSelector)
Set the child selector.
Definition: interest.hpp:331
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:184
Interest & setExclude(const Exclude &exclude)
Set this interest to use a copy of the given Exclude object.
Definition: interest.hpp:438
Interest()
Create a new Interest with an empty name and "none" for all values.
Definition: interest.hpp:111
int DEPRECATED_IN_NDN_CPP getAnswerOriginKind() const
Definition: interest.cpp:246
const PublisherPublicKeyDigest &DEPRECATED_IN_NDN_CPP getPublisherPublicKeyDigest() const
Definition: interest.hpp:215
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:167
Interest & setName(const Name &name)
Set the interest name.
Definition: interest.hpp:290
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Set the max suffix components count.
Definition: interest.hpp:318
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:36
Interest &DEPRECATED_IN_NDN_CPP setAnswerOriginKind(int answerOriginKind)
Definition: interest.cpp:256
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:225
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:37
int maxSuffixComponents
-1 for none
Definition: interest-types.h:73
Interest &DEPRECATED_IN_NDN_CPP setNonce(const Blob &nonce)
Definition: interest.hpp:404
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: interest.hpp:478
Definition: key-locator.hpp:36
bool getMustBeFresh() const
Return true if the content must be fresh.
Definition: interest.hpp:243
void set(const struct ndn_Interest &interestStruct)
Clear this interest, and set the values by copying from the interest struct.
Definition: interest.cpp:91
SignedBlob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Interest for a particular wire format.
Definition: interest.cpp:128