blob: a22578f79c73d001f6b23fd513c5f60cbd3f4ce5 [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 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800160 // Getters/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 Afanasyev5964fb72014-02-18 12:42:45 -0800270 // ! do not reset Interest's wire !
271 return *this;
272 }
273
274 //
275
276 // NextHopFaceId helpers make sense only for Interests
277
278 uint64_t
279 getNextHopFaceId() const
280 {
281 return getLocalControlHeader().getNextHopFaceId();
282 }
283
284 Interest&
285 setNextHopFaceId(uint64_t nextHopFaceId)
286 {
287 getLocalControlHeader().setNextHopFaceId(nextHopFaceId);
288 // ! do not reset Interest's wire !
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800289 return *this;
290 }
291
292 //
293
294 ///////////////////////////////////////////////////////////////////////////////
295 ///////////////////////////////////////////////////////////////////////////////
296 ///////////////////////////////////////////////////////////////////////////////
297 // Wrappers for Selectors
298 //
299
300 int
301 getMinSuffixComponents() const
302 {
303 return m_selectors.getMinSuffixComponents();
304 }
305
306 Interest&
307 setMinSuffixComponents(int minSuffixComponents)
308 {
309 m_selectors.setMinSuffixComponents(minSuffixComponents);
310 m_wire.reset();
311 return *this;
312 }
313
314 //
315
316 int
317 getMaxSuffixComponents() const
318 {
319 return m_selectors.getMaxSuffixComponents();
320 }
321
322 Interest&
323 setMaxSuffixComponents(int maxSuffixComponents)
324 {
325 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
326 m_wire.reset();
327 return *this;
328 }
329
330 //
331
332 const Exclude&
333 getExclude() const
334 {
335 return m_selectors.getExclude();
336 }
337
338 Interest&
339 setExclude(const Exclude& exclude)
340 {
341 m_selectors.setExclude(exclude);
342 m_wire.reset();
343 return *this;
344 }
345
346 //
347
348 int
349 getChildSelector() const
350 {
351 return m_selectors.getChildSelector();
352 }
353
354 Interest&
355 setChildSelector(int childSelector)
356 {
357 m_selectors.setChildSelector(childSelector);
358 m_wire.reset();
359 return *this;
360 }
361
362 //
363
364 int
365 getMustBeFresh() const
366 {
367 return m_selectors.getMustBeFresh();
368 }
369
370 Interest&
371 setMustBeFresh(bool mustBeFresh)
372 {
373 m_selectors.setMustBeFresh(mustBeFresh);
374 m_wire.reset();
375 return *this;
376 }
377
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800378private:
379 Name m_name;
380 Selectors m_selectors;
381 mutable uint32_t m_nonce;
382 int m_scope;
383 Milliseconds m_interestLifetime;
384
385 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800386
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800387 nfd::LocalControlHeader m_localControlHeader;
388 friend class nfd::LocalControlHeader;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700389};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800390
391std::ostream &
392operator << (std::ostream &os, const Interest &interest);
393
394inline std::string
395Interest::toUri() const
396{
397 std::ostringstream os;
398 os << *this;
399 return os.str();
400}
401
402inline bool
403Interest::hasSelectors() const
404{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800405 return !m_selectors.empty();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800406}
407
408inline bool
409Interest::hasGuiders() const
410{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800411 return m_scope >= 0 ||
412 m_interestLifetime >= 0 ||
413 m_nonce > 0;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800414}
415
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800416template<bool T>
417inline size_t
418Interest::wireEncode(EncodingImpl<T> &block) const
419{
420 size_t total_len = 0;
421
422 // Interest ::= INTEREST-TYPE TLV-LENGTH
423 // Name
424 // Selectors?
425 // Nonce
426 // Scope?
427 // InterestLifetime?
428
429 // (reverse encoding)
430
431 // InterestLifetime
432 if (getInterestLifetime() >= 0 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
433 {
434 total_len += prependNonNegativeIntegerBlock(block, Tlv::InterestLifetime, getInterestLifetime());
435 }
436
437 // Scope
438 if (getScope() >= 0)
439 {
440 total_len += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
441 }
442
443 // Nonce
444 total_len += prependNonNegativeIntegerBlock(block, Tlv::Nonce, getNonce());
445
446 // Selectors
447 if (!getSelectors().empty())
448 {
449 total_len += getSelectors().wireEncode(block);
450 }
451
452 // Name
453 total_len += getName().wireEncode(block);
454
455 total_len += block.prependVarNumber (total_len);
456 total_len += block.prependVarNumber (Tlv::Interest);
457 return total_len;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700458}
459
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800460inline const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800461Interest::wireEncode() const
462{
463 if (m_wire.hasWire())
464 return m_wire;
465
466 EncodingEstimator estimator;
467 size_t estimatedSize = wireEncode(estimator);
468
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800469 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800470 wireEncode(buffer);
471
472 m_wire = buffer.block();
473 return m_wire;
474}
475
476inline void
477Interest::wireDecode(const Block &wire)
478{
479 m_wire = wire;
480 m_wire.parse();
481
482 // Interest ::= INTEREST-TYPE TLV-LENGTH
483 // Name
484 // Selectors?
485 // Nonce
486 // Scope?
487 // InterestLifetime?
488
489 if (m_wire.type() != Tlv::Interest)
490 throw Tlv::Error("Unexpected TLV number when decoding Interest");
491
492 // Name
493 m_name.wireDecode(m_wire.get(Tlv::Name));
494
495 // Selectors
496 Block::element_const_iterator val = m_wire.find(Tlv::Selectors);
497 if (val != m_wire.elements_end())
498 {
499 m_selectors.wireDecode(*val);
500 }
501 else
502 m_selectors = Selectors();
503
504 // Nonce
505 val = m_wire.find(Tlv::Nonce);
506 if (val != m_wire.elements_end())
507 {
508 m_nonce = readNonNegativeInteger(*val);
509 }
510 else
511 m_nonce = 0;
512
513 // Scope
514 val = m_wire.find(Tlv::Scope);
515 if (val != m_wire.elements_end())
516 {
517 m_scope = readNonNegativeInteger(*val);
518 }
519 else
520 m_scope = -1;
521
522 // InterestLifetime
523 val = m_wire.find(Tlv::InterestLifetime);
524 if (val != m_wire.elements_end())
525 {
526 m_interestLifetime = readNonNegativeInteger(*val);
527 }
528 else
529 {
530 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
531 }
532}
533
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800534inline bool
535Interest::hasWire() const
536{
537 return m_wire.hasWire();
538}
539
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800540
541} // namespace ndn
542
543#endif // NDN_INTEREST_HPP