Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 2 | /* |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "channel-status.hpp" |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 23 | #include "encoding/block-helpers.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 24 | #include "encoding/tlv-nfd.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 25 | #include "util/concepts.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<ChannelStatus>)); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 32 | ChannelStatus::ChannelStatus() = default; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 33 | |
| 34 | ChannelStatus::ChannelStatus(const Block& payload) |
| 35 | { |
| 36 | this->wireDecode(payload); |
| 37 | } |
| 38 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 39 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 40 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 41 | ChannelStatus::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 42 | { |
| 43 | size_t totalLength = 0; |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 44 | totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 45 | totalLength += encoder.prependVarNumber(totalLength); |
| 46 | totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus); |
| 47 | return totalLength; |
| 48 | } |
| 49 | |
| 50 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 51 | ChannelStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 52 | |
| 53 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 54 | ChannelStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 55 | |
| 56 | const Block& |
| 57 | ChannelStatus::wireEncode() const |
| 58 | { |
| 59 | if (m_wire.hasWire()) |
| 60 | return m_wire; |
| 61 | |
| 62 | EncodingEstimator estimator; |
| 63 | size_t estimatedSize = wireEncode(estimator); |
| 64 | |
| 65 | EncodingBuffer buffer(estimatedSize, 0); |
| 66 | wireEncode(buffer); |
| 67 | |
| 68 | m_wire = buffer.block(); |
| 69 | return m_wire; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | ChannelStatus::wireDecode(const Block& block) |
| 74 | { |
| 75 | if (block.type() != tlv::nfd::ChannelStatus) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 76 | BOOST_THROW_EXCEPTION(Error("Expecting ChannelStatus block")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 77 | } |
| 78 | m_wire = block; |
| 79 | m_wire.parse(); |
| 80 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 81 | |
| 82 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame^] | 83 | m_localUri = readString(*val); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 84 | ++val; |
| 85 | } |
| 86 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 87 | BOOST_THROW_EXCEPTION(Error("Missing required LocalUri field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | ChannelStatus& |
| 92 | ChannelStatus::setLocalUri(const std::string localUri) |
| 93 | { |
| 94 | m_wire.reset(); |
| 95 | m_localUri = localUri; |
| 96 | return *this; |
| 97 | } |
| 98 | |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 99 | bool |
| 100 | operator==(const ChannelStatus& a, const ChannelStatus& b) |
| 101 | { |
| 102 | return a.getLocalUri() == b.getLocalUri(); |
| 103 | } |
| 104 | |
| 105 | std::ostream& |
Davide Pesavento | 25e3d8c | 2017-02-08 22:17:46 -0500 | [diff] [blame] | 106 | operator<<(std::ostream& os, const ChannelStatus& status) |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 107 | { |
Davide Pesavento | 25e3d8c | 2017-02-08 22:17:46 -0500 | [diff] [blame] | 108 | return os << "Channel(LocalUri: " << status.getLocalUri() << ")"; |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 111 | } // namespace nfd |
| 112 | } // namespace ndn |