All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_INTEREST_HPP
8 #define NDN_INTEREST_HPP
9 
10 #include "common.hpp"
11 #include "name.hpp"
12 #include "selectors.hpp"
14 
15 namespace ndn {
16 
17 class Data;
18 
19 const time::seconds DEFAULT_INTEREST_LIFETIME = time::seconds(4);
20 
24 class Interest : public enable_shared_from_this<Interest>
25 {
26 public:
31  : m_nonce(0)
32  , m_scope(-1)
33  , m_interestLifetime(time::milliseconds::min())
34  {
35  }
36 
42  Interest(const Name& name)
43  : m_name(name)
44  , m_nonce(0)
45  , m_scope(-1)
46  , m_interestLifetime(time::milliseconds::min())
47  {
48  }
49 
55  Interest(const Name& name, const time::milliseconds& interestLifetime)
56  : m_name(name)
57  , m_nonce(0)
58  , m_scope(-1)
59  , m_interestLifetime(interestLifetime)
60  {
61  }
62 
63  Interest(const Name& name,
64  const Selectors& selectors,
65  int scope,
66  const time::milliseconds& interestLifetime,
67  uint32_t nonce = 0)
68  : m_name(name)
69  , m_selectors(selectors)
70  , m_nonce(nonce)
71  , m_scope(scope)
72  , m_interestLifetime(interestLifetime)
73  {
74  }
75 
91  Interest(const Name& name,
92  int minSuffixComponents, int maxSuffixComponents,
93  const Exclude& exclude,
94  int childSelector,
95  bool mustBeFresh,
96  int scope,
97  const time::milliseconds& interestLifetime,
98  uint32_t nonce = 0)
99  : m_name(name)
100  , m_selectors(minSuffixComponents, maxSuffixComponents, exclude, childSelector, mustBeFresh)
101  , m_nonce(nonce)
102  , m_scope(scope)
103  , m_interestLifetime(interestLifetime)
104  {
105  }
106 
110  explicit
111  Interest(const Block& wire)
112  {
113  wireDecode(wire);
114  }
115 
119  template<bool T>
120  inline size_t
121  wireEncode(EncodingImpl<T>& block) const;
122 
126  inline const Block&
127  wireEncode() const;
128 
132  inline void
133  wireDecode(const Block& wire);
134 
138  inline bool
139  hasWire() const;
140 
146  inline std::string
147  toUri() const;
148 
149  inline bool
150  hasSelectors() const;
151 
152  inline bool
153  hasGuiders() const;
154 
162  bool
163  matchesName(const Name& name) const;
164 
173  bool
174  matchesData(const Data& data) const;
175 
179  // Getters/setters
180 
181  const Name&
182  getName() const
183  {
184  return m_name;
185  }
186 
187  Interest&
188  setName(const Name& name)
189  {
190  m_name = name;
191  m_wire.reset();
192  return *this;
193  }
194 
195  //
196 
197  const Selectors&
198  getSelectors() const
199  {
200  return m_selectors;
201  }
202 
203  Interest&
204  setSelectors(const Selectors& selectors)
205  {
206  m_selectors = selectors;
207  m_wire.reset();
208  return *this;
209  }
210 
211  //
212 
213  int
214  getScope() const
215  {
216  return m_scope;
217  }
218 
219  Interest&
220  setScope(int scope)
221  {
222  m_scope = scope;
223  m_wire.reset();
224  return *this;
225  }
226 
227  //
228 
229  const time::milliseconds&
231  {
232  return m_interestLifetime;
233  }
234 
235  Interest&
236  setInterestLifetime(const time::milliseconds& interestLifetime)
237  {
238  m_interestLifetime = interestLifetime;
239  m_wire.reset();
240  return *this;
241  }
242 
243  //
244 
252  const uint32_t&
253  getNonce() const;
254 
255  Interest&
256  setNonce(uint32_t nonce)
257  {
258  m_nonce = nonce;
259  m_wire.reset();
260  return *this;
261  }
262 
263  //
264 
267  {
268  return m_localControlHeader;
269  }
270 
273  {
274  return m_localControlHeader;
275  }
276 
277  // helper methods for LocalControlHeader
278 
279  uint64_t
281  {
283  }
284 
285  Interest&
286  setIncomingFaceId(uint64_t incomingFaceId)
287  {
288  getLocalControlHeader().setIncomingFaceId(incomingFaceId);
289  // ! do not reset Interest's wire !
290  return *this;
291  }
292 
293  //
294 
295  // NextHopFaceId helpers make sense only for Interests
296 
297  uint64_t
299  {
301  }
302 
303  Interest&
304  setNextHopFaceId(uint64_t nextHopFaceId)
305  {
306  getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
307  // ! do not reset Interest's wire !
308  return *this;
309  }
310 
311  //
312 
316  // Wrappers for Selectors
317  //
318 
319  int
321  {
322  return m_selectors.getMinSuffixComponents();
323  }
324 
325  Interest&
326  setMinSuffixComponents(int minSuffixComponents)
327  {
328  m_selectors.setMinSuffixComponents(minSuffixComponents);
329  m_wire.reset();
330  return *this;
331  }
332 
333  //
334 
335  int
337  {
338  return m_selectors.getMaxSuffixComponents();
339  }
340 
341  Interest&
342  setMaxSuffixComponents(int maxSuffixComponents)
343  {
344  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
345  m_wire.reset();
346  return *this;
347  }
348 
349  //
350 
351  const KeyLocator&
353  {
354  return m_selectors.getPublisherPublicKeyLocator();
355  }
356 
357  Interest&
359  {
360  m_selectors.setPublisherPublicKeyLocator(keyLocator);
361  m_wire.reset();
362  return *this;
363  }
364 
365  //
366 
367  const Exclude&
368  getExclude() const
369  {
370  return m_selectors.getExclude();
371  }
372 
373  Interest&
374  setExclude(const Exclude& exclude)
375  {
376  m_selectors.setExclude(exclude);
377  m_wire.reset();
378  return *this;
379  }
380 
381  //
382 
383  int
385  {
386  return m_selectors.getChildSelector();
387  }
388 
389  Interest&
390  setChildSelector(int childSelector)
391  {
392  m_selectors.setChildSelector(childSelector);
393  m_wire.reset();
394  return *this;
395  }
396 
397  //
398 
399  int
401  {
402  return m_selectors.getMustBeFresh();
403  }
404 
405  Interest&
406  setMustBeFresh(bool mustBeFresh)
407  {
408  m_selectors.setMustBeFresh(mustBeFresh);
409  m_wire.reset();
410  return *this;
411  }
412 
413 public: // EqualityComparable concept
414  bool
415  operator==(const Interest& other) const
416  {
417  return wireEncode() == other.wireEncode();
418  }
419 
420  bool
421  operator!=(const Interest& other) const
422  {
423  return !(*this == other);
424  }
425 
426 private:
427  Name m_name;
428  Selectors m_selectors;
429  mutable uint32_t m_nonce;
430  int m_scope;
431  time::milliseconds m_interestLifetime;
432 
433  mutable Block m_wire;
434 
435  nfd::LocalControlHeader m_localControlHeader;
437 };
438 
439 std::ostream&
440 operator<<(std::ostream& os, const Interest& interest);
441 
442 inline std::string
444 {
445  std::ostringstream os;
446  os << *this;
447  return os.str();
448 }
449 
450 inline bool
452 {
453  return !m_selectors.empty();
454 }
455 
456 inline bool
458 {
459  return m_scope >= 0 ||
460  m_interestLifetime >= time::milliseconds::zero() ||
461  m_nonce > 0;
462 }
463 
464 template<bool T>
465 inline size_t
467 {
468  size_t totalLength = 0;
469 
470  // Interest ::= INTEREST-TYPE TLV-LENGTH
471  // Name
472  // Selectors?
473  // Nonce
474  // Scope?
475  // InterestLifetime?
476 
477  // (reverse encoding)
478 
479  // InterestLifetime
480  if (getInterestLifetime() >= time::milliseconds::zero() &&
482  {
483  totalLength += prependNonNegativeIntegerBlock(block,
485  getInterestLifetime().count());
486  }
487 
488  // Scope
489  if (getScope() >= 0)
490  {
491  totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
492  }
493 
494  // Nonce
495  totalLength += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
496 
497  // Selectors
498  if (!getSelectors().empty())
499  {
500  totalLength += getSelectors().wireEncode(block);
501  }
502 
503  // Name
504  totalLength += getName().wireEncode(block);
505 
506  totalLength += block.prependVarNumber (totalLength);
507  totalLength += block.prependVarNumber (Tlv::Interest);
508  return totalLength;
509 }
510 
511 inline const Block&
513 {
514  if (m_wire.hasWire())
515  return m_wire;
516 
517  EncodingEstimator estimator;
518  size_t estimatedSize = wireEncode(estimator);
519 
520  EncodingBuffer buffer(estimatedSize, 0);
521  wireEncode(buffer);
522 
523  m_wire = buffer.block();
524  return m_wire;
525 }
526 
527 inline void
529 {
530  m_wire = wire;
531  m_wire.parse();
532 
533  // Interest ::= INTEREST-TYPE TLV-LENGTH
534  // Name
535  // Selectors?
536  // Nonce
537  // Scope?
538  // InterestLifetime?
539 
540  if (m_wire.type() != Tlv::Interest)
541  throw Tlv::Error("Unexpected TLV number when decoding Interest");
542 
543  // Name
544  m_name.wireDecode(m_wire.get(Tlv::Name));
545 
546  // Selectors
548  if (val != m_wire.elements_end())
549  {
550  m_selectors.wireDecode(*val);
551  }
552  else
553  m_selectors = Selectors();
554 
555  // Nonce
556  val = m_wire.find(Tlv::Nonce);
557  if (val != m_wire.elements_end())
558  {
559  m_nonce = readNonNegativeInteger(*val);
560  }
561  else
562  m_nonce = 0;
563 
564  // Scope
565  val = m_wire.find(Tlv::Scope);
566  if (val != m_wire.elements_end())
567  {
568  m_scope = readNonNegativeInteger(*val);
569  }
570  else
571  m_scope = -1;
572 
573  // InterestLifetime
574  val = m_wire.find(Tlv::InterestLifetime);
575  if (val != m_wire.elements_end())
576  {
577  m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
578  }
579  else
580  {
581  m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
582  }
583 }
584 
585 inline bool
587 {
588  return m_wire.hasWire();
589 }
590 
591 
592 } // namespace ndn
593 
594 #endif // NDN_INTEREST_HPP
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: selectors.hpp:285
int getMinSuffixComponents() const
Definition: interest.hpp:320
int getMaxSuffixComponents() const
Definition: interest.hpp:336
const nfd::LocalControlHeader & getLocalControlHeader() const
Definition: interest.hpp:272
const Name & getName() const
Definition: interest.hpp:182
bool hasGuiders() const
Definition: interest.hpp:457
bool operator==(const Interest &other) const
Definition: interest.hpp:415
bool matchesName(const Name &name) const
Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms...
Definition: interest.cpp:27
Interest & setMustBeFresh(bool mustBeFresh)
Definition: interest.hpp:406
Interest(const Name &name, int minSuffixComponents, int maxSuffixComponents, const Exclude &exclude, int childSelector, bool mustBeFresh, int scope, const time::milliseconds &interestLifetime, uint32_t nonce=0)
Create a new Interest for the given name and values.
Definition: interest.hpp:91
element_const_iterator find(uint32_t type) const
Definition: block.hpp:343
bool hasWire() const
Check if already has wire.
Definition: interest.hpp:586
Selectors & setChildSelector(int childSelector)
Definition: selectors.hpp:150
bool hasSelectors() const
Definition: interest.hpp:451
Interest & setScope(int scope)
Definition: interest.hpp:220
Interest & setNonce(uint32_t nonce)
Definition: interest.hpp:256
const Block & wireEncode() const
Encode to a wire format.
Definition: interest.hpp:512
Class representing wire element of the NDN packet.
Definition: block.hpp:26
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:24
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: selectors.hpp:211
uint64_t readNonNegativeInteger(const Block &block)
std::string toUri() const
Encode the name according to the &quot;NDN URI Scheme&quot;.
Definition: interest.hpp:443
const time::milliseconds & getInterestLifetime() const
Definition: interest.hpp:230
int getChildSelector() const
Definition: interest.hpp:384
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.hpp:118
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: interest.hpp:466
Interest(const Block &wire)
Create from wire encoding.
Definition: interest.hpp:111
element_const_iterator elements_end() const
Definition: block.hpp:476
uint64_t getNextHopFaceId() const
Definition: interest.hpp:298
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:112
Interest()
Create a new Interest with an empty name and &quot;none&quot; for all values.
Definition: interest.hpp:30
const Exclude & getExclude() const
Definition: selectors.hpp:128
const Selectors & getSelectors() const
Definition: interest.hpp:198
int getMustBeFresh() const
Definition: interest.hpp:400
int getChildSelector() const
Definition: selectors.hpp:144
const time::seconds DEFAULT_INTEREST_LIFETIME
Definition: interest.hpp:19
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.hpp:102
Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:374
bool empty() const
Definition: selectors.hpp:198
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:390
Interest & setName(const Name &name)
Definition: interest.hpp:188
int getMustBeFresh() const
Definition: selectors.hpp:160
Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:358
const Exclude & getExclude() const
Definition: interest.hpp:368
Abstraction implementing Interest selectors.
Definition: selectors.hpp:20
Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:326
uint64_t getIncomingFaceId() const
Definition: interest.hpp:280
const Block & get(uint32_t type) const
Get the first subelement of the requested type.
Definition: block.hpp:326
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
Interest(const Name &name)
Create a new Interest with the given name and &quot;none&quot; for other values.
Definition: interest.hpp:42
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:342
Selectors & setMustBeFresh(bool mustBeFresh)
Definition: selectors.hpp:166
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
bool matchesData(const Data &data) const
Determines whether this Interest can be satisfied by data.
Definition: interest.cpp:54
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: name.hpp:711
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:204
uint32_t type() const
Definition: block.hpp:320
Interest & setNextHopFaceId(uint64_t nextHopFaceId)
Definition: interest.hpp:304
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: interest.hpp:528
Interest & setIncomingFaceId(uint64_t incomingFaceId)
Definition: interest.hpp:286
Interest(const Name &name, const time::milliseconds &interestLifetime)
Create a new Interest with the given name and interest lifetime and &quot;none&quot; for other values...
Definition: interest.hpp:55
int getScope() const
Definition: interest.hpp:214
bool operator!=(const Interest &other) const
Definition: interest.hpp:421
int getMinSuffixComponents() const
Definition: selectors.hpp:80
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.hpp:134
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:352
size_t prependNonNegativeIntegerBlock(EncodingImpl< P > &encoder, uint32_t type, uint64_t number)
helper methods
void setIncomingFaceId(uint64_t incomingFaceId)
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.hpp:86
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.hpp:523
void wireDecode(const Block &wire)
Definition: name.hpp:746
int getMaxSuffixComponents() const
Definition: selectors.hpp:96
Interest & setInterestLifetime(const time::milliseconds &interestLifetime)
Definition: interest.hpp:236
void setNextHopFaceId(uint64_t nextHopFaceId)
Class representing wire element of the NDN packet.
Class to represent Exclude component in NDN interests.
Definition: exclude.hpp:21
const uint32_t & getNonce() const
Get Interest&#39;s nonce.
Definition: interest.cpp:17
Interest(const Name &name, const Selectors &selectors, int scope, const time::milliseconds &interestLifetime, uint32_t nonce=0)
Definition: interest.hpp:63
nfd::LocalControlHeader & getLocalControlHeader()
Definition: interest.hpp:266