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