blob: 2d9bbd5669dd6f2cb221cd0d63dd596c28cb90a3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040020 */
21
22#ifndef NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP
23#define NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP
24
25#include "../encoding/tlv-nfd.hpp"
26#include "../encoding/encoding-buffer.hpp"
27#include "../encoding/block-helpers.hpp"
28#include "../name.hpp"
29
30namespace ndn {
31namespace nfd {
32
33/**
34 * @ingroup management
35 * @brief represents NFD StrategyChoice dataset
36 * @sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy-Choice-Dataset
37 */
38class StrategyChoice
39{
40public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060041 class Error : public tlv::Error
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040042 {
43 public:
44 explicit
45 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060046 : tlv::Error(what)
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040047 {
48 }
49 };
50
51 StrategyChoice()
52 {
53 }
54
55 explicit
56 StrategyChoice(const Block& payload)
57 {
58 this->wireDecode(payload);
59 }
60
61 template<bool T>
62 size_t
63 wireEncode(EncodingImpl<T>& encoder) const;
64
65 const Block&
66 wireEncode() const;
67
68 void
69 wireDecode(const Block& wire);
70
71public: // getters & setters
72 const Name&
73 getName() const
74 {
75 return m_name;
76 }
77
78 StrategyChoice&
79 setName(const Name& name)
80 {
81 m_wire.reset();
82 m_name = name;
83 return *this;
84 }
85
86 const Name&
87 getStrategy() const
88 {
89 return m_strategy;
90 }
91
92 StrategyChoice&
93 setStrategy(const Name& strategy)
94 {
95 m_wire.reset();
96 m_strategy = strategy;
97 return *this;
98 }
99
100private:
101 Name m_name; // namespace
102 Name m_strategy; // strategy for the namespace
103
104 mutable Block m_wire;
105};
106
107
108template<bool T>
109inline size_t
110StrategyChoice::wireEncode(EncodingImpl<T>& encoder) const
111{
112 size_t totalLength = 0;
113
114 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
115 totalLength += m_name.wireEncode(encoder);
116
117 totalLength += encoder.prependVarNumber(totalLength);
118 totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
119 return totalLength;
120}
121
122inline const Block&
123StrategyChoice::wireEncode() const
124{
125 if (m_wire.hasWire())
126 return m_wire;
127
128 EncodingEstimator estimator;
129 size_t estimatedSize = wireEncode(estimator);
130
131 EncodingBuffer buffer(estimatedSize, 0);
132 wireEncode(buffer);
133
134 m_wire = buffer.block();
135 return m_wire;
136}
137
138inline void
139StrategyChoice::wireDecode(const Block& block)
140{
141 if (block.type() != tlv::nfd::StrategyChoice) {
142 throw Error("expecting StrategyChoice block");
143 }
144 m_wire = block;
145 m_wire.parse();
146 Block::element_const_iterator val = m_wire.elements_begin();
147
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600148 if (val != m_wire.elements_end() && val->type() == tlv::Name) {
Alexander Afanasyev4671bf72014-05-19 09:01:37 -0400149 m_name.wireDecode(*val);
150 ++val;
151 }
152 else {
153 throw Error("missing required Name field");
154 }
155
156 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
157 val->parse();
158 if (val->elements().empty()) {
159 throw Error("expecting Strategy/Name");
160 }
161 else {
162 m_strategy.wireDecode(*val->elements_begin());
163 }
164 ++val;
165 }
166 else {
167 throw Error("missing required Strategy field");
168 }
169}
170
171} // namespace nfd
172} // namespace ndn
173
174#endif // NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP