All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
selectors.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_SELECTORS_HPP
8 #define NDN_SELECTORS_HPP
9 
10 #include "common.hpp"
11 #include "key-locator.hpp"
12 #include "exclude.hpp"
14 
15 namespace ndn {
16 
20 class Selectors
21 {
22 public:
24  : m_minSuffixComponents(-1)
25  , m_maxSuffixComponents(-1)
26  , m_childSelector(-1)
27  , m_mustBeFresh(false)
28  {
29  }
30 
33  Selectors(int minSuffixComponents, int maxSuffixComponents,
34  const Exclude& exclude,
35  int childSelector,
36  bool mustBeFresh)
37  : m_minSuffixComponents(minSuffixComponents)
38  , m_maxSuffixComponents(maxSuffixComponents)
39  , m_exclude(exclude)
40  , m_childSelector(childSelector)
41  , m_mustBeFresh(mustBeFresh)
42  {
43  }
44 
48  Selectors(const Block& wire)
49  {
50  wireDecode(wire);
51  }
52 
53  bool
54  empty() const;
55 
59  template<bool T>
60  size_t
61  wireEncode(EncodingImpl<T>& block) const;
62 
66  const Block&
67  wireEncode() const;
68 
72  void
73  wireDecode(const Block& wire);
74 
78 
79  int
81  {
82  return m_minSuffixComponents;
83  }
84 
85  Selectors&
86  setMinSuffixComponents(int minSuffixComponents)
87  {
88  m_minSuffixComponents = minSuffixComponents;
89  m_wire.reset();
90  return *this;
91  }
92 
93  //
94 
95  int
97  {
98  return m_maxSuffixComponents;
99  }
100 
101  Selectors&
102  setMaxSuffixComponents(int maxSuffixComponents)
103  {
104  m_maxSuffixComponents = maxSuffixComponents;
105  m_wire.reset();
106  return *this;
107  }
108 
109  //
110 
111  const KeyLocator&
113  {
114  return m_publisherPublicKeyLocator;
115  }
116 
117  Selectors&
119  {
120  m_publisherPublicKeyLocator = keyLocator;
121  m_wire.reset();
122  return *this;
123  }
124 
125  //
126 
127  const Exclude&
128  getExclude() const
129  {
130  return m_exclude;
131  }
132 
133  Selectors&
134  setExclude(const Exclude& exclude)
135  {
136  m_exclude = exclude;
137  m_wire.reset();
138  return *this;
139  }
140 
141  //
142 
143  int
145  {
146  return m_childSelector;
147  }
148 
149  Selectors&
150  setChildSelector(int childSelector)
151  {
152  m_childSelector = childSelector;
153  m_wire.reset();
154  return *this;
155  }
156 
157  //
158 
159  int
161  {
162  return m_mustBeFresh;
163  }
164 
165  Selectors&
166  setMustBeFresh(bool mustBeFresh)
167  {
168  m_mustBeFresh = mustBeFresh;
169  m_wire.reset();
170  return *this;
171  }
172 
173 public: // EqualityComparable concept
174  bool
175  operator==(const Selectors& other) const
176  {
177  return wireEncode() == other.wireEncode();
178  }
179 
180  bool
181  operator!=(const Selectors& other) const
182  {
183  return !(*this == other);
184  }
185 
186 private:
187  int m_minSuffixComponents;
188  int m_maxSuffixComponents;
189  KeyLocator m_publisherPublicKeyLocator;
190  Exclude m_exclude;
191  int m_childSelector;
192  bool m_mustBeFresh;
193 
194  mutable Block m_wire;
195 };
196 
197 inline bool
199 {
200  return
201  (m_minSuffixComponents < 0 &&
202  m_maxSuffixComponents < 0 &&
203  m_publisherPublicKeyLocator.empty() &&
204  m_exclude.empty() &&
205  m_childSelector < 0 &&
206  !m_mustBeFresh);
207 }
208 
209 template<bool T>
210 inline size_t
212 {
213  size_t totalLength = 0;
214 
215  // Selectors ::= SELECTORS-TYPE TLV-LENGTH
216  // MinSuffixComponents?
217  // MaxSuffixComponents?
218  // PublisherPublicKeyLocator?
219  // Exclude?
220  // ChildSelector?
221  // MustBeFresh?
222 
223  // (reverse encoding)
224 
225  // MustBeFresh
226  if (getMustBeFresh())
227  {
228  totalLength += prependBooleanBlock(block, Tlv::MustBeFresh);
229  }
230 
231  // ChildSelector
232  if (getChildSelector() >= 0)
233  {
235  }
236 
237  // Exclude
238  if (!getExclude().empty())
239  {
240  totalLength += getExclude().wireEncode(block);
241  }
242 
243  // PublisherPublicKeyLocator
245  {
246  totalLength += getPublisherPublicKeyLocator().wireEncode(block);
247  }
248 
249  // MaxSuffixComponents
250  if (getMaxSuffixComponents() >= 0)
251  {
254  }
255 
256  // MinSuffixComponents
257  if (getMinSuffixComponents() >= 0)
258  {
261  }
262 
263  totalLength += block.prependVarNumber(totalLength);
264  totalLength += block.prependVarNumber(Tlv::Selectors);
265  return totalLength;
266 }
267 
268 inline const Block&
270 {
271  if (m_wire.hasWire())
272  return m_wire;
273 
274  EncodingEstimator estimator;
275  size_t estimatedSize = wireEncode(estimator);
276 
277  EncodingBuffer buffer(estimatedSize, 0);
278  wireEncode(buffer);
279 
280  m_wire = buffer.block();
281  return m_wire;
282 }
283 
284 inline void
286 {
287  if (wire.type() != Tlv::Selectors)
288  throw Tlv::Error("Unexpected TLV type when decoding Selectors");
289 
290  *this = Selectors();
291 
292  m_wire = wire;
293  m_wire.parse();
294 
295  // MinSuffixComponents
297  if (val != m_wire.elements_end())
298  {
299  m_minSuffixComponents = readNonNegativeInteger(*val);
300  }
301 
302  // MaxSuffixComponents
303  val = m_wire.find(Tlv::MaxSuffixComponents);
304  if (val != m_wire.elements_end())
305  {
306  m_maxSuffixComponents = readNonNegativeInteger(*val);
307  }
308 
309  // PublisherPublicKeyLocator
310  val = m_wire.find(Tlv::KeyLocator);
311  if (val != m_wire.elements_end())
312  {
313  m_publisherPublicKeyLocator.wireDecode(*val);
314  }
315 
316  // Exclude
317  val = m_wire.find(Tlv::Exclude);
318  if (val != m_wire.elements_end())
319  {
320  m_exclude.wireDecode(*val);
321  }
322 
323  // ChildSelector
324  val = m_wire.find(Tlv::ChildSelector);
325  if (val != m_wire.elements_end())
326  {
327  m_childSelector = readNonNegativeInteger(*val);
328  }
329 
330  //MustBeFresh aka AnswerOriginKind
331  val = m_wire.find(Tlv::MustBeFresh);
332  if (val != m_wire.elements_end())
333  {
334  m_mustBeFresh = true;
335  }
336 }
337 
338 } // namespace ndn
339 
340 #endif // NDN_SELECTORS_HPP
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: selectors.hpp:285
element_const_iterator find(uint32_t type) const
Definition: block.hpp:343
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: exclude.hpp:255
Selectors & setChildSelector(int childSelector)
Definition: selectors.hpp:150
void wireDecode(const Block &wire)
Class representing wire element of the NDN packet.
Definition: block.hpp:26
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: selectors.hpp:211
uint64_t readNonNegativeInteger(const Block &block)
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.hpp:118
element_const_iterator elements_end() const
Definition: block.hpp:476
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:112
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: exclude.hpp:296
const Exclude & getExclude() const
Definition: selectors.hpp:128
int getChildSelector() const
Definition: selectors.hpp:144
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.hpp:102
bool empty() const
Definition: selectors.hpp:198
const Block & wireEncode() const
Encode to a wire format.
Definition: selectors.hpp:269
bool empty() const
Definition: key-locator.hpp:70
int getMustBeFresh() const
Definition: selectors.hpp:160
size_t prependBooleanBlock(EncodingImpl< P > &encoder, uint32_t type)
Abstraction implementing Interest selectors.
Definition: selectors.hpp:20
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
Selectors & setMustBeFresh(bool mustBeFresh)
Definition: selectors.hpp:166
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
bool operator==(const Selectors &other) const
Definition: selectors.hpp:175
Selectors(const Block &wire)
Create from wire encoding.
Definition: selectors.hpp:48
bool empty() const
Check if exclude filter is empty.
Definition: exclude.hpp:203
int getMinSuffixComponents() const
Definition: selectors.hpp:80
Selectors(int minSuffixComponents, int maxSuffixComponents, const Exclude &exclude, int childSelector, bool mustBeFresh)
Definition: selectors.hpp:33
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.hpp:134
size_t prependNonNegativeIntegerBlock(EncodingImpl< P > &encoder, uint32_t type, uint64_t number)
helper methods
size_t wireEncode(EncodingImpl< T > &block) const
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.hpp:86
bool operator!=(const Selectors &other) const
Definition: selectors.hpp:181
int getMaxSuffixComponents() const
Definition: selectors.hpp:96
Class representing wire element of the NDN packet.
Class to represent Exclude component in NDN interests.
Definition: exclude.hpp:21