Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |
| 14 | #define NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |
| 15 | |
Alexander Afanasyev | 274f2b0 | 2014-01-30 20:08:45 -0800 | [diff] [blame] | 16 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 17 | #include "../encoding/tlv-nfd.hpp" |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 20 | namespace nfd { |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * @brief Class defining abstraction of ControlResponse for NFD Control Protocol |
| 24 | * |
| 25 | * @see http://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
| 26 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 27 | class ControlResponse |
| 28 | { |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 29 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 30 | class Error : public Tlv::Error |
| 31 | { |
| 32 | public: |
| 33 | explicit |
| 34 | Error(const std::string& what) |
| 35 | : Tlv::Error(what) |
| 36 | { |
| 37 | } |
| 38 | }; |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 39 | |
| 40 | ControlResponse() |
| 41 | : m_code(200) |
| 42 | { |
| 43 | } |
| 44 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | ControlResponse(uint32_t code, const std::string& text) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 46 | : m_code(code) |
| 47 | , m_text(text) |
| 48 | { |
| 49 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 50 | |
| 51 | ControlResponse(const Block& block) |
| 52 | { |
| 53 | wireDecode(block); |
| 54 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 56 | inline uint32_t |
| 57 | getCode() const; |
| 58 | |
| 59 | inline void |
| 60 | setCode(uint32_t code); |
| 61 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | inline const std::string& |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 63 | getText() const; |
| 64 | |
| 65 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | setText(const std::string& text); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 67 | |
| 68 | inline const Block& |
| 69 | getBody() const; |
| 70 | |
| 71 | inline void |
| 72 | setBody(const Block& body); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 74 | inline const Block& |
| 75 | wireEncode() const; |
| 76 | |
| 77 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 78 | wireDecode(const Block& block); |
| 79 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 80 | protected: |
| 81 | uint32_t m_code; |
| 82 | std::string m_text; |
| 83 | Block m_body; |
| 84 | |
| 85 | mutable Block m_wire; |
| 86 | }; |
| 87 | |
| 88 | inline uint32_t |
| 89 | ControlResponse::getCode() const |
| 90 | { |
| 91 | return m_code; |
| 92 | } |
| 93 | |
| 94 | inline void |
| 95 | ControlResponse::setCode(uint32_t code) |
| 96 | { |
| 97 | m_code = code; |
| 98 | m_wire.reset(); |
| 99 | } |
| 100 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 101 | inline const std::string& |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 102 | ControlResponse::getText() const |
| 103 | { |
| 104 | return m_text; |
| 105 | } |
| 106 | |
| 107 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 108 | ControlResponse::setText(const std::string& text) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 109 | { |
| 110 | m_text = text; |
| 111 | m_wire.reset(); |
| 112 | } |
| 113 | |
| 114 | inline const Block& |
| 115 | ControlResponse::getBody() const |
| 116 | { |
| 117 | return m_body; |
| 118 | } |
| 119 | |
| 120 | inline void |
| 121 | ControlResponse::setBody(const Block& body) |
| 122 | { |
| 123 | m_body = body; |
| 124 | m_body.encode(); // will do nothing if already encoded |
| 125 | m_wire.reset(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | inline const Block& |
| 130 | ControlResponse::wireEncode() const |
| 131 | { |
| 132 | if (m_wire.hasWire()) |
| 133 | return m_wire; |
| 134 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 135 | m_wire = Block(tlv::nfd::ControlResponse); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 136 | m_wire.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 137 | (nonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code)); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 138 | |
| 139 | m_wire.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 140 | (dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size())); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 141 | |
| 142 | if (m_body.hasWire()) |
| 143 | { |
| 144 | m_wire.push_back(m_body); |
| 145 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | |
| 147 | m_wire.encode(); |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 148 | return m_wire; |
| 149 | } |
| 150 | |
| 151 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | ControlResponse::wireDecode(const Block& wire) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 153 | { |
| 154 | m_wire = wire; |
| 155 | m_wire.parse(); |
| 156 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 157 | if (m_wire.type() != tlv::nfd::ControlResponse) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 158 | throw Error("Requested decoding of ControlResponse, but Block is of different type"); |
| 159 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 160 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 161 | if (val == m_wire.elements_end() || |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 162 | val->type() != tlv::nfd::StatusCode) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 163 | { |
| 164 | throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)"); |
| 165 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 166 | |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 167 | m_code = readNonNegativeInteger(*val); |
| 168 | ++val; |
| 169 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 170 | if (val == m_wire.elements_end() || |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 171 | val->type() != tlv::nfd::StatusText) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 172 | { |
| 173 | throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)"); |
| 174 | } |
| 175 | m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 176 | ++val; |
| 177 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 178 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 179 | m_body = *val; |
| 180 | else |
| 181 | m_body = Block(); |
| 182 | } |
| 183 | |
| 184 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 185 | operator << (std::ostream& os, const ControlResponse& status) |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 186 | { |
| 187 | os << status.getCode() << " " << status.getText(); |
| 188 | return os; |
| 189 | } |
| 190 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 191 | } // namespace nfd |
Alexander Afanasyev | eaf105c | 2014-01-30 17:40:24 -0800 | [diff] [blame] | 192 | } // namespace ndn |
| 193 | |
| 194 | #endif // NDN_MANAGEMENT_CONTROL_RESPONSE_HPP |