blob: c2a9b630b1ea5185c11c2485007871019f4bd9e0 [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Junxiao Shi65f1a712014-11-20 14:59:36 -07004 *
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 Shi7357ef22016-09-07 02:39:37 +000022#include "channel-status.hpp"
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -050023#include "encoding/encoding-buffer.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070024#include "encoding/tlv-nfd.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070025#include "util/concepts.hpp"
26
27namespace ndn {
28namespace nfd {
29
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -050030BOOST_CONCEPT_ASSERT((StatusDatasetItem<ChannelStatus>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070031
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -050032ChannelStatus::ChannelStatus() = default;
Junxiao Shi65f1a712014-11-20 14:59:36 -070033
34ChannelStatus::ChannelStatus(const Block& payload)
35{
36 this->wireDecode(payload);
37}
38
Alexander Afanasyev74633892015-02-08 18:08:46 -080039template<encoding::Tag TAG>
Junxiao Shi65f1a712014-11-20 14:59:36 -070040size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080041ChannelStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi65f1a712014-11-20 14:59:36 -070042{
43 size_t totalLength = 0;
44
Alexander Afanasyev74633892015-02-08 18:08:46 -080045 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -050046 reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
Junxiao Shi65f1a712014-11-20 14:59:36 -070047
48 totalLength += encoder.prependVarNumber(totalLength);
49 totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus);
50 return totalLength;
51}
52
53template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080054ChannelStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070055
56template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080057ChannelStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070058
59const Block&
60ChannelStatus::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
75void
76ChannelStatus::wireDecode(const Block& block)
77{
78 if (block.type() != tlv::nfd::ChannelStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070079 BOOST_THROW_EXCEPTION(Error("Expecting ChannelStatus block"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070080 }
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 Mastorakis0d2ed2e2015-07-27 19:09:12 -070090 BOOST_THROW_EXCEPTION(Error("Missing required LocalUri field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070091 }
92}
93
94ChannelStatus&
95ChannelStatus::setLocalUri(const std::string localUri)
96{
97 m_wire.reset();
98 m_localUri = localUri;
99 return *this;
100}
101
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -0500102bool
103operator==(const ChannelStatus& a, const ChannelStatus& b)
104{
105 return a.getLocalUri() == b.getLocalUri();
106}
107
108std::ostream&
109operator<<(std::ostream& os, const ChannelStatus& cs)
110{
111 return os << "Channel(" << "LocalUri: " << cs.getLocalUri() << ")";
112}
113
Junxiao Shi65f1a712014-11-20 14:59:36 -0700114} // namespace nfd
115} // namespace ndn