blob: 8788fd870c526c3e0531df6763379d216e17d5f0 [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/**
Jeff Thompson4dfca982013-12-03 11:53:26 -080023 * An Exclude holds a vector of Exclude::Entry.
Jeff Thompson8238d002013-07-10 11:56:49 -070024 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070025class Exclude {
26public:
27 /**
28 * Create a new Exclude with no entries.
29 */
30 Exclude() {
31 }
Jeff Thompsonf62f9f22013-11-26 17:22:54 -080032
33 /**
34 * An Exclude::Entry holds an ndn_ExcludeType, and if it is a COMPONENT, it holds the component value.
35 */
36 class Entry {
37 public:
38 /**
39 * Create an Exclude::Entry of type ndn_Exclude_ANY
40 */
41 Entry()
42 : type_(ndn_Exclude_ANY)
43 {
44 }
45
46 /**
47 * Create an Exclude::Entry of type ndn_Exclude_COMPONENT.
48 */
49 Entry(uint8_t *component, size_t componentLen)
50 : type_(ndn_Exclude_COMPONENT), component_(component, componentLen)
51 {
52 }
53
54 /**
55 * Create an Exclude::Entry of type ndn_Exclude_COMPONENT.
56 */
57 Entry(const Blob& component)
58 : type_(ndn_Exclude_COMPONENT), component_(component)
59 {
60 }
61
62 /**
63 * Set the type in the excludeEntryStruct and to point to this entry, without copying any memory.
64 * WARNING: The resulting pointer in excludeEntryStruct is invalid after a further use of this object which could reallocate memory.
65 * @param excludeEntryStruct the C ndn_ExcludeEntry struct to receive the pointer
66 */
67 void
68 get(struct ndn_ExcludeEntry& excludeEntryStruct) const;
69
70 ndn_ExcludeType getType() const { return type_; }
71
72 const Name::Component& getComponent() const { return component_; }
73
74 private:
75 ndn_ExcludeType type_;
76 Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
77 };
78
79 /**
80 * Get the number of entries.
81 * @return The number of entries.
82 */
Jeff Thompson97223af2013-09-24 17:01:27 -070083 size_t
Jeff Thompsonf62f9f22013-11-26 17:22:54 -080084 size() const { return entries_.size(); }
Jeff Thompsonfe556862013-07-09 13:52:55 -070085
Jeff Thompsonf62f9f22013-11-26 17:22:54 -080086 /**
87 * Get the entry at the given index.
88 * @param i The index of the entry, starting from 0.
89 * @return The entry at the index.
90 */
91 const Exclude::Entry&
92 get(size_t i) const { return entries_[i]; }
93
94 /**
95 * @deprecated Use size().
96 */
97 size_t
98 getEntryCount() const { return entries_.size(); }
99
100 /**
101 * @deprecated Use get(i).
102 */
103 const Exclude::Entry&
Jeff Thompson97223af2013-09-24 17:01:27 -0700104 getEntry(size_t i) const { return entries_[i]; }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700105
106 /**
Jeff Thompson8238d002013-07-10 11:56:49 -0700107 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -0700108 * 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 -0700109 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -0700110 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700111 void
112 get(struct ndn_Exclude& excludeStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700113
114 /**
Jeff Thompson8238d002013-07-10 11:56:49 -0700115 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -0700116 * @param excludeStruct a C ndn_Exclude struct
117 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700118 void
119 set(const struct ndn_Exclude& excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -0700120
121 /**
Jeff Thompson832d2972013-10-31 11:24:55 -0700122 * Append a new entry of type ndn_Exclude_ANY.
123 * @return This Exclude so that you can chain calls to append.
Jeff Thompsonfe556862013-07-09 13:52:55 -0700124 */
Jeff Thompson832d2972013-10-31 11:24:55 -0700125 Exclude&
126 appendAny()
Jeff Thompsonfe556862013-07-09 13:52:55 -0700127 {
Jeff Thompsonf62f9f22013-11-26 17:22:54 -0800128 entries_.push_back(Entry());
Jeff Thompson832d2972013-10-31 11:24:55 -0700129 return *this;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700130 }
131
132 /**
Jeff Thompsonb096e492013-10-31 11:29:28 -0700133 * Append a new entry of type ndn_Exclude_COMPONENT, copying from component of length componentLength.
Jeff Thompson832d2972013-10-31 11:24:55 -0700134 * @param component A pointer to the component byte array.
135 * @param componentLength The length of component.
136 * @return This Exclude so that you can chain calls to append.
Jeff Thompsonfe556862013-07-09 13:52:55 -0700137 */
Jeff Thompson832d2972013-10-31 11:24:55 -0700138 Exclude&
139 appendComponent(uint8_t *component, size_t componentLength)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700140 {
Jeff Thompsonf62f9f22013-11-26 17:22:54 -0800141 entries_.push_back(Entry(component, componentLength));
Jeff Thompson832d2972013-10-31 11:24:55 -0700142 return *this;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700143 }
Jeff Thompson832d2972013-10-31 11:24:55 -0700144
145 /**
Jeff Thompsonb096e492013-10-31 11:29:28 -0700146 * Append a new entry of type ndn_Exclude_COMPONENT, taking another pointer to the Blob value.
Jeff Thompson832d2972013-10-31 11:24:55 -0700147 * @param component A blob with a pointer to an immutable array. The pointer is copied.
148 * @return This Exclude so that you can chain calls to append.
149 */
150 Exclude&
151 appendComponent(const Blob &component)
152 {
Jeff Thompsonf62f9f22013-11-26 17:22:54 -0800153 entries_.push_back(Entry(component));
Jeff Thompson832d2972013-10-31 11:24:55 -0700154 return *this;
155 }
156
157 /**
158 * @deprecated Use appendAny.
159 */
160 Exclude&
161 addAny() { return appendAny(); }
162
163 /**
164 * @deprecated Use appendComponent.
165 */
166 Exclude&
167 addComponent(uint8_t *component, size_t componentLength) { return appendComponent(component, componentLength); }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700168
169 /**
170 * Clear all the entries.
171 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700172 void
Jeff Thompsonf62f9f22013-11-26 17:22:54 -0800173 clear()
174 {
Jeff Thompsonfe556862013-07-09 13:52:55 -0700175 entries_.clear();
176 }
177
Jeff Thompson37527d62013-08-21 11:15:54 -0700178 /**
179 * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*".
180 * @return the URI string
181 */
182 std::string toUri() const;
183
Jeff Thompsonfe556862013-07-09 13:52:55 -0700184private:
Jeff Thompsonf62f9f22013-11-26 17:22:54 -0800185 std::vector<Entry> entries_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700186};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700187
Jeff Thompson8238d002013-07-10 11:56:49 -0700188/**
189 * An Interest holds a Name and other fields for an interest.
190 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700191class Interest {
192public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800193 /**
194 * Create a new Interest for the given name and values.
195 * @param name
196 * @param minSuffixComponents
197 * @param maxSuffixComponents
198 * @param publisherPublicKeyDigest
199 * @param exclude
200 * @param childSelector
201 * @param answerOriginKind
202 * @param scope
203 * @param interestLifetimeMilliseconds
204 * @param nonce
205 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700206 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
207 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800208 int scope, Milliseconds interestLifetimeMilliseconds, const Blob& nonce)
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700209 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
210 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
211 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
212 nonce_(nonce)
213 {
214 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700215
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800216 /**
217 * Create a new Interest with the given name and values, and "none" for the nonce.
218 * @param name
219 * @param minSuffixComponents
220 * @param maxSuffixComponents
221 * @param publisherPublicKeyDigest
222 * @param exclude
223 * @param childSelector
224 * @param answerOriginKind
225 * @param scope
226 * @param interestLifetimeMilliseconds
227 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700228 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
229 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700230 int scope, Milliseconds interestLifetimeMilliseconds)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700231 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
232 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
233 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
234 {
235 }
236
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800237 /**
238 * Create a new Interest with the given name and interest lifetime and "none" for other values.
239 * @param name The name for the interest.
240 * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none.
241 */
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700242 Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700243 : name_(name)
244 {
245 construct();
246 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
247 }
248
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800249 /**
250 * Create a new Interest with the given name and "none" for other values.
251 * @param name The name for the interest.
252 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700253 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700254 : name_(name)
255 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700256 construct();
257 }
258
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800259 /**
260 * Create a new Interest with an empty name and "none" for all values.
261 */
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700262 Interest()
263 {
264 construct();
265 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700266
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800267 /**
268 * Encode this Interest for a particular wire format.
269 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
270 * @return The encoded byte array.
271 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700272 Blob
273 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700274 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700275 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700276 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700277
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800278 /**
279 * Decode the input using a particular wire format and update this Interest.
280 * @param input The input byte array to be decoded.
281 * @param inputLength The length of input.
282 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
283 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700284 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700285 wireDecode(const uint8_t *input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700286 {
287 wireFormat.decodeInterest(*this, input, inputLength);
288 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700289
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800290 /**
291 * Decode the input using a particular wire format and update this Interest.
292 * @param input The input byte array to be decoded.
293 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
294 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700295 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700296 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700297 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700298 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700299 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700300
301 /**
302 * Set the interestStruct to point to the components in this interest, without copying any memory.
303 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
304 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
305 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700306 void
307 get(struct ndn_Interest& interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700308
Jeff Thompson0050abe2013-09-17 12:50:25 -0700309 Name&
310 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700311
Jeff Thompson0050abe2013-09-17 12:50:25 -0700312 const Name&
313 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700314
Jeff Thompson0050abe2013-09-17 12:50:25 -0700315 int
316 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700317
Jeff Thompson0050abe2013-09-17 12:50:25 -0700318 int
319 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700320
Jeff Thompson0050abe2013-09-17 12:50:25 -0700321 PublisherPublicKeyDigest&
322 getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
323
324 const PublisherPublicKeyDigest&
325 getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700326
Jeff Thompson0050abe2013-09-17 12:50:25 -0700327 Exclude&
328 getExclude() { return exclude_; }
329
330 const Exclude&
331 getExclude() const { return exclude_; }
332
333 int
334 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700335
Jeff Thompson0050abe2013-09-17 12:50:25 -0700336 int
337 getAnswerOriginKind() const { return answerOriginKind_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700338
Jeff Thompson0050abe2013-09-17 12:50:25 -0700339 int
340 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700341
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700342 Milliseconds
Jeff Thompson0050abe2013-09-17 12:50:25 -0700343 getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
344
345 const Blob&
346 getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700347
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700348 /**
349 * Clear this interest, and set the values by copying from the interest struct.
350 * @param interestStruct a C ndn_Interest struct
351 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700352 void
353 set(const struct ndn_Interest& interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700354
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700355 void
356 setName(const Name& name) { name_ = name; }
357
Jeff Thompson0050abe2013-09-17 12:50:25 -0700358 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800359 setMinSuffixComponents(int minSuffixComponents) { minSuffixComponents_ = minSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700360
Jeff Thompson0050abe2013-09-17 12:50:25 -0700361 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800362 setMaxSuffixComponents(int maxSuffixComponents) { maxSuffixComponents_ = maxSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700363
Jeff Thompson0050abe2013-09-17 12:50:25 -0700364 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800365 setChildSelector(int childSelector) { childSelector_ = childSelector; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700366
Jeff Thompson0050abe2013-09-17 12:50:25 -0700367 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800368 setAnswerOriginKind(int answerOriginKind) { answerOriginKind_ = answerOriginKind; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700369
Jeff Thompson0050abe2013-09-17 12:50:25 -0700370 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800371 setScope(int scope) { scope_ = scope; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700372
Jeff Thompson0050abe2013-09-17 12:50:25 -0700373 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800374 setInterestLifetimeMilliseconds(Milliseconds interestLifetimeMilliseconds) { interestLifetimeMilliseconds_ = interestLifetimeMilliseconds; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700375
Jeff Thompson0050abe2013-09-17 12:50:25 -0700376 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800377 setNonce(const Blob& nonce) { nonce_ = nonce; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700378
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700379private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700380 void
381 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700382 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700383 minSuffixComponents_ = -1;
384 maxSuffixComponents_ = -1;
385 childSelector_ = -1;
386 answerOriginKind_ = -1;
387 scope_ = -1;
388 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700389 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700390
391 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700392 int minSuffixComponents_;
393 int maxSuffixComponents_;
394 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700395 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700396 int childSelector_;
397 int answerOriginKind_;
398 int scope_;
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700399 Milliseconds interestLifetimeMilliseconds_;
Jeff Thompson412226d2013-09-12 15:55:46 -0700400 Blob nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700401};
402
403}
404
405#endif