NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
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(DEFAULT_CHILD_SELECTOR)
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 == DEFAULT_CHILD_SELECTOR &&
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
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 
113 
114 const Block&
116 {
117  if (m_wire.hasWire())
118  return m_wire;
119 
120  EncodingEstimator estimator;
121  size_t estimatedSize = wireEncode(estimator);
122 
123  EncodingBuffer buffer(estimatedSize, 0);
124  wireEncode(buffer);
125 
126  m_wire = buffer.block();
127  return m_wire;
128 }
129 
130 void
132 {
133  if (wire.type() != tlv::Selectors)
134  BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Selectors"));
135 
136  *this = Selectors();
137 
138  m_wire = wire;
139  m_wire.parse();
140 
141  // MinSuffixComponents
143  if (val != m_wire.elements_end()) {
144  m_minSuffixComponents = readNonNegativeIntegerAs<int>(*val);
145  }
146 
147  // MaxSuffixComponents
148  val = m_wire.find(tlv::MaxSuffixComponents);
149  if (val != m_wire.elements_end()) {
150  m_maxSuffixComponents = readNonNegativeIntegerAs<int>(*val);
151  }
152 
153  // PublisherPublicKeyLocator
154  val = m_wire.find(tlv::KeyLocator);
155  if (val != m_wire.elements_end()) {
156  m_publisherPublicKeyLocator.wireDecode(*val);
157  }
158 
159  // Exclude
160  val = m_wire.find(tlv::Exclude);
161  if (val != m_wire.elements_end()) {
162  m_exclude.wireDecode(*val);
163  }
164 
165  // ChildSelector
166  val = m_wire.find(tlv::ChildSelector);
167  if (val != m_wire.elements_end()) {
168  m_childSelector = readNonNegativeIntegerAs<bool>(*val);
169  }
170  else {
171  m_childSelector = DEFAULT_CHILD_SELECTOR;
172  }
173 
174  // MustBeFresh
175  val = m_wire.find(tlv::MustBeFresh);
176  if (val != m_wire.elements_end()) {
177  m_mustBeFresh = true;
178  }
179 }
180 
181 Selectors&
182 Selectors::setMinSuffixComponents(int minSuffixComponents)
183 {
184  m_minSuffixComponents = minSuffixComponents;
185  m_wire.reset();
186  return *this;
187 }
188 
189 Selectors&
190 Selectors::setMaxSuffixComponents(int maxSuffixComponents)
191 {
192  m_maxSuffixComponents = maxSuffixComponents;
193  m_wire.reset();
194  return *this;
195 }
196 
197 Selectors&
199 {
200  m_publisherPublicKeyLocator = keyLocator;
201  m_wire.reset();
202  return *this;
203 }
204 
205 Selectors&
207 {
208  m_exclude = exclude;
209  m_wire.reset();
210  return *this;
211 }
212 
213 Selectors&
214 Selectors::setChildSelector(int childSelector)
215 {
216  if (childSelector != 0 && childSelector != 1) {
217  BOOST_THROW_EXCEPTION(std::invalid_argument("ChildSelector must be 0 or 1"));
218  }
219  m_childSelector = childSelector;
220  m_wire.reset();
221  return *this;
222 }
223 
224 Selectors&
225 Selectors::setMustBeFresh(bool mustBeFresh)
226 {
227  m_mustBeFresh = mustBeFresh;
228  m_wire.reset();
229  return *this;
230 }
231 
232 bool
233 Selectors::operator==(const Selectors& other) const
234 {
235  return wireEncode() == other.wireEncode();
236 }
237 
238 } // namespace ndn
void wireDecode(const Block &wire)
Decode the input from wire format.
Definition: selectors.cpp:131
int getMinSuffixComponents() const
Definition: selectors.hpp:81
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:251
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Selectors & setMustBeFresh(bool mustBeFresh)
Definition: selectors.cpp:225
bool empty() const
Definition: selectors.cpp:49
bool empty() const
Definition: exclude.hpp:322
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
int getMustBeFresh() const
Definition: selectors.hpp:130
void wireDecode(const Block &wire)
decode from wire encoding
Definition: key-locator.cpp:96
const int DEFAULT_CHILD_SELECTOR
Definition: selectors.hpp:31
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:335
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
int getChildSelector() const
Definition: selectors.hpp:117
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
Definition: selectors.cpp:190
Selectors & setExclude(const Exclude &exclude)
Definition: selectors.cpp:206
Selectors & setChildSelector(int childSelector)
set ChildSelector
Definition: selectors.cpp:214
Selectors & setMinSuffixComponents(int minSuffixComponents)
Definition: selectors.cpp:182
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: exclude.cpp:166
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
const KeyLocator & getPublisherPublicKeyLocator() const
Definition: selectors.hpp:99
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Definition: block.cpp:429
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Prepend an empty TLV element.
bool empty() const
Definition: key-locator.hpp:95
const Block & wireEncode() const
Encode to a wire format.
Definition: selectors.cpp:115
Abstraction implementing Interest selectors.
Definition: selectors.hpp:36
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:257
bool operator==(const Selectors &other) const
Definition: selectors.cpp:233
int getMaxSuffixComponents() const
Definition: selectors.hpp:90
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:52
const Exclude & getExclude() const
Definition: selectors.hpp:108
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:122
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
EncodingImpl< EstimatorTag > EncodingEstimator
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:37
Selectors & setPublisherPublicKeyLocator(const KeyLocator &keyLocator)
Definition: selectors.cpp:198