blob: 36a3859e09618969d1d8e7161a14325d7870b99f [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 Thompson832d2972013-10-31 11:24:55 -0700114 * Append a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength.
115 * @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 /**
127 * Append a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength.
128 * @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 Thompson1656e6a2013-08-29 18:01:48 -0700173 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
174 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700175 int scope, Milliseconds interestLifetimeMilliseconds, const std::vector<uint8_t>& nonce)
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700176 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
177 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
178 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
179 nonce_(nonce)
180 {
181 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700182
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700183 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
184 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700185 int scope, Milliseconds interestLifetimeMilliseconds)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700186 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
187 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
188 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
189 {
190 }
191
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700192 Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700193 : name_(name)
194 {
195 construct();
196 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
197 }
198
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700199 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700200 : name_(name)
201 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700202 construct();
203 }
204
205 Interest()
206 {
207 construct();
208 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700209
Jeff Thompson0050abe2013-09-17 12:50:25 -0700210 Blob
211 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700212 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700213 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700214 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700215
216 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700217 wireDecode(const uint8_t *input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700218 {
219 wireFormat.decodeInterest(*this, input, inputLength);
220 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700221
222 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700223 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700224 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700225 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700226 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700227
228 /**
229 * Set the interestStruct to point to the components in this interest, without copying any memory.
230 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
231 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
232 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700233 void
234 get(struct ndn_Interest& interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700235
Jeff Thompson0050abe2013-09-17 12:50:25 -0700236 Name&
237 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700238
Jeff Thompson0050abe2013-09-17 12:50:25 -0700239 const Name&
240 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700241
Jeff Thompson0050abe2013-09-17 12:50:25 -0700242 int
243 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700244
Jeff Thompson0050abe2013-09-17 12:50:25 -0700245 int
246 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700247
Jeff Thompson0050abe2013-09-17 12:50:25 -0700248 PublisherPublicKeyDigest&
249 getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
250
251 const PublisherPublicKeyDigest&
252 getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700253
Jeff Thompson0050abe2013-09-17 12:50:25 -0700254 Exclude&
255 getExclude() { return exclude_; }
256
257 const Exclude&
258 getExclude() const { return exclude_; }
259
260 int
261 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700262
Jeff Thompson0050abe2013-09-17 12:50:25 -0700263 int
264 getAnswerOriginKind() const { return answerOriginKind_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700265
Jeff Thompson0050abe2013-09-17 12:50:25 -0700266 int
267 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700268
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700269 Milliseconds
Jeff Thompson0050abe2013-09-17 12:50:25 -0700270 getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
271
272 const Blob&
273 getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700274
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700275 /**
276 * Clear this interest, and set the values by copying from the interest struct.
277 * @param interestStruct a C ndn_Interest struct
278 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700279 void
280 set(const struct ndn_Interest& interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700281
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700282 void
283 setName(const Name& name) { name_ = name; }
284
Jeff Thompson0050abe2013-09-17 12:50:25 -0700285 void
286 setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700287
Jeff Thompson0050abe2013-09-17 12:50:25 -0700288 void
289 setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700290
Jeff Thompson0050abe2013-09-17 12:50:25 -0700291 void
292 setChildSelector(int value) { childSelector_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700293
Jeff Thompson0050abe2013-09-17 12:50:25 -0700294 void
295 setAnswerOriginKind(int value) { answerOriginKind_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700296
Jeff Thompson0050abe2013-09-17 12:50:25 -0700297 void
298 setScope(int value) { scope_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700299
Jeff Thompson0050abe2013-09-17 12:50:25 -0700300 void
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700301 setInterestLifetimeMilliseconds(Milliseconds value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700302
Jeff Thompson0050abe2013-09-17 12:50:25 -0700303 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700304 setNonce(const std::vector<uint8_t>& value) { nonce_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700305
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700306private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700307 void
308 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700309 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700310 minSuffixComponents_ = -1;
311 maxSuffixComponents_ = -1;
312 childSelector_ = -1;
313 answerOriginKind_ = -1;
314 scope_ = -1;
315 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700316 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700317
318 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700319 int minSuffixComponents_;
320 int maxSuffixComponents_;
321 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700322 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700323 int childSelector_;
324 int answerOriginKind_;
325 int scope_;
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700326 Milliseconds interestLifetimeMilliseconds_;
Jeff Thompson412226d2013-09-12 15:55:46 -0700327 Blob nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700328};
329
330}
331
332#endif