blob: e4e20bab8dca91866eb83330cbb12f69b0e90ff3 [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
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "name.hpp"
12#include "publisher-public-key-digest.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070013#include "c/interest-types.h"
14#include "encoding/wire-format.hpp"
15
16struct ndn_ExcludeEntry;
17struct ndn_Exclude;
18struct ndn_Interest;
Jeff Thompsonb7f95562013-07-03 18:36:42 -070019
20namespace ndn {
Jeff Thompsonfe556862013-07-09 13:52:55 -070021
Jeff Thompson8238d002013-07-10 11:56:49 -070022/**
23 * An ExcludeEntry holds an ndn_ExcludeType, and if it is a COMPONENT, it holds the component value.
24 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070025class ExcludeEntry {
26public:
27 /**
28 * Create an ExcludeEntry of type ndn_Exclude_ANY
29 */
30 ExcludeEntry()
31 : type_(ndn_Exclude_ANY)
32 {
33 }
34
35 /**
Jeff Thompson832d2972013-10-31 11:24:55 -070036 * Create an ExcludeEntry of type ndn_Exclude_COMPONENT.
Jeff Thompsonfe556862013-07-09 13:52:55 -070037 */
Jeff Thompson97223af2013-09-24 17:01:27 -070038 ExcludeEntry(uint8_t *component, size_t componentLen)
Jeff Thompson38d0e082013-08-12 18:07:44 -070039 : type_(ndn_Exclude_COMPONENT), component_(component, componentLen)
Jeff Thompsonfe556862013-07-09 13:52:55 -070040 {
41 }
42
43 /**
Jeff Thompson832d2972013-10-31 11:24:55 -070044 * Create an ExcludeEntry of type ndn_Exclude_COMPONENT.
45 */
46 ExcludeEntry(const Blob& component)
47 : type_(ndn_Exclude_COMPONENT), component_(component)
48 {
49 }
50
51 /**
Jeff Thompsonfe556862013-07-09 13:52:55 -070052 * Set the type in the excludeEntryStruct and to point to this component, without copying any memory.
53 * WARNING: The resulting pointer in excludeEntryStruct is invalid after a further use of this object which could reallocate memory.
Jeff Thompson8238d002013-07-10 11:56:49 -070054 * @param excludeEntryStruct the C ndn_NameComponent struct to receive the pointer
Jeff Thompsonfe556862013-07-09 13:52:55 -070055 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070056 void
Jeff Thompson25b4e612013-10-10 16:03:24 -070057 get(struct ndn_ExcludeEntry& excludeEntryStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -070058
59 ndn_ExcludeType getType() const { return type_; }
60
Jeff Thompson1656e6a2013-08-29 18:01:48 -070061 const Name::Component& getComponent() const { return component_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070062
63private:
64 ndn_ExcludeType type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070065 Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
Jeff Thompsonfe556862013-07-09 13:52:55 -070066};
67
Jeff Thompson8238d002013-07-10 11:56:49 -070068/**
69 * An Exclude holds a vector of ExcludeEntry.
70 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070071class Exclude {
72public:
73 /**
74 * Create a new Exclude with no entries.
75 */
76 Exclude() {
77 }
78
Jeff Thompson97223af2013-09-24 17:01:27 -070079 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070080 getEntryCount() const {
Jeff Thompsonfe556862013-07-09 13:52:55 -070081 return entries_.size();
82 }
83
Jeff Thompson0050abe2013-09-17 12:50:25 -070084 const ExcludeEntry&
Jeff Thompson97223af2013-09-24 17:01:27 -070085 getEntry(size_t i) const { return entries_[i]; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070086
87 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070088 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070089 * WARNING: The resulting pointers in excludeStruct are invalid after a further use of this object which could reallocate memory.
Jeff Thompson8238d002013-07-10 11:56:49 -070090 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070091 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070092 void
93 get(struct ndn_Exclude& excludeStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -070094
95 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070096 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070097 * @param excludeStruct a C ndn_Exclude struct
98 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070099 void
100 set(const struct ndn_Exclude& excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -0700101
102 /**
Jeff Thompson832d2972013-10-31 11:24:55 -0700103 * Append a new entry of type ndn_Exclude_ANY.
104 * @return This Exclude so that you can chain calls to append.
Jeff Thompsonfe556862013-07-09 13:52:55 -0700105 */
Jeff Thompson832d2972013-10-31 11:24:55 -0700106 Exclude&
107 appendAny()
Jeff Thompsonfe556862013-07-09 13:52:55 -0700108 {
109 entries_.push_back(ExcludeEntry());
Jeff Thompson832d2972013-10-31 11:24:55 -0700110 return *this;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700111 }
112
113 /**
Jeff Thompsonb096e492013-10-31 11:29:28 -0700114 * Append a new entry of type ndn_Exclude_COMPONENT, copying from component of length componentLength.
Jeff Thompson832d2972013-10-31 11:24:55 -0700115 * @param component A pointer to the component byte array.
116 * @param componentLength The length of component.
117 * @return This Exclude so that you can chain calls to append.
Jeff Thompsonfe556862013-07-09 13:52:55 -0700118 */
Jeff Thompson832d2972013-10-31 11:24:55 -0700119 Exclude&
120 appendComponent(uint8_t *component, size_t componentLength)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700121 {
Jeff Thompson832d2972013-10-31 11:24:55 -0700122 entries_.push_back(ExcludeEntry(component, componentLength));
123 return *this;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700124 }
Jeff Thompson832d2972013-10-31 11:24:55 -0700125
126 /**
Jeff Thompsonb096e492013-10-31 11:29:28 -0700127 * Append a new entry of type ndn_Exclude_COMPONENT, taking another pointer to the Blob value.
Jeff Thompson832d2972013-10-31 11:24:55 -0700128 * @param component A blob with a pointer to an immutable array. The pointer is copied.
129 * @return This Exclude so that you can chain calls to append.
130 */
131 Exclude&
132 appendComponent(const Blob &component)
133 {
134 entries_.push_back(ExcludeEntry(component));
135 return *this;
136 }
137
138 /**
139 * @deprecated Use appendAny.
140 */
141 Exclude&
142 addAny() { return appendAny(); }
143
144 /**
145 * @deprecated Use appendComponent.
146 */
147 Exclude&
148 addComponent(uint8_t *component, size_t componentLength) { return appendComponent(component, componentLength); }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700149
150 /**
151 * Clear all the entries.
152 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700153 void
154 clear() {
Jeff Thompsonfe556862013-07-09 13:52:55 -0700155 entries_.clear();
156 }
157
Jeff Thompson37527d62013-08-21 11:15:54 -0700158 /**
159 * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*".
160 * @return the URI string
161 */
162 std::string toUri() const;
163
Jeff Thompsonfe556862013-07-09 13:52:55 -0700164private:
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700165 std::vector<ExcludeEntry> entries_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700166};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700167
Jeff Thompson8238d002013-07-10 11:56:49 -0700168/**
169 * An Interest holds a Name and other fields for an interest.
170 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700171class Interest {
172public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800173 /**
174 * Create a new Interest for the given name and values.
175 * @param name
176 * @param minSuffixComponents
177 * @param maxSuffixComponents
178 * @param publisherPublicKeyDigest
179 * @param exclude
180 * @param childSelector
181 * @param answerOriginKind
182 * @param scope
183 * @param interestLifetimeMilliseconds
184 * @param nonce
185 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700186 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
187 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700188 int scope, Milliseconds interestLifetimeMilliseconds, const std::vector<uint8_t>& nonce)
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700189 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
190 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
191 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
192 nonce_(nonce)
193 {
194 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700195
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800196 /**
197 * Create a new Interest with the given name and values, and "none" for the nonce.
198 * @param name
199 * @param minSuffixComponents
200 * @param maxSuffixComponents
201 * @param publisherPublicKeyDigest
202 * @param exclude
203 * @param childSelector
204 * @param answerOriginKind
205 * @param scope
206 * @param interestLifetimeMilliseconds
207 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700208 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
209 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700210 int scope, Milliseconds interestLifetimeMilliseconds)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700211 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
212 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
213 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
214 {
215 }
216
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800217 /**
218 * Create a new Interest with the given name and interest lifetime and "none" for other values.
219 * @param name The name for the interest.
220 * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none.
221 */
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700222 Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700223 : name_(name)
224 {
225 construct();
226 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
227 }
228
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800229 /**
230 * Create a new Interest with the given name and "none" for other values.
231 * @param name The name for the interest.
232 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700233 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700234 : name_(name)
235 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700236 construct();
237 }
238
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800239 /**
240 * Create a new Interest with an empty name and "none" for all values.
241 */
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700242 Interest()
243 {
244 construct();
245 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700246
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800247 /**
248 * Encode this Interest for a particular wire format.
249 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
250 * @return The encoded byte array.
251 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700252 Blob
253 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700254 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700255 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700256 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700257
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800258 /**
259 * Decode the input using a particular wire format and update this Interest.
260 * @param input The input byte array to be decoded.
261 * @param inputLength The length of input.
262 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
263 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700264 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700265 wireDecode(const uint8_t *input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700266 {
267 wireFormat.decodeInterest(*this, input, inputLength);
268 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700269
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800270 /**
271 * Decode the input using a particular wire format and update this Interest.
272 * @param input The input byte array to be decoded.
273 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
274 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700275 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700276 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700277 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700278 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700279 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700280
281 /**
282 * Set the interestStruct to point to the components in this interest, without copying any memory.
283 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
284 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
285 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700286 void
287 get(struct ndn_Interest& interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700288
Jeff Thompson0050abe2013-09-17 12:50:25 -0700289 Name&
290 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700291
Jeff Thompson0050abe2013-09-17 12:50:25 -0700292 const Name&
293 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700294
Jeff Thompson0050abe2013-09-17 12:50:25 -0700295 int
296 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700297
Jeff Thompson0050abe2013-09-17 12:50:25 -0700298 int
299 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700300
Jeff Thompson0050abe2013-09-17 12:50:25 -0700301 PublisherPublicKeyDigest&
302 getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
303
304 const PublisherPublicKeyDigest&
305 getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700306
Jeff Thompson0050abe2013-09-17 12:50:25 -0700307 Exclude&
308 getExclude() { return exclude_; }
309
310 const Exclude&
311 getExclude() const { return exclude_; }
312
313 int
314 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700315
Jeff Thompson0050abe2013-09-17 12:50:25 -0700316 int
317 getAnswerOriginKind() const { return answerOriginKind_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700318
Jeff Thompson0050abe2013-09-17 12:50:25 -0700319 int
320 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700321
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700322 Milliseconds
Jeff Thompson0050abe2013-09-17 12:50:25 -0700323 getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
324
325 const Blob&
326 getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700327
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700328 /**
329 * Clear this interest, and set the values by copying from the interest struct.
330 * @param interestStruct a C ndn_Interest struct
331 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700332 void
333 set(const struct ndn_Interest& interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700334
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700335 void
336 setName(const Name& name) { name_ = name; }
337
Jeff Thompson0050abe2013-09-17 12:50:25 -0700338 void
339 setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700340
Jeff Thompson0050abe2013-09-17 12:50:25 -0700341 void
342 setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700343
Jeff Thompson0050abe2013-09-17 12:50:25 -0700344 void
345 setChildSelector(int value) { childSelector_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700346
Jeff Thompson0050abe2013-09-17 12:50:25 -0700347 void
348 setAnswerOriginKind(int value) { answerOriginKind_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700349
Jeff Thompson0050abe2013-09-17 12:50:25 -0700350 void
351 setScope(int value) { scope_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700352
Jeff Thompson0050abe2013-09-17 12:50:25 -0700353 void
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700354 setInterestLifetimeMilliseconds(Milliseconds value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700355
Jeff Thompson0050abe2013-09-17 12:50:25 -0700356 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700357 setNonce(const std::vector<uint8_t>& value) { nonce_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700358
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700359private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700360 void
361 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700362 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700363 minSuffixComponents_ = -1;
364 maxSuffixComponents_ = -1;
365 childSelector_ = -1;
366 answerOriginKind_ = -1;
367 scope_ = -1;
368 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700369 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700370
371 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700372 int minSuffixComponents_;
373 int maxSuffixComponents_;
374 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700375 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700376 int childSelector_;
377 int answerOriginKind_;
378 int scope_;
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700379 Milliseconds interestLifetimeMilliseconds_;
Jeff Thompson412226d2013-09-12 15:55:46 -0700380 Blob nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700381};
382
383}
384
385#endif