blob: 3b285d8bb28e14c32bcb834fda43a7a876a60f07 [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 Thompson1656e6a2013-08-29 18:01:48 -070042 void get(struct ndn_ExcludeEntry& excludeEntryStruct) const
Jeff Thompsonfe556862013-07-09 13:52:55 -070043 {
44 excludeEntryStruct.type = type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070045 if (type_ == ndn_Exclude_COMPONENT)
46 component_.get(excludeEntryStruct.component);
Jeff Thompsonfe556862013-07-09 13:52:55 -070047 }
48
49 ndn_ExcludeType getType() const { return type_; }
50
Jeff Thompson1656e6a2013-08-29 18:01:48 -070051 const Name::Component& getComponent() const { return component_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070052
53private:
54 ndn_ExcludeType type_;
Jeff Thompson38d0e082013-08-12 18:07:44 -070055 Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
Jeff Thompsonfe556862013-07-09 13:52:55 -070056};
57
Jeff Thompson8238d002013-07-10 11:56:49 -070058/**
59 * An Exclude holds a vector of ExcludeEntry.
60 */
Jeff Thompsonfe556862013-07-09 13:52:55 -070061class Exclude {
62public:
63 /**
64 * Create a new Exclude with no entries.
65 */
66 Exclude() {
67 }
68
69 unsigned int getEntryCount() const {
70 return entries_.size();
71 }
72
Jeff Thompson1656e6a2013-08-29 18:01:48 -070073 const ExcludeEntry& getEntry(unsigned int i) const { return entries_[i]; }
Jeff Thompsonfe556862013-07-09 13:52:55 -070074
75 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070076 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070077 * 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 -070078 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070079 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070080 void get(struct ndn_Exclude& excludeStruct) const;
Jeff Thompsonfe556862013-07-09 13:52:55 -070081
82 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070083 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070084 * @param excludeStruct a C ndn_Exclude struct
85 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070086 void set(const struct ndn_Exclude& excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -070087
88 /**
89 * Add a new entry of type ndn_Exclude_ANY
90 */
91 void addAny()
92 {
93 entries_.push_back(ExcludeEntry());
94 }
95
96 /**
97 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
98 */
99 void addComponent(unsigned char *component, unsigned int componentLen)
100 {
101 entries_.push_back(ExcludeEntry(component, componentLen));
102 }
103
104 /**
105 * Clear all the entries.
106 */
107 void clear() {
108 entries_.clear();
109 }
110
Jeff Thompson37527d62013-08-21 11:15:54 -0700111 /**
112 * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*".
113 * @return the URI string
114 */
115 std::string toUri() const;
116
Jeff Thompsonfe556862013-07-09 13:52:55 -0700117private:
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700118 std::vector<ExcludeEntry> entries_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700119};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700120
Jeff Thompson8238d002013-07-10 11:56:49 -0700121/**
122 * An Interest holds a Name and other fields for an interest.
123 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700124class Interest {
125public:
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700126 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
127 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
128 int scope, double interestLifetimeMilliseconds, const std::vector<unsigned char>& nonce)
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700129 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
130 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
131 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds),
132 nonce_(nonce)
133 {
134 }
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700135
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700136 Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents,
137 const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind,
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700138 int scope, double interestLifetimeMilliseconds)
139 : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents),
140 publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector),
141 answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds)
142 {
143 }
144
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700145 Interest(const Name& name, double interestLifetimeMilliseconds)
Jeff Thompsonf62382a2013-08-21 16:33:39 -0700146 : name_(name)
147 {
148 construct();
149 interestLifetimeMilliseconds_ = interestLifetimeMilliseconds;
150 }
151
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700152 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700153 : name_(name)
154 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -0700155 construct();
156 }
157
158 Interest()
159 {
160 construct();
161 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -0700162
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700163 Blob wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700164 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700165 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700166 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700167 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700168 {
169 wireFormat.decodeInterest(*this, input, inputLength);
170 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700171 void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700172 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700173 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700174 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700175
176 /**
177 * Set the interestStruct to point to the components in this interest, without copying any memory.
178 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
179 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
180 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700181 void get(struct ndn_Interest& interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700182
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700183 Name& getName() { return name_; }
184 const Name& getName() const { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700185
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700186 int getMinSuffixComponents() const { return minSuffixComponents_; }
187
188 int getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700189
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700190 PublisherPublicKeyDigest& getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
191 const PublisherPublicKeyDigest& getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700192
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700193 Exclude& getExclude() { return exclude_; }
194 const Exclude& getExclude() const { return exclude_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700195
196 int getChildSelector() const { return childSelector_; }
197
198 int getAnswerOriginKind() const { return answerOriginKind_; }
199
200 int getScope() const { return scope_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700201
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700202 double getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700203
Jeff Thompson412226d2013-09-12 15:55:46 -0700204 const Blob& getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700205
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700206 /**
207 * Clear this interest, and set the values by copying from the interest struct.
208 * @param interestStruct a C ndn_Interest struct
209 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700210 void set(const struct ndn_Interest& interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700211
212 void setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
213
214 void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
215
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700216 void setChildSelector(int value) { childSelector_ = value; }
217
218 void setAnswerOriginKind(int value) { answerOriginKind_ = value; }
219
220 void setScope(int value) { scope_ = value; }
221
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700222 void setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700223
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700224 void setNonce(const std::vector<unsigned char>& value) { nonce_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700225
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700226private:
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700227 void construct()
228 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700229 minSuffixComponents_ = -1;
230 maxSuffixComponents_ = -1;
231 childSelector_ = -1;
232 answerOriginKind_ = -1;
233 scope_ = -1;
234 interestLifetimeMilliseconds_ = -1.0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700235 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700236
237 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700238 int minSuffixComponents_;
239 int maxSuffixComponents_;
240 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700241 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700242 int childSelector_;
243 int answerOriginKind_;
244 int scope_;
245 double interestLifetimeMilliseconds_;
Jeff Thompson412226d2013-09-12 15:55:46 -0700246 Blob nonce_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700247};
248
249}
250
251#endif