interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_INTEREST_HPP
23 #define NDN_INTEREST_HPP
24 
25 #include "delegation-list.hpp"
26 #include "name.hpp"
27 #include "packet-base.hpp"
28 #include "selectors.hpp"
29 #include "util/time.hpp"
30 
31 namespace ndn {
32 
33 class Data;
34 
38 const time::milliseconds DEFAULT_INTEREST_LIFETIME = time::milliseconds(4000);
39 
42 class Interest : public PacketBase, public enable_shared_from_this<Interest>
43 {
44 public:
45  class Error : public tlv::Error
46  {
47  public:
48  explicit
49  Error(const std::string& what)
50  : tlv::Error(what)
51  {
52  }
53  };
54 
60  explicit
61  Interest(const Name& name = Name(), time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME);
62 
67  explicit
68  Interest(const Block& wire);
69 
73  template<encoding::Tag TAG>
74  size_t
75  wireEncode(EncodingImpl<TAG>& encoder) const;
76 
80  const Block&
81  wireEncode() const;
82 
86  void
87  wireDecode(const Block& wire);
88 
92  bool
93  hasWire() const
94  {
95  return m_wire.hasWire();
96  }
97 
104  std::string
105  toUri() const;
106 
107 public: // matching
112  bool
113  matchesName(const Name& name) const;
114 
122  bool
123  matchesData(const Data& data) const;
124 
134  bool
135  matchesInterest(const Interest& other) const;
136 
137 public: // Name, Nonce, and Guiders
138  const Name&
139  getName() const
140  {
141  return m_name;
142  }
143 
144  Interest&
145  setName(const Name& name)
146  {
147  m_name = name;
148  m_wire.reset();
149  return *this;
150  }
151 
154  bool
155  hasNonce() const
156  {
157  return static_cast<bool>(m_nonce);
158  }
159 
164  uint32_t
165  getNonce() const;
166 
169  Interest&
170  setNonce(uint32_t nonce);
171 
179  void
180  refreshNonce();
181 
182  time::milliseconds
184  {
185  return m_interestLifetime;
186  }
187 
192  Interest&
193  setInterestLifetime(time::milliseconds interestLifetime);
194 
195  const DelegationList&
197  {
198  return m_forwardingHint;
199  }
200 
201  Interest&
202  setForwardingHint(const DelegationList& value);
203 
214  template<typename Modifier>
215  Interest&
216  modifyForwardingHint(const Modifier& modifier)
217  {
218  modifier(m_forwardingHint);
219  m_wire.reset();
220  return *this;
221  }
222 
223 public: // Selectors
227  bool
228  hasSelectors() const
229  {
230  return !m_selectors.empty();
231  }
232 
233  const Selectors&
234  getSelectors() const
235  {
236  return m_selectors;
237  }
238 
239  Interest&
240  setSelectors(const Selectors& selectors)
241  {
242  m_selectors = selectors;
243  m_wire.reset();
244  return *this;
245  }
246 
247  int
249  {
250  return m_selectors.getMinSuffixComponents();
251  }
252 
253  Interest&
254  setMinSuffixComponents(int minSuffixComponents)
255  {
256  m_selectors.setMinSuffixComponents(minSuffixComponents);
257  m_wire.reset();
258  return *this;
259  }
260 
261  int
263  {
264  return m_selectors.getMaxSuffixComponents();
265  }
266 
267  Interest&
268  setMaxSuffixComponents(int maxSuffixComponents)
269  {
270  m_selectors.setMaxSuffixComponents(maxSuffixComponents);
271  m_wire.reset();
272  return *this;
273  }
274 
275  const KeyLocator&
277  {
278  return m_selectors.getPublisherPublicKeyLocator();
279  }
280 
281  Interest&
283  {
284  m_selectors.setPublisherPublicKeyLocator(keyLocator);
285  m_wire.reset();
286  return *this;
287  }
288 
289  const Exclude&
290  getExclude() const
291  {
292  return m_selectors.getExclude();
293  }
294 
295  Interest&
296  setExclude(const Exclude& exclude)
297  {
298  m_selectors.setExclude(exclude);
299  m_wire.reset();
300  return *this;
301  }
302 
303  int
305  {
306  return m_selectors.getChildSelector();
307  }
308 
309  Interest&
310  setChildSelector(int childSelector)
311  {
312  m_selectors.setChildSelector(childSelector);
313  m_wire.reset();
314  return *this;
315  }
316 
317  int
319  {
320  return m_selectors.getMustBeFresh();
321  }
322 
323  Interest&
324  setMustBeFresh(bool mustBeFresh)
325  {
326  m_selectors.setMustBeFresh(mustBeFresh);
327  m_wire.reset();
328  return *this;
329  }
330 
331 private:
332  Name m_name;
333  Selectors m_selectors;
334  mutable optional<uint32_t> m_nonce;
335  time::milliseconds m_interestLifetime;
336  DelegationList m_forwardingHint;
337 
338  mutable Block m_wire;
339 };
340 
342 
343 std::ostream&
344 operator<<(std::ostream& os, const Interest& interest);
345 
346 inline bool
347 operator==(const Interest& lhs, const Interest& rhs)
348 {
349  return lhs.wireEncode() == rhs.wireEncode();
350 }
351 
352 inline bool
353 operator!=(const Interest& lhs, const Interest& rhs)
354 {
355  return !(lhs == rhs);
356 }
357 
358 } // namespace ndn
359 
360 #endif // NDN_INTEREST_HPP
int getMinSuffixComponents() const
Definition: interest.hpp:248
int getMaxSuffixComponents() const
Definition: interest.hpp:262
const Name & getName() const
Definition: interest.hpp:139
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
Definition: interest.cpp:180
Interest & modifyForwardingHint(const Modifier &modifier)
modify ForwardingHint in-place
Definition: interest.hpp:216
Interest & setMustBeFresh(bool mustBeFresh)
Definition: interest.hpp:324
void refreshNonce()
Refresh nonce.
Definition: interest.cpp:318
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:274
bool hasWire() const
Check if already has wire.
Definition: interest.hpp:93
bool hasSelectors() const
Definition: interest.hpp:228
const Block & wireEncode() const
Encode to a wire format.
Definition: interest.cpp:103
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
represents an Interest packet
Definition: interest.hpp:42
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:259
std::string toUri() const
Encode the name according to the NDN URI Scheme.
Definition: interest.cpp:170
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
int getChildSelector() const
Definition: interest.hpp:304
uint32_t getNonce() const
Get nonce.
Definition: interest.cpp:301
Interest(const Name &name=Name(), time::milliseconds interestLifetime=DEFAULT_INTEREST_LIFETIME)
Create a new Interest with the given name and interest lifetime.
Definition: interest.cpp:38
const Selectors & getSelectors() const
Definition: interest.hpp:234
int getMustBeFresh() const
Definition: interest.hpp:318
Error(const std::string &what)
Definition: interest.hpp:49
Interest & setExclude(const Exclude &exclude)
Definition: interest.hpp:296
Interest & setChildSelector(int childSelector)
Definition: interest.hpp:310
Interest & setName(const Name &name)
Definition: interest.hpp:145
Interest & setNonce(uint32_t nonce)
Set nonce.
Definition: interest.cpp:310
Interest & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: interest.hpp:282
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: interest.cpp:56
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
const Exclude & getExclude() const
Definition: interest.hpp:290
Abstraction implementing Interest selectors.
Definition: selectors.hpp:36
Interest & setMinSuffixComponents(int minSuffixComponents)
Definition: interest.hpp:254
Interest & setMaxSuffixComponents(int maxSuffixComponents)
Definition: interest.hpp:268
Represents an absolute name.
Definition: name.hpp:42
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:207
Interest & setSelectors(const Selectors &selectors)
Definition: interest.hpp:240
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: interest.cpp:119
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
Definition: interest.cpp:291
const DelegationList & getForwardingHint() const
Definition: interest.hpp:196
Interest & setInterestLifetime(time::milliseconds interestLifetime)
Set Interest&#39;s lifetime.
Definition: interest.cpp:332
represents a list of Delegations
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:265
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:343
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: interest.hpp:276
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:38
time::milliseconds getInterestLifetime() const
Definition: interest.hpp:183
Represents a Data packet.
Definition: data.hpp:35
represents an error in TLV encoding or decoding
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:37
bool hasNonce() const
Check if Nonce set.
Definition: interest.hpp:155