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