blob: 5fd93b19a1fccc14e1ce44da13a9298f9ca4d42b [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
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"
Junxiao Shi5d75fd92017-08-08 18:09:20 +000023#include "encoding/block-helpers.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;
Junxiao Shi5d75fd92017-08-08 18:09:20 +000044 totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
Junxiao Shi65f1a712014-11-20 14:59:36 -070045 totalLength += encoder.prependVarNumber(totalLength);
46 totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus);
47 return totalLength;
48}
49
Davide Pesavento88a0d812017-08-19 21:31:42 -040050NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus);
Junxiao Shi65f1a712014-11-20 14:59:36 -070051
52const Block&
53ChannelStatus::wireEncode() const
54{
55 if (m_wire.hasWire())
56 return m_wire;
57
58 EncodingEstimator estimator;
59 size_t estimatedSize = wireEncode(estimator);
60
61 EncodingBuffer buffer(estimatedSize, 0);
62 wireEncode(buffer);
63
64 m_wire = buffer.block();
65 return m_wire;
66}
67
68void
69ChannelStatus::wireDecode(const Block& block)
70{
71 if (block.type() != tlv::nfd::ChannelStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070072 BOOST_THROW_EXCEPTION(Error("Expecting ChannelStatus block"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070073 }
74 m_wire = block;
75 m_wire.parse();
76 Block::element_const_iterator val = m_wire.elements_begin();
77
78 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000079 m_localUri = readString(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -070080 ++val;
81 }
82 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070083 BOOST_THROW_EXCEPTION(Error("Missing required LocalUri field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070084 }
85}
86
87ChannelStatus&
88ChannelStatus::setLocalUri(const std::string localUri)
89{
90 m_wire.reset();
91 m_localUri = localUri;
92 return *this;
93}
94
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -050095bool
96operator==(const ChannelStatus& a, const ChannelStatus& b)
97{
98 return a.getLocalUri() == b.getLocalUri();
99}
100
101std::ostream&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500102operator<<(std::ostream& os, const ChannelStatus& status)
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -0500103{
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500104 return os << "Channel(LocalUri: " << status.getLocalUri() << ")";
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -0500105}
106
Junxiao Shi65f1a712014-11-20 14:59:36 -0700107} // namespace nfd
108} // namespace ndn