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" |
| 11 | #include "../encoding/tlv-nfd-control.hpp" |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 12 | |
| 13 | namespace 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 | */ |
| 20 | class ControlResponse { |
| 21 | public: |
| 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 | |
| 59 | protected: |
| 60 | uint32_t m_code; |
| 61 | std::string m_text; |
| 62 | Block m_body; |
| 63 | |
| 64 | mutable Block m_wire; |
| 65 | }; |
| 66 | |
| 67 | inline uint32_t |
| 68 | ControlResponse::getCode() const |
| 69 | { |
| 70 | return m_code; |
| 71 | } |
| 72 | |
| 73 | inline void |
| 74 | ControlResponse::setCode(uint32_t code) |
| 75 | { |
| 76 | m_code = code; |
| 77 | m_wire.reset(); |
| 78 | } |
| 79 | |
| 80 | inline const std::string & |
| 81 | ControlResponse::getText() const |
| 82 | { |
| 83 | return m_text; |
| 84 | } |
| 85 | |
| 86 | inline void |
| 87 | ControlResponse::setText(const std::string &text) |
| 88 | { |
| 89 | m_text = text; |
| 90 | m_wire.reset(); |
| 91 | } |
| 92 | |
| 93 | inline const Block& |
| 94 | ControlResponse::getBody() const |
| 95 | { |
| 96 | return m_body; |
| 97 | } |
| 98 | |
| 99 | inline void |
| 100 | ControlResponse::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 | |
| 108 | inline const Block& |
| 109 | ControlResponse::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 | |
| 130 | inline void |
| 131 | ControlResponse::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 | |
| 160 | inline std::ostream& |
| 161 | operator << (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 |