blob: cb17036df3b992a2d4c0cb1de68d05916d757519 [file] [log] [blame]
Alexander Afanasyevc348f832014-02-17 16:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_SELECTORS_HPP
9#define NDN_SELECTORS_HPP
10
11#include "common.hpp"
Junxiao Shib332e782014-03-31 14:23:46 -070012#include "key-locator.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080013#include "exclude.hpp"
14#include "encoding/encoding-buffer.hpp"
15
16namespace ndn {
Junxiao Shib332e782014-03-31 14:23:46 -070017
Alexander Afanasyevc348f832014-02-17 16:35:17 -080018/**
19 * @brief Abstraction implementing Interest selectors
20 */
21class Selectors
22{
Junxiao Shib332e782014-03-31 14:23:46 -070023public:
24 Selectors()
Alexander Afanasyevc348f832014-02-17 16:35:17 -080025 : m_minSuffixComponents(-1)
26 , m_maxSuffixComponents(-1)
27 , m_childSelector(-1)
28 , m_mustBeFresh(false)
29 {
30 }
31
Junxiao Shib332e782014-03-31 14:23:46 -070032 /** @deprecated Selectors().setX(...).setY(...)
33 */
34 Selectors(int minSuffixComponents, int maxSuffixComponents,
Alexander Afanasyevc348f832014-02-17 16:35:17 -080035 const Exclude& exclude,
36 int childSelector,
Junxiao Shib332e782014-03-31 14:23:46 -070037 bool mustBeFresh)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080038 : m_minSuffixComponents(minSuffixComponents)
39 , m_maxSuffixComponents(maxSuffixComponents)
40 , m_exclude(exclude)
41 , m_childSelector(childSelector)
42 , m_mustBeFresh(mustBeFresh)
43 {
44 }
Junxiao Shib332e782014-03-31 14:23:46 -070045
46 Selectors(const Block& wire)
Alexander Afanasyevc348f832014-02-17 16:35:17 -080047 {
48 wireDecode(wire);
49 }
50
51 bool
52 empty() const;
Junxiao Shib332e782014-03-31 14:23:46 -070053
Alexander Afanasyevc348f832014-02-17 16:35:17 -080054 /**
55 * @brief Fast encoding or block size estimation
56 */
57 template<bool T>
58 size_t
59 wireEncode(EncodingImpl<T> &block) const;
Junxiao Shib332e782014-03-31 14:23:46 -070060
Alexander Afanasyevc348f832014-02-17 16:35:17 -080061 /**
62 * @brief Encode to a wire format
63 */
64 const Block&
65 wireEncode() const;
66
67 /**
68 * @brief Decode the input from wire format
69 */
Junxiao Shib332e782014-03-31 14:23:46 -070070 void
Alexander Afanasyevc348f832014-02-17 16:35:17 -080071 wireDecode(const Block &wire);
72
73 ///////////////////////////////////////////////////////////////////////////////
74 ///////////////////////////////////////////////////////////////////////////////
75 ///////////////////////////////////////////////////////////////////////////////
Junxiao Shib332e782014-03-31 14:23:46 -070076
77 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -080078 getMinSuffixComponents() const
79 {
80 return m_minSuffixComponents;
81 }
Junxiao Shib332e782014-03-31 14:23:46 -070082
83 Selectors&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080084 setMinSuffixComponents(int minSuffixComponents)
85 {
86 m_minSuffixComponents = minSuffixComponents;
87 wire_.reset();
88 return *this;
89 }
90
91 //
Junxiao Shib332e782014-03-31 14:23:46 -070092
93 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -080094 getMaxSuffixComponents() const
95 {
96 return m_maxSuffixComponents;
97 }
98
Junxiao Shib332e782014-03-31 14:23:46 -070099 Selectors&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800100 setMaxSuffixComponents(int maxSuffixComponents)
101 {
102 m_maxSuffixComponents = maxSuffixComponents;
103 wire_.reset();
104 return *this;
105 }
Junxiao Shib332e782014-03-31 14:23:46 -0700106
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800107 //
108
Junxiao Shib332e782014-03-31 14:23:46 -0700109 const KeyLocator&
110 getPublisherPublicKeyLocator() const
111 {
112 return m_publisherPublicKeyLocator;
113 }
114
115 Selectors&
116 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
117 {
118 m_publisherPublicKeyLocator = keyLocator;
119 wire_.reset();
120 return *this;
121 }
122
123 //
124
125 const Exclude&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800126 getExclude() const
127 {
128 return m_exclude;
129 }
130
Junxiao Shib332e782014-03-31 14:23:46 -0700131 Selectors&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800132 setExclude(const Exclude& exclude)
133 {
134 m_exclude = exclude;
135 wire_.reset();
136 return *this;
137 }
138
139 //
Junxiao Shib332e782014-03-31 14:23:46 -0700140
141 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800142 getChildSelector() const
143 {
144 return m_childSelector;
145 }
146
Junxiao Shib332e782014-03-31 14:23:46 -0700147 Selectors&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800148 setChildSelector(int childSelector)
149 {
150 m_childSelector = childSelector;
151 wire_.reset();
152 return *this;
153 }
154
155 //
156
Junxiao Shib332e782014-03-31 14:23:46 -0700157 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800158 getMustBeFresh() const
159 {
160 return m_mustBeFresh;
161 }
162
Junxiao Shib332e782014-03-31 14:23:46 -0700163 Selectors&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800164 setMustBeFresh(bool mustBeFresh)
165 {
166 m_mustBeFresh = mustBeFresh;
167 wire_.reset();
168 return *this;
169 }
170
171private:
172 int m_minSuffixComponents;
Junxiao Shib332e782014-03-31 14:23:46 -0700173 int m_maxSuffixComponents;
174 KeyLocator m_publisherPublicKeyLocator;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800175 Exclude m_exclude;
176 int m_childSelector;
177 bool m_mustBeFresh;
178
179 mutable Block wire_;
180};
181
182inline bool
183Selectors::empty() const
184{
185 return
186 (m_minSuffixComponents < 0 &&
187 m_maxSuffixComponents < 0 &&
Junxiao Shib332e782014-03-31 14:23:46 -0700188 m_publisherPublicKeyLocator.empty() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800189 m_exclude.empty() &&
190 m_childSelector < 0 &&
191 !m_mustBeFresh);
192}
193
194template<bool T>
195inline size_t
196Selectors::wireEncode(EncodingImpl<T> &block) const
197{
198 size_t total_len = 0;
199
200 // Selectors ::= SELECTORS-TYPE TLV-LENGTH
201 // MinSuffixComponents?
202 // MaxSuffixComponents?
203 // PublisherPublicKeyLocator?
204 // Exclude?
205 // ChildSelector?
Junxiao Shib332e782014-03-31 14:23:46 -0700206 // MustBeFresh?
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800207
208 // (reverse encoding)
209
210 // MustBeFresh
211 if (getMustBeFresh())
212 {
213 total_len += prependBooleanBlock(block, Tlv::MustBeFresh);
214 }
215
216 // ChildSelector
217 if (getChildSelector() >= 0)
218 {
219 total_len += prependNonNegativeIntegerBlock(block, Tlv::ChildSelector, getChildSelector());
220 }
221
222 // Exclude
223 if (!getExclude().empty())
224 {
225 total_len += getExclude().wireEncode(block);
226 }
Junxiao Shib332e782014-03-31 14:23:46 -0700227
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800228 // PublisherPublicKeyLocator
Junxiao Shib332e782014-03-31 14:23:46 -0700229 if (!getPublisherPublicKeyLocator().empty())
230 {
231 total_len += getPublisherPublicKeyLocator().wireEncode(block);
232 }
233
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800234 // MaxSuffixComponents
235 if (getMaxSuffixComponents() >= 0)
236 {
237 total_len += prependNonNegativeIntegerBlock(block, Tlv::MaxSuffixComponents,
238 getMaxSuffixComponents());
239 }
240
241 // MinSuffixComponents
242 if (getMinSuffixComponents() >= 0)
243 {
244 total_len += prependNonNegativeIntegerBlock(block, Tlv::MinSuffixComponents,
245 getMinSuffixComponents());
246 }
Junxiao Shib332e782014-03-31 14:23:46 -0700247
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800248 total_len += block.prependVarNumber(total_len);
249 total_len += block.prependVarNumber(Tlv::Selectors);
250 return total_len;
251}
252
253inline const Block &
254Selectors::wireEncode() const
255{
256 if (wire_.hasWire())
257 return wire_;
258
259 EncodingEstimator estimator;
260 size_t estimatedSize = wireEncode(estimator);
Junxiao Shib332e782014-03-31 14:23:46 -0700261
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800262 EncodingBuffer buffer(estimatedSize, 0);
263 wireEncode(buffer);
264
265 wire_ = buffer.block();
266 return wire_;
267}
268
269inline void
Junxiao Shib332e782014-03-31 14:23:46 -0700270Selectors::wireDecode(const Block &wire)
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800271{
272 if (wire.type() != Tlv::Selectors)
273 throw Tlv::Error("Unexpected TLV type when decoding Selectors");
274
275 *this = Selectors();
Junxiao Shib332e782014-03-31 14:23:46 -0700276
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800277 wire_ = wire;
278 wire_.parse();
279
280 // MinSuffixComponents
281 Block::element_const_iterator val = wire_.find(Tlv::MinSuffixComponents);
282 if (val != wire_.elements_end())
283 {
284 m_minSuffixComponents = readNonNegativeInteger(*val);
285 }
286
287 // MaxSuffixComponents
288 val = wire_.find(Tlv::MaxSuffixComponents);
289 if (val != wire_.elements_end())
290 {
291 m_maxSuffixComponents = readNonNegativeInteger(*val);
292 }
293
Junxiao Shib332e782014-03-31 14:23:46 -0700294 // PublisherPublicKeyLocator
295 val = wire_.find(Tlv::KeyLocator);
296 if (val != wire_.elements_end())
297 {
298 m_publisherPublicKeyLocator.wireDecode(*val);
299 }
300
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800301 // Exclude
302 val = wire_.find(Tlv::Exclude);
303 if (val != wire_.elements_end())
304 {
305 m_exclude.wireDecode(*val);
306 }
307
308 // ChildSelector
309 val = wire_.find(Tlv::ChildSelector);
310 if (val != wire_.elements_end())
311 {
312 m_childSelector = readNonNegativeInteger(*val);
313 }
314
315 //MustBeFresh aka AnswerOriginKind
316 val = wire_.find(Tlv::MustBeFresh);
317 if (val != wire_.elements_end())
318 {
319 m_mustBeFresh = true;
320 }
321}
Junxiao Shib332e782014-03-31 14:23:46 -0700322
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800323} // namespace ndn
324
325#endif // NDN_SELECTORS_HPP