blob: e4aeeb19e9c5a714cd80d0fc64c450d84e486693 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07004 */
5
6#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -07008
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "name.hpp"
10#include "publisher-public-key-digest.hpp"
11#include "c/interest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070012
13namespace ndn {
Jeff Thompsonfe556862013-07-09 13:52:55 -070014
Jeff Thompson8238d002013-07-10 11:56:49 -070015/**
16 * An ExcludeEntry holds an ndn_ExcludeType, and if it is a COMPONENT, it holds the component value.
17 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070018class ExcludeEntry {
19public:
20 /**
21 * Create an ExcludeEntry of type ndn_Exclude_ANY
22 */
23 ExcludeEntry()
24 : type_(ndn_Exclude_ANY)
25 {
26 }
27
28 /**
29 * Create an ExcludeEntry of type ndn_Exclude_COMPONENT
30 */
31 ExcludeEntry(unsigned char *component, unsigned int componentLen)
Jeff Thompson38d0e082013-08-12 18:07:44 -070032 : type_(ndn_Exclude_COMPONENT), component_(component, componentLen)
Jeff Thompsonfe556862013-07-09 13:52:55 -070033 {
34 }
35
36 /**
37 * Set the type in the excludeEntryStruct and to point to this component, without copying any memory.
38 * 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 -070039 * @param excludeEntryStruct the C ndn_NameComponent struct to receive the pointer
Jeff Thompsonfe556862013-07-09 13:52:55 -070040 */
41 void get(struct ndn_ExcludeEntry &excludeEntryStruct) const
42 {
43 excludeEntryStruct.type = type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070044 if (type_ == ndn_Exclude_COMPONENT)
45 component_.get(excludeEntryStruct.component);
Jeff Thompsonfe556862013-07-09 13:52:55 -070046 }
47
48 ndn_ExcludeType getType() const { return type_; }
49
Jeff Thompson38d0e082013-08-12 18:07:44 -070050 const Name::Component &getComponent() const { return component_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070051
52private:
53 ndn_ExcludeType type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070054 Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
Jeff Thompsonfe556862013-07-09 13:52:55 -070055};
56
Jeff Thompson8238d002013-07-10 11:56:49 -070057/**
58 * An Exclude holds a vector of ExcludeEntry.
59 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070060class Exclude {
61public:
62 /**
63 * Create a new Exclude with no entries.
64 */
65 Exclude() {
66 }
67
68 unsigned int getEntryCount() const {
69 return entries_.size();
70 }
71
72 const ExcludeEntry &getEntry(unsigned int i) const { return entries_[i]; }
73
74 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070075 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070076 * 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 -070077 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070078 */
79 void get(struct ndn_Exclude &excludeStruct) const;
80
81 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070082 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070083 * @param excludeStruct a C ndn_Exclude struct
84 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -070085 void set(const struct ndn_Exclude &excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -070086
87 /**
88 * Add a new entry of type ndn_Exclude_ANY
89 */
90 void addAny()
91 {
92 entries_.push_back(ExcludeEntry());
93 }
94
95 /**
96 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
97 */
98 void addComponent(unsigned char *component, unsigned int componentLen)
99 {
100 entries_.push_back(ExcludeEntry(component, componentLen));
101 }
102
103 /**
104 * Clear all the entries.
105 */
106 void clear() {
107 entries_.clear();
108 }
109
Jeff Thompson37527d62013-08-21 11:15:54 -0700110 /**
111 * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*".
112 * @return the URI string
113 */
114 std::string toUri() const;
115
Jeff Thompsonfe556862013-07-09 13:52:55 -0700116private:
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700117 std::vector<ExcludeEntry> entries_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700118};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700119
Jeff Thompson8238d002013-07-10 11:56:49 -0700120/**
121 * An Interest holds a Name and other fields for an interest.
122 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700123class Interest {
124public:
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700125 Interest(const Name &name, int minSuffixComponents, int maxSuffixComponents,
126 const PublisherPublicKeyDigest &publisherPublicKeyDigest, const Exclude &exclude, int childSelector, int answerOriginKind,
127 int scope, double interestLifetimeMilliseconds, const std::vector<unsigned char> &nonce)
128 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
129 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
130 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
131 nonce_(nonce)
132 {
133 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700134
135 Interest(const Name &name, int minSuffixComponents, int maxSuffixComponents,
136 const PublisherPublicKeyDigest &publisherPublicKeyDigest, const Exclude &exclude, int childSelector, int answerOriginKind,
137 int scope, double interestLifetimeMilliseconds)
138 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
139 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
140 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
141 {
142 }
143
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700144 Interest(const Name &name, double interestLifetimeMilliseconds)
145 : name_(name)
146 {
147 construct();
148 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
149 }
150
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700151 Interest(const Name &name)
152 : name_(name)
153 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700154 construct();
155 }
156
157 Interest()
158 {
159 construct();
160 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700161
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700162 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700163 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700164 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700165 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700166 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700167 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700168 return wireEncode(*WireFormat::getDefaultWireFormat());
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700169 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700170 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700171 {
172 wireFormat.decodeInterest(*this, input, inputLength);
173 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700174 void wireDecode(const unsigned char *input, unsigned int inputLength)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700175 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700176 wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700177 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700178 void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700179 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700180 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700181 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700182 void wireDecode(const std::vector<unsigned char> &input)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700183 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700184 wireDecode(&input[0], input.size());
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700185 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700186
187 /**
188 * Set the interestStruct to point to the components in this interest, without copying any memory.
189 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
190 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
191 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700192 void get(struct ndn_Interest &interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700193
Jeff Thompson12c27762013-07-09 15:05:11 -0700194 Name &getName() { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700195 const Name &getName() const { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700196
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700197 int getMinSuffixComponents() const { return minSuffixComponents_; }
198
199 int getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700200
Jeff Thompson8238d002013-07-10 11:56:49 -0700201 PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
202 const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700203
Jeff Thompson12c27762013-07-09 15:05:11 -0700204 Exclude &getExclude() { return exclude_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700205 const Exclude &getExclude() const { return exclude_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700206
207 int getChildSelector() const { return childSelector_; }
208
209 int getAnswerOriginKind() const { return answerOriginKind_; }
210
211 int getScope() const { return scope_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700212
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700213 double getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700214
Jeff Thompson4962c622013-08-13 13:47:21 -0700215 const std::vector<unsigned char> &getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700216
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700217 /**
218 * Clear this interest, and set the values by copying from the interest struct.
219 * @param interestStruct a C ndn_Interest struct
220 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700221 void set(const struct ndn_Interest &interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700222
223 void setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
224
225 void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
226
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700227 void setChildSelector(int value) { childSelector_ = value; }
228
229 void setAnswerOriginKind(int value) { answerOriginKind_ = value; }
230
231 void setScope(int value) { scope_ = value; }
232
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700233 void setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700234
235 void setNonce(const std::vector<unsigned char> &value) { nonce_ = value; }
236
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700237private:
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700238 void construct()
239 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700240 minSuffixComponents_ = -1;
241 maxSuffixComponents_ = -1;
242 childSelector_ = -1;
243 answerOriginKind_ = -1;
244 scope_ = -1;
245 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700246 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700247
248 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700249 int minSuffixComponents_;
250 int maxSuffixComponents_;
251 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700252 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700253 int childSelector_;
254 int answerOriginKind_;
255 int scope_;
256 double interestLifetimeMilliseconds_;
257 std::vector<unsigned char> nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700258};
259
260}
261
262#endif