Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 14 | * |
Alexander Afanasyev | 80b68e1 | 2015-09-17 17:01:04 -0700 | [diff] [blame] | 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. |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "control-response.hpp" |
| 23 | #include "../encoding/block-helpers.hpp" |
| 24 | #include "../encoding/tlv-nfd.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace mgmt { |
| 28 | |
| 29 | // BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlResponse>)); |
| 30 | BOOST_CONCEPT_ASSERT((WireEncodable<ControlResponse>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireDecodable<ControlResponse>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, ControlResponse::Error>::value, |
| 33 | "ControlResponse::Error must inherit from tlv::Error"); |
| 34 | |
| 35 | ControlResponse::ControlResponse() |
| 36 | : m_code(200) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | ControlResponse::ControlResponse(uint32_t code, const std::string& text) |
| 41 | : m_code(code) |
| 42 | , m_text(text) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | ControlResponse::ControlResponse(const Block& block) |
| 47 | { |
| 48 | wireDecode(block); |
| 49 | } |
| 50 | |
| 51 | const Block& |
| 52 | ControlResponse::wireEncode() const |
| 53 | { |
| 54 | if (m_wire.hasWire()) |
| 55 | return m_wire; |
| 56 | |
| 57 | m_wire = Block(tlv::nfd::ControlResponse); |
Junxiao Shi | 19d7a57 | 2016-07-26 12:55:09 +0000 | [diff] [blame] | 58 | m_wire.push_back(makeNonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code)); |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 59 | m_wire.push_back(makeStringBlock(tlv::nfd::StatusText, m_text)); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 60 | |
| 61 | if (m_body.hasWire()) { |
| 62 | m_wire.push_back(m_body); |
| 63 | } |
| 64 | |
| 65 | m_wire.encode(); |
| 66 | return m_wire; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | ControlResponse::wireDecode(const Block& wire) |
| 71 | { |
| 72 | m_wire = wire; |
| 73 | m_wire.parse(); |
| 74 | |
| 75 | if (m_wire.type() != tlv::nfd::ControlResponse) |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 76 | BOOST_THROW_EXCEPTION(Error("expected ControlResponse, got " + to_string(m_wire.type()) + " element")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 77 | |
| 78 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 79 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 80 | BOOST_THROW_EXCEPTION(Error("missing StatusCode sub-element")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 81 | } |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 82 | m_code = readNonNegativeInteger(*val); |
| 83 | ++val; |
| 84 | |
| 85 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 86 | BOOST_THROW_EXCEPTION(Error("missing StatusText sub-element")); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 87 | } |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 88 | m_text = readString(*val); |
Yanbiao Li | 8ee37ed | 2015-05-19 12:44:04 -0700 | [diff] [blame] | 89 | ++val; |
| 90 | |
| 91 | if (val != m_wire.elements_end()) |
| 92 | m_body = *val; |
| 93 | else |
| 94 | m_body = Block(); |
| 95 | } |
| 96 | |
| 97 | std::ostream& |
| 98 | operator<<(std::ostream& os, const ControlResponse& response) |
| 99 | { |
| 100 | os << response.getCode() << " " << response.getText(); |
| 101 | return os; |
| 102 | } |
| 103 | |
| 104 | } // namespace mgmt |
| 105 | } // namespace ndn |