blob: 3f9fa8c780b643538a51eb330a243b5b6ccbdc7b [file] [log] [blame]
Alexander Afanasyev4671bf72014-05-19 09:01:37 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 */
12
13#ifndef NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP
14#define NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP
15
16#include "../encoding/tlv-nfd.hpp"
17#include "../encoding/encoding-buffer.hpp"
18#include "../encoding/block-helpers.hpp"
19#include "../name.hpp"
20
21namespace ndn {
22namespace nfd {
23
24/**
25 * @ingroup management
26 * @brief represents NFD StrategyChoice dataset
27 * @sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy-Choice-Dataset
28 */
29class StrategyChoice
30{
31public:
32 class Error : public Tlv::Error
33 {
34 public:
35 explicit
36 Error(const std::string& what)
37 : Tlv::Error(what)
38 {
39 }
40 };
41
42 StrategyChoice()
43 {
44 }
45
46 explicit
47 StrategyChoice(const Block& payload)
48 {
49 this->wireDecode(payload);
50 }
51
52 template<bool T>
53 size_t
54 wireEncode(EncodingImpl<T>& encoder) const;
55
56 const Block&
57 wireEncode() const;
58
59 void
60 wireDecode(const Block& wire);
61
62public: // getters & setters
63 const Name&
64 getName() const
65 {
66 return m_name;
67 }
68
69 StrategyChoice&
70 setName(const Name& name)
71 {
72 m_wire.reset();
73 m_name = name;
74 return *this;
75 }
76
77 const Name&
78 getStrategy() const
79 {
80 return m_strategy;
81 }
82
83 StrategyChoice&
84 setStrategy(const Name& strategy)
85 {
86 m_wire.reset();
87 m_strategy = strategy;
88 return *this;
89 }
90
91private:
92 Name m_name; // namespace
93 Name m_strategy; // strategy for the namespace
94
95 mutable Block m_wire;
96};
97
98
99template<bool T>
100inline size_t
101StrategyChoice::wireEncode(EncodingImpl<T>& encoder) const
102{
103 size_t totalLength = 0;
104
105 totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
106 totalLength += m_name.wireEncode(encoder);
107
108 totalLength += encoder.prependVarNumber(totalLength);
109 totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
110 return totalLength;
111}
112
113inline const Block&
114StrategyChoice::wireEncode() const
115{
116 if (m_wire.hasWire())
117 return m_wire;
118
119 EncodingEstimator estimator;
120 size_t estimatedSize = wireEncode(estimator);
121
122 EncodingBuffer buffer(estimatedSize, 0);
123 wireEncode(buffer);
124
125 m_wire = buffer.block();
126 return m_wire;
127}
128
129inline void
130StrategyChoice::wireDecode(const Block& block)
131{
132 if (block.type() != tlv::nfd::StrategyChoice) {
133 throw Error("expecting StrategyChoice block");
134 }
135 m_wire = block;
136 m_wire.parse();
137 Block::element_const_iterator val = m_wire.elements_begin();
138
139 if (val != m_wire.elements_end() && val->type() == Tlv::Name) {
140 m_name.wireDecode(*val);
141 ++val;
142 }
143 else {
144 throw Error("missing required Name field");
145 }
146
147 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
148 val->parse();
149 if (val->elements().empty()) {
150 throw Error("expecting Strategy/Name");
151 }
152 else {
153 m_strategy.wireDecode(*val->elements_begin());
154 }
155 ++val;
156 }
157 else {
158 throw Error("missing required Strategy field");
159 }
160}
161
162} // namespace nfd
163} // namespace ndn
164
165#endif // NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP