blob: 60d846d7d574e6fbb86fad3e547829e452e83dc9 [file] [log] [blame]
hilata7d160f22014-03-13 19:51:42 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (C) 2014 Named Data Networking Project
4* See COPYING for copyright and distribution information.
5*/
6
7#ifndef NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_OPTIONS_HPP
8#define NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_OPTIONS_HPP
9
10#include "../encoding/block.hpp"
11#include "../encoding/encoding-buffer.hpp"
12#include "../encoding/tlv-nfd.hpp"
13#include "../name.hpp"
14
15namespace ndn {
16namespace nfd {
17
18class StrategyChoiceOptions{
19public:
20 struct Error : public Tlv::Error
21 {
22 Error(const std::string& what) : Tlv::Error(what)
23 {
24 }
25 };
26
27 StrategyChoiceOptions()
28 {
29 }
30
31 StrategyChoiceOptions(const Block& block)
32 {
33 wireDecode(block);
34 }
35
36 const Name&
37 getName() const
38 {
39 return m_name;
40 }
41
42 StrategyChoiceOptions&
43 setName(const Name& name)
44 {
45 m_name = name;
46 m_wire.reset();
47 return *this;
48 }
49
50 const Name&
51 getStrategy() const
52 {
53 return m_strategy;
54 }
55
56 StrategyChoiceOptions&
57 setStrategy(const Name& strategy)
58 {
59 m_strategy = strategy;
60 m_wire.reset();
61 return *this;
62 }
63
64 template<bool T>
65 size_t
66 wireEncode(EncodingImpl<T>& block) const;
67
68 const Block&
69 wireEncode() const;
70
71 void
72 wireDecode(const Block& wire);
73
74private:
75 Name m_name;
76 Name m_strategy;
77
78 mutable Block m_wire;
79};
80
81template<bool T>
82inline size_t
83StrategyChoiceOptions::wireEncode(EncodingImpl<T>& block) const
84{
85 size_t totalLength = 0;
86
87 if (!m_strategy.empty())
88 {
89 totalLength += prependNestedBlock(block, tlv::nfd::Strategy, m_strategy);
90 }
91
92 totalLength += m_name.wireEncode(block);
93
94 totalLength += block.prependVarNumber(totalLength);
95 totalLength += block.prependVarNumber(tlv::nfd::StrategyChoiceOptions);
96 return totalLength;
97}
98
99inline const Block&
100StrategyChoiceOptions::wireEncode() const
101{
102 if (m_wire.hasWire())
103 return m_wire;
104
105 EncodingEstimator estimator;
106 size_t estimatedSize = wireEncode(estimator);
107
108 EncodingBuffer buffer(estimatedSize, 0);
109 wireEncode(buffer);
110
111 m_wire = buffer.block();
112 return m_wire;
113}
114
115inline void
116StrategyChoiceOptions::wireDecode(const Block& wire)
117{
118 m_name.clear();
119 m_strategy.clear();
120
121 m_wire = wire;
122
123 if (m_wire.type() != tlv::nfd::StrategyChoiceOptions)
124 throw Error("Requested decoding of StrategyChoiceOptions, but Block is of different type");
125
126 m_wire.parse();
127
128 // Name
129 Block::element_const_iterator val = m_wire.find(Tlv::Name);
130 if (val != m_wire.elements_end())
131 {
132 m_name.wireDecode(*val);
133 }
134
135 // Strategy
136 val = m_wire.find(tlv::nfd::Strategy);
137 if (val != m_wire.elements_end())
138 {
139 val->parse();
140 if (!val->elements().empty())
141 m_strategy.wireDecode(*val->elements_begin());
142 }
143}
144
145inline std::ostream&
146operator << (std::ostream& os, const StrategyChoiceOptions& option)
147{
148 os << "StrategyChoiceOptions(";
149
150 // Name
151 os << "Name: " << option.getName() << ", ";
152
153 // Strategy
154 os << "Strategy: " << option.getStrategy() << ", ";
155
156 os << ")";
157 return os;
158}
159
160} // namespace nfd
161} // namespace ndn
162
163#endif // NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_MANAGEMENT_OPTIONS_HPP