All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nfd-control-response.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_MANAGEMENT_CONTROL_RESPONSE_HPP
8 #define NDN_MANAGEMENT_CONTROL_RESPONSE_HPP
9 
10 #include "../encoding/block.hpp"
11 #include "../encoding/tlv-nfd.hpp"
12 
13 namespace ndn {
14 namespace nfd {
15 
22 {
23 public:
24  class Error : public Tlv::Error
25  {
26  public:
27  explicit
28  Error(const std::string& what)
29  : Tlv::Error(what)
30  {
31  }
32  };
33 
35  : m_code(200)
36  {
37  }
38 
39  ControlResponse(uint32_t code, const std::string& text)
40  : m_code(code)
41  , m_text(text)
42  {
43  }
44 
45  ControlResponse(const Block& block)
46  {
47  wireDecode(block);
48  }
49 
50  inline uint32_t
51  getCode() const;
52 
53  inline void
54  setCode(uint32_t code);
55 
56  inline const std::string&
57  getText() const;
58 
59  inline void
60  setText(const std::string& text);
61 
62  inline const Block&
63  getBody() const;
64 
65  inline void
66  setBody(const Block& body);
67 
68  inline const Block&
69  wireEncode() const;
70 
71  inline void
72  wireDecode(const Block& block);
73 
74 protected:
75  uint32_t m_code;
76  std::string m_text;
78 
79  mutable Block m_wire;
80 };
81 
82 inline uint32_t
84 {
85  return m_code;
86 }
87 
88 inline void
90 {
91  m_code = code;
92  m_wire.reset();
93 }
94 
95 inline const std::string&
97 {
98  return m_text;
99 }
100 
101 inline void
102 ControlResponse::setText(const std::string& text)
103 {
104  m_text = text;
105  m_wire.reset();
106 }
107 
108 inline const Block&
110 {
111  return m_body;
112 }
113 
114 inline void
116 {
117  m_body = body;
118  m_body.encode(); // will do nothing if already encoded
119  m_wire.reset();
120 }
121 
122 
123 inline const Block&
125 {
126  if (m_wire.hasWire())
127  return m_wire;
128 
132 
134  (dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size()));
135 
136  if (m_body.hasWire())
137  {
139  }
140 
141  m_wire.encode();
142  return m_wire;
143 }
144 
145 inline void
147 {
148  m_wire = wire;
149  m_wire.parse();
150 
152  throw Error("Requested decoding of ControlResponse, but Block is of different type");
153 
155  if (val == m_wire.elements_end() ||
156  val->type() != tlv::nfd::StatusCode)
157  {
158  throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)");
159  }
160 
162  ++val;
163 
164  if (val == m_wire.elements_end() ||
165  val->type() != tlv::nfd::StatusText)
166  {
167  throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)");
168  }
169  m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
170  ++val;
171 
172  if (val != m_wire.elements_end())
173  m_body = *val;
174  else
175  m_body = Block();
176 }
177 
178 inline std::ostream&
179 operator << (std::ostream& os, const ControlResponse& status)
180 {
181  os << status.getCode() << " " << status.getText();
182  return os;
183 }
184 
185 } // namespace nfd
186 } // namespace ndn
187 
188 #endif // NDN_MANAGEMENT_CONTROL_RESPONSE_HPP
std::ostream & operator<<(std::ostream &os, const ControlParameters &parameters)
Block nonNegativeIntegerBlock(uint32_t type, uint64_t value)
ControlResponse(const Block &block)
const Block & getBody() const
ControlResponse(uint32_t code, const std::string &text)
const Block & wireEncode() const
void setBody(const Block &body)
Class representing wire element of the NDN packet.
Definition: block.hpp:26
void setText(const std::string &text)
uint64_t readNonNegativeInteger(const Block &block)
element_const_iterator elements_end() const
Definition: block.hpp:476
element_const_iterator elements_begin() const
Definition: block.hpp:470
Class defining abstraction of ControlResponse for NFD Control Protocol.
const std::string & getText() const
void wireDecode(const Block &block)
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
void push_back(const Block &element)
Definition: block.hpp:390
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:298
Block dataBlock(uint32_t type, const char *data, size_t dataSize)
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288