blob: 2a3def0485642e2759be82f9d35bfd0d8d204d0d [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 Thompsond345a5b2013-07-08 16:18:23 -0700144 const Name &getName() const { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700145
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700146 int getMinSuffixComponents() const { return minSuffixComponents_; }
147
148 int getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700149
150 const std::vector<unsigned char> getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
151
Jeff Thompsonfe556862013-07-09 13:52:55 -0700152 const Exclude &getExclude() const { return exclude_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700153
154 int getChildSelector() const { return childSelector_; }
155
156 int getAnswerOriginKind() const { return answerOriginKind_; }
157
158 int getScope() const { return scope_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700159
160 int getInterestLifetime() const { return interestLifetime_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700161
162 const std::vector<unsigned char> getNonce() const { return nonce_; }
Jeff Thompson22552902013-07-07 21:26:20 -0700163
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700164 /**
165 * Clear this interest, and set the values by copying from the interest struct.
166 * @param interestStruct a C ndn_Interest struct
167 */
168 void set(struct ndn_Interest &interestStruct);
169
170private:
171
172 Name name_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700173 int minSuffixComponents_;
Jeff Thompsonf2e5e282013-07-08 15:26:16 -0700174 int maxSuffixComponents_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700175 std::vector<unsigned char> publisherPublicKeyDigest_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700176 Exclude exclude_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700177 int childSelector_;
178 int answerOriginKind_;
179 int scope_;
180 int interestLifetime_;
181 std::vector<unsigned char> nonce_;
182};
183
184}
185
186#endif