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