blob: e0496d62d2aac4db46d435e094e7a8ffa7e29d52 [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
Junxiao Shibc6beb12017-01-11 23:20:39 +00003 * 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 "strategy-choice.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/block-helpers.hpp"
Davide Pesaventoeba76ad2017-02-05 02:48:24 -050024#include "encoding/encoding-buffer.hpp"
25#include "encoding/tlv-nfd.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070026#include "util/concepts.hpp"
27
28namespace ndn {
29namespace nfd {
30
Davide Pesaventoeba76ad2017-02-05 02:48:24 -050031BOOST_CONCEPT_ASSERT((StatusDatasetItem<StrategyChoice>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070032
Junxiao Shibc6beb12017-01-11 23:20:39 +000033StrategyChoice::StrategyChoice() = default;
Junxiao Shi65f1a712014-11-20 14:59:36 -070034
35StrategyChoice::StrategyChoice(const Block& payload)
36{
37 this->wireDecode(payload);
38}
39
Alexander Afanasyev74633892015-02-08 18:08:46 -080040template<encoding::Tag TAG>
Junxiao Shi65f1a712014-11-20 14:59:36 -070041size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080042StrategyChoice::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi65f1a712014-11-20 14:59:36 -070043{
44 size_t totalLength = 0;
45
46 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
47 totalLength += m_name.wireEncode(encoder);
48
49 totalLength += encoder.prependVarNumber(totalLength);
50 totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
51 return totalLength;
52}
53
Davide Pesavento88a0d812017-08-19 21:31:42 -040054NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(StrategyChoice);
Junxiao Shi65f1a712014-11-20 14:59:36 -070055
56const Block&
57StrategyChoice::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
73StrategyChoice::wireDecode(const Block& block)
74{
75 if (block.type() != tlv::nfd::StrategyChoice) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070076 BOOST_THROW_EXCEPTION(Error("expecting StrategyChoice 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::Name) {
83 m_name.wireDecode(*val);
84 ++val;
85 }
86 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070087 BOOST_THROW_EXCEPTION(Error("missing required Name field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070088 }
89
90 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
91 val->parse();
92 if (val->elements().empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070093 BOOST_THROW_EXCEPTION(Error("expecting Strategy/Name"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070094 }
95 else {
96 m_strategy.wireDecode(*val->elements_begin());
97 }
98 ++val;
99 }
100 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700101 BOOST_THROW_EXCEPTION(Error("missing required Strategy field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700102 }
103}
104
105StrategyChoice&
106StrategyChoice::setName(const Name& name)
107{
108 m_wire.reset();
109 m_name = name;
110 return *this;
111}
112
113StrategyChoice&
114StrategyChoice::setStrategy(const Name& strategy)
115{
116 m_wire.reset();
117 m_strategy = strategy;
118 return *this;
119}
120
Junxiao Shibc6beb12017-01-11 23:20:39 +0000121bool
122operator==(const StrategyChoice& a, const StrategyChoice& b)
123{
124 return a.getName() == b.getName() && a.getStrategy() == b.getStrategy();
125}
126
127std::ostream&
128operator<<(std::ostream& os, const StrategyChoice& sc)
129{
130 return os << "StrategyChoice("
131 << "Name: " << sc.getName() << ", "
132 << "Strategy: " << sc.getStrategy()
133 << ")";
134}
135
Junxiao Shi65f1a712014-11-20 14:59:36 -0700136} // namespace nfd
137} // namespace ndn