blob: e6e53f4956bcbc956355cd8904e8e834cbec4b6b [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
50template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080051ChannelStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070052
53template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080054ChannelStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070055
56const Block&
57ChannelStatus::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
72void
73ChannelStatus::wireDecode(const Block& block)
74{
75 if (block.type() != tlv::nfd::ChannelStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070076 BOOST_THROW_EXCEPTION(Error("Expecting ChannelStatus block"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070077 }
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 Shi5d75fd92017-08-08 18:09:20 +000083 m_localUri = readString(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -070084 ++val;
85 }
86 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070087 BOOST_THROW_EXCEPTION(Error("Missing required LocalUri field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070088 }
89}
90
91ChannelStatus&
92ChannelStatus::setLocalUri(const std::string localUri)
93{
94 m_wire.reset();
95 m_localUri = localUri;
96 return *this;
97}
98
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -050099bool
100operator==(const ChannelStatus& a, const ChannelStatus& b)
101{
102 return a.getLocalUri() == b.getLocalUri();
103}
104
105std::ostream&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500106operator<<(std::ostream& os, const ChannelStatus& status)
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -0500107{
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500108 return os << "Channel(LocalUri: " << status.getLocalUri() << ")";
Davide Pesavento9f9bd8a2017-02-06 00:33:32 -0500109}
110
Junxiao Shi65f1a712014-11-20 14:59:36 -0700111} // namespace nfd
112} // namespace ndn