blob: 3643be66ed12766db4f59d54d8d6b5f94b3f3987 [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#include "util/concepts.hpp"
26
27namespace ndn {
28namespace nfd {
29
Junxiao Shibc6beb12017-01-11 23:20:39 +000030BOOST_CONCEPT_ASSERT((boost::EqualityComparable<StrategyChoice>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070031BOOST_CONCEPT_ASSERT((WireEncodable<StrategyChoice>));
Junxiao Shibc6beb12017-01-11 23:20:39 +000032BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<StrategyChoice>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070033BOOST_CONCEPT_ASSERT((WireDecodable<StrategyChoice>));
34static_assert(std::is_base_of<tlv::Error, StrategyChoice::Error>::value,
35 "StrategyChoice::Error must inherit from tlv::Error");
36
Junxiao Shibc6beb12017-01-11 23:20:39 +000037StrategyChoice::StrategyChoice() = default;
Junxiao Shi65f1a712014-11-20 14:59:36 -070038
39StrategyChoice::StrategyChoice(const Block& payload)
40{
41 this->wireDecode(payload);
42}
43
Alexander Afanasyev74633892015-02-08 18:08:46 -080044template<encoding::Tag TAG>
Junxiao Shi65f1a712014-11-20 14:59:36 -070045size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080046StrategyChoice::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi65f1a712014-11-20 14:59:36 -070047{
48 size_t totalLength = 0;
49
50 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
51 totalLength += m_name.wireEncode(encoder);
52
53 totalLength += encoder.prependVarNumber(totalLength);
54 totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
55 return totalLength;
56}
57
58template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080059StrategyChoice::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070060
61template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080062StrategyChoice::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070063
64const Block&
65StrategyChoice::wireEncode() const
66{
67 if (m_wire.hasWire())
68 return m_wire;
69
70 EncodingEstimator estimator;
71 size_t estimatedSize = wireEncode(estimator);
72
73 EncodingBuffer buffer(estimatedSize, 0);
74 wireEncode(buffer);
75
76 m_wire = buffer.block();
77 return m_wire;
78}
79
80void
81StrategyChoice::wireDecode(const Block& block)
82{
83 if (block.type() != tlv::nfd::StrategyChoice) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070084 BOOST_THROW_EXCEPTION(Error("expecting StrategyChoice block"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070085 }
86 m_wire = block;
87 m_wire.parse();
88 Block::element_const_iterator val = m_wire.elements_begin();
89
90 if (val != m_wire.elements_end() && val->type() == tlv::Name) {
91 m_name.wireDecode(*val);
92 ++val;
93 }
94 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070095 BOOST_THROW_EXCEPTION(Error("missing required Name field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -070096 }
97
98 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
99 val->parse();
100 if (val->elements().empty()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700101 BOOST_THROW_EXCEPTION(Error("expecting Strategy/Name"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700102 }
103 else {
104 m_strategy.wireDecode(*val->elements_begin());
105 }
106 ++val;
107 }
108 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700109 BOOST_THROW_EXCEPTION(Error("missing required Strategy field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700110 }
111}
112
113StrategyChoice&
114StrategyChoice::setName(const Name& name)
115{
116 m_wire.reset();
117 m_name = name;
118 return *this;
119}
120
121StrategyChoice&
122StrategyChoice::setStrategy(const Name& strategy)
123{
124 m_wire.reset();
125 m_strategy = strategy;
126 return *this;
127}
128
Junxiao Shibc6beb12017-01-11 23:20:39 +0000129bool
130operator==(const StrategyChoice& a, const StrategyChoice& b)
131{
132 return a.getName() == b.getName() && a.getStrategy() == b.getStrategy();
133}
134
135std::ostream&
136operator<<(std::ostream& os, const StrategyChoice& sc)
137{
138 return os << "StrategyChoice("
139 << "Name: " << sc.getName() << ", "
140 << "Strategy: " << sc.getStrategy()
141 << ")";
142}
143
Junxiao Shi65f1a712014-11-20 14:59:36 -0700144} // namespace nfd
145} // namespace ndn