blob: 2e294d728855032d06c769e618edd95d951bef6a [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7357ef22016-09-07 02:39:37 +00003 * Copyright (c) 2013-2016 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/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#include "util/concepts.hpp"
26
27namespace ndn {
28namespace nfd {
29
30//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<StrategyChoice>));
31BOOST_CONCEPT_ASSERT((WireEncodable<StrategyChoice>));
32BOOST_CONCEPT_ASSERT((WireDecodable<StrategyChoice>));
33static_assert(std::is_base_of<tlv::Error, StrategyChoice::Error>::value,
34 "StrategyChoice::Error must inherit from tlv::Error");
35
36StrategyChoice::StrategyChoice()
37{
38}
39
40StrategyChoice::StrategyChoice(const Block& payload)
41{
42 this->wireDecode(payload);
43}
44
Alexander Afanasyev74633892015-02-08 18:08:46 -080045template<encoding::Tag TAG>
Junxiao Shi65f1a712014-11-20 14:59:36 -070046size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080047StrategyChoice::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi65f1a712014-11-20 14:59:36 -070048{
49 size_t totalLength = 0;
50
51 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
52 totalLength += m_name.wireEncode(encoder);
53
54 totalLength += encoder.prependVarNumber(totalLength);
55 totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
56 return totalLength;
57}
58
59template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080060StrategyChoice::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070061
62template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080063StrategyChoice::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070064
65const Block&
66StrategyChoice::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
81void
82StrategyChoice::wireDecode(const Block& block)
83{
84 if (block.type() != tlv::nfd::StrategyChoice) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070085 BOOST_THROW_EXCEPTION(Error("expecting StrategyChoice block"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070086 }
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::Name) {
92 m_name.wireDecode(*val);
93 ++val;
94 }
95 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070096 BOOST_THROW_EXCEPTION(Error("missing required Name field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070097 }
98
99 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
100 val->parse();
101 if (val->elements().empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700102 BOOST_THROW_EXCEPTION(Error("expecting Strategy/Name"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700103 }
104 else {
105 m_strategy.wireDecode(*val->elements_begin());
106 }
107 ++val;
108 }
109 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700110 BOOST_THROW_EXCEPTION(Error("missing required Strategy field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700111 }
112}
113
114StrategyChoice&
115StrategyChoice::setName(const Name& name)
116{
117 m_wire.reset();
118 m_name = name;
119 return *this;
120}
121
122StrategyChoice&
123StrategyChoice::setStrategy(const Name& strategy)
124{
125 m_wire.reset();
126 m_strategy = strategy;
127 return *this;
128}
129
130} // namespace nfd
131} // namespace ndn