Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |
| 23 | #define NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |
| 24 | |
Alexander Afanasyev | 274f2b0 | 2014-01-30 20:08:45 -0800 | [diff] [blame] | 25 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "../encoding/block-helpers.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 27 | #include "../encoding/tlv-nfd.hpp" |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 30 | namespace nfd { |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 33 | * @ingroup management |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 34 | * @brief Class defining abstraction of ControlResponse for NFD Control Protocol |
| 35 | * |
| 36 | * @see http://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
| 37 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 38 | class ControlResponse |
| 39 | { |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 40 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 41 | class Error : public Tlv::Error |
| 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | Error(const std::string& what) |
| 46 | : Tlv::Error(what) |
| 47 | { |
| 48 | } |
| 49 | }; |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 50 | |
| 51 | ControlResponse() |
| 52 | : m_code(200) |
| 53 | { |
| 54 | } |
| 55 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 56 | ControlResponse(uint32_t code, const std::string& text) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 57 | : m_code(code) |
| 58 | , m_text(text) |
| 59 | { |
| 60 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 61 | |
| 62 | ControlResponse(const Block& block) |
| 63 | { |
| 64 | wireDecode(block); |
| 65 | } |
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 uint32_t |
| 68 | getCode() const; |
| 69 | |
| 70 | inline void |
| 71 | setCode(uint32_t code); |
| 72 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 73 | inline const std::string& |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 74 | getText() const; |
| 75 | |
| 76 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 77 | setText(const std::string& text); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 78 | |
| 79 | inline const Block& |
| 80 | getBody() const; |
| 81 | |
| 82 | inline void |
| 83 | setBody(const Block& body); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 85 | inline const Block& |
| 86 | wireEncode() const; |
| 87 | |
| 88 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | wireDecode(const Block& block); |
| 90 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 91 | protected: |
| 92 | uint32_t m_code; |
| 93 | std::string m_text; |
| 94 | Block m_body; |
| 95 | |
| 96 | mutable Block m_wire; |
| 97 | }; |
| 98 | |
| 99 | inline uint32_t |
| 100 | ControlResponse::getCode() const |
| 101 | { |
| 102 | return m_code; |
| 103 | } |
| 104 | |
| 105 | inline void |
| 106 | ControlResponse::setCode(uint32_t code) |
| 107 | { |
| 108 | m_code = code; |
| 109 | m_wire.reset(); |
| 110 | } |
| 111 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | inline const std::string& |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 113 | ControlResponse::getText() const |
| 114 | { |
| 115 | return m_text; |
| 116 | } |
| 117 | |
| 118 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 119 | ControlResponse::setText(const std::string& text) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 120 | { |
| 121 | m_text = text; |
| 122 | m_wire.reset(); |
| 123 | } |
| 124 | |
| 125 | inline const Block& |
| 126 | ControlResponse::getBody() const |
| 127 | { |
| 128 | return m_body; |
| 129 | } |
| 130 | |
| 131 | inline void |
| 132 | ControlResponse::setBody(const Block& body) |
| 133 | { |
| 134 | m_body = body; |
| 135 | m_body.encode(); // will do nothing if already encoded |
| 136 | m_wire.reset(); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | inline const Block& |
| 141 | ControlResponse::wireEncode() const |
| 142 | { |
| 143 | if (m_wire.hasWire()) |
| 144 | return m_wire; |
| 145 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 146 | m_wire = Block(tlv::nfd::ControlResponse); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 147 | m_wire.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 148 | (nonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code)); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 149 | |
| 150 | m_wire.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 151 | (dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size())); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 152 | |
| 153 | if (m_body.hasWire()) |
| 154 | { |
| 155 | m_wire.push_back(m_body); |
| 156 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 157 | |
| 158 | m_wire.encode(); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 159 | return m_wire; |
| 160 | } |
| 161 | |
| 162 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 163 | ControlResponse::wireDecode(const Block& wire) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 164 | { |
| 165 | m_wire = wire; |
| 166 | m_wire.parse(); |
| 167 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 168 | if (m_wire.type() != tlv::nfd::ControlResponse) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 169 | throw Error("Requested decoding of ControlResponse, but Block is of different type"); |
| 170 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 171 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 172 | if (val == m_wire.elements_end() || |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 173 | val->type() != tlv::nfd::StatusCode) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 174 | { |
| 175 | throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)"); |
| 176 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 178 | m_code = readNonNegativeInteger(*val); |
| 179 | ++val; |
| 180 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 181 | if (val == m_wire.elements_end() || |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 182 | val->type() != tlv::nfd::StatusText) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 183 | { |
| 184 | throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)"); |
| 185 | } |
| 186 | m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 187 | ++val; |
| 188 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 189 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 190 | m_body = *val; |
| 191 | else |
| 192 | m_body = Block(); |
| 193 | } |
| 194 | |
| 195 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 196 | operator << (std::ostream& os, const ControlResponse& status) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 197 | { |
| 198 | os << status.getCode() << " " << status.getText(); |
| 199 | return os; |
| 200 | } |
| 201 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 202 | } // namespace nfd |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 203 | } // namespace ndn |
| 204 | |
| 205 | #endif // NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |