blob: 749363f308a51f94a8798a2ef9f4f12fa540d988 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
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
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "name.hpp"
11#include "publisher-public-key-digest.hpp"
12#include "c/interest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070013
14namespace ndn {
Jeff Thompsonfe556862013-07-09 13:52:55 -070015
Jeff Thompson8238d002013-07-10 11:56:49 -070016/**
17 * An ExcludeEntry holds an ndn_ExcludeType, and if it is a COMPONENT, it holds the component value.
18 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070019class ExcludeEntry {
20public:
21 /**
22 * Create an ExcludeEntry of type ndn_Exclude_ANY
23 */
24 ExcludeEntry()
25 : type_(ndn_Exclude_ANY)
26 {
27 }
28
29 /**
30 * Create an ExcludeEntry of type ndn_Exclude_COMPONENT
31 */
32 ExcludeEntry(unsigned char *component, unsigned int componentLen)
Jeff Thompson38d0e082013-08-12 18:07:44 -070033 : type_(ndn_Exclude_COMPONENT), component_(component, componentLen)
Jeff Thompsonfe556862013-07-09 13:52:55 -070034 {
35 }
36
37 /**
38 * Set the type in the excludeEntryStruct and to point to this component, without copying any memory.
39 * 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 -070040 * @param excludeEntryStruct the C ndn_NameComponent struct to receive the pointer
Jeff Thompsonfe556862013-07-09 13:52:55 -070041 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070042 void
43 get(struct ndn_ExcludeEntry& excludeEntryStruct) const
Jeff Thompsonfe556862013-07-09 13:52:55 -070044 {
45 excludeEntryStruct.type = type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070046 if (type_ == ndn_Exclude_COMPONENT)
47 component_.get(excludeEntryStruct.component);
Jeff Thompsonfe556862013-07-09 13:52:55 -070048 }
49
50 ndn_ExcludeType getType() const { return type_; }
51
Jeff Thompson1656e6a2013-08-29 18:01:48 -070052 const Name::Component& getComponent() const { return component_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070053
54private:
55 ndn_ExcludeType type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070056 Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
Jeff Thompsonfe556862013-07-09 13:52:55 -070057};
58
Jeff Thompson8238d002013-07-10 11:56:49 -070059/**
60 * An Exclude holds a vector of ExcludeEntry.
61 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070062class Exclude {
63public:
64 /**
65 * Create a new Exclude with no entries.
66 */
67 Exclude() {
68 }
69
Jeff Thompson0050abe2013-09-17 12:50:25 -070070 unsigned int
71 getEntryCount() const {
Jeff Thompsonfe556862013-07-09 13:52:55 -070072 return entries_.size();
73 }
74
Jeff Thompson0050abe2013-09-17 12:50:25 -070075 const ExcludeEntry&
76 getEntry(unsigned int i) const { return entries_[i]; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070077
78 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070079 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070080 * 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 -070081 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070082 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070083 void
84 get(struct ndn_Exclude& excludeStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -070085
86 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070087 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070088 * @param excludeStruct a C ndn_Exclude struct
89 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070090 void
91 set(const struct ndn_Exclude& excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -070092
93 /**
94 * Add a new entry of type ndn_Exclude_ANY
95 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070096 void
97 addAny()
Jeff Thompsonfe556862013-07-09 13:52:55 -070098 {
99 entries_.push_back(ExcludeEntry());
100 }
101
102 /**
103 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
104 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105 void
106 addComponent(unsigned char *component, unsigned int componentLen)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700107 {
108 entries_.push_back(ExcludeEntry(component, componentLen));
109 }
110
111 /**
112 * Clear all the entries.
113 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700114 void
115 clear() {
Jeff Thompsonfe556862013-07-09 13:52:55 -0700116 entries_.clear();
117 }
118
Jeff Thompson37527d62013-08-21 11:15:54 -0700119 /**
120 * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*".
121 * @return the URI string
122 */
123 std::string toUri() const;
124
Jeff Thompsonfe556862013-07-09 13:52:55 -0700125private:
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700126 std::vector<ExcludeEntry> entries_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700127};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700128
Jeff Thompson8238d002013-07-10 11:56:49 -0700129/**
130 * An Interest holds a Name and other fields for an interest.
131 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700132class Interest {
133public:
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700134 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
135 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
136 int scope, double interestLifetimeMilliseconds, const std::vector<unsigned char>& nonce)
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700137 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
138 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
139 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
140 nonce_(nonce)
141 {
142 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700143
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700144 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
145 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700146 int scope, double interestLifetimeMilliseconds)
147 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
148 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
149 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
150 {
151 }
152
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700153 Interest(const Name& name, double interestLifetimeMilliseconds)
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700154 : name_(name)
155 {
156 construct();
157 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
158 }
159
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700160 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700161 : name_(name)
162 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700163 construct();
164 }
165
166 Interest()
167 {
168 construct();
169 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700170
Jeff Thompson0050abe2013-09-17 12:50:25 -0700171 Blob
172 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700173 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700174 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700175 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700176
177 void
178 wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700179 {
180 wireFormat.decodeInterest(*this, input, inputLength);
181 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700182
183 void
184 wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700185 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700186 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700187 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700188
189 /**
190 * Set the interestStruct to point to the components in this interest, without copying any memory.
191 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
192 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
193 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700194 void
195 get(struct ndn_Interest& interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700196
Jeff Thompson0050abe2013-09-17 12:50:25 -0700197 Name&
198 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700199
Jeff Thompson0050abe2013-09-17 12:50:25 -0700200 const Name&
201 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700202
Jeff Thompson0050abe2013-09-17 12:50:25 -0700203 int
204 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700205
Jeff Thompson0050abe2013-09-17 12:50:25 -0700206 int
207 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700208
Jeff Thompson0050abe2013-09-17 12:50:25 -0700209 PublisherPublicKeyDigest&
210 getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
211
212 const PublisherPublicKeyDigest&
213 getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700214
Jeff Thompson0050abe2013-09-17 12:50:25 -0700215 Exclude&
216 getExclude() { return exclude_; }
217
218 const Exclude&
219 getExclude() const { return exclude_; }
220
221 int
222 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700223
Jeff Thompson0050abe2013-09-17 12:50:25 -0700224 int
225 getAnswerOriginKind() const { return answerOriginKind_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700226
Jeff Thompson0050abe2013-09-17 12:50:25 -0700227 int
228 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700229
Jeff Thompson0050abe2013-09-17 12:50:25 -0700230 double
231 getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
232
233 const Blob&
234 getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700235
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700236 /**
237 * Clear this interest, and set the values by copying from the interest struct.
238 * @param interestStruct a C ndn_Interest struct
239 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700240 void
241 set(const struct ndn_Interest& interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700242
Jeff Thompson0050abe2013-09-17 12:50:25 -0700243 void
244 setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700245
Jeff Thompson0050abe2013-09-17 12:50:25 -0700246 void
247 setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700248
Jeff Thompson0050abe2013-09-17 12:50:25 -0700249 void
250 setChildSelector(int value) { childSelector_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700251
Jeff Thompson0050abe2013-09-17 12:50:25 -0700252 void
253 setAnswerOriginKind(int value) { answerOriginKind_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700254
Jeff Thompson0050abe2013-09-17 12:50:25 -0700255 void
256 setScope(int value) { scope_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700257
Jeff Thompson0050abe2013-09-17 12:50:25 -0700258 void
259 setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700260
Jeff Thompson0050abe2013-09-17 12:50:25 -0700261 void
262 setNonce(const std::vector<unsigned char>& value) { nonce_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700263
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700264private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700265 void
266 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700267 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700268 minSuffixComponents_ = -1;
269 maxSuffixComponents_ = -1;
270 childSelector_ = -1;
271 answerOriginKind_ = -1;
272 scope_ = -1;
273 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700274 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700275
276 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700277 int minSuffixComponents_;
278 int maxSuffixComponents_;
279 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700280 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700281 int childSelector_;
282 int answerOriginKind_;
283 int scope_;
284 double interestLifetimeMilliseconds_;
Jeff Thompson412226d2013-09-12 15:55:46 -0700285 Blob nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700286};
287
288}
289
290#endif