Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 274f2b0 | 2014-01-30 20:08:45 -0800 | [diff] [blame] | 10 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 11 | #include "../encoding/tlv-nfd.hpp" |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 14 | namespace nfd { |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * @brief Class defining abstraction of ControlResponse for NFD Control Protocol |
| 18 | * |
| 19 | * @see http://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
| 20 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame^] | 21 | class ControlResponse |
| 22 | { |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 23 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 24 | class Error : public Tlv::Error |
| 25 | { |
| 26 | public: |
| 27 | explicit |
| 28 | Error(const std::string& what) |
| 29 | : Tlv::Error(what) |
| 30 | { |
| 31 | } |
| 32 | }; |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 33 | |
| 34 | ControlResponse() |
| 35 | : m_code(200) |
| 36 | { |
| 37 | } |
| 38 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 39 | ControlResponse(uint32_t code, const std::string& text) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 40 | : m_code(code) |
| 41 | , m_text(text) |
| 42 | { |
| 43 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 44 | |
| 45 | ControlResponse(const Block& block) |
| 46 | { |
| 47 | wireDecode(block); |
| 48 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 50 | inline uint32_t |
| 51 | getCode() const; |
| 52 | |
| 53 | inline void |
| 54 | setCode(uint32_t code); |
| 55 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 56 | inline const std::string& |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 57 | getText() const; |
| 58 | |
| 59 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | setText(const std::string& text); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 61 | |
| 62 | inline const Block& |
| 63 | getBody() const; |
| 64 | |
| 65 | inline void |
| 66 | setBody(const Block& body); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 68 | inline const Block& |
| 69 | wireEncode() const; |
| 70 | |
| 71 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | wireDecode(const Block& block); |
| 73 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 74 | protected: |
| 75 | uint32_t m_code; |
| 76 | std::string m_text; |
| 77 | Block m_body; |
| 78 | |
| 79 | mutable Block m_wire; |
| 80 | }; |
| 81 | |
| 82 | inline uint32_t |
| 83 | ControlResponse::getCode() const |
| 84 | { |
| 85 | return m_code; |
| 86 | } |
| 87 | |
| 88 | inline void |
| 89 | ControlResponse::setCode(uint32_t code) |
| 90 | { |
| 91 | m_code = code; |
| 92 | m_wire.reset(); |
| 93 | } |
| 94 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | inline const std::string& |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 96 | ControlResponse::getText() const |
| 97 | { |
| 98 | return m_text; |
| 99 | } |
| 100 | |
| 101 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | ControlResponse::setText(const std::string& text) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 103 | { |
| 104 | m_text = text; |
| 105 | m_wire.reset(); |
| 106 | } |
| 107 | |
| 108 | inline const Block& |
| 109 | ControlResponse::getBody() const |
| 110 | { |
| 111 | return m_body; |
| 112 | } |
| 113 | |
| 114 | inline void |
| 115 | ControlResponse::setBody(const Block& body) |
| 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& |
| 124 | ControlResponse::wireEncode() const |
| 125 | { |
| 126 | if (m_wire.hasWire()) |
| 127 | return m_wire; |
| 128 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 129 | m_wire = Block(tlv::nfd::ControlResponse); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 130 | m_wire.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 131 | (nonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code)); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 132 | |
| 133 | m_wire.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 134 | (dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size())); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 135 | |
| 136 | if (m_body.hasWire()) |
| 137 | { |
| 138 | m_wire.push_back(m_body); |
| 139 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 140 | |
| 141 | m_wire.encode(); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 142 | return m_wire; |
| 143 | } |
| 144 | |
| 145 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | ControlResponse::wireDecode(const Block& wire) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 147 | { |
| 148 | m_wire = wire; |
| 149 | m_wire.parse(); |
| 150 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 151 | if (m_wire.type() != tlv::nfd::ControlResponse) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | throw Error("Requested decoding of ControlResponse, but Block is of different type"); |
| 153 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 154 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 155 | if (val == m_wire.elements_end() || |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 156 | val->type() != tlv::nfd::StatusCode) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 157 | { |
| 158 | throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)"); |
| 159 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 161 | m_code = readNonNegativeInteger(*val); |
| 162 | ++val; |
| 163 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 164 | if (val == m_wire.elements_end() || |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 165 | val->type() != tlv::nfd::StatusText) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 172 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 173 | m_body = *val; |
| 174 | else |
| 175 | m_body = Block(); |
| 176 | } |
| 177 | |
| 178 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 179 | operator << (std::ostream& os, const ControlResponse& status) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 180 | { |
| 181 | os << status.getCode() << " " << status.getText(); |
| 182 | return os; |
| 183 | } |
| 184 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 185 | } // namespace nfd |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 186 | } // namespace ndn |
| 187 | |
| 188 | #endif // NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |