Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | 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" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
| 30 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ChannelStatus>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<ChannelStatus>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<ChannelStatus>)); |
| 33 | static_assert(std::is_base_of<tlv::Error, ChannelStatus::Error>::value, |
| 34 | "ChannelStatus::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | ChannelStatus::ChannelStatus() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | ChannelStatus::ChannelStatus(const Block& payload) |
| 41 | { |
| 42 | this->wireDecode(payload); |
| 43 | } |
| 44 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 45 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 46 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 47 | ChannelStatus::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 48 | { |
| 49 | size_t totalLength = 0; |
| 50 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 51 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri, |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 52 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 53 | |
| 54 | totalLength += encoder.prependVarNumber(totalLength); |
| 55 | totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus); |
| 56 | return totalLength; |
| 57 | } |
| 58 | |
| 59 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 60 | ChannelStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 61 | |
| 62 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 63 | ChannelStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 64 | |
| 65 | const Block& |
| 66 | ChannelStatus::wireEncode() const |
| 67 | { |
| 68 | if (m_wire.hasWire()) |
| 69 | return m_wire; |
| 70 | |
| 71 | EncodingEstimator estimator; |
| 72 | size_t estimatedSize = wireEncode(estimator); |
| 73 | |
| 74 | EncodingBuffer buffer(estimatedSize, 0); |
| 75 | wireEncode(buffer); |
| 76 | |
| 77 | m_wire = buffer.block(); |
| 78 | return m_wire; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | ChannelStatus::wireDecode(const Block& block) |
| 83 | { |
| 84 | if (block.type() != tlv::nfd::ChannelStatus) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 85 | BOOST_THROW_EXCEPTION(Error("Expecting ChannelStatus block")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 86 | } |
| 87 | m_wire = block; |
| 88 | m_wire.parse(); |
| 89 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 90 | |
| 91 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 92 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 93 | ++val; |
| 94 | } |
| 95 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 96 | BOOST_THROW_EXCEPTION(Error("Missing required LocalUri field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | ChannelStatus& |
| 101 | ChannelStatus::setLocalUri(const std::string localUri) |
| 102 | { |
| 103 | m_wire.reset(); |
| 104 | m_localUri = localUri; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | } // namespace nfd |
| 109 | } // namespace ndn |