blob: a0d99d8583a0941ce68ff1e476ad905f399260f6 [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
17const Milliseconds DEFAULT_INTEREST_LIFETIME = 4000;
18
Jeff Thompson8238d002013-07-10 11:56:49 -070019/**
Jeff Thompson8238d002013-07-10 11:56:49 -070020 * An Interest holds a Name and other fields for an interest.
21 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -080022class Interest : public enable_shared_from_this<Interest>
23{
Jeff Thompsonb7f95562013-07-03 18:36:42 -070024public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080025 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080026 * @brief Create a new Interest with an empty name and "none" for all values.
27 */
28 Interest()
29 : m_nonce(0)
30 , m_scope(-1)
31 , m_interestLifetime(-1.0)
32 {
33 }
34
35 /**
36 * @brief Create a new Interest with the given name and "none" for other values.
37 *
38 * @param name The name for the interest.
39 */
40 Interest(const Name& name)
41 : m_name(name)
42 , m_nonce(0)
43 , m_scope(-1)
44 , m_interestLifetime(-1.0)
45 {
46 }
47
48 /**
49 * Create a new Interest with the given name and interest lifetime and "none" for other values.
50 * @param name The name for the interest.
51 * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none.
52 */
53 Interest(const Name& name, Milliseconds interestLifetime)
54 : m_name(name)
55 , m_nonce(0)
56 , m_scope(-1)
57 , m_interestLifetime(interestLifetime)
58 {
59 }
60
61 Interest(const Name& name,
62 const Selectors& selectors,
63 int scope,
64 Milliseconds interestLifetime,
65 uint32_t nonce = 0)
66 : m_name(name)
67 , m_selectors(selectors)
68 , m_nonce(nonce)
69 , m_scope(scope)
70 , m_interestLifetime(interestLifetime)
71 {
72 }
73
74 /**
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080075 * Create a new Interest for the given name and values.
76 * @param name
77 * @param minSuffixComponents
78 * @param maxSuffixComponents
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080079 * @param exclude
80 * @param childSelector
Alexander Afanasyev84681982014-01-03 13:26:09 -080081 * @param mustBeFresh
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080082 * @param scope
Alexander Afanasyev84681982014-01-03 13:26:09 -080083 * @param interestLifetime
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080084 * @param nonce
85 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080086 Interest(const Name& name,
87 int minSuffixComponents, int maxSuffixComponents,
88 const Exclude& exclude,
89 int childSelector,
90 bool mustBeFresh,
91 int scope,
92 Milliseconds interestLifetime,
93 uint32_t nonce = 0)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080094 : m_name(name)
95 , m_selectors(minSuffixComponents, maxSuffixComponents, exclude, childSelector, mustBeFresh)
96 , m_nonce(nonce)
97 , m_scope(scope)
98 , m_interestLifetime(interestLifetime)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070099 {
100 }
101
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800102 explicit
103 Interest(const Block& wire)
104 {
105 wireDecode(wire);
106 }
107
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800108 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800109 * @brief Fast encoding or block size estimation
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800110 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800111 template<bool T>
112 inline size_t
113 wireEncode(EncodingImpl<T> &block) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800114
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800115 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800116 * @brief Encode to a wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800117 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800118 inline const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800119 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800120
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800121 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800122 * @brief Decode from the wire format
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800123 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800124 inline void
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800125 wireDecode(const Block &wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800126
127 /**
128 * @brief Check if already has wire
129 */
130 inline bool
131 hasWire() const;
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700132
133 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -0800134 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
135 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
136 * @return The URI string.
137 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800138 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800139 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700140
Alexander Afanasyev84681982014-01-03 13:26:09 -0800141 inline bool
142 hasSelectors() const;
143
144 inline bool
145 hasGuiders() const;
146
147 /**
148 * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the
149 * interest selectors.
150 * @param self A pointer to the ndn_Interest struct.
151 * @param name A pointer to the name to check.
152 * @return 1 if the name and interest selectors match, 0 otherwise.
153 */
154 bool
155 matchesName(const Name &name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800156
157 ///////////////////////////////////////////////////////////////////////////////
158 ///////////////////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////////////////
160 // Gettest/setters
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700161
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800162 const Name&
163 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700164 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800165 return m_name;
166 }
167
168 Interest&
169 setName(const Name& name)
170 {
171 m_name = name;
172 m_wire.reset();
173 return *this;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700174 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700175
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800176 //
Alexander Afanasyev84681982014-01-03 13:26:09 -0800177
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800178 const Selectors&
179 getSelectors() const
180 {
181 return m_selectors;
182 }
183
184 Interest&
185 setSelectors(const Selectors& selectors)
186 {
187 m_selectors = selectors;
188 m_wire.reset();
189 return *this;
190 }
191
192 //
193
194 int
195 getScope() const
196 {
197 return m_scope;
198 }
199
200 Interest&
201 setScope(int scope)
202 {
203 m_scope = scope;
204 m_wire.reset();
205 return *this;
206 }
207
208 //
209
210 Milliseconds
211 getInterestLifetime() const
212 {
213 return m_interestLifetime;
214 }
215
216 Interest&
217 setInterestLifetime(Milliseconds interestLifetime)
218 {
219 m_interestLifetime = interestLifetime;
220 m_wire.reset();
221 return *this;
222 }
223
224 //
225
226 /**
227 * @brief Get Interest's nonce
228 *
229 * If nonce was not set before this call, it will be automatically assigned to a random value
230 *
231 * Const reference needed for C decoding
232 */
233 const uint32_t&
234 getNonce() const;
235
236 Interest&
237 setNonce(uint32_t nonce)
238 {
239 m_nonce = nonce;
240 m_wire.reset();
241 return *this;
242 }
243
244 //
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800245
246 nfd::LocalControlHeader&
247 getLocalControlHeader()
248 {
249 return m_localControlHeader;
250 }
251
252 const nfd::LocalControlHeader&
253 getLocalControlHeader() const
254 {
255 return m_localControlHeader;
256 }
257
258 // helper methods for LocalControlHeader
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800259
260 uint64_t
261 getIncomingFaceId() const
262 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800263 return getLocalControlHeader().getIncomingFaceId();
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800264 }
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800265
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800266 Interest&
267 setIncomingFaceId(uint64_t incomingFaceId)
268 {
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800269 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800270 return *this;
271 }
272
273 //
274
275 ///////////////////////////////////////////////////////////////////////////////
276 ///////////////////////////////////////////////////////////////////////////////
277 ///////////////////////////////////////////////////////////////////////////////
278 // Wrappers for Selectors
279 //
280
281 int
282 getMinSuffixComponents() const
283 {
284 return m_selectors.getMinSuffixComponents();
285 }
286
287 Interest&
288 setMinSuffixComponents(int minSuffixComponents)
289 {
290 m_selectors.setMinSuffixComponents(minSuffixComponents);
291 m_wire.reset();
292 return *this;
293 }
294
295 //
296
297 int
298 getMaxSuffixComponents() const
299 {
300 return m_selectors.getMaxSuffixComponents();
301 }
302
303 Interest&
304 setMaxSuffixComponents(int maxSuffixComponents)
305 {
306 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
307 m_wire.reset();
308 return *this;
309 }
310
311 //
312
313 const Exclude&
314 getExclude() const
315 {
316 return m_selectors.getExclude();
317 }
318
319 Interest&
320 setExclude(const Exclude& exclude)
321 {
322 m_selectors.setExclude(exclude);
323 m_wire.reset();
324 return *this;
325 }
326
327 //
328
329 int
330 getChildSelector() const
331 {
332 return m_selectors.getChildSelector();
333 }
334
335 Interest&
336 setChildSelector(int childSelector)
337 {
338 m_selectors.setChildSelector(childSelector);
339 m_wire.reset();
340 return *this;
341 }
342
343 //
344
345 int
346 getMustBeFresh() const
347 {
348 return m_selectors.getMustBeFresh();
349 }
350
351 Interest&
352 setMustBeFresh(bool mustBeFresh)
353 {
354 m_selectors.setMustBeFresh(mustBeFresh);
355 m_wire.reset();
356 return *this;
357 }
358
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800359private:
360 Name m_name;
361 Selectors m_selectors;
362 mutable uint32_t m_nonce;
363 int m_scope;
364 Milliseconds m_interestLifetime;
365
366 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800367
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800368 nfd::LocalControlHeader m_localControlHeader;
369 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700370};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800371
372std::ostream &
373operator << (std::ostream &os, const Interest &interest);
374
375inline std::string
376Interest::toUri() const
377{
378 std::ostringstream os;
379 os << *this;
380 return os.str();
381}
382
383inline bool
384Interest::hasSelectors() const
385{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800386 return !m_selectors.empty();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800387}
388
389inline bool
390Interest::hasGuiders() const
391{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800392 return m_scope >= 0 ||
393 m_interestLifetime >= 0 ||
394 m_nonce > 0;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800395}
396
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800397template<bool T>
398inline size_t
399Interest::wireEncode(EncodingImpl<T> &block) const
400{
401 size_t total_len = 0;
402
403 // Interest ::= INTEREST-TYPE TLV-LENGTH
404 // Name
405 // Selectors?
406 // Nonce
407 // Scope?
408 // InterestLifetime?
409
410 // (reverse encoding)
411
412 // InterestLifetime
413 if (getInterestLifetime() >= 0 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
414 {
415 total_len += prependNonNegativeIntegerBlock(block, Tlv::InterestLifetime, getInterestLifetime());
416 }
417
418 // Scope
419 if (getScope() >= 0)
420 {
421 total_len += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
422 }
423
424 // Nonce
425 total_len += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
426
427 // Selectors
428 if (!getSelectors().empty())
429 {
430 total_len += getSelectors().wireEncode(block);
431 }
432
433 // Name
434 total_len += getName().wireEncode(block);
435
436 total_len += block.prependVarNumber (total_len);
437 total_len += block.prependVarNumber (Tlv::Interest);
438 return total_len;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700439}
440
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800441inline const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800442Interest::wireEncode() const
443{
444 if (m_wire.hasWire())
445 return m_wire;
446
447 EncodingEstimator estimator;
448 size_t estimatedSize = wireEncode(estimator);
449
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800450 EncodingBuffer buffer(estimatedSize + nfd::ESTIMATED_LOCAL_HEADER_RESERVE, 0);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800451 wireEncode(buffer);
452
453 m_wire = buffer.block();
454 return m_wire;
455}
456
457inline void
458Interest::wireDecode(const Block &wire)
459{
460 m_wire = wire;
461 m_wire.parse();
462
463 // Interest ::= INTEREST-TYPE TLV-LENGTH
464 // Name
465 // Selectors?
466 // Nonce
467 // Scope?
468 // InterestLifetime?
469
470 if (m_wire.type() != Tlv::Interest)
471 throw Tlv::Error("Unexpected TLV number when decoding Interest");
472
473 // Name
474 m_name.wireDecode(m_wire.get(Tlv::Name));
475
476 // Selectors
477 Block::element_const_iterator val = m_wire.find(Tlv::Selectors);
478 if (val != m_wire.elements_end())
479 {
480 m_selectors.wireDecode(*val);
481 }
482 else
483 m_selectors = Selectors();
484
485 // Nonce
486 val = m_wire.find(Tlv::Nonce);
487 if (val != m_wire.elements_end())
488 {
489 m_nonce = readNonNegativeInteger(*val);
490 }
491 else
492 m_nonce = 0;
493
494 // Scope
495 val = m_wire.find(Tlv::Scope);
496 if (val != m_wire.elements_end())
497 {
498 m_scope = readNonNegativeInteger(*val);
499 }
500 else
501 m_scope = -1;
502
503 // InterestLifetime
504 val = m_wire.find(Tlv::InterestLifetime);
505 if (val != m_wire.elements_end())
506 {
507 m_interestLifetime = readNonNegativeInteger(*val);
508 }
509 else
510 {
511 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
512 }
513}
514
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800515inline bool
516Interest::hasWire() const
517{
518 return m_wire.hasWire();
519}
520
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800521
522} // namespace ndn
523
524#endif // NDN_INTEREST_HPP