NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
selectors.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "selectors.hpp"
25 
26 namespace ndn {
27 
28 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Selectors>));
29 BOOST_CONCEPT_ASSERT((WireEncodable<Selectors>));
31 BOOST_CONCEPT_ASSERT((WireDecodable<Selectors>));
32 static_assert(std::is_base_of<tlv::Error, Selectors::Error>::value,
33  "Selectors::Error must inherit from tlv::Error");
34 
36  : m_minSuffixComponents(-1)
37  , m_maxSuffixComponents(-1)
38  , m_childSelector(-1)
39  , m_mustBeFresh(false)
40 {
41 }
42 
44 {
45  wireDecode(wire);
46 }
47 
48 bool
50 {
51  return m_minSuffixComponents < 0 &&
52  m_maxSuffixComponents < 0 &&
53  m_publisherPublicKeyLocator.empty() &&
54  m_exclude.empty() &&
55  m_childSelector < 0 &&
56  !m_mustBeFresh;
57 }
58 
59 template<encoding::Tag TAG>
60 size_t
62 {
63  size_t totalLength = 0;
64 
65  // Selectors ::= SELECTORS-TYPE TLV-LENGTH
66  // MinSuffixComponents?
67  // MaxSuffixComponents?
68  // PublisherPublicKeyLocator?
69  // Exclude?
70  // ChildSelector?
71  // MustBeFresh?
72 
73  // (reverse encoding)
74 
75  // MustBeFresh
76  if (getMustBeFresh()) {
77  totalLength += prependEmptyBlock(encoder, tlv::MustBeFresh);
78  }
79 
80  // ChildSelector
81  if (getChildSelector() >= 0) {
83  }
84 
85  // Exclude
86  if (!getExclude().empty()) {
87  totalLength += getExclude().wireEncode(encoder);
88  }
89 
90  // PublisherPublicKeyLocator
92  totalLength += getPublisherPublicKeyLocator().wireEncode(encoder);
93  }
94 
95  // MaxSuffixComponents
96  if (getMaxSuffixComponents() >= 0) {
99  }
100 
101  // MinSuffixComponents
102  if (getMinSuffixComponents() >= 0) {
105  }
106 
107  totalLength += encoder.prependVarNumber(totalLength);
108  totalLength += encoder.prependVarNumber(tlv::Selectors);
109  return totalLength;
110 }
111 
112 template size_t
113 Selectors::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
114 
115 template size_t
116 Selectors::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
117 
118 const Block&
120 {
121  if (m_wire.hasWire())
122  return m_wire;
123 
124  EncodingEstimator estimator;
125  size_t estimatedSize = wireEncode(estimator);
126 
127  EncodingBuffer buffer(estimatedSize, 0);
128  wireEncode(buffer);
129 
130  m_wire = buffer.block();
131  return m_wire;
132 }
133 
134 void
136 {
137  if (wire.type() != tlv::Selectors)
138  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Selectors"));
139 
140  *this = Selectors();
141 
142  m_wire = wire;
143  m_wire.parse();
144 
145  // MinSuffixComponents
147  if (val != m_wire.elements_end()) {
148  m_minSuffixComponents = readNonNegativeInteger(*val);
149  }
150 
151  // MaxSuffixComponents
152  val = m_wire.find(tlv::MaxSuffixComponents);
153  if (val != m_wire.elements_end()) {
154  m_maxSuffixComponents = readNonNegativeInteger(*val);
155  }
156 
157  // PublisherPublicKeyLocator
158  val = m_wire.find(tlv::KeyLocator);
159  if (val != m_wire.elements_end()) {
160  m_publisherPublicKeyLocator.wireDecode(*val);
161  }
162 
163  // Exclude
164  val = m_wire.find(tlv::Exclude);
165  if (val != m_wire.elements_end()) {
166  m_exclude.wireDecode(*val);
167  }
168 
169  // ChildSelector
170  val = m_wire.find(tlv::ChildSelector);
171  if (val != m_wire.elements_end()) {
172  m_childSelector = readNonNegativeInteger(*val);
173  }
174 
175  // MustBeFresh
176  val = m_wire.find(tlv::MustBeFresh);
177  if (val != m_wire.elements_end()) {
178  m_mustBeFresh = true;
179  }
180 }
181 
182 Selectors&
183 Selectors::setMinSuffixComponents(int minSuffixComponents)
184 {
185  m_minSuffixComponents = minSuffixComponents;
186  m_wire.reset();
187  return *this;
188 }
189 
190 Selectors&
191 Selectors::setMaxSuffixComponents(int maxSuffixComponents)
192 {
193  m_maxSuffixComponents = maxSuffixComponents;
194  m_wire.reset();
195  return *this;
196 }
197 
198 Selectors&
200 {
201  m_publisherPublicKeyLocator = keyLocator;
202  m_wire.reset();
203  return *this;
204 }
205 
206 Selectors&
208 {
209  m_exclude = exclude;
210  m_wire.reset();
211  return *this;
212 }
213 
214 Selectors&
215 Selectors::setChildSelector(int childSelector)
216 {
217  m_childSelector = childSelector;
218  m_wire.reset();
219  return *this;
220 }
221 
222 Selectors&
223 Selectors::setMustBeFresh(bool mustBeFresh)
224 {
225  m_mustBeFresh = mustBeFresh;
226  m_wire.reset();
227  return *this;
228 }
229 
230 bool
231 Selectors::operator==(const Selectors& other) const
232 {
233  return wireEncode() == other.wireEncode();
234 }
235 
236 } // namespace ndn
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: selectors.cpp:135
int getMinSuffixComponents() const
Definition: selectors.hpp:79
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
Selectors & setMustBeFresh(bool mustBeFresh)
Definition: selectors.cpp:223
bool empty() const
Definition: selectors.cpp:49
bool empty() const
Check if exclude filter is empty.
Definition: exclude.hpp:223
EncodingImpl< EstimatorTag > EncodingEstimator
int getMustBeFresh() const
Definition: selectors.hpp:124
void wireDecode(const Block &wire)
decode from wire encoding
Definition: key-locator.cpp:99
element_const_iterator elements_end() const
Definition: block.cpp:595
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
int getChildSelector() const
Definition: selectors.hpp:115
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.cpp:191
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.cpp:207
Selectors & setChildSelector(int childSelector)
Definition: selectors.cpp:215
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.cpp:183
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: exclude.cpp:97
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:97
EncodingImpl< EncoderTag > EncodingBuffer
element_const_iterator find(uint32_t type) const
Definition: block.cpp:420
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Helper to prepend TLV block type type containing no value (i.e., a boolean block) ...
bool empty() const
Definition: key-locator.hpp:95
const Block & wireEncode() const
Encode to a wire format.
Definition: selectors.cpp:119
Abstraction implementing Interest selectors.
Definition: selectors.hpp:34
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
bool operator==(const Selectors &other) const
Definition: selectors.cpp:231
int getMaxSuffixComponents() const
Definition: selectors.hpp:88
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:51
const Exclude & getExclude() const
Definition: selectors.hpp:106
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: selectors.cpp:61
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: exclude.cpp:49
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
uint32_t type() const
Definition: block.hpp:346
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
Class to represent Exclude component in NDN interests.
Definition: exclude.hpp:38
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.cpp:199