Chengyu Fan | fc8aebb | 2014-10-22 16:35:23 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 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 "nfd-face-query-filter.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame^] | 23 | #include "encoding/tlv-nfd.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/concepts.hpp" |
Chengyu Fan | fc8aebb | 2014-10-22 16:35:23 -0600 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame^] | 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 | |
Chengyu Fan | fc8aebb | 2014-10-22 16:35:23 -0600 | [diff] [blame] | 36 | FaceQueryFilter::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 | |
| 47 | FaceQueryFilter::FaceQueryFilter(const Block& block) |
| 48 | { |
| 49 | this->wireDecode(block); |
| 50 | } |
| 51 | |
| 52 | template<bool T> |
| 53 | size_t |
| 54 | FaceQueryFilter::wireEncode(EncodingImpl<T>& encoder) const |
| 55 | { |
| 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) { |
| 74 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 75 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 76 | } |
| 77 | |
| 78 | if (m_hasRemoteUri) { |
| 79 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri, |
| 80 | reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size()); |
| 81 | } |
| 82 | |
| 83 | if (m_hasUriScheme) { |
| 84 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::UriScheme, |
| 85 | 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 | |
| 98 | template size_t |
| 99 | FaceQueryFilter::wireEncode<true>(EncodingImpl<true>& block) const; |
| 100 | |
| 101 | template size_t |
| 102 | FaceQueryFilter::wireEncode<false>(EncodingImpl<false>& block) const; |
| 103 | |
| 104 | const Block& |
| 105 | FaceQueryFilter::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 | |
| 120 | void |
| 121 | FaceQueryFilter::wireDecode(const Block& block) |
| 122 | { |
| 123 | //all fields are optional |
| 124 | if (block.type() != tlv::nfd::FaceQueryFilter) { |
| 125 | throw Error("expecting FaceQueryFilter block"); |
| 126 | } |
| 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 | |
| 197 | FaceQueryFilter& |
| 198 | FaceQueryFilter::setFaceId(uint64_t faceId) |
| 199 | { |
| 200 | m_wire.reset(); |
| 201 | m_faceId = faceId; |
| 202 | m_hasFaceId = true; |
| 203 | return *this; |
| 204 | } |
| 205 | |
| 206 | FaceQueryFilter& |
| 207 | FaceQueryFilter::unsetFaceId() |
| 208 | { |
| 209 | m_wire.reset(); |
| 210 | m_hasFaceId = false; |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | FaceQueryFilter& |
| 215 | FaceQueryFilter::setUriScheme(const std::string& uriScheme) |
| 216 | { |
| 217 | m_wire.reset(); |
| 218 | m_uriScheme = uriScheme; |
| 219 | m_hasUriScheme = true; |
| 220 | return *this; |
| 221 | } |
| 222 | |
| 223 | FaceQueryFilter& |
| 224 | FaceQueryFilter::unsetUriScheme() |
| 225 | { |
| 226 | m_wire.reset(); |
| 227 | m_hasUriScheme = false; |
| 228 | return *this; |
| 229 | } |
| 230 | |
| 231 | FaceQueryFilter& |
| 232 | FaceQueryFilter::setRemoteUri(const std::string& remoteUri) |
| 233 | { |
| 234 | m_wire.reset(); |
| 235 | m_remoteUri = remoteUri; |
| 236 | m_hasRemoteUri = true; |
| 237 | return *this; |
| 238 | } |
| 239 | |
| 240 | FaceQueryFilter& |
| 241 | FaceQueryFilter::unsetRemoteUri() |
| 242 | { |
| 243 | m_wire.reset(); |
| 244 | m_hasRemoteUri = false; |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | FaceQueryFilter& |
| 249 | FaceQueryFilter::setLocalUri(const std::string& localUri) |
| 250 | { |
| 251 | m_wire.reset(); |
| 252 | m_localUri = localUri; |
| 253 | m_hasLocalUri = true; |
| 254 | return *this; |
| 255 | } |
| 256 | |
| 257 | FaceQueryFilter& |
| 258 | FaceQueryFilter::unsetLocalUri() |
| 259 | { |
| 260 | m_wire.reset(); |
| 261 | m_hasLocalUri = false; |
| 262 | return *this; |
| 263 | } |
| 264 | |
| 265 | FaceQueryFilter& |
| 266 | FaceQueryFilter::setFaceScope(FaceScope faceScope) |
| 267 | { |
| 268 | m_wire.reset(); |
| 269 | m_faceScope = faceScope; |
| 270 | m_hasFaceScope = true; |
| 271 | return *this; |
| 272 | } |
| 273 | |
| 274 | FaceQueryFilter& |
| 275 | FaceQueryFilter::unsetFaceScope() |
| 276 | { |
| 277 | m_wire.reset(); |
| 278 | m_hasFaceScope = false; |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | FaceQueryFilter& |
| 283 | FaceQueryFilter::setFacePersistency(FacePersistency facePersistency) |
| 284 | { |
| 285 | m_wire.reset(); |
| 286 | m_facePersistency = facePersistency; |
| 287 | m_hasFacePersistency = true; |
| 288 | return *this; |
| 289 | } |
| 290 | |
| 291 | FaceQueryFilter& |
| 292 | FaceQueryFilter::unsetFacePersistency() |
| 293 | { |
| 294 | m_wire.reset(); |
| 295 | m_hasFacePersistency = false; |
| 296 | return *this; |
| 297 | } |
| 298 | |
| 299 | FaceQueryFilter& |
| 300 | FaceQueryFilter::setLinkType(LinkType linkType) |
| 301 | { |
| 302 | m_wire.reset(); |
| 303 | m_linkType = linkType; |
| 304 | m_hasLinkType = true; |
| 305 | return *this; |
| 306 | } |
| 307 | |
| 308 | FaceQueryFilter& |
| 309 | FaceQueryFilter::unsetLinkType() |
| 310 | { |
| 311 | m_wire.reset(); |
| 312 | m_hasLinkType = false; |
| 313 | return *this; |
| 314 | } |
| 315 | |
| 316 | std::ostream& |
| 317 | operator<<(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 |
| 353 | |