blob: 36e78ca22b7126e333973f622cf271ae83e4ac2e [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
9#include <vector>
10#include "Name.hpp"
Jeff Thompson8238d002013-07-10 11:56:49 -070011#include "PublisherPublicKeyDigest.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070012#include "c/Interest.h"
13
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)
33 : type_(ndn_Exclude_COMPONENT), component_(component, component + componentLen)
34 {
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 */
42 void get(struct ndn_ExcludeEntry &excludeEntryStruct) const
43 {
44 excludeEntryStruct.type = type_;
45 if (type_ == ndn_Exclude_COMPONENT) {
46 excludeEntryStruct.componentLength = component_.size();
47 excludeEntryStruct.component = (unsigned char *)&component_[0];
48 }
49 }
50
51 ndn_ExcludeType getType() const { return type_; }
52
53 const std::vector<unsigned char> &getComponent() const { return component_; }
54
55private:
56 ndn_ExcludeType type_;
57 std::vector<unsigned char> component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
58};
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
71 unsigned int getEntryCount() const {
72 return entries_.size();
73 }
74
75 const ExcludeEntry &getEntry(unsigned int i) const { return entries_[i]; }
76
77 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070078 * Set the excludeStruct to point to the entries in this Exclude, without copying any memory.
Jeff Thompsonfe556862013-07-09 13:52:55 -070079 * 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 -070080 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated
Jeff Thompsonfe556862013-07-09 13:52:55 -070081 */
82 void get(struct ndn_Exclude &excludeStruct) const;
83
84 /**
Jeff Thompson8238d002013-07-10 11:56:49 -070085 * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct.
Jeff Thompsonfe556862013-07-09 13:52:55 -070086 * @param excludeStruct a C ndn_Exclude struct
87 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -070088 void set(const struct ndn_Exclude &excludeStruct);
Jeff Thompsonfe556862013-07-09 13:52:55 -070089
90 /**
91 * Add a new entry of type ndn_Exclude_ANY
92 */
93 void addAny()
94 {
95 entries_.push_back(ExcludeEntry());
96 }
97
98 /**
99 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
100 */
101 void addComponent(unsigned char *component, unsigned int componentLen)
102 {
103 entries_.push_back(ExcludeEntry(component, componentLen));
104 }
105
106 /**
107 * Clear all the entries.
108 */
109 void clear() {
110 entries_.clear();
111 }
112
113private:
114 std::vector<ExcludeEntry> entries_;
115};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700116
Jeff Thompson8238d002013-07-10 11:56:49 -0700117/**
118 * An Interest holds a Name and other fields for an interest.
119 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700120class Interest {
121public:
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700122 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700123 {
124 wireFormat.encodeInterest(*this, output);
125 }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700126 void encode(std::vector<unsigned char> &output) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700127 {
128 encode(output, BinaryXMLWireFormat::instance());
129 }
130 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
131 {
132 wireFormat.decodeInterest(*this, input, inputLength);
133 }
134 void decode(const unsigned char *input, unsigned int inputLength)
135 {
136 decode(input, inputLength, BinaryXMLWireFormat::instance());
137 }
138 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
139 {
140 decode(&input[0], input.size(), wireFormat);
141 }
142 void decode(const std::vector<unsigned char> &input)
143 {
144 decode(&input[0], input.size());
145 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700146
147 /**
148 * Set the interestStruct to point to the components in this interest, without copying any memory.
149 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
150 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
151 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700152 void get(struct ndn_Interest &interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700153
Jeff Thompson12c27762013-07-09 15:05:11 -0700154 Name &getName() { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700155 const Name &getName() const { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700156
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700157 int getMinSuffixComponents() const { return minSuffixComponents_; }
158
159 int getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700160
Jeff Thompson8238d002013-07-10 11:56:49 -0700161 PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
162 const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700163
Jeff Thompson12c27762013-07-09 15:05:11 -0700164 Exclude &getExclude() { return exclude_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700165 const Exclude &getExclude() const { return exclude_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700166
167 int getChildSelector() const { return childSelector_; }
168
169 int getAnswerOriginKind() const { return answerOriginKind_; }
170
171 int getScope() const { return scope_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700172
173 int getInterestLifetime() const { return interestLifetime_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700174
175 const std::vector<unsigned char> getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700176
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700177 /**
178 * Clear this interest, and set the values by copying from the interest struct.
179 * @param interestStruct a C ndn_Interest struct
180 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700181 void set(const struct ndn_Interest &interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700182
183 void setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
184
185 void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
186
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700187 void setChildSelector(int value) { childSelector_ = value; }
188
189 void setAnswerOriginKind(int value) { answerOriginKind_ = value; }
190
191 void setScope(int value) { scope_ = value; }
192
193 void setInterestLifetime(int value) { interestLifetime_ = value; }
194
195 void setNonce(const std::vector<unsigned char> &value) { nonce_ = value; }
196
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700197private:
198
199 Name name_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700200 int minSuffixComponents_;
Jeff Thompsonf2e5e282013-07-08 15:26:16 -0700201 int maxSuffixComponents_;
Jeff Thompson8238d002013-07-10 11:56:49 -0700202 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700203 Exclude exclude_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700204 int childSelector_;
205 int answerOriginKind_;
206 int scope_;
207 int interestLifetime_;
208 std::vector<unsigned char> nonce_;
209};
210
211}
212
213#endif