blob: d67579ed0087e6963c98b15d1eb869f81bf98138 [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 Afanasyev90164962014-03-06 08:29:59 +000021#include "interest-filter.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080022#include "management/nfd-local-control-header.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070023
24namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080025
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070026class Data;
27
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070028const time::seconds DEFAULT_INTEREST_LIFETIME = time::seconds(4);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080029
Jeff Thompson8238d002013-07-10 11:56:49 -070030/**
Jeff Thompson8238d002013-07-10 11:56:49 -070031 * An Interest holds a Name and other fields for an interest.
32 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -080033class Interest : public enable_shared_from_this<Interest>
34{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080036 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080037 * @brief Create a new Interest with an empty name and "none" for all values.
38 */
39 Interest()
40 : m_nonce(0)
41 , m_scope(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 , m_interestLifetime(time::milliseconds::min())
Alexander Afanasyevc348f832014-02-17 16:35:17 -080043 {
44 }
45
46 /**
47 * @brief Create a new Interest with the given name and "none" for other values.
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048 *
Alexander Afanasyevc348f832014-02-17 16:35:17 -080049 * @param name The name for the interest.
50 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051 Interest(const Name& name)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080052 : m_name(name)
53 , m_nonce(0)
54 , m_scope(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070055 , m_interestLifetime(time::milliseconds::min())
Alexander Afanasyevc348f832014-02-17 16:35:17 -080056 {
57 }
58
59 /**
60 * Create a new Interest with the given name and interest lifetime and "none" for other values.
61 * @param name The name for the interest.
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062 * @param interestLifetimeMilliseconds The interest lifetime in time::milliseconds, or -1 for none.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080063 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064 Interest(const Name& name, const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080065 : m_name(name)
66 , m_nonce(0)
67 , m_scope(-1)
68 , m_interestLifetime(interestLifetime)
69 {
70 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071
Alexander Afanasyevc348f832014-02-17 16:35:17 -080072 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 const Selectors& selectors,
Alexander Afanasyevc348f832014-02-17 16:35:17 -080074 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075 const time::milliseconds& interestLifetime,
76 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080077 : m_name(name)
78 , m_selectors(selectors)
79 , m_nonce(nonce)
80 , m_scope(scope)
81 , m_interestLifetime(interestLifetime)
82 {
83 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084
Alexander Afanasyevc348f832014-02-17 16:35:17 -080085 /**
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080086 * Create a new Interest for the given name and values.
87 * @param name
88 * @param minSuffixComponents
89 * @param maxSuffixComponents
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080090 * @param exclude
91 * @param childSelector
Alexander Afanasyev84681982014-01-03 13:26:09 -080092 * @param mustBeFresh
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080093 * @param scope
Alexander Afanasyev84681982014-01-03 13:26:09 -080094 * @param interestLifetime
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080095 * @param nonce
Junxiao Shib332e782014-03-31 14:23:46 -070096 *
97 * @deprecated Interest().setX(...).setY(...)
98 * or use the overload taking Selectors
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080099 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800100 Interest(const Name& name,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101 int minSuffixComponents, int maxSuffixComponents,
Alexander Afanasyev84681982014-01-03 13:26:09 -0800102 const Exclude& exclude,
103 int childSelector,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700104 bool mustBeFresh,
Alexander Afanasyev84681982014-01-03 13:26:09 -0800105 int scope,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700106 const time::milliseconds& interestLifetime,
107 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800108 : m_name(name)
109 , m_selectors(minSuffixComponents, maxSuffixComponents, exclude, childSelector, mustBeFresh)
110 , m_nonce(nonce)
111 , m_scope(scope)
112 , m_interestLifetime(interestLifetime)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700113 {
114 }
115
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116 /**
117 * @brief Create from wire encoding
118 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800119 explicit
120 Interest(const Block& wire)
121 {
122 wireDecode(wire);
123 }
124
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800125 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800126 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800127 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800128 template<bool T>
129 inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700130 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800131
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800132 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800133 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800134 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800135 inline const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800136 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800137
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800138 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800139 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800140 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700141 inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700142 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800143
144 /**
145 * @brief Check if already has wire
146 */
147 inline bool
148 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700149
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700150 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -0800151 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
152 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
153 * @return The URI string.
154 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800155 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800156 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700157
Alexander Afanasyev84681982014-01-03 13:26:09 -0800158 inline bool
159 hasSelectors() const;
160
161 inline bool
162 hasGuiders() const;
163
164 /**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700165 * @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 -0800166 * interest selectors.
167 * @param self A pointer to the ndn_Interest struct.
168 * @param name A pointer to the name to check.
169 * @return 1 if the name and interest selectors match, 0 otherwise.
170 */
171 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700172 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800173
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700174 /** @brief Determines whether this Interest can be satisfied by @p data.
175 *
176 * This method considers Name, MinSuffixComponents, MaxSuffixComponents,
177 * PublisherPublicKeyLocator, and Exclude.
178 * This method does not consider ChildSelector and MustBeFresh.
179 *
180 * @todo recognize implicit digest component
181 */
182 bool
183 matchesData(const Data& data) const;
184
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800185 ///////////////////////////////////////////////////////////////////////////////
186 ///////////////////////////////////////////////////////////////////////////////
187 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800188 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700189
190 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800191 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700192 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800193 return m_name;
194 }
195
196 Interest&
197 setName(const Name& name)
198 {
199 m_name = name;
200 m_wire.reset();
201 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700202 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700203
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800204 //
Alexander Afanasyev84681982014-01-03 13:26:09 -0800205
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800206 const Selectors&
207 getSelectors() const
208 {
209 return m_selectors;
210 }
211
212 Interest&
213 setSelectors(const Selectors& selectors)
214 {
215 m_selectors = selectors;
216 m_wire.reset();
217 return *this;
218 }
219
220 //
221
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700222 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800223 getScope() const
224 {
225 return m_scope;
226 }
227
228 Interest&
229 setScope(int scope)
230 {
231 m_scope = scope;
232 m_wire.reset();
233 return *this;
234 }
235
236 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700237
238 const time::milliseconds&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800239 getInterestLifetime() const
240 {
241 return m_interestLifetime;
242 }
243
244 Interest&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700245 setInterestLifetime(const time::milliseconds& interestLifetime)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800246 {
247 m_interestLifetime = interestLifetime;
248 m_wire.reset();
249 return *this;
250 }
251
252 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700253
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800254 /**
255 * @brief Get Interest's nonce
256 *
257 * If nonce was not set before this call, it will be automatically assigned to a random value
258 *
259 * Const reference needed for C decoding
260 */
261 const uint32_t&
262 getNonce() const;
263
264 Interest&
265 setNonce(uint32_t nonce)
266 {
267 m_nonce = nonce;
268 m_wire.reset();
269 return *this;
270 }
271
272 //
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800273
274 nfd::LocalControlHeader&
275 getLocalControlHeader()
276 {
277 return m_localControlHeader;
278 }
279
280 const nfd::LocalControlHeader&
281 getLocalControlHeader() const
282 {
283 return m_localControlHeader;
284 }
285
286 // helper methods for LocalControlHeader
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700287
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800288 uint64_t
289 getIncomingFaceId() const
290 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800291 return getLocalControlHeader().getIncomingFaceId();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800292 }
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800293
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800294 Interest&
295 setIncomingFaceId(uint64_t incomingFaceId)
296 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800297 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800298 // ! do not reset Interest's wire !
299 return *this;
300 }
301
302 //
303
304 // NextHopFaceId helpers make sense only for Interests
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700305
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800306 uint64_t
307 getNextHopFaceId() const
308 {
309 return getLocalControlHeader().getNextHopFaceId();
310 }
311
312 Interest&
313 setNextHopFaceId(uint64_t nextHopFaceId)
314 {
315 getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
316 // ! do not reset Interest's wire !
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800317 return *this;
318 }
319
320 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700321
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800322 ///////////////////////////////////////////////////////////////////////////////
323 ///////////////////////////////////////////////////////////////////////////////
324 ///////////////////////////////////////////////////////////////////////////////
325 // Wrappers for Selectors
326 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700327
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800328 int
329 getMinSuffixComponents() const
330 {
331 return m_selectors.getMinSuffixComponents();
332 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700333
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800334 Interest&
335 setMinSuffixComponents(int minSuffixComponents)
336 {
337 m_selectors.setMinSuffixComponents(minSuffixComponents);
338 m_wire.reset();
339 return *this;
340 }
341
342 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700343
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800344 int
345 getMaxSuffixComponents() const
346 {
347 return m_selectors.getMaxSuffixComponents();
348 }
349
350 Interest&
351 setMaxSuffixComponents(int maxSuffixComponents)
352 {
353 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
354 m_wire.reset();
355 return *this;
356 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700357
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800358 //
359
Junxiao Shib332e782014-03-31 14:23:46 -0700360 const KeyLocator&
361 getPublisherPublicKeyLocator() const
362 {
363 return m_selectors.getPublisherPublicKeyLocator();
364 }
365
366 Interest&
367 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
368 {
369 m_selectors.setPublisherPublicKeyLocator(keyLocator);
370 m_wire.reset();
371 return *this;
372 }
373
374 //
375
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800376 const Exclude&
377 getExclude() const
378 {
379 return m_selectors.getExclude();
380 }
381
382 Interest&
383 setExclude(const Exclude& exclude)
384 {
385 m_selectors.setExclude(exclude);
386 m_wire.reset();
387 return *this;
388 }
389
390 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700391
392 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800393 getChildSelector() const
394 {
395 return m_selectors.getChildSelector();
396 }
397
398 Interest&
399 setChildSelector(int childSelector)
400 {
401 m_selectors.setChildSelector(childSelector);
402 m_wire.reset();
403 return *this;
404 }
405
406 //
407
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700408 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800409 getMustBeFresh() const
410 {
411 return m_selectors.getMustBeFresh();
412 }
413
414 Interest&
415 setMustBeFresh(bool mustBeFresh)
416 {
417 m_selectors.setMustBeFresh(mustBeFresh);
418 m_wire.reset();
419 return *this;
420 }
421
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700422public: // EqualityComparable concept
423 bool
424 operator==(const Interest& other) const
425 {
426 return wireEncode() == other.wireEncode();
427 }
428
429 bool
430 operator!=(const Interest& other) const
431 {
432 return !(*this == other);
433 }
434
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800435private:
436 Name m_name;
437 Selectors m_selectors;
438 mutable uint32_t m_nonce;
439 int m_scope;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700440 time::milliseconds m_interestLifetime;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800441
442 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800443
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800444 nfd::LocalControlHeader m_localControlHeader;
445 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700446};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800447
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700448std::ostream&
449operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800450
451inline std::string
452Interest::toUri() const
453{
454 std::ostringstream os;
455 os << *this;
456 return os.str();
457}
458
459inline bool
460Interest::hasSelectors() const
461{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800462 return !m_selectors.empty();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800463}
464
465inline bool
466Interest::hasGuiders() const
467{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800468 return m_scope >= 0 ||
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700469 m_interestLifetime >= time::milliseconds::zero() ||
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800470 m_nonce > 0;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800471}
472
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800473template<bool T>
474inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700475Interest::wireEncode(EncodingImpl<T>& block) const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800476{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700477 size_t totalLength = 0;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800478
479 // Interest ::= INTEREST-TYPE TLV-LENGTH
480 // Name
481 // Selectors?
482 // Nonce
483 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700484 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800485
486 // (reverse encoding)
487
488 // InterestLifetime
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700489 if (getInterestLifetime() >= time::milliseconds::zero() &&
490 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800491 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700492 totalLength += prependNonNegativeIntegerBlock(block,
493 Tlv::InterestLifetime,
494 getInterestLifetime().count());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800495 }
496
497 // Scope
498 if (getScope() >= 0)
499 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700500 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800501 }
502
503 // Nonce
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700504 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800505
506 // Selectors
507 if (!getSelectors().empty())
508 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700509 totalLength += getSelectors().wireEncode(block);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800510 }
511
512 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700513 totalLength += getName().wireEncode(block);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700514
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700515 totalLength += block.prependVarNumber (totalLength);
516 totalLength += block.prependVarNumber (Tlv::Interest);
517 return totalLength;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700518}
519
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800520inline const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800521Interest::wireEncode() const
522{
523 if (m_wire.hasWire())
524 return m_wire;
525
526 EncodingEstimator estimator;
527 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700528
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800529 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800530 wireEncode(buffer);
531
532 m_wire = buffer.block();
533 return m_wire;
534}
535
536inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700537Interest::wireDecode(const Block& wire)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800538{
539 m_wire = wire;
540 m_wire.parse();
541
542 // Interest ::= INTEREST-TYPE TLV-LENGTH
543 // Name
544 // Selectors?
545 // Nonce
546 // Scope?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700547 // InterestLifetime?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800548
549 if (m_wire.type() != Tlv::Interest)
550 throw Tlv::Error("Unexpected TLV number when decoding Interest");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700551
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800552 // Name
553 m_name.wireDecode(m_wire.get(Tlv::Name));
554
555 // Selectors
556 Block::element_const_iterator val = m_wire.find(Tlv::Selectors);
557 if (val != m_wire.elements_end())
558 {
559 m_selectors.wireDecode(*val);
560 }
561 else
562 m_selectors = Selectors();
563
564 // Nonce
565 val = m_wire.find(Tlv::Nonce);
566 if (val != m_wire.elements_end())
567 {
568 m_nonce = readNonNegativeInteger(*val);
569 }
570 else
571 m_nonce = 0;
572
573 // Scope
574 val = m_wire.find(Tlv::Scope);
575 if (val != m_wire.elements_end())
576 {
577 m_scope = readNonNegativeInteger(*val);
578 }
579 else
580 m_scope = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700581
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800582 // InterestLifetime
583 val = m_wire.find(Tlv::InterestLifetime);
584 if (val != m_wire.elements_end())
585 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700586 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800587 }
588 else
589 {
590 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
591 }
592}
593
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800594inline bool
595Interest::hasWire() const
596{
597 return m_wire.hasWire();
598}
599
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800600
601} // namespace ndn
602
603#endif // NDN_INTEREST_HPP