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" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace nfd { |
| 26 | |
| 27 | FaceQueryFilter::FaceQueryFilter() |
| 28 | : m_hasFaceId(false) |
| 29 | , m_hasUriScheme(false) |
| 30 | , m_hasRemoteUri(false) |
| 31 | , m_hasLocalUri(false) |
| 32 | , m_hasFaceScope(false) |
| 33 | , m_hasFacePersistency(false) |
| 34 | , m_hasLinkType(false) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | FaceQueryFilter::FaceQueryFilter(const Block& block) |
| 39 | { |
| 40 | this->wireDecode(block); |
| 41 | } |
| 42 | |
| 43 | template<bool T> |
| 44 | size_t |
| 45 | FaceQueryFilter::wireEncode(EncodingImpl<T>& encoder) const |
| 46 | { |
| 47 | size_t totalLength = 0; |
| 48 | |
| 49 | if (m_hasLinkType) { |
| 50 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 51 | tlv::nfd::LinkType, m_linkType); |
| 52 | } |
| 53 | |
| 54 | if (m_hasFacePersistency) { |
| 55 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 56 | tlv::nfd::FacePersistency, m_facePersistency); |
| 57 | } |
| 58 | |
| 59 | if (m_hasFaceScope) { |
| 60 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 61 | tlv::nfd::FaceScope, m_faceScope); |
| 62 | } |
| 63 | |
| 64 | if (m_hasLocalUri) { |
| 65 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 66 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 67 | } |
| 68 | |
| 69 | if (m_hasRemoteUri) { |
| 70 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri, |
| 71 | reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size()); |
| 72 | } |
| 73 | |
| 74 | if (m_hasUriScheme) { |
| 75 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::UriScheme, |
| 76 | reinterpret_cast<const uint8_t*>(m_uriScheme.c_str()), m_uriScheme.size()); |
| 77 | } |
| 78 | |
| 79 | if (m_hasFaceId) { |
| 80 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 81 | tlv::nfd::FaceId, m_faceId); |
| 82 | } |
| 83 | |
| 84 | totalLength += encoder.prependVarNumber(totalLength); |
| 85 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceQueryFilter); |
| 86 | return totalLength; |
| 87 | } |
| 88 | |
| 89 | template size_t |
| 90 | FaceQueryFilter::wireEncode<true>(EncodingImpl<true>& block) const; |
| 91 | |
| 92 | template size_t |
| 93 | FaceQueryFilter::wireEncode<false>(EncodingImpl<false>& block) const; |
| 94 | |
| 95 | const Block& |
| 96 | FaceQueryFilter::wireEncode() const |
| 97 | { |
| 98 | if (m_wire.hasWire()) |
| 99 | return m_wire; |
| 100 | |
| 101 | EncodingEstimator estimator; |
| 102 | size_t estimatedSize = wireEncode(estimator); |
| 103 | |
| 104 | EncodingBuffer buffer(estimatedSize, 0); |
| 105 | wireEncode(buffer); |
| 106 | |
| 107 | m_wire = buffer.block(); |
| 108 | return m_wire; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | FaceQueryFilter::wireDecode(const Block& block) |
| 113 | { |
| 114 | //all fields are optional |
| 115 | if (block.type() != tlv::nfd::FaceQueryFilter) { |
| 116 | throw Error("expecting FaceQueryFilter block"); |
| 117 | } |
| 118 | |
| 119 | m_wire = block; |
| 120 | m_wire.parse(); |
| 121 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 122 | |
| 123 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 124 | m_faceId = readNonNegativeInteger(*val); |
| 125 | m_hasFaceId = true; |
| 126 | ++val; |
| 127 | } |
| 128 | else { |
| 129 | m_hasFaceId = false; |
| 130 | } |
| 131 | |
| 132 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::UriScheme) { |
| 133 | m_uriScheme.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 134 | m_hasUriScheme = true; |
| 135 | ++val; |
| 136 | } |
| 137 | else { |
| 138 | m_hasUriScheme = false; |
| 139 | } |
| 140 | |
| 141 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) { |
| 142 | m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 143 | m_hasRemoteUri = true; |
| 144 | ++val; |
| 145 | } |
| 146 | else { |
| 147 | m_hasRemoteUri = false; |
| 148 | } |
| 149 | |
| 150 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 151 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 152 | m_hasLocalUri = true; |
| 153 | ++val; |
| 154 | } |
| 155 | else { |
| 156 | m_hasLocalUri = false; |
| 157 | } |
| 158 | |
| 159 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) { |
| 160 | m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val)); |
| 161 | m_hasFaceScope = true; |
| 162 | ++val; |
| 163 | } |
| 164 | else { |
| 165 | m_hasFaceScope = false; |
| 166 | } |
| 167 | |
| 168 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) { |
| 169 | m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val)); |
| 170 | m_hasFacePersistency = true; |
| 171 | ++val; |
| 172 | } |
| 173 | else { |
| 174 | m_hasFacePersistency = false; |
| 175 | } |
| 176 | |
| 177 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) { |
| 178 | m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val)); |
| 179 | m_hasLinkType = true; |
| 180 | ++val; |
| 181 | } |
| 182 | else { |
| 183 | m_hasLinkType = false; |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | FaceQueryFilter& |
| 189 | FaceQueryFilter::setFaceId(uint64_t faceId) |
| 190 | { |
| 191 | m_wire.reset(); |
| 192 | m_faceId = faceId; |
| 193 | m_hasFaceId = true; |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | FaceQueryFilter& |
| 198 | FaceQueryFilter::unsetFaceId() |
| 199 | { |
| 200 | m_wire.reset(); |
| 201 | m_hasFaceId = false; |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | FaceQueryFilter& |
| 206 | FaceQueryFilter::setUriScheme(const std::string& uriScheme) |
| 207 | { |
| 208 | m_wire.reset(); |
| 209 | m_uriScheme = uriScheme; |
| 210 | m_hasUriScheme = true; |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | FaceQueryFilter& |
| 215 | FaceQueryFilter::unsetUriScheme() |
| 216 | { |
| 217 | m_wire.reset(); |
| 218 | m_hasUriScheme = false; |
| 219 | return *this; |
| 220 | } |
| 221 | |
| 222 | FaceQueryFilter& |
| 223 | FaceQueryFilter::setRemoteUri(const std::string& remoteUri) |
| 224 | { |
| 225 | m_wire.reset(); |
| 226 | m_remoteUri = remoteUri; |
| 227 | m_hasRemoteUri = true; |
| 228 | return *this; |
| 229 | } |
| 230 | |
| 231 | FaceQueryFilter& |
| 232 | FaceQueryFilter::unsetRemoteUri() |
| 233 | { |
| 234 | m_wire.reset(); |
| 235 | m_hasRemoteUri = false; |
| 236 | return *this; |
| 237 | } |
| 238 | |
| 239 | FaceQueryFilter& |
| 240 | FaceQueryFilter::setLocalUri(const std::string& localUri) |
| 241 | { |
| 242 | m_wire.reset(); |
| 243 | m_localUri = localUri; |
| 244 | m_hasLocalUri = true; |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | FaceQueryFilter& |
| 249 | FaceQueryFilter::unsetLocalUri() |
| 250 | { |
| 251 | m_wire.reset(); |
| 252 | m_hasLocalUri = false; |
| 253 | return *this; |
| 254 | } |
| 255 | |
| 256 | FaceQueryFilter& |
| 257 | FaceQueryFilter::setFaceScope(FaceScope faceScope) |
| 258 | { |
| 259 | m_wire.reset(); |
| 260 | m_faceScope = faceScope; |
| 261 | m_hasFaceScope = true; |
| 262 | return *this; |
| 263 | } |
| 264 | |
| 265 | FaceQueryFilter& |
| 266 | FaceQueryFilter::unsetFaceScope() |
| 267 | { |
| 268 | m_wire.reset(); |
| 269 | m_hasFaceScope = false; |
| 270 | return *this; |
| 271 | } |
| 272 | |
| 273 | FaceQueryFilter& |
| 274 | FaceQueryFilter::setFacePersistency(FacePersistency facePersistency) |
| 275 | { |
| 276 | m_wire.reset(); |
| 277 | m_facePersistency = facePersistency; |
| 278 | m_hasFacePersistency = true; |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | FaceQueryFilter& |
| 283 | FaceQueryFilter::unsetFacePersistency() |
| 284 | { |
| 285 | m_wire.reset(); |
| 286 | m_hasFacePersistency = false; |
| 287 | return *this; |
| 288 | } |
| 289 | |
| 290 | FaceQueryFilter& |
| 291 | FaceQueryFilter::setLinkType(LinkType linkType) |
| 292 | { |
| 293 | m_wire.reset(); |
| 294 | m_linkType = linkType; |
| 295 | m_hasLinkType = true; |
| 296 | return *this; |
| 297 | } |
| 298 | |
| 299 | FaceQueryFilter& |
| 300 | FaceQueryFilter::unsetLinkType() |
| 301 | { |
| 302 | m_wire.reset(); |
| 303 | m_hasLinkType = false; |
| 304 | return *this; |
| 305 | } |
| 306 | |
| 307 | std::ostream& |
| 308 | operator<<(std::ostream& os, const FaceQueryFilter& filter) |
| 309 | { |
| 310 | os << "FaceQueryFilter("; |
| 311 | if (filter.hasFaceId()) { |
| 312 | os << "FaceID: " << filter.getFaceId() << ",\n"; |
| 313 | } |
| 314 | |
| 315 | if (filter.hasUriScheme()) { |
| 316 | os << "UriScheme: " << filter.getUriScheme() << ",\n"; |
| 317 | } |
| 318 | |
| 319 | if (filter.hasRemoteUri()) { |
| 320 | os << "RemoteUri: " << filter.getRemoteUri() << ",\n"; |
| 321 | } |
| 322 | |
| 323 | if (filter.hasLocalUri()) { |
| 324 | os << "LocalUri: " << filter.getLocalUri() << ",\n"; |
| 325 | } |
| 326 | |
| 327 | if (filter.hasFaceScope()) { |
| 328 | os << "FaceScope: " << filter.getFaceScope() << ",\n"; |
| 329 | } |
| 330 | |
| 331 | if (filter.hasFacePersistency()) { |
| 332 | os << "FacePersistency: " << filter.getFacePersistency() << ",\n"; |
| 333 | } |
| 334 | |
| 335 | if (filter.hasLinkType()) { |
| 336 | os << "LinkType: " << filter.getLinkType() << ",\n"; |
| 337 | } |
| 338 | os << ")"; |
| 339 | return os; |
| 340 | } |
| 341 | |
| 342 | } // namespace nfd |
| 343 | } // namespace ndn |
| 344 | |