blob: f493f5a9ab1abf744c5151b8d54633947d4e50ad [file] [log] [blame]
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_MANAGEMENT_CONTROL_RESPONSE_HPP
8#define NDN_MANAGEMENT_CONTROL_RESPONSE_HPP
9
Alexander Afanasyev274f2b02014-01-30 20:08:45 -080010#include "../encoding/block.hpp"
11#include "../encoding/tlv-nfd-control.hpp"
Alexander Afanasyeveaf105c2014-01-30 17:40:24 -080012
13namespace ndn {
14
15/**
16 * @brief Class defining abstraction of ControlResponse for NFD Control Protocol
17 *
18 * @see http://redmine.named-data.net/projects/nfd/wiki/ControlCommand
19 */
20class ControlResponse {
21public:
22 struct Error : public Tlv::Error { Error(const std::string &what) : Tlv::Error(what) {} };
23
24 ControlResponse()
25 : m_code(200)
26 {
27 }
28
29 ControlResponse(uint32_t code, const std::string &text)
30 : m_code(code)
31 , m_text(text)
32 {
33 }
34
35 inline uint32_t
36 getCode() const;
37
38 inline void
39 setCode(uint32_t code);
40
41 inline const std::string &
42 getText() const;
43
44 inline void
45 setText(const std::string &text);
46
47 inline const Block&
48 getBody() const;
49
50 inline void
51 setBody(const Block& body);
52
53 inline const Block&
54 wireEncode() const;
55
56 inline void
57 wireDecode(const Block &block);
58
59protected:
60 uint32_t m_code;
61 std::string m_text;
62 Block m_body;
63
64 mutable Block m_wire;
65};
66
67inline uint32_t
68ControlResponse::getCode() const
69{
70 return m_code;
71}
72
73inline void
74ControlResponse::setCode(uint32_t code)
75{
76 m_code = code;
77 m_wire.reset();
78}
79
80inline const std::string &
81ControlResponse::getText() const
82{
83 return m_text;
84}
85
86inline void
87ControlResponse::setText(const std::string &text)
88{
89 m_text = text;
90 m_wire.reset();
91}
92
93inline const Block&
94ControlResponse::getBody() const
95{
96 return m_body;
97}
98
99inline void
100ControlResponse::setBody(const Block& body)
101{
102 m_body = body;
103 m_body.encode(); // will do nothing if already encoded
104 m_wire.reset();
105}
106
107
108inline const Block&
109ControlResponse::wireEncode() const
110{
111 if (m_wire.hasWire())
112 return m_wire;
113
114 m_wire = Block(tlv::nfd_control::ControlResponse);
115 m_wire.push_back
116 (nonNegativeIntegerBlock(tlv::nfd_control::StatusCode, m_code));
117
118 m_wire.push_back
119 (dataBlock(tlv::nfd_control::StatusText, m_text.c_str(), m_text.size()));
120
121 if (m_body.hasWire())
122 {
123 m_wire.push_back(m_body);
124 }
125
126 m_wire.encode();
127 return m_wire;
128}
129
130inline void
131ControlResponse::wireDecode(const Block &wire)
132{
133 m_wire = wire;
134 m_wire.parse();
135
136 Block::element_iterator val = m_wire.getAll().begin();
137 if (val == m_wire.getAll().end() ||
138 val->type() != tlv::nfd_control::StatusCode)
139 {
140 throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)");
141 }
142
143 m_code = readNonNegativeInteger(*val);
144 ++val;
145
146 if (val == m_wire.getAll().end() ||
147 val->type() != tlv::nfd_control::StatusText)
148 {
149 throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)");
150 }
151 m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
152 ++val;
153
154 if (val != m_wire.getAll().end())
155 m_body = *val;
156 else
157 m_body = Block();
158}
159
160inline std::ostream&
161operator << (std::ostream &os, const ControlResponse &status)
162{
163 os << status.getCode() << " " << status.getText();
164 return os;
165}
166
167} // namespace ndn
168
169#endif // NDN_MANAGEMENT_CONTROL_RESPONSE_HPP