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 "key-locator.hpp"
27 #include "lite/interest-lite.hpp"
28 #include "encoding/wire-format.hpp"
29 #include "util/signed-blob.hpp"
30 #include "util/change-counter.hpp"
31 #include "exclude.hpp"
32 
33 namespace ndn {
34 
38 class Interest {
39 public:
45  Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
46  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
47  {
48  construct();
49  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
50  }
51 
56  Interest(const Name& name)
57  : name_(name), getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
58  {
59  construct();
60  }
61 
62  Interest(const Interest& interest)
63  : name_(interest.name_), minSuffixComponents_(interest.minSuffixComponents_),
64  maxSuffixComponents_(interest.maxSuffixComponents_),
65  keyLocator_(interest.keyLocator_), exclude_(interest.exclude_),
66  childSelector_(interest.childSelector_),
67  mustBeFresh_(interest.mustBeFresh_),
68  interestLifetimeMilliseconds_(interest.interestLifetimeMilliseconds_),
69  nonce_(interest.nonce_), getNonceChangeCount_(0), changeCount_(0)
70  {
71  setDefaultWireEncoding
72  (interest.getDefaultWireEncoding(), interest.defaultWireEncodingFormat_);
73  }
74 
79  : getNonceChangeCount_(0), changeCount_(0), getDefaultWireEncodingChangeCount_(0)
80  {
81  construct();
82  }
83 
84  Interest& operator=(const Interest& interest);
85 
97 
106  void
107  wireDecode
108  (const Blob& input,
110 
120  void
121  wireDecode
122  (const uint8_t *input, size_t inputLength,
124 
133  void
134  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
135  {
136  wireDecode(&input[0], input.size(), wireFormat);
137  }
138 
147  std::string
148  toUri() const;
149 
158  void
159  get(InterestLite& interestLite) const;
160 
165  void
166  set(const InterestLite& interestLite);
167 
168  Name&
169  getName() { return name_.get(); }
170 
171  const Name&
172  getName() const { return name_.get(); }
173 
174  int
175  getMinSuffixComponents() const { return minSuffixComponents_; }
176 
177  int
178  getMaxSuffixComponents() const { return maxSuffixComponents_; }
179 
180  const KeyLocator&
181  getKeyLocator() const { return keyLocator_.get(); }
182 
183  KeyLocator&
184  getKeyLocator() { return keyLocator_.get(); }
185 
186  Exclude&
187  getExclude() { return exclude_.get(); }
188 
189  const Exclude&
190  getExclude() const { return exclude_.get(); }
191 
192  int
193  getChildSelector() const { return childSelector_; }
194 
199  bool
200  getMustBeFresh() const { return mustBeFresh_; }
201 
203  getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
204 
210  const Blob&
211  getNonce() const
212  {
213  if (getNonceChangeCount_ != getChangeCount()) {
214  // The values have changed, so the existing nonce is invalidated.
215  // This method can be called on a const object, but we want to be able to update the default cached value.
216  const_cast<Interest*>(this)->nonce_ = Blob();
217  const_cast<Interest*>(this)->getNonceChangeCount_ = getChangeCount();
218  }
219 
220  return nonce_;
221  }
222 
229  Interest&
230  setName(const Name& name)
231  {
232  name_.set(name);
233  ++changeCount_;
234  return *this;
235  }
236 
243  Interest&
244  setMinSuffixComponents(int minSuffixComponents)
245  {
246  minSuffixComponents_ = minSuffixComponents;
247  ++changeCount_;
248  return *this;
249  }
250 
257  Interest&
258  setMaxSuffixComponents(int maxSuffixComponents)
259  {
260  maxSuffixComponents_ = maxSuffixComponents;
261  ++changeCount_;
262  return *this;
263  }
264 
270  Interest&
271  setChildSelector(int childSelector)
272  {
273  childSelector_ = childSelector;
274  ++changeCount_;
275  return *this;
276  }
277 
284  Interest&
285  setMustBeFresh(bool mustBeFresh)
286  {
287  mustBeFresh_ = mustBeFresh;
288  ++changeCount_;
289  return *this;
290  }
291 
298  Interest&
299  setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds)
300  {
301  interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
302  ++changeCount_;
303  return *this;
304  }
305 
309  Interest&
310  DEPRECATED_IN_NDN_CPP setNonce(const Blob& nonce)
311  {
312  nonce_ = nonce;
313  // Set getNonceChangeCount_ so that the next call to getNonce() won't clear nonce_.
314  ++changeCount_;
315  getNonceChangeCount_ = getChangeCount();
316  return *this;
317  }
318 
327  Interest&
328  setKeyLocator(const KeyLocator& keyLocator)
329  {
330  keyLocator_ = keyLocator;
331  ++changeCount_;
332  return *this;
333  }
334 
343  Interest&
344  setExclude(const Exclude& exclude)
345  {
346  exclude_ = exclude;
347  ++changeCount_;
348  return *this;
349  }
350 
357  bool
358  matchesName(const Name& name) const;
359 
364  const SignedBlob&
366  {
367  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
368  // The values have changed, so the default wire encoding is invalidated.
369  // This method can be called on a const object, but we want to be able to update the default cached value.
370  const_cast<Interest*>(this)->defaultWireEncoding_ = SignedBlob();
371  const_cast<Interest*>(this)->defaultWireEncodingFormat_ = 0;
372  const_cast<Interest*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
373  }
374 
375  return defaultWireEncoding_;
376  }
377 
383  WireFormat*
384  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
385 
390  uint64_t
392  {
393  // Make sure each of the checkChanged is called.
394  bool changed = name_.checkChanged();
395  changed = keyLocator_.checkChanged() || changed;
396  changed = exclude_.checkChanged() || changed;
397  if (changed)
398  // A child object has changed, so update the change count.
399  // This method can be called on a const object, but we want to be able to update the changeCount_.
400  ++const_cast<Interest*>(this)->changeCount_;
401 
402  return changeCount_;
403  }
404 
405 private:
406  void
407  construct()
408  {
409  minSuffixComponents_ = -1;
410  maxSuffixComponents_ = -1;
411  childSelector_ = -1;
412  mustBeFresh_ = true;
413  interestLifetimeMilliseconds_ = -1.0;
414  }
415 
416  void
417  setDefaultWireEncoding
418  (const SignedBlob& defaultWireEncoding,
419  WireFormat *defaultWireEncodingFormat)
420  {
421  defaultWireEncoding_ = defaultWireEncoding;
422  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
423  // Set getDefaultWireEncodingChangeCount_ so that the next call to
424  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
425  getDefaultWireEncodingChangeCount_ = getChangeCount();
426  }
427 
428  ChangeCounter<Name> name_;
429  int minSuffixComponents_;
430  int maxSuffixComponents_;
431  ChangeCounter<KeyLocator> keyLocator_;
432  ChangeCounter<Exclude> exclude_;
433  int childSelector_;
434  bool mustBeFresh_;
435  Milliseconds interestLifetimeMilliseconds_;
436  Blob nonce_;
437  uint64_t getNonceChangeCount_;
438  SignedBlob defaultWireEncoding_;
439  WireFormat *defaultWireEncodingFormat_;
440  uint64_t getDefaultWireEncodingChangeCount_;
441  uint64_t changeCount_;
442 };
443 
444 }
445 
446 #endif
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: interest.hpp:365
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:299
Interest & setKeyLocator(const KeyLocator &keyLocator)
Set this interest to use a copy of the given KeyLocator object.
Definition: interest.hpp:328
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: interest.hpp:391
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:285
Interest & setMinSuffixComponents(int minSuffixComponents)
Set the min suffix components count.
Definition: interest.hpp:244
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:56
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:45
void set(const InterestLite &interestLite)
Clear this interest, and set the values by copying from interestLite.
Definition: interest.cpp:63
const Blob & getNonce() const
Return the nonce value from the incoming interest.
Definition: interest.hpp:211
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:103
Interest & setChildSelector(int childSelector)
Set the child selector.
Definition: interest.hpp:271
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:38
std::string toUri() const
Encode the name according to the "NDN URI Scheme".
Definition: interest.cpp:138
Interest & setExclude(const Exclude &exclude)
Set this interest to use a copy of the given Exclude object.
Definition: interest.hpp:344
Interest()
Create a new Interest with an empty name and "none" for all values.
Definition: interest.hpp:78
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:134
Interest & setName(const Name &name)
Set the interest name.
Definition: interest.hpp:230
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Set the max suffix components count.
Definition: interest.hpp:258
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:172
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:36
Interest &DEPRECATED_IN_NDN_CPP setNonce(const Blob &nonce)
Definition: interest.hpp:310
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: interest.hpp:384
Definition: key-locator.hpp:35
bool getMustBeFresh() const
Return true if the content must be fresh.
Definition: interest.hpp:200
SignedBlob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Interest for a particular wire format.
Definition: interest.cpp:82