face-query-filter.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 "face-query-filter.hpp"
24 #include "encoding/tlv-nfd.hpp"
25 #include "util/concepts.hpp"
26 
27 namespace ndn {
28 namespace nfd {
29 
30 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceQueryFilter>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<FaceQueryFilter>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<FaceQueryFilter>));
33 static_assert(std::is_base_of<tlv::Error, FaceQueryFilter::Error>::value,
34  "FaceQueryFilter::Error must inherit from tlv::Error");
35 
37 
39 {
40  this->wireDecode(block);
41 }
42 
43 template<encoding::Tag TAG>
44 size_t
45 FaceQueryFilter::wireEncode(EncodingImpl<TAG>& encoder) const
46 {
47  size_t totalLength = 0;
48 
49  if (m_linkType) {
50  totalLength += prependNonNegativeIntegerBlock(encoder,
51  tlv::nfd::LinkType, *m_linkType);
52  }
53 
54  if (m_facePersistency) {
55  totalLength += prependNonNegativeIntegerBlock(encoder,
56  tlv::nfd::FacePersistency, *m_facePersistency);
57  }
58 
59  if (m_faceScope) {
60  totalLength += prependNonNegativeIntegerBlock(encoder,
61  tlv::nfd::FaceScope, *m_faceScope);
62  }
63 
64  if (hasLocalUri()) {
65  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
66  }
67 
68  if (hasRemoteUri()) {
69  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
70  }
71 
72  if (hasUriScheme()) {
73  totalLength += prependStringBlock(encoder, tlv::nfd::UriScheme, m_uriScheme);
74  }
75 
76  if (m_faceId) {
77  totalLength += prependNonNegativeIntegerBlock(encoder,
78  tlv::nfd::FaceId, *m_faceId);
79  }
80 
81  totalLength += encoder.prependVarNumber(totalLength);
82  totalLength += encoder.prependVarNumber(tlv::nfd::FaceQueryFilter);
83  return totalLength;
84 }
85 
87 
88 const Block&
90 {
91  if (m_wire.hasWire())
92  return m_wire;
93 
94  EncodingEstimator estimator;
95  size_t estimatedSize = wireEncode(estimator);
96 
97  EncodingBuffer buffer(estimatedSize, 0);
98  wireEncode(buffer);
99 
100  m_wire = buffer.block();
101  return m_wire;
102 }
103 
104 void
106 {
107  // all fields are optional
108  if (block.type() != tlv::nfd::FaceQueryFilter) {
109  BOOST_THROW_EXCEPTION(Error("expecting FaceQueryFilter block"));
110  }
111 
112  m_wire = block;
113  m_wire.parse();
115 
116  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
117  m_faceId = readNonNegativeInteger(*val);
118  ++val;
119  }
120  else {
121  m_faceId = nullopt;
122  }
123 
124  if (val != m_wire.elements_end() && val->type() == tlv::nfd::UriScheme) {
125  m_uriScheme = readString(*val);
126  ++val;
127  }
128  else {
129  m_uriScheme.clear();
130  }
131 
132  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
133  m_remoteUri = readString(*val);
134  ++val;
135  }
136  else {
137  m_remoteUri.clear();
138  }
139 
140  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
141  m_localUri = readString(*val);
142  ++val;
143  }
144  else {
145  m_localUri.clear();
146  }
147 
148  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
149  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
150  ++val;
151  }
152  else {
153  m_faceScope = nullopt;
154  }
155 
156  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
157  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
158  ++val;
159  }
160  else {
161  m_facePersistency = nullopt;
162  }
163 
164  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
165  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
166  ++val;
167  }
168  else {
169  m_linkType = nullopt;
170  }
171 }
172 
173 bool
175 {
176  return !this->hasFaceId() &&
177  !this->hasUriScheme() &&
178  !this->hasRemoteUri() &&
179  !this->hasLocalUri() &&
180  !this->hasFaceScope() &&
181  !this->hasFacePersistency() &&
182  !this->hasLinkType();
183 }
184 
187 {
188  m_wire.reset();
189  m_faceId = faceId;
190  return *this;
191 }
192 
195 {
196  m_wire.reset();
197  m_faceId = nullopt;
198  return *this;
199 }
200 
202 FaceQueryFilter::setUriScheme(const std::string& uriScheme)
203 {
204  m_wire.reset();
205  m_uriScheme = uriScheme;
206  return *this;
207 }
208 
211 {
212  return this->setUriScheme("");
213 }
214 
216 FaceQueryFilter::setRemoteUri(const std::string& remoteUri)
217 {
218  m_wire.reset();
219  m_remoteUri = remoteUri;
220  return *this;
221 }
222 
225 {
226  return this->setRemoteUri("");
227 }
228 
230 FaceQueryFilter::setLocalUri(const std::string& localUri)
231 {
232  m_wire.reset();
233  m_localUri = localUri;
234  return *this;
235 }
236 
239 {
240  return this->setLocalUri("");
241 }
242 
245 {
246  m_wire.reset();
247  m_faceScope = faceScope;
248  return *this;
249 }
250 
253 {
254  m_wire.reset();
255  m_faceScope = nullopt;
256  return *this;
257 }
258 
261 {
262  m_wire.reset();
263  m_facePersistency = facePersistency;
264  return *this;
265 }
266 
269 {
270  m_wire.reset();
271  m_facePersistency = nullopt;
272  return *this;
273 }
274 
277 {
278  m_wire.reset();
279  m_linkType = linkType;
280  return *this;
281 }
282 
285 {
286  m_wire.reset();
287  m_linkType = nullopt;
288  return *this;
289 }
290 
291 bool
293 {
294  return a.hasFaceId() == b.hasFaceId() &&
295  (!a.hasFaceId() || a.getFaceId() == b.getFaceId()) &&
296  a.hasUriScheme() == b.hasUriScheme() &&
297  (!a.hasUriScheme() || a.getUriScheme() == b.getUriScheme()) &&
298  a.hasRemoteUri() == b.hasRemoteUri() &&
299  (!a.hasRemoteUri() || a.getRemoteUri() == b.getRemoteUri()) &&
300  a.hasLocalUri() == b.hasLocalUri() &&
301  (!a.hasLocalUri() || a.getLocalUri() == b.getLocalUri()) &&
302  a.hasFaceScope() == b.hasFaceScope() &&
303  (!a.hasFaceScope() || a.getFaceScope() == b.getFaceScope()) &&
306  a.hasLinkType() == b.hasLinkType() &&
307  (!a.hasLinkType() || a.getLinkType() == b.getLinkType());
308 }
309 
310 std::ostream&
311 operator<<(std::ostream& os, const FaceQueryFilter& filter)
312 {
313  os << "FaceQueryFilter(";
314  if (filter.hasFaceId()) {
315  os << "FaceID: " << filter.getFaceId() << ",\n";
316  }
317 
318  if (filter.hasUriScheme()) {
319  os << "UriScheme: " << filter.getUriScheme() << ",\n";
320  }
321 
322  if (filter.hasRemoteUri()) {
323  os << "RemoteUri: " << filter.getRemoteUri() << ",\n";
324  }
325 
326  if (filter.hasLocalUri()) {
327  os << "LocalUri: " << filter.getLocalUri() << ",\n";
328  }
329 
330  if (filter.hasFaceScope()) {
331  os << "FaceScope: " << filter.getFaceScope() << ",\n";
332  }
333 
334  if (filter.hasFacePersistency()) {
335  os << "FacePersistency: " << filter.getFacePersistency() << ",\n";
336  }
337 
338  if (filter.hasLinkType()) {
339  os << "LinkType: " << filter.getLinkType() << ",\n";
340  }
341  os << ")";
342  return os;
343 }
344 
345 } // namespace nfd
346 } // namespace ndn
const std::string & getUriScheme() const
constexpr nullopt_t nullopt
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
FaceQueryFilter & unsetLocalUri()
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
FaceQueryFilter & unsetFaceScope()
FaceQueryFilter & setLocalUri(const std::string &localUri)
const std::string & getLocalUri() const
FacePersistency getFacePersistency() const
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
FaceQueryFilter & setFaceScope(FaceScope faceScope)
FaceQueryFilter & unsetFaceId()
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:258
FaceQueryFilter & setUriScheme(const std::string &uriScheme)
const Block & wireEncode() const
encode FaceQueryFilter
FaceQueryFilter & unsetFacePersistency()
represents Face Query Filter
FaceQueryFilter & unsetUriScheme()
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:336
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
const std::string & getRemoteUri() const
FaceQueryFilter & unsetRemoteUri()
FaceQueryFilter & setRemoteUri(const std::string &remoteUri)
FaceQueryFilter & setFaceId(uint64_t faceId)
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:252
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
FaceQueryFilter & setLinkType(LinkType linkType)
FaceQueryFilter & setFacePersistency(FacePersistency facePersistency)
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
void wireDecode(const Block &wire)
decode FaceQueryFilter
EncodingImpl< EncoderTag > EncodingBuffer
EncodingImpl< EstimatorTag > EncodingEstimator
FaceScope getFaceScope() const
FaceQueryFilter & unsetLinkType()