blob: 23f7d1f81143538b41d8bd77f00b898ddce948af [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"
11#include "c/Interest.h"
12
13namespace ndn {
Jeff Thompsonfe556862013-07-09 13:52:55 -070014
15class ExcludeEntry {
16public:
17 /**
18 * Create an ExcludeEntry of type ndn_Exclude_ANY
19 */
20 ExcludeEntry()
21 : type_(ndn_Exclude_ANY)
22 {
23 }
24
25 /**
26 * Create an ExcludeEntry of type ndn_Exclude_COMPONENT
27 */
28 ExcludeEntry(unsigned char *component, unsigned int componentLen)
29 : type_(ndn_Exclude_COMPONENT), component_(component, component + componentLen)
30 {
31 }
32
33 /**
34 * Set the type in the excludeEntryStruct and to point to this component, without copying any memory.
35 * WARNING: The resulting pointer in excludeEntryStruct is invalid after a further use of this object which could reallocate memory.
36 * @param excludeEntryStruct the C ndn_NameComponent struct to receive the pointer.
37 */
38 void get(struct ndn_ExcludeEntry &excludeEntryStruct) const
39 {
40 excludeEntryStruct.type = type_;
41 if (type_ == ndn_Exclude_COMPONENT) {
42 excludeEntryStruct.componentLength = component_.size();
43 excludeEntryStruct.component = (unsigned char *)&component_[0];
44 }
45 }
46
47 ndn_ExcludeType getType() const { return type_; }
48
49 const std::vector<unsigned char> &getComponent() const { return component_; }
50
51private:
52 ndn_ExcludeType type_;
53 std::vector<unsigned char> component_; /**< only used if type_ is ndn_Exclude_COMPONENT */
54};
55
56class Exclude {
57public:
58 /**
59 * Create a new Exclude with no entries.
60 */
61 Exclude() {
62 }
63
64 unsigned int getEntryCount() const {
65 return entries_.size();
66 }
67
68 const ExcludeEntry &getEntry(unsigned int i) const { return entries_[i]; }
69
70 /**
71 * Set the excludeStruct to point to the entries in this exclude, without copying any memory.
72 * WARNING: The resulting pointers in excludeStruct are invalid after a further use of this object which could reallocate memory.
73 * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated.
74 */
75 void get(struct ndn_Exclude &excludeStruct) const;
76
77 /**
78 * Clear this exclude, and set the entries by copying from the ndn_Exclude struct.
79 * @param excludeStruct a C ndn_Exclude struct
80 */
81 void set(struct ndn_Exclude &excludeStruct);
82
83 /**
84 * Add a new entry of type ndn_Exclude_ANY
85 */
86 void addAny()
87 {
88 entries_.push_back(ExcludeEntry());
89 }
90
91 /**
92 * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength
93 */
94 void addComponent(unsigned char *component, unsigned int componentLen)
95 {
96 entries_.push_back(ExcludeEntry(component, componentLen));
97 }
98
99 /**
100 * Clear all the entries.
101 */
102 void clear() {
103 entries_.clear();
104 }
105
106private:
107 std::vector<ExcludeEntry> entries_;
108};
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700109
110class Interest {
111public:
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700112 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700113 {
114 wireFormat.encodeInterest(*this, output);
115 }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700116 void encode(std::vector<unsigned char> &output) const
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700117 {
118 encode(output, BinaryXMLWireFormat::instance());
119 }
120 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
121 {
122 wireFormat.decodeInterest(*this, input, inputLength);
123 }
124 void decode(const unsigned char *input, unsigned int inputLength)
125 {
126 decode(input, inputLength, BinaryXMLWireFormat::instance());
127 }
128 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
129 {
130 decode(&input[0], input.size(), wireFormat);
131 }
132 void decode(const std::vector<unsigned char> &input)
133 {
134 decode(&input[0], input.size());
135 }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700136
137 /**
138 * Set the interestStruct to point to the components in this interest, without copying any memory.
139 * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory.
140 * @param interestStruct a C ndn_Interest struct where the name components array is already allocated.
141 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700142 void get(struct ndn_Interest &interestStruct) const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700143
Jeff Thompson12c27762013-07-09 15:05:11 -0700144 Name &getName() { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700145 const Name &getName() const { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700146
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700147 int getMinSuffixComponents() const { return minSuffixComponents_; }
148
149 int getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700150
151 const std::vector<unsigned char> getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
152
Jeff Thompson12c27762013-07-09 15:05:11 -0700153 Exclude &getExclude() { return exclude_; }
Jeff Thompsonfe556862013-07-09 13:52:55 -0700154 const Exclude &getExclude() const { return exclude_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700155
156 int getChildSelector() const { return childSelector_; }
157
158 int getAnswerOriginKind() const { return answerOriginKind_; }
159
160 int getScope() const { return scope_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700161
162 int getInterestLifetime() const { return interestLifetime_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700163
164 const std::vector<unsigned char> getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700165
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700166 /**
167 * Clear this interest, and set the values by copying from the interest struct.
168 * @param interestStruct a C ndn_Interest struct
169 */
170 void set(struct ndn_Interest &interestStruct);
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700171
172 void setMinSuffixComponents(int value) { minSuffixComponents_ = value; }
173
174 void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; }
175
176 void setPublisherPublicKeyDigest(const std::vector<unsigned char> &value) { publisherPublicKeyDigest_ = value; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700177
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700178 void setChildSelector(int value) { childSelector_ = value; }
179
180 void setAnswerOriginKind(int value) { answerOriginKind_ = value; }
181
182 void setScope(int value) { scope_ = value; }
183
184 void setInterestLifetime(int value) { interestLifetime_ = value; }
185
186 void setNonce(const std::vector<unsigned char> &value) { nonce_ = value; }
187
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700188private:
189
190 Name name_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700191 int minSuffixComponents_;
Jeff Thompsonf2e5e282013-07-08 15:26:16 -0700192 int maxSuffixComponents_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700193 std::vector<unsigned char> publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700194 Exclude exclude_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700195 int childSelector_;
196 int answerOriginKind_;
197 int scope_;
198 int interestLifetime_;
199 std::vector<unsigned char> nonce_;
200};
201
202}
203
204#endif