Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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" |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 23 | #include "encoding/encoding-buffer.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; |
| 44 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 45 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri, |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 46 | reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size()); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 47 | |
| 48 | totalLength += encoder.prependVarNumber(totalLength); |
| 49 | totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus); |
| 50 | return totalLength; |
| 51 | } |
| 52 | |
| 53 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 54 | ChannelStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 55 | |
| 56 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 57 | ChannelStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 58 | |
| 59 | const Block& |
| 60 | ChannelStatus::wireEncode() const |
| 61 | { |
| 62 | if (m_wire.hasWire()) |
| 63 | return m_wire; |
| 64 | |
| 65 | EncodingEstimator estimator; |
| 66 | size_t estimatedSize = wireEncode(estimator); |
| 67 | |
| 68 | EncodingBuffer buffer(estimatedSize, 0); |
| 69 | wireEncode(buffer); |
| 70 | |
| 71 | m_wire = buffer.block(); |
| 72 | return m_wire; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | ChannelStatus::wireDecode(const Block& block) |
| 77 | { |
| 78 | if (block.type() != tlv::nfd::ChannelStatus) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 79 | BOOST_THROW_EXCEPTION(Error("Expecting ChannelStatus block")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 80 | } |
| 81 | m_wire = block; |
| 82 | m_wire.parse(); |
| 83 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 84 | |
| 85 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 86 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 87 | ++val; |
| 88 | } |
| 89 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 90 | BOOST_THROW_EXCEPTION(Error("Missing required LocalUri field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | ChannelStatus& |
| 95 | ChannelStatus::setLocalUri(const std::string localUri) |
| 96 | { |
| 97 | m_wire.reset(); |
| 98 | m_localUri = localUri; |
| 99 | return *this; |
| 100 | } |
| 101 | |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 102 | bool |
| 103 | operator==(const ChannelStatus& a, const ChannelStatus& b) |
| 104 | { |
| 105 | return a.getLocalUri() == b.getLocalUri(); |
| 106 | } |
| 107 | |
| 108 | std::ostream& |
Davide Pesavento | 25e3d8c | 2017-02-08 22:17:46 -0500 | [diff] [blame] | 109 | operator<<(std::ostream& os, const ChannelStatus& status) |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 110 | { |
Davide Pesavento | 25e3d8c | 2017-02-08 22:17:46 -0500 | [diff] [blame] | 111 | return os << "Channel(LocalUri: " << status.getLocalUri() << ")"; |
Davide Pesavento | 9f9bd8a | 2017-02-06 00:33:32 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 114 | } // namespace nfd |
| 115 | } // namespace ndn |