blob: 2e2b8afa7f4de9b1194d4a5d97149fad011f6318 [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 Afanasyev6d48bc12014-02-18 00:10:51 -0800107 explicit
108 Interest(const Block& wire)
109 {
110 wireDecode(wire);
111 }
112
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800113 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800114 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800115 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800116 template<bool T>
117 inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700118 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800119
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800120 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800121 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800122 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800123 inline const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800124 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800125
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800126 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800127 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800128 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700129 inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700130 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800131
132 /**
133 * @brief Check if already has wire
134 */
135 inline bool
136 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700137
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700138 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -0800139 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
140 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
141 * @return The URI string.
142 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800143 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800144 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700145
Alexander Afanasyev84681982014-01-03 13:26:09 -0800146 inline bool
147 hasSelectors() const;
148
149 inline bool
150 hasGuiders() const;
151
152 /**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153 * @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 -0800154 * interest selectors.
155 * @param self A pointer to the ndn_Interest struct.
156 * @param name A pointer to the name to check.
157 * @return 1 if the name and interest selectors match, 0 otherwise.
158 */
159 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700160 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800161
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700162 /** @brief Determines whether this Interest can be satisfied by @p data.
163 *
164 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
165 * PublisherPublicKeyLocator, and Exclude.
166 * This method does not consider ChildSelector and MustBeFresh.
167 *
168 * @todo recognize implicit digest component
169 */
170 bool
171 matchesData(const Data& data) const;
172
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800173 ///////////////////////////////////////////////////////////////////////////////
174 ///////////////////////////////////////////////////////////////////////////////
175 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800176 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700177
178 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800179 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700180 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800181 return m_name;
182 }
183
184 Interest&
185 setName(const Name& name)
186 {
187 m_name = name;
188 m_wire.reset();
189 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700190 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700191
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800192 //
Alexander Afanasyev84681982014-01-03 13:26:09 -0800193
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800194 const Selectors&
195 getSelectors() const
196 {
197 return m_selectors;
198 }
199
200 Interest&
201 setSelectors(const Selectors& selectors)
202 {
203 m_selectors = selectors;
204 m_wire.reset();
205 return *this;
206 }
207
208 //
209
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700210 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800211 getScope() const
212 {
213 return m_scope;
214 }
215
216 Interest&
217 setScope(int scope)
218 {
219 m_scope = scope;
220 m_wire.reset();
221 return *this;
222 }
223
224 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700225
226 const time::milliseconds&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800227 getInterestLifetime() const
228 {
229 return m_interestLifetime;
230 }
231
232 Interest&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700233 setInterestLifetime(const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800234 {
235 m_interestLifetime = interestLifetime;
236 m_wire.reset();
237 return *this;
238 }
239
240 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700241
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800242 /**
243 * @brief Get Interest's nonce
244 *
245 * If nonce was not set before this call, it will be automatically assigned to a random value
246 *
247 * Const reference needed for C decoding
248 */
249 const uint32_t&
250 getNonce() const;
251
252 Interest&
253 setNonce(uint32_t nonce)
254 {
255 m_nonce = nonce;
256 m_wire.reset();
257 return *this;
258 }
259
260 //
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800261
262 nfd::LocalControlHeader&
263 getLocalControlHeader()
264 {
265 return m_localControlHeader;
266 }
267
268 const nfd::LocalControlHeader&
269 getLocalControlHeader() const
270 {
271 return m_localControlHeader;
272 }
273
274 // helper methods for LocalControlHeader
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700275
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800276 uint64_t
277 getIncomingFaceId() const
278 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800279 return getLocalControlHeader().getIncomingFaceId();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800280 }
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800281
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800282 Interest&
283 setIncomingFaceId(uint64_t incomingFaceId)
284 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800285 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800286 // ! do not reset Interest's wire !
287 return *this;
288 }
289
290 //
291
292 // NextHopFaceId helpers make sense only for Interests
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700293
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800294 uint64_t
295 getNextHopFaceId() const
296 {
297 return getLocalControlHeader().getNextHopFaceId();
298 }
299
300 Interest&
301 setNextHopFaceId(uint64_t nextHopFaceId)
302 {
303 getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
304 // ! do not reset Interest's wire !
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800305 return *this;
306 }
307
308 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700309
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800310 ///////////////////////////////////////////////////////////////////////////////
311 ///////////////////////////////////////////////////////////////////////////////
312 ///////////////////////////////////////////////////////////////////////////////
313 // Wrappers for Selectors
314 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700315
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800316 int
317 getMinSuffixComponents() const
318 {
319 return m_selectors.getMinSuffixComponents();
320 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700321
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800322 Interest&
323 setMinSuffixComponents(int minSuffixComponents)
324 {
325 m_selectors.setMinSuffixComponents(minSuffixComponents);
326 m_wire.reset();
327 return *this;
328 }
329
330 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700331
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800332 int
333 getMaxSuffixComponents() const
334 {
335 return m_selectors.getMaxSuffixComponents();
336 }
337
338 Interest&
339 setMaxSuffixComponents(int maxSuffixComponents)
340 {
341 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
342 m_wire.reset();
343 return *this;
344 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700345
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800346 //
347
Junxiao Shib332e782014-03-31 14:23:46 -0700348 const KeyLocator&
349 getPublisherPublicKeyLocator() const
350 {
351 return m_selectors.getPublisherPublicKeyLocator();
352 }
353
354 Interest&
355 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
356 {
357 m_selectors.setPublisherPublicKeyLocator(keyLocator);
358 m_wire.reset();
359 return *this;
360 }
361
362 //
363
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800364 const Exclude&
365 getExclude() const
366 {
367 return m_selectors.getExclude();
368 }
369
370 Interest&
371 setExclude(const Exclude& exclude)
372 {
373 m_selectors.setExclude(exclude);
374 m_wire.reset();
375 return *this;
376 }
377
378 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700379
380 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800381 getChildSelector() const
382 {
383 return m_selectors.getChildSelector();
384 }
385
386 Interest&
387 setChildSelector(int childSelector)
388 {
389 m_selectors.setChildSelector(childSelector);
390 m_wire.reset();
391 return *this;
392 }
393
394 //
395
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700396 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800397 getMustBeFresh() const
398 {
399 return m_selectors.getMustBeFresh();
400 }
401
402 Interest&
403 setMustBeFresh(bool mustBeFresh)
404 {
405 m_selectors.setMustBeFresh(mustBeFresh);
406 m_wire.reset();
407 return *this;
408 }
409
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700410public: // EqualityComparable concept
411 bool
412 operator==(const Interest& other) const
413 {
414 return wireEncode() == other.wireEncode();
415 }
416
417 bool
418 operator!=(const Interest& other) const
419 {
420 return !(*this == other);
421 }
422
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800423private:
424 Name m_name;
425 Selectors m_selectors;
426 mutable uint32_t m_nonce;
427 int m_scope;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700428 time::milliseconds m_interestLifetime;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800429
430 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800431
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800432 nfd::LocalControlHeader m_localControlHeader;
433 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700434};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800435
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700436std::ostream&
437operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800438
439inline std::string
440Interest::toUri() const
441{
442 std::ostringstream os;
443 os << *this;
444 return os.str();
445}
446
447inline bool
448Interest::hasSelectors() const
449{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800450 return !m_selectors.empty();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800451}
452
453inline bool
454Interest::hasGuiders() const
455{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800456 return m_scope >= 0 ||
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700457 m_interestLifetime >= time::milliseconds::zero() ||
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800458 m_nonce > 0;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800459}
460
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800461template<bool T>
462inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700463Interest::wireEncode(EncodingImpl<T>& block) const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800464{
465 size_t total_len = 0;
466
467 // Interest ::= INTEREST-TYPE TLV-LENGTH
468 // Name
469 // Selectors?
470 // Nonce
471 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700472 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800473
474 // (reverse encoding)
475
476 // InterestLifetime
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700477 if (getInterestLifetime() >= time::milliseconds::zero()
478 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800479 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700480 total_len += prependNonNegativeIntegerBlock(block, Tlv::InterestLifetime, getInterestLifetime().count());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800481 }
482
483 // Scope
484 if (getScope() >= 0)
485 {
486 total_len += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
487 }
488
489 // Nonce
490 total_len += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
491
492 // Selectors
493 if (!getSelectors().empty())
494 {
495 total_len += getSelectors().wireEncode(block);
496 }
497
498 // Name
499 total_len += getName().wireEncode(block);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700500
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800501 total_len += block.prependVarNumber (total_len);
502 total_len += block.prependVarNumber (Tlv::Interest);
503 return total_len;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700504}
505
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800506inline const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800507Interest::wireEncode() const
508{
509 if (m_wire.hasWire())
510 return m_wire;
511
512 EncodingEstimator estimator;
513 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700514
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800515 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800516 wireEncode(buffer);
517
518 m_wire = buffer.block();
519 return m_wire;
520}
521
522inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700523Interest::wireDecode(const Block& wire)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800524{
525 m_wire = wire;
526 m_wire.parse();
527
528 // Interest ::= INTEREST-TYPE TLV-LENGTH
529 // Name
530 // Selectors?
531 // Nonce
532 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700533 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800534
535 if (m_wire.type() != Tlv::Interest)
536 throw Tlv::Error("Unexpected TLV number when decoding Interest");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700537
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800538 // Name
539 m_name.wireDecode(m_wire.get(Tlv::Name));
540
541 // Selectors
542 Block::element_const_iterator val = m_wire.find(Tlv::Selectors);
543 if (val != m_wire.elements_end())
544 {
545 m_selectors.wireDecode(*val);
546 }
547 else
548 m_selectors = Selectors();
549
550 // Nonce
551 val = m_wire.find(Tlv::Nonce);
552 if (val != m_wire.elements_end())
553 {
554 m_nonce = readNonNegativeInteger(*val);
555 }
556 else
557 m_nonce = 0;
558
559 // Scope
560 val = m_wire.find(Tlv::Scope);
561 if (val != m_wire.elements_end())
562 {
563 m_scope = readNonNegativeInteger(*val);
564 }
565 else
566 m_scope = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700567
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800568 // InterestLifetime
569 val = m_wire.find(Tlv::InterestLifetime);
570 if (val != m_wire.elements_end())
571 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700572 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800573 }
574 else
575 {
576 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
577 }
578}
579
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800580inline bool
581Interest::hasWire() const
582{
583 return m_wire.hasWire();
584}
585
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800586
587} // namespace ndn
588
589#endif // NDN_INTEREST_HPP