blob: ffaae903b88189d0683ad0f24073ce8034bc7754 [file] [log] [blame]
Chengyu Fanfc8aebb2014-10-22 16:35:23 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7357ef22016-09-07 02:39:37 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Chengyu Fanfc8aebb2014-10-22 16:35:23 -06004 *
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
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "face-query-filter.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#include "util/concepts.hpp"
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060026
27namespace ndn {
28namespace nfd {
29
Junxiao Shi65f1a712014-11-20 14:59:36 -070030//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceQueryFilter>));
31BOOST_CONCEPT_ASSERT((WireEncodable<FaceQueryFilter>));
32BOOST_CONCEPT_ASSERT((WireDecodable<FaceQueryFilter>));
33static_assert(std::is_base_of<tlv::Error, FaceQueryFilter::Error>::value,
34 "FaceQueryFilter::Error must inherit from tlv::Error");
35
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060036FaceQueryFilter::FaceQueryFilter()
37 : m_hasFaceId(false)
38 , m_hasUriScheme(false)
39 , m_hasRemoteUri(false)
40 , m_hasLocalUri(false)
41 , m_hasFaceScope(false)
42 , m_hasFacePersistency(false)
43 , m_hasLinkType(false)
44{
45}
46
47FaceQueryFilter::FaceQueryFilter(const Block& block)
48{
49 this->wireDecode(block);
50}
51
Alexander Afanasyev74633892015-02-08 18:08:46 -080052template<encoding::Tag TAG>
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060053size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080054FaceQueryFilter::wireEncode(EncodingImpl<TAG>& encoder) const
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060055{
56 size_t totalLength = 0;
57
58 if (m_hasLinkType) {
59 totalLength += prependNonNegativeIntegerBlock(encoder,
60 tlv::nfd::LinkType, m_linkType);
61 }
62
63 if (m_hasFacePersistency) {
64 totalLength += prependNonNegativeIntegerBlock(encoder,
65 tlv::nfd::FacePersistency, m_facePersistency);
66 }
67
68 if (m_hasFaceScope) {
69 totalLength += prependNonNegativeIntegerBlock(encoder,
70 tlv::nfd::FaceScope, m_faceScope);
71 }
72
73 if (m_hasLocalUri) {
Alexander Afanasyev74633892015-02-08 18:08:46 -080074 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060075 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
76 }
77
78 if (m_hasRemoteUri) {
Alexander Afanasyev74633892015-02-08 18:08:46 -080079 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060080 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
81 }
82
83 if (m_hasUriScheme) {
Alexander Afanasyev74633892015-02-08 18:08:46 -080084 totalLength += encoder.prependByteArrayBlock(tlv::nfd::UriScheme,
Chengyu Fanfc8aebb2014-10-22 16:35:23 -060085 reinterpret_cast<const uint8_t*>(m_uriScheme.c_str()), m_uriScheme.size());
86 }
87
88 if (m_hasFaceId) {
89 totalLength += prependNonNegativeIntegerBlock(encoder,
90 tlv::nfd::FaceId, m_faceId);
91 }
92
93 totalLength += encoder.prependVarNumber(totalLength);
94 totalLength += encoder.prependVarNumber(tlv::nfd::FaceQueryFilter);
95 return totalLength;
96}
97
98template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080099FaceQueryFilter::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Chengyu Fanfc8aebb2014-10-22 16:35:23 -0600100
101template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800102FaceQueryFilter::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Chengyu Fanfc8aebb2014-10-22 16:35:23 -0600103
104const Block&
105FaceQueryFilter::wireEncode() const
106{
107 if (m_wire.hasWire())
108 return m_wire;
109
110 EncodingEstimator estimator;
111 size_t estimatedSize = wireEncode(estimator);
112
113 EncodingBuffer buffer(estimatedSize, 0);
114 wireEncode(buffer);
115
116 m_wire = buffer.block();
117 return m_wire;
118}
119
120void
121FaceQueryFilter::wireDecode(const Block& block)
122{
123 //all fields are optional
124 if (block.type() != tlv::nfd::FaceQueryFilter) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700125 BOOST_THROW_EXCEPTION(Error("expecting FaceQueryFilter block"));
Chengyu Fanfc8aebb2014-10-22 16:35:23 -0600126 }
127
128 m_wire = block;
129 m_wire.parse();
130 Block::element_const_iterator val = m_wire.elements_begin();
131
132 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
133 m_faceId = readNonNegativeInteger(*val);
134 m_hasFaceId = true;
135 ++val;
136 }
137 else {
138 m_hasFaceId = false;
139 }
140
141 if (val != m_wire.elements_end() && val->type() == tlv::nfd::UriScheme) {
142 m_uriScheme.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
143 m_hasUriScheme = true;
144 ++val;
145 }
146 else {
147 m_hasUriScheme = false;
148 }
149
150 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
151 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
152 m_hasRemoteUri = true;
153 ++val;
154 }
155 else {
156 m_hasRemoteUri = false;
157 }
158
159 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
160 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
161 m_hasLocalUri = true;
162 ++val;
163 }
164 else {
165 m_hasLocalUri = false;
166 }
167
168 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
169 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
170 m_hasFaceScope = true;
171 ++val;
172 }
173 else {
174 m_hasFaceScope = false;
175 }
176
177 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
178 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
179 m_hasFacePersistency = true;
180 ++val;
181 }
182 else {
183 m_hasFacePersistency = false;
184 }
185
186 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
187 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
188 m_hasLinkType = true;
189 ++val;
190 }
191 else {
192 m_hasLinkType = false;
193 }
194
195}
196
197FaceQueryFilter&
198FaceQueryFilter::setFaceId(uint64_t faceId)
199{
200 m_wire.reset();
201 m_faceId = faceId;
202 m_hasFaceId = true;
203 return *this;
204}
205
206FaceQueryFilter&
207FaceQueryFilter::unsetFaceId()
208{
209 m_wire.reset();
210 m_hasFaceId = false;
211 return *this;
212}
213
214FaceQueryFilter&
215FaceQueryFilter::setUriScheme(const std::string& uriScheme)
216{
217 m_wire.reset();
218 m_uriScheme = uriScheme;
219 m_hasUriScheme = true;
220 return *this;
221}
222
223FaceQueryFilter&
224FaceQueryFilter::unsetUriScheme()
225{
226 m_wire.reset();
227 m_hasUriScheme = false;
228 return *this;
229}
230
231FaceQueryFilter&
232FaceQueryFilter::setRemoteUri(const std::string& remoteUri)
233{
234 m_wire.reset();
235 m_remoteUri = remoteUri;
236 m_hasRemoteUri = true;
237 return *this;
238}
239
240FaceQueryFilter&
241FaceQueryFilter::unsetRemoteUri()
242{
243 m_wire.reset();
244 m_hasRemoteUri = false;
245 return *this;
246}
247
248FaceQueryFilter&
249FaceQueryFilter::setLocalUri(const std::string& localUri)
250{
251 m_wire.reset();
252 m_localUri = localUri;
253 m_hasLocalUri = true;
254 return *this;
255}
256
257FaceQueryFilter&
258FaceQueryFilter::unsetLocalUri()
259{
260 m_wire.reset();
261 m_hasLocalUri = false;
262 return *this;
263}
264
265FaceQueryFilter&
266FaceQueryFilter::setFaceScope(FaceScope faceScope)
267{
268 m_wire.reset();
269 m_faceScope = faceScope;
270 m_hasFaceScope = true;
271 return *this;
272}
273
274FaceQueryFilter&
275FaceQueryFilter::unsetFaceScope()
276{
277 m_wire.reset();
278 m_hasFaceScope = false;
279 return *this;
280}
281
282FaceQueryFilter&
283FaceQueryFilter::setFacePersistency(FacePersistency facePersistency)
284{
285 m_wire.reset();
286 m_facePersistency = facePersistency;
287 m_hasFacePersistency = true;
288 return *this;
289}
290
291FaceQueryFilter&
292FaceQueryFilter::unsetFacePersistency()
293{
294 m_wire.reset();
295 m_hasFacePersistency = false;
296 return *this;
297}
298
299FaceQueryFilter&
300FaceQueryFilter::setLinkType(LinkType linkType)
301{
302 m_wire.reset();
303 m_linkType = linkType;
304 m_hasLinkType = true;
305 return *this;
306}
307
308FaceQueryFilter&
309FaceQueryFilter::unsetLinkType()
310{
311 m_wire.reset();
312 m_hasLinkType = false;
313 return *this;
314}
315
316std::ostream&
317operator<<(std::ostream& os, const FaceQueryFilter& filter)
318{
319 os << "FaceQueryFilter(";
320 if (filter.hasFaceId()) {
321 os << "FaceID: " << filter.getFaceId() << ",\n";
322 }
323
324 if (filter.hasUriScheme()) {
325 os << "UriScheme: " << filter.getUriScheme() << ",\n";
326 }
327
328 if (filter.hasRemoteUri()) {
329 os << "RemoteUri: " << filter.getRemoteUri() << ",\n";
330 }
331
332 if (filter.hasLocalUri()) {
333 os << "LocalUri: " << filter.getLocalUri() << ",\n";
334 }
335
336 if (filter.hasFaceScope()) {
337 os << "FaceScope: " << filter.getFaceScope() << ",\n";
338 }
339
340 if (filter.hasFacePersistency()) {
341 os << "FacePersistency: " << filter.getFacePersistency() << ",\n";
342 }
343
344 if (filter.hasLinkType()) {
345 os << "LinkType: " << filter.getLinkType() << ",\n";
346 }
347 os << ")";
348 return os;
349}
350
351} // namespace nfd
352} // namespace ndn