blob: 0e07f81d07c92e52998809036f57aad73302fa67 [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070013 */
14
15#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070016#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -070017
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080018#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070019#include "name.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080020#include "selectors.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080021#include "management/nfd-local-control-header.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070022
23namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080024
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070025class Data;
26
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070027const time::seconds DEFAULT_INTEREST_LIFETIME = time::seconds(4);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080028
Jeff Thompson8238d002013-07-10 11:56:49 -070029/**
Jeff Thompson8238d002013-07-10 11:56:49 -070030 * An Interest holds a Name and other fields for an interest.
31 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -080032class Interest : public enable_shared_from_this<Interest>
33{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070034public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080035 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080036 * @brief Create a new Interest with an empty name and "none" for all values.
37 */
38 Interest()
39 : m_nonce(0)
40 , m_scope(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041 , m_interestLifetime(time::milliseconds::min())
Alexander Afanasyevc348f832014-02-17 16:35:17 -080042 {
43 }
44
45 /**
46 * @brief Create a new Interest with the given name and "none" for other values.
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047 *
Alexander Afanasyevc348f832014-02-17 16:35:17 -080048 * @param name The name for the interest.
49 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050 Interest(const Name& name)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080051 : m_name(name)
52 , m_nonce(0)
53 , m_scope(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070054 , m_interestLifetime(time::milliseconds::min())
Alexander Afanasyevc348f832014-02-17 16:35:17 -080055 {
56 }
57
58 /**
59 * Create a new Interest with the given name and interest lifetime and "none" for other values.
60 * @param name The name for the interest.
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 * @param interestLifetimeMilliseconds The interest lifetime in time::milliseconds, or -1 for none.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080062 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063 Interest(const Name& name, const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080064 : m_name(name)
65 , m_nonce(0)
66 , m_scope(-1)
67 , m_interestLifetime(interestLifetime)
68 {
69 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070
Alexander Afanasyevc348f832014-02-17 16:35:17 -080071 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 const Selectors& selectors,
Alexander Afanasyevc348f832014-02-17 16:35:17 -080073 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 const time::milliseconds& interestLifetime,
75 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080076 : m_name(name)
77 , m_selectors(selectors)
78 , m_nonce(nonce)
79 , m_scope(scope)
80 , m_interestLifetime(interestLifetime)
81 {
82 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083
Alexander Afanasyevc348f832014-02-17 16:35:17 -080084 /**
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080085 * Create a new Interest for the given name and values.
86 * @param name
87 * @param minSuffixComponents
88 * @param maxSuffixComponents
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080089 * @param exclude
90 * @param childSelector
Alexander Afanasyev84681982014-01-03 13:26:09 -080091 * @param mustBeFresh
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080092 * @param scope
Alexander Afanasyev84681982014-01-03 13:26:09 -080093 * @param interestLifetime
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080094 * @param nonce
Junxiao Shib332e782014-03-31 14:23:46 -070095 *
96 * @deprecated Interest().setX(...).setY(...)
97 * or use the overload taking Selectors
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080098 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080099 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 int minSuffixComponents, int maxSuffixComponents,
Alexander Afanasyev84681982014-01-03 13:26:09 -0800101 const Exclude& exclude,
102 int childSelector,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700103 bool mustBeFresh,
Alexander Afanasyev84681982014-01-03 13:26:09 -0800104 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105 const time::milliseconds& interestLifetime,
106 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800107 : m_name(name)
108 , m_selectors(minSuffixComponents, maxSuffixComponents, exclude, childSelector, mustBeFresh)
109 , m_nonce(nonce)
110 , m_scope(scope)
111 , m_interestLifetime(interestLifetime)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700112 {
113 }
114
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115 /**
116 * @brief Create from wire encoding
117 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800118 explicit
119 Interest(const Block& wire)
120 {
121 wireDecode(wire);
122 }
123
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800124 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800125 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800126 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800127 template<bool T>
128 inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700129 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800130
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800131 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800132 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800133 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800134 inline const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800135 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800136
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800137 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800138 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800139 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700140 inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700141 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800142
143 /**
144 * @brief Check if already has wire
145 */
146 inline bool
147 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700149 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -0800150 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
151 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
152 * @return The URI string.
153 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800154 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800155 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700156
Alexander Afanasyev84681982014-01-03 13:26:09 -0800157 inline bool
158 hasSelectors() const;
159
160 inline bool
161 hasGuiders() const;
162
163 /**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700164 * @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 -0800165 * interest selectors.
166 * @param self A pointer to the ndn_Interest struct.
167 * @param name A pointer to the name to check.
168 * @return 1 if the name and interest selectors match, 0 otherwise.
169 */
170 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700171 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800172
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700173 /** @brief Determines whether this Interest can be satisfied by @p data.
174 *
175 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
176 * PublisherPublicKeyLocator, and Exclude.
177 * This method does not consider ChildSelector and MustBeFresh.
178 *
179 * @todo recognize implicit digest component
180 */
181 bool
182 matchesData(const Data& data) const;
183
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800184 ///////////////////////////////////////////////////////////////////////////////
185 ///////////////////////////////////////////////////////////////////////////////
186 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800187 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700188
189 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800190 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700191 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800192 return m_name;
193 }
194
195 Interest&
196 setName(const Name& name)
197 {
198 m_name = name;
199 m_wire.reset();
200 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700201 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700202
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800203 //
Alexander Afanasyev84681982014-01-03 13:26:09 -0800204
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800205 const Selectors&
206 getSelectors() const
207 {
208 return m_selectors;
209 }
210
211 Interest&
212 setSelectors(const Selectors& selectors)
213 {
214 m_selectors = selectors;
215 m_wire.reset();
216 return *this;
217 }
218
219 //
220
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700221 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800222 getScope() const
223 {
224 return m_scope;
225 }
226
227 Interest&
228 setScope(int scope)
229 {
230 m_scope = scope;
231 m_wire.reset();
232 return *this;
233 }
234
235 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700236
237 const time::milliseconds&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800238 getInterestLifetime() const
239 {
240 return m_interestLifetime;
241 }
242
243 Interest&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700244 setInterestLifetime(const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800245 {
246 m_interestLifetime = interestLifetime;
247 m_wire.reset();
248 return *this;
249 }
250
251 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700252
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800253 /**
254 * @brief Get Interest's nonce
255 *
256 * If nonce was not set before this call, it will be automatically assigned to a random value
257 *
258 * Const reference needed for C decoding
259 */
260 const uint32_t&
261 getNonce() const;
262
263 Interest&
264 setNonce(uint32_t nonce)
265 {
266 m_nonce = nonce;
267 m_wire.reset();
268 return *this;
269 }
270
271 //
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800272
273 nfd::LocalControlHeader&
274 getLocalControlHeader()
275 {
276 return m_localControlHeader;
277 }
278
279 const nfd::LocalControlHeader&
280 getLocalControlHeader() const
281 {
282 return m_localControlHeader;
283 }
284
285 // helper methods for LocalControlHeader
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700286
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800287 uint64_t
288 getIncomingFaceId() const
289 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800290 return getLocalControlHeader().getIncomingFaceId();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800291 }
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800292
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800293 Interest&
294 setIncomingFaceId(uint64_t incomingFaceId)
295 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800296 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800297 // ! do not reset Interest's wire !
298 return *this;
299 }
300
301 //
302
303 // NextHopFaceId helpers make sense only for Interests
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700304
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800305 uint64_t
306 getNextHopFaceId() const
307 {
308 return getLocalControlHeader().getNextHopFaceId();
309 }
310
311 Interest&
312 setNextHopFaceId(uint64_t nextHopFaceId)
313 {
314 getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
315 // ! do not reset Interest's wire !
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800316 return *this;
317 }
318
319 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700320
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800321 ///////////////////////////////////////////////////////////////////////////////
322 ///////////////////////////////////////////////////////////////////////////////
323 ///////////////////////////////////////////////////////////////////////////////
324 // Wrappers for Selectors
325 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700326
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800327 int
328 getMinSuffixComponents() const
329 {
330 return m_selectors.getMinSuffixComponents();
331 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700332
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800333 Interest&
334 setMinSuffixComponents(int minSuffixComponents)
335 {
336 m_selectors.setMinSuffixComponents(minSuffixComponents);
337 m_wire.reset();
338 return *this;
339 }
340
341 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700342
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800343 int
344 getMaxSuffixComponents() const
345 {
346 return m_selectors.getMaxSuffixComponents();
347 }
348
349 Interest&
350 setMaxSuffixComponents(int maxSuffixComponents)
351 {
352 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
353 m_wire.reset();
354 return *this;
355 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700356
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800357 //
358
Junxiao Shib332e782014-03-31 14:23:46 -0700359 const KeyLocator&
360 getPublisherPublicKeyLocator() const
361 {
362 return m_selectors.getPublisherPublicKeyLocator();
363 }
364
365 Interest&
366 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
367 {
368 m_selectors.setPublisherPublicKeyLocator(keyLocator);
369 m_wire.reset();
370 return *this;
371 }
372
373 //
374
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800375 const Exclude&
376 getExclude() const
377 {
378 return m_selectors.getExclude();
379 }
380
381 Interest&
382 setExclude(const Exclude& exclude)
383 {
384 m_selectors.setExclude(exclude);
385 m_wire.reset();
386 return *this;
387 }
388
389 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700390
391 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800392 getChildSelector() const
393 {
394 return m_selectors.getChildSelector();
395 }
396
397 Interest&
398 setChildSelector(int childSelector)
399 {
400 m_selectors.setChildSelector(childSelector);
401 m_wire.reset();
402 return *this;
403 }
404
405 //
406
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700407 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800408 getMustBeFresh() const
409 {
410 return m_selectors.getMustBeFresh();
411 }
412
413 Interest&
414 setMustBeFresh(bool mustBeFresh)
415 {
416 m_selectors.setMustBeFresh(mustBeFresh);
417 m_wire.reset();
418 return *this;
419 }
420
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700421public: // EqualityComparable concept
422 bool
423 operator==(const Interest& other) const
424 {
425 return wireEncode() == other.wireEncode();
426 }
427
428 bool
429 operator!=(const Interest& other) const
430 {
431 return !(*this == other);
432 }
433
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800434private:
435 Name m_name;
436 Selectors m_selectors;
437 mutable uint32_t m_nonce;
438 int m_scope;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700439 time::milliseconds m_interestLifetime;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800440
441 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800442
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800443 nfd::LocalControlHeader m_localControlHeader;
444 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700445};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800446
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700447std::ostream&
448operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800449
450inline std::string
451Interest::toUri() const
452{
453 std::ostringstream os;
454 os << *this;
455 return os.str();
456}
457
458inline bool
459Interest::hasSelectors() const
460{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800461 return !m_selectors.empty();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800462}
463
464inline bool
465Interest::hasGuiders() const
466{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800467 return m_scope >= 0 ||
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700468 m_interestLifetime >= time::milliseconds::zero() ||
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800469 m_nonce > 0;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800470}
471
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800472template<bool T>
473inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700474Interest::wireEncode(EncodingImpl<T>& block) const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800475{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700476 size_t totalLength = 0;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800477
478 // Interest ::= INTEREST-TYPE TLV-LENGTH
479 // Name
480 // Selectors?
481 // Nonce
482 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700483 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800484
485 // (reverse encoding)
486
487 // InterestLifetime
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700488 if (getInterestLifetime() >= time::milliseconds::zero() &&
489 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800490 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700491 totalLength += prependNonNegativeIntegerBlock(block,
492 Tlv::InterestLifetime,
493 getInterestLifetime().count());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800494 }
495
496 // Scope
497 if (getScope() >= 0)
498 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700499 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800500 }
501
502 // Nonce
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700503 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800504
505 // Selectors
506 if (!getSelectors().empty())
507 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700508 totalLength += getSelectors().wireEncode(block);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800509 }
510
511 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700512 totalLength += getName().wireEncode(block);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700513
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700514 totalLength += block.prependVarNumber (totalLength);
515 totalLength += block.prependVarNumber (Tlv::Interest);
516 return totalLength;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700517}
518
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800519inline const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800520Interest::wireEncode() const
521{
522 if (m_wire.hasWire())
523 return m_wire;
524
525 EncodingEstimator estimator;
526 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700527
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800528 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800529 wireEncode(buffer);
530
531 m_wire = buffer.block();
532 return m_wire;
533}
534
535inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700536Interest::wireDecode(const Block& wire)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800537{
538 m_wire = wire;
539 m_wire.parse();
540
541 // Interest ::= INTEREST-TYPE TLV-LENGTH
542 // Name
543 // Selectors?
544 // Nonce
545 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700546 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800547
548 if (m_wire.type() != Tlv::Interest)
549 throw Tlv::Error("Unexpected TLV number when decoding Interest");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700550
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800551 // Name
552 m_name.wireDecode(m_wire.get(Tlv::Name));
553
554 // Selectors
555 Block::element_const_iterator val = m_wire.find(Tlv::Selectors);
556 if (val != m_wire.elements_end())
557 {
558 m_selectors.wireDecode(*val);
559 }
560 else
561 m_selectors = Selectors();
562
563 // Nonce
564 val = m_wire.find(Tlv::Nonce);
565 if (val != m_wire.elements_end())
566 {
567 m_nonce = readNonNegativeInteger(*val);
568 }
569 else
570 m_nonce = 0;
571
572 // Scope
573 val = m_wire.find(Tlv::Scope);
574 if (val != m_wire.elements_end())
575 {
576 m_scope = readNonNegativeInteger(*val);
577 }
578 else
579 m_scope = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700580
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800581 // InterestLifetime
582 val = m_wire.find(Tlv::InterestLifetime);
583 if (val != m_wire.elements_end())
584 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700585 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800586 }
587 else
588 {
589 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
590 }
591}
592
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800593inline bool
594Interest::hasWire() const
595{
596 return m_wire.hasWire();
597}
598
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800599
600} // namespace ndn
601
602#endif // NDN_INTEREST_HPP