blob: 64746bf66def95ad9f81822d6c1f11feac11b0e3 [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
7#define NDN_INTEREST_HPP
8
Jeff Thompsonb7f95562013-07-03 18:36:42 -07009#include "Name.hpp"
Jeff Thompson8238d002013-07-10 11:56:49 -070010#include "PublisherPublicKeyDigest.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070011#include "c/Interest.h"
12
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)
32 : type_(ndn_Exclude_COMPONENT), component_(component, component + componentLen)
33 {
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_;
44 if (type_ == ndn_Exclude_COMPONENT) {
45 excludeEntryStruct.componentLength = component_.size();
46 excludeEntryStruct.component = (unsigned char *)&component_[0];
47 }
48 }
49
50 ndn_ExcludeType getType() const { return type_; }
51
52 const std::vector<unsigned char> &getComponent() const { return component_; }
53
54private:
55 ndn_ExcludeType type_;
56 std::vector<unsigned char> component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
57};
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
70 unsigned int getEntryCount() const {
71 return entries_.size();
72 }
73
74 const ExcludeEntry &getEntry(unsigned int i) const { return entries_[i]; }
75
76 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070077 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070078 * 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 -070079 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070080 */
81 void get(struct ndn_Exclude &excludeStruct) const;
82
83 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070084 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070085 * @param excludeStruct a C ndn_Exclude struct
86 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -070087 void set(const struct ndn_Exclude &excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -070088
89 /**
90 * Add a new entry of type ndn_Exclude_ANY
91 */
92 void addAny()
93 {
94 entries_.push_back(ExcludeEntry());
95 }
96
97 /**
98 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
99 */
100 void addComponent(unsigned char *component, unsigned int componentLen)
101 {
102 entries_.push_back(ExcludeEntry(component, componentLen));
103 }
104
105 /**
106 * Clear all the entries.
107 */
108 void clear() {
109 entries_.clear();
110 }
111
112private:
113 std::vector<ExcludeEntry> entries_;
114};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700115
Jeff Thompson8238d002013-07-10 11:56:49 -0700116/**
117 * An Interest holds a Name and other fields for an interest.
118 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700119class Interest {
120public:
Jeff Thompson34238652013-07-10 18:25:00 -0700121 Interest()
122 {
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700123 construct();
124 }
125
126 Interest(const Name &name)
127 {
128 name_ = name;
129 construct();
Jeff Thompson34238652013-07-10 18:25:00 -0700130 }
131
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700132 ptr_lib::shared_ptr<std::vector<unsigned char> > encode(WireFormat &wireFormat) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700133 {
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700134 return wireFormat.encodeInterest(*this);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700135 }
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700136 ptr_lib::shared_ptr<std::vector<unsigned char> > encode() const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700137 {
Jeff Thompsonb915b1d2013-07-31 10:53:13 -0700138 return encode(BinaryXmlWireFormat::getInstance());
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700139 }
140 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
141 {
142 wireFormat.decodeInterest(*this, input, inputLength);
143 }
144 void decode(const unsigned char *input, unsigned int inputLength)
145 {
Jeff Thompsonb915b1d2013-07-31 10:53:13 -0700146 decode(input, inputLength, BinaryXmlWireFormat::getInstance());
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700147 }
148 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
149 {
150 decode(&input[0], input.size(), wireFormat);
151 }
152 void decode(const std::vector<unsigned char> &input)
153 {
154 decode(&input[0], input.size());
155 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700156
157 /**
158 * Set the interestStruct to point to the components in this interest, without copying any memory.
159 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
160 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
161 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700162 void get(struct ndn_Interest &interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700163
Jeff Thompson12c27762013-07-09 15:05:11 -0700164 Name &getName() { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700165 const Name &getName() const { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700166
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700167 int getMinSuffixComponents() const { return minSuffixComponents_; }
168
169 int getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700170
Jeff Thompson8238d002013-07-10 11:56:49 -0700171 PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
172 const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700173
Jeff Thompson12c27762013-07-09 15:05:11 -0700174 Exclude &getExclude() { return exclude_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700175 const Exclude &getExclude() const { return exclude_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700176
177 int getChildSelector() const { return childSelector_; }
178
179 int getAnswerOriginKind() const { return answerOriginKind_; }
180
181 int getScope() const { return scope_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700182
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700183 double getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700184
185 const std::vector<unsigned char> getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700186
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700187 /**
188 * Clear this interest, and set the values by copying from the interest struct.
189 * @param interestStruct a C ndn_Interest struct
190 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700191 void set(const struct ndn_Interest &interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700192
193 void setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
194
195 void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
196
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700197 void setChildSelector(int value) { childSelector_ = value; }
198
199 void setAnswerOriginKind(int value) { answerOriginKind_ = value; }
200
201 void setScope(int value) { scope_ = value; }
202
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700203 void setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700204
205 void setNonce(const std::vector<unsigned char> &value) { nonce_ = value; }
206
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700207private:
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700208 void construct()
209 {
210 minSuffixComponents_ = -1;
211 maxSuffixComponents_ = -1;
212 childSelector_ = -1;
213 answerOriginKind_ = -1;
214 scope_ = -1;
215 interestLifetimeMilliseconds_ = -1.0;
216 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700217
218 Name name_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700219 int minSuffixComponents_;
Jeff Thompsonf2e5e282013-07-08 15:26:16 -0700220 int maxSuffixComponents_;
Jeff Thompson8238d002013-07-10 11:56:49 -0700221 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700222 Exclude exclude_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700223 int childSelector_;
224 int answerOriginKind_;
225 int scope_;
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700226 double interestLifetimeMilliseconds_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700227 std::vector<unsigned char> nonce_;
228};
229
230}
231
232#endif