blob: 2c9688ad798307024fadb0065de72cd2b38f246c [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 /**
36 * Create an ExcludeEntry of type ndn_Exclude_COMPONENT
37 */
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 /**
44 * Set the type in the excludeEntryStruct and to point to this component, without copying any memory.
45 * 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 -070046 * @param excludeEntryStruct the C ndn_NameComponent struct to receive the pointer
Jeff Thompsonfe556862013-07-09 13:52:55 -070047 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 void
Jeff Thompson25b4e612013-10-10 16:03:24 -070049 get(struct ndn_ExcludeEntry& excludeEntryStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -070050
51 ndn_ExcludeType getType() const { return type_; }
52
Jeff Thompson1656e6a2013-08-29 18:01:48 -070053 const Name::Component& getComponent() const { return component_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070054
55private:
56 ndn_ExcludeType type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070057 Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
Jeff Thompsonfe556862013-07-09 13:52:55 -070058};
59
Jeff Thompson8238d002013-07-10 11:56:49 -070060/**
61 * An Exclude holds a vector of ExcludeEntry.
62 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070063class Exclude {
64public:
65 /**
66 * Create a new Exclude with no entries.
67 */
68 Exclude() {
69 }
70
Jeff Thompson97223af2013-09-24 17:01:27 -070071 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070072 getEntryCount() const {
Jeff Thompsonfe556862013-07-09 13:52:55 -070073 return entries_.size();
74 }
75
Jeff Thompson0050abe2013-09-17 12:50:25 -070076 const ExcludeEntry&
Jeff Thompson97223af2013-09-24 17:01:27 -070077 getEntry(size_t i) const { return entries_[i]; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070078
79 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070080 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070081 * 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 -070082 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070083 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070084 void
85 get(struct ndn_Exclude& excludeStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -070086
87 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070088 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070089 * @param excludeStruct a C ndn_Exclude struct
90 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070091 void
92 set(const struct ndn_Exclude& excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -070093
94 /**
95 * Add a new entry of type ndn_Exclude_ANY
96 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070097 void
98 addAny()
Jeff Thompsonfe556862013-07-09 13:52:55 -070099 {
100 entries_.push_back(ExcludeEntry());
101 }
102
103 /**
104 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
105 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700106 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700107 addComponent(uint8_t *component, size_t componentLen)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700108 {
109 entries_.push_back(ExcludeEntry(component, componentLen));
110 }
111
112 /**
113 * Clear all the entries.
114 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700115 void
116 clear() {
Jeff Thompsonfe556862013-07-09 13:52:55 -0700117 entries_.clear();
118 }
119
Jeff Thompson37527d62013-08-21 11:15:54 -0700120 /**
121 * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*".
122 * @return the URI string
123 */
124 std::string toUri() const;
125
Jeff Thompsonfe556862013-07-09 13:52:55 -0700126private:
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700127 std::vector<ExcludeEntry> entries_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700128};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700129
Jeff Thompson8238d002013-07-10 11:56:49 -0700130/**
131 * An Interest holds a Name and other fields for an interest.
132 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700133class Interest {
134public:
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700135 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
136 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700137 int scope, Milliseconds interestLifetimeMilliseconds, const std::vector<uint8_t>& nonce)
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700138 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
139 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
140 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
141 nonce_(nonce)
142 {
143 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700144
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700145 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
146 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700147 int scope, Milliseconds interestLifetimeMilliseconds)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700148 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
149 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
150 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
151 {
152 }
153
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700154 Interest(const Name& name, Milliseconds interestLifetimeMilliseconds)
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700155 : name_(name)
156 {
157 construct();
158 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
159 }
160
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700161 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700162 : name_(name)
163 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700164 construct();
165 }
166
167 Interest()
168 {
169 construct();
170 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700171
Jeff Thompson0050abe2013-09-17 12:50:25 -0700172 Blob
173 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700174 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700175 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700176 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700177
178 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700179 wireDecode(const uint8_t *input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700180 {
181 wireFormat.decodeInterest(*this, input, inputLength);
182 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700183
184 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700185 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700186 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700187 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700188 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700189
190 /**
191 * Set the interestStruct to point to the components in this interest, without copying any memory.
192 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
193 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
194 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700195 void
196 get(struct ndn_Interest& interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700197
Jeff Thompson0050abe2013-09-17 12:50:25 -0700198 Name&
199 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700200
Jeff Thompson0050abe2013-09-17 12:50:25 -0700201 const Name&
202 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700203
Jeff Thompson0050abe2013-09-17 12:50:25 -0700204 int
205 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700206
Jeff Thompson0050abe2013-09-17 12:50:25 -0700207 int
208 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700209
Jeff Thompson0050abe2013-09-17 12:50:25 -0700210 PublisherPublicKeyDigest&
211 getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
212
213 const PublisherPublicKeyDigest&
214 getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700215
Jeff Thompson0050abe2013-09-17 12:50:25 -0700216 Exclude&
217 getExclude() { return exclude_; }
218
219 const Exclude&
220 getExclude() const { return exclude_; }
221
222 int
223 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700224
Jeff Thompson0050abe2013-09-17 12:50:25 -0700225 int
226 getAnswerOriginKind() const { return answerOriginKind_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700227
Jeff Thompson0050abe2013-09-17 12:50:25 -0700228 int
229 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700230
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700231 Milliseconds
Jeff Thompson0050abe2013-09-17 12:50:25 -0700232 getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
233
234 const Blob&
235 getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700236
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700237 /**
238 * Clear this interest, and set the values by copying from the interest struct.
239 * @param interestStruct a C ndn_Interest struct
240 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700241 void
242 set(const struct ndn_Interest& interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700243
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700244 void
245 setName(const Name& name) { name_ = name; }
246
Jeff Thompson0050abe2013-09-17 12:50:25 -0700247 void
248 setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700249
Jeff Thompson0050abe2013-09-17 12:50:25 -0700250 void
251 setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700252
Jeff Thompson0050abe2013-09-17 12:50:25 -0700253 void
254 setChildSelector(int value) { childSelector_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700255
Jeff Thompson0050abe2013-09-17 12:50:25 -0700256 void
257 setAnswerOriginKind(int value) { answerOriginKind_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700258
Jeff Thompson0050abe2013-09-17 12:50:25 -0700259 void
260 setScope(int value) { scope_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700261
Jeff Thompson0050abe2013-09-17 12:50:25 -0700262 void
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700263 setInterestLifetimeMilliseconds(Milliseconds value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700264
Jeff Thompson0050abe2013-09-17 12:50:25 -0700265 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700266 setNonce(const std::vector<uint8_t>& value) { nonce_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700267
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700268private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700269 void
270 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700271 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700272 minSuffixComponents_ = -1;
273 maxSuffixComponents_ = -1;
274 childSelector_ = -1;
275 answerOriginKind_ = -1;
276 scope_ = -1;
277 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700278 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700279
280 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700281 int minSuffixComponents_;
282 int maxSuffixComponents_;
283 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700284 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700285 int childSelector_;
286 int answerOriginKind_;
287 int scope_;
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700288 Milliseconds interestLifetimeMilliseconds_;
Jeff Thompson412226d2013-09-12 15:55:46 -0700289 Blob nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700290};
291
292}
293
294#endif