blob: c4d7bc6710c588b2dd12c381fbd50208265df11c [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.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
8#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -070010
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080011#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "name.hpp"
Alexander Afanasyev84681982014-01-03 13:26:09 -080013#include "exclude.hpp"
14#include "encoding/block.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070015
16namespace ndn {
Jeff Thompsonfe556862013-07-09 13:52:55 -070017
Jeff Thompson8238d002013-07-10 11:56:49 -070018/**
Jeff Thompson8238d002013-07-10 11:56:49 -070019 * An Interest holds a Name and other fields for an interest.
20 */
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080021class Interest : public enable_shared_from_this<Interest> {
Jeff Thompsonb7f95562013-07-03 18:36:42 -070022public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080023 /**
24 * Create a new Interest for the given name and values.
25 * @param name
26 * @param minSuffixComponents
27 * @param maxSuffixComponents
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080028 * @param exclude
29 * @param childSelector
Alexander Afanasyev84681982014-01-03 13:26:09 -080030 * @param mustBeFresh
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080031 * @param scope
Alexander Afanasyev84681982014-01-03 13:26:09 -080032 * @param interestLifetime
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080033 * @param nonce
34 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080035 Interest(const Name& name,
36 int minSuffixComponents, int maxSuffixComponents,
37 const Exclude& exclude,
38 int childSelector,
39 bool mustBeFresh,
40 int scope,
41 Milliseconds interestLifetime,
42 uint32_t nonce = 0)
43 : name_(name)
44 , minSuffixComponents_(minSuffixComponents)
45 , maxSuffixComponents_(maxSuffixComponents)
46 , exclude_(exclude), childSelector_(childSelector)
47 , mustBeFresh_(mustBeFresh)
48 , scope_(scope)
49 , interestLifetime_(interestLifetime)
50 , nonce_(nonce)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070051 {
52 }
53
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080054 /**
55 * Create a new Interest with the given name and interest lifetime and "none" for other values.
56 * @param name The name for the interest.
57 * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none.
58 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080059 Interest(const Name& name, Milliseconds interestLifetime)
Jeff Thompsonf62382a2013-08-21 16:33:39 -070060 : name_(name)
61 {
62 construct();
Alexander Afanasyev84681982014-01-03 13:26:09 -080063 interestLifetime_ = interestLifetime;
Jeff Thompsonf62382a2013-08-21 16:33:39 -070064 }
65
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080066 /**
67 * Create a new Interest with the given name and "none" for other values.
68 * @param name The name for the interest.
69 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070070 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070071 : name_(name)
72 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070073 construct();
74 }
75
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080076 /**
77 * Create a new Interest with an empty name and "none" for all values.
78 */
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070079 Interest()
80 {
81 construct();
82 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -070083
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080084 /**
85 * Encode this Interest for a particular wire format.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080086 * @return The encoded byte array.
87 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080088 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080089 wireEncode() const;
Jeff Thompson0050abe2013-09-17 12:50:25 -070090
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080091 /**
92 * Decode the input using a particular wire format and update this Interest.
93 * @param input The input byte array to be decoded.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080094 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070095 void
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080096 wireDecode(const Block &wire);
Jeff Thompsond9e278c2013-07-08 15:20:13 -070097
98 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -080099 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
100 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
101 * @return The URI string.
102 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800103 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800104 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700105
Jeff Thompson0050abe2013-09-17 12:50:25 -0700106 Name&
107 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700108
Jeff Thompson0050abe2013-09-17 12:50:25 -0700109 const Name&
110 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700111
Jeff Thompson0050abe2013-09-17 12:50:25 -0700112 int
113 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700114
Jeff Thompson0050abe2013-09-17 12:50:25 -0700115 int
116 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700117
Jeff Thompson0050abe2013-09-17 12:50:25 -0700118 Exclude&
119 getExclude() { return exclude_; }
120
121 const Exclude&
122 getExclude() const { return exclude_; }
123
124 int
125 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700126
Jeff Thompson0050abe2013-09-17 12:50:25 -0700127 int
Alexander Afanasyev84681982014-01-03 13:26:09 -0800128 getMustBeFresh() const { return mustBeFresh_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700129
Jeff Thompson0050abe2013-09-17 12:50:25 -0700130 int
131 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700132
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700133 Milliseconds
Alexander Afanasyev84681982014-01-03 13:26:09 -0800134 getInterestLifetime() const { return interestLifetime_; }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700135
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800136 /**
137 * @brief Get Interest's nonce
138 *
139 * If nonce was not set before this call, it will be automatically assigned to a random value
Alexander Afanasyev85480842014-01-06 14:46:54 -0800140 *
141 * Const reference needed for C decoding
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800142 */
Alexander Afanasyev85480842014-01-06 14:46:54 -0800143 const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800144 getNonce() const;
Yingdi Yua4e57672014-02-06 11:16:17 -0800145
146 uint64_t
147 getIncomingFaceId() const { return m_incomingFaceId; }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800148
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700149 void
150 setName(const Name& name) { name_ = name; }
151
Jeff Thompson0050abe2013-09-17 12:50:25 -0700152 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800153 setMinSuffixComponents(int minSuffixComponents) { minSuffixComponents_ = minSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700154
Jeff Thompson0050abe2013-09-17 12:50:25 -0700155 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800156 setMaxSuffixComponents(int maxSuffixComponents) { maxSuffixComponents_ = maxSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700157
Jeff Thompson0050abe2013-09-17 12:50:25 -0700158 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800159 setChildSelector(int childSelector) { childSelector_ = childSelector; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700160
Jeff Thompson0050abe2013-09-17 12:50:25 -0700161 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800162 setMustBeFresh(bool mustBeFresh) { mustBeFresh_ = mustBeFresh; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700163
Jeff Thompson0050abe2013-09-17 12:50:25 -0700164 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800165 setScope(int scope) { scope_ = scope; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700166
Jeff Thompson0050abe2013-09-17 12:50:25 -0700167 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800168 setInterestLifetime(Milliseconds interestLifetime) { interestLifetime_ = interestLifetime; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700169
Jeff Thompson0050abe2013-09-17 12:50:25 -0700170 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800171 setNonce(uint32_t nonce) { nonce_ = nonce; }
172
Yingdi Yua4e57672014-02-06 11:16:17 -0800173 void
174 setIncomingFaceId(uint64_t incomingFaceId) { m_incomingFaceId = incomingFaceId; }
175
Alexander Afanasyev84681982014-01-03 13:26:09 -0800176 inline bool
177 hasSelectors() const;
178
179 inline bool
180 hasGuiders() const;
181
182 /**
183 * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the
184 * interest selectors.
185 * @param self A pointer to the ndn_Interest struct.
186 * @param name A pointer to the name to check.
187 * @return 1 if the name and interest selectors match, 0 otherwise.
188 */
189 bool
190 matchesName(const Name &name) const;
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700191
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700192private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700193 void
194 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700195 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700196 minSuffixComponents_ = -1;
197 maxSuffixComponents_ = -1;
198 childSelector_ = -1;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800199 mustBeFresh_ = false; // default
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700200 scope_ = -1;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800201 interestLifetime_ = -1.0;
202 nonce_ = 0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700203 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700204
205 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700206 int minSuffixComponents_;
207 int maxSuffixComponents_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700208 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700209 int childSelector_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800210 bool mustBeFresh_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700211 int scope_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800212 Milliseconds interestLifetime_;
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800213 mutable uint32_t nonce_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800214
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800215 mutable Block wire_;
Yingdi Yua4e57672014-02-06 11:16:17 -0800216
217 uint64_t m_incomingFaceId;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700218};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800219
220std::ostream &
221operator << (std::ostream &os, const Interest &interest);
222
223inline std::string
224Interest::toUri() const
225{
226 std::ostringstream os;
227 os << *this;
228 return os.str();
229}
230
231inline bool
232Interest::hasSelectors() const
233{
234 return minSuffixComponents_ >= 0 ||
235 maxSuffixComponents_ >= 0 ||
236 !exclude_.empty() ||
237 childSelector_ >= 0 ||
238 mustBeFresh_ == true ||
239 scope_ >= 0;
240}
241
242inline bool
243Interest::hasGuiders() const
244{
245 return scope_ >= 0 ||
246 interestLifetime_ >= 0 ||
247 nonce_ > 0;
248}
249
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700250}
251
252#endif