blob: 30457838a7b49799a9d8e50c7c1fa39ac2ad1127 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07004 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07006 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07007 * 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 Li8ee37ed2015-05-19 12:44:04 -070010 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070011 * 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 Li8ee37ed2015-05-19 12:44:04 -070014 *
Alexander Afanasyev80b68e12015-09-17 17:01:04 -070015 * 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 Li8ee37ed2015-05-19 12:44:04 -070020 */
21
22#include "control-response.hpp"
23#include "../encoding/block-helpers.hpp"
24#include "../encoding/tlv-nfd.hpp"
25
26namespace ndn {
27namespace mgmt {
28
29// BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlResponse>));
30BOOST_CONCEPT_ASSERT((WireEncodable<ControlResponse>));
31BOOST_CONCEPT_ASSERT((WireDecodable<ControlResponse>));
32static_assert(std::is_base_of<tlv::Error, ControlResponse::Error>::value,
33 "ControlResponse::Error must inherit from tlv::Error");
34
35ControlResponse::ControlResponse()
36 : m_code(200)
37{
38}
39
40ControlResponse::ControlResponse(uint32_t code, const std::string& text)
41 : m_code(code)
42 , m_text(text)
43{
44}
45
46ControlResponse::ControlResponse(const Block& block)
47{
48 wireDecode(block);
49}
50
51const Block&
52ControlResponse::wireEncode() const
53{
54 if (m_wire.hasWire())
55 return m_wire;
56
57 m_wire = Block(tlv::nfd::ControlResponse);
Junxiao Shi19d7a572016-07-26 12:55:09 +000058 m_wire.push_back(makeNonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code));
Junxiao Shi5d75fd92017-08-08 18:09:20 +000059 m_wire.push_back(makeStringBlock(tlv::nfd::StatusText, m_text));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070060
61 if (m_body.hasWire()) {
62 m_wire.push_back(m_body);
63 }
64
65 m_wire.encode();
66 return m_wire;
67}
68
69void
70ControlResponse::wireDecode(const Block& wire)
71{
72 m_wire = wire;
73 m_wire.parse();
74
75 if (m_wire.type() != tlv::nfd::ControlResponse)
Junxiao Shi5d75fd92017-08-08 18:09:20 +000076 BOOST_THROW_EXCEPTION(Error("expected ControlResponse, got " + to_string(m_wire.type()) + " element"));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070077
78 Block::element_const_iterator val = m_wire.elements_begin();
79 if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000080 BOOST_THROW_EXCEPTION(Error("missing StatusCode sub-element"));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070081 }
Junxiao Shi51742322017-08-13 17:04:52 +000082 m_code = readNonNegativeIntegerAs<uint32_t>(*val);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070083 ++val;
84
85 if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000086 BOOST_THROW_EXCEPTION(Error("missing StatusText sub-element"));
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070087 }
Junxiao Shi5d75fd92017-08-08 18:09:20 +000088 m_text = readString(*val);
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070089 ++val;
90
91 if (val != m_wire.elements_end())
92 m_body = *val;
93 else
94 m_body = Block();
95}
96
97std::ostream&
98operator<<(std::ostream& os, const ControlResponse& response)
99{
100 os << response.getCode() << " " << response.getText();
101 return os;
102}
103
104} // namespace mgmt
105} // namespace ndn