blob: 3cfacf1cd43534f92c1cfce11e2c722c0c5643eb [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07005 */
6
7#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -07009
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080010#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "name.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080012#include "selectors.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080013#include "management/nfd-local-control-header.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070014
15namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080016
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070017class Data;
18
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070019const time::seconds DEFAULT_INTEREST_LIFETIME = time::seconds(4);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080020
Jeff Thompson8238d002013-07-10 11:56:49 -070021/**
Jeff Thompson8238d002013-07-10 11:56:49 -070022 * An Interest holds a Name and other fields for an interest.
23 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -080024class Interest : public enable_shared_from_this<Interest>
25{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070026public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080027 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080028 * @brief Create a new Interest with an empty name and "none" for all values.
29 */
30 Interest()
31 : m_nonce(0)
32 , m_scope(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033 , m_interestLifetime(time::milliseconds::min())
Alexander Afanasyevc348f832014-02-17 16:35:17 -080034 {
35 }
36
37 /**
38 * @brief Create a new Interest with the given name and "none" for other values.
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 *
Alexander Afanasyevc348f832014-02-17 16:35:17 -080040 * @param name The name for the interest.
41 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 Interest(const Name& name)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080043 : m_name(name)
44 , m_nonce(0)
45 , m_scope(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046 , m_interestLifetime(time::milliseconds::min())
Alexander Afanasyevc348f832014-02-17 16:35:17 -080047 {
48 }
49
50 /**
51 * Create a new Interest with the given name and interest lifetime and "none" for other values.
52 * @param name The name for the interest.
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 * @param interestLifetimeMilliseconds The interest lifetime in time::milliseconds, or -1 for none.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080054 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070055 Interest(const Name& name, const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080056 : m_name(name)
57 , m_nonce(0)
58 , m_scope(-1)
59 , m_interestLifetime(interestLifetime)
60 {
61 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062
Alexander Afanasyevc348f832014-02-17 16:35:17 -080063 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064 const Selectors& selectors,
Alexander Afanasyevc348f832014-02-17 16:35:17 -080065 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066 const time::milliseconds& interestLifetime,
67 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080068 : m_name(name)
69 , m_selectors(selectors)
70 , m_nonce(nonce)
71 , m_scope(scope)
72 , m_interestLifetime(interestLifetime)
73 {
74 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075
Alexander Afanasyevc348f832014-02-17 16:35:17 -080076 /**
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080077 * Create a new Interest for the given name and values.
78 * @param name
79 * @param minSuffixComponents
80 * @param maxSuffixComponents
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080081 * @param exclude
82 * @param childSelector
Alexander Afanasyev84681982014-01-03 13:26:09 -080083 * @param mustBeFresh
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080084 * @param scope
Alexander Afanasyev84681982014-01-03 13:26:09 -080085 * @param interestLifetime
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080086 * @param nonce
Junxiao Shib332e782014-03-31 14:23:46 -070087 *
88 * @deprecated Interest().setX(...).setY(...)
89 * or use the overload taking Selectors
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080090 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080091 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070092 int minSuffixComponents, int maxSuffixComponents,
Alexander Afanasyev84681982014-01-03 13:26:09 -080093 const Exclude& exclude,
94 int childSelector,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095 bool mustBeFresh,
Alexander Afanasyev84681982014-01-03 13:26:09 -080096 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097 const time::milliseconds& interestLifetime,
98 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080099 : m_name(name)
100 , m_selectors(minSuffixComponents, maxSuffixComponents, exclude, childSelector, mustBeFresh)
101 , m_nonce(nonce)
102 , m_scope(scope)
103 , m_interestLifetime(interestLifetime)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700104 {
105 }
106
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 /**
108 * @brief Create from wire encoding
109 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800110 explicit
111 Interest(const Block& wire)
112 {
113 wireDecode(wire);
114 }
115
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800116 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800117 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800118 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800119 template<bool T>
120 inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700121 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800122
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800123 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800124 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800125 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800126 inline const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800127 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800128
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800129 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800130 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800131 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132 inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700133 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800134
135 /**
136 * @brief Check if already has wire
137 */
138 inline bool
139 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700140
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700141 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -0800142 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
143 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
144 * @return The URI string.
145 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800146 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800147 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700148
Alexander Afanasyev84681982014-01-03 13:26:09 -0800149 inline bool
150 hasSelectors() const;
151
152 inline bool
153 hasGuiders() const;
154
155 /**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700156 * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the
Alexander Afanasyev84681982014-01-03 13:26:09 -0800157 * interest selectors.
158 * @param self A pointer to the ndn_Interest struct.
159 * @param name A pointer to the name to check.
160 * @return 1 if the name and interest selectors match, 0 otherwise.
161 */
162 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700163 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800164
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700165 /** @brief Determines whether this Interest can be satisfied by @p data.
166 *
167 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
168 * PublisherPublicKeyLocator, and Exclude.
169 * This method does not consider ChildSelector and MustBeFresh.
170 *
171 * @todo recognize implicit digest component
172 */
173 bool
174 matchesData(const Data& data) const;
175
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800176 ///////////////////////////////////////////////////////////////////////////////
177 ///////////////////////////////////////////////////////////////////////////////
178 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800179 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700180
181 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800182 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700183 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800184 return m_name;
185 }
186
187 Interest&
188 setName(const Name& name)
189 {
190 m_name = name;
191 m_wire.reset();
192 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700193 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700194
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800195 //
Alexander Afanasyev84681982014-01-03 13:26:09 -0800196
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800197 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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700213 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800214 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 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700228
229 const time::milliseconds&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800230 getInterestLifetime() const
231 {
232 return m_interestLifetime;
233 }
234
235 Interest&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700236 setInterestLifetime(const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800237 {
238 m_interestLifetime = interestLifetime;
239 m_wire.reset();
240 return *this;
241 }
242
243 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700244
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800245 /**
246 * @brief Get Interest's nonce
247 *
248 * If nonce was not set before this call, it will be automatically assigned to a random value
249 *
250 * Const reference needed for C decoding
251 */
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 //
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800264
265 nfd::LocalControlHeader&
266 getLocalControlHeader()
267 {
268 return m_localControlHeader;
269 }
270
271 const nfd::LocalControlHeader&
272 getLocalControlHeader() const
273 {
274 return m_localControlHeader;
275 }
276
277 // helper methods for LocalControlHeader
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700278
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800279 uint64_t
280 getIncomingFaceId() const
281 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800282 return getLocalControlHeader().getIncomingFaceId();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800283 }
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800284
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800285 Interest&
286 setIncomingFaceId(uint64_t incomingFaceId)
287 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800288 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800289 // ! do not reset Interest's wire !
290 return *this;
291 }
292
293 //
294
295 // NextHopFaceId helpers make sense only for Interests
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700296
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800297 uint64_t
298 getNextHopFaceId() const
299 {
300 return getLocalControlHeader().getNextHopFaceId();
301 }
302
303 Interest&
304 setNextHopFaceId(uint64_t nextHopFaceId)
305 {
306 getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
307 // ! do not reset Interest's wire !
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800308 return *this;
309 }
310
311 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700312
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800313 ///////////////////////////////////////////////////////////////////////////////
314 ///////////////////////////////////////////////////////////////////////////////
315 ///////////////////////////////////////////////////////////////////////////////
316 // Wrappers for Selectors
317 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700318
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800319 int
320 getMinSuffixComponents() const
321 {
322 return m_selectors.getMinSuffixComponents();
323 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700324
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800325 Interest&
326 setMinSuffixComponents(int minSuffixComponents)
327 {
328 m_selectors.setMinSuffixComponents(minSuffixComponents);
329 m_wire.reset();
330 return *this;
331 }
332
333 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700334
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800335 int
336 getMaxSuffixComponents() const
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 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700348
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800349 //
350
Junxiao Shib332e782014-03-31 14:23:46 -0700351 const KeyLocator&
352 getPublisherPublicKeyLocator() const
353 {
354 return m_selectors.getPublisherPublicKeyLocator();
355 }
356
357 Interest&
358 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
359 {
360 m_selectors.setPublisherPublicKeyLocator(keyLocator);
361 m_wire.reset();
362 return *this;
363 }
364
365 //
366
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800367 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 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700382
383 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800384 getChildSelector() const
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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700399 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800400 getMustBeFresh() const
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
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700413public: // 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
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800426private:
427 Name m_name;
428 Selectors m_selectors;
429 mutable uint32_t m_nonce;
430 int m_scope;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700431 time::milliseconds m_interestLifetime;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800432
433 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800434
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800435 nfd::LocalControlHeader m_localControlHeader;
436 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700437};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800438
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700439std::ostream&
440operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800441
442inline std::string
443Interest::toUri() const
444{
445 std::ostringstream os;
446 os << *this;
447 return os.str();
448}
449
450inline bool
451Interest::hasSelectors() const
452{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800453 return !m_selectors.empty();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800454}
455
456inline bool
457Interest::hasGuiders() const
458{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800459 return m_scope >= 0 ||
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700460 m_interestLifetime >= time::milliseconds::zero() ||
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800461 m_nonce > 0;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800462}
463
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800464template<bool T>
465inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700466Interest::wireEncode(EncodingImpl<T>& block) const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800467{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700468 size_t totalLength = 0;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800469
470 // Interest ::= INTEREST-TYPE TLV-LENGTH
471 // Name
472 // Selectors?
473 // Nonce
474 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700475 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800476
477 // (reverse encoding)
478
479 // InterestLifetime
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700480 if (getInterestLifetime() >= time::milliseconds::zero() &&
481 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800482 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700483 totalLength += prependNonNegativeIntegerBlock(block,
484 Tlv::InterestLifetime,
485 getInterestLifetime().count());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800486 }
487
488 // Scope
489 if (getScope() >= 0)
490 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700491 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800492 }
493
494 // Nonce
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700495 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800496
497 // Selectors
498 if (!getSelectors().empty())
499 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700500 totalLength += getSelectors().wireEncode(block);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800501 }
502
503 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700504 totalLength += getName().wireEncode(block);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700505
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700506 totalLength += block.prependVarNumber (totalLength);
507 totalLength += block.prependVarNumber (Tlv::Interest);
508 return totalLength;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700509}
510
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800511inline const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800512Interest::wireEncode() const
513{
514 if (m_wire.hasWire())
515 return m_wire;
516
517 EncodingEstimator estimator;
518 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700519
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800520 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800521 wireEncode(buffer);
522
523 m_wire = buffer.block();
524 return m_wire;
525}
526
527inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700528Interest::wireDecode(const Block& wire)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800529{
530 m_wire = wire;
531 m_wire.parse();
532
533 // Interest ::= INTEREST-TYPE TLV-LENGTH
534 // Name
535 // Selectors?
536 // Nonce
537 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700538 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800539
540 if (m_wire.type() != Tlv::Interest)
541 throw Tlv::Error("Unexpected TLV number when decoding Interest");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700542
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800543 // Name
544 m_name.wireDecode(m_wire.get(Tlv::Name));
545
546 // Selectors
547 Block::element_const_iterator val = m_wire.find(Tlv::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;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700572
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800573 // InterestLifetime
574 val = m_wire.find(Tlv::InterestLifetime);
575 if (val != m_wire.elements_end())
576 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700577 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800578 }
579 else
580 {
581 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
582 }
583}
584
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800585inline bool
586Interest::hasWire() const
587{
588 return m_wire.hasWire();
589}
590
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800591
592} // namespace ndn
593
594#endif // NDN_INTEREST_HPP