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