blob: b85b2d4f74054e7025f8c05f08f83e6ac9702c61 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev80b68e12015-09-17 17:01:04 -07003 * Copyright (c) 2013-2015 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);
58 m_wire.push_back(nonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code));
59
60 m_wire.push_back(dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size()));
61
62 if (m_body.hasWire()) {
63 m_wire.push_back(m_body);
64 }
65
66 m_wire.encode();
67 return m_wire;
68}
69
70void
71ControlResponse::wireDecode(const Block& wire)
72{
73 m_wire = wire;
74 m_wire.parse();
75
76 if (m_wire.type() != tlv::nfd::ControlResponse)
77 throw Error("Requested decoding of ControlResponse, but Block is of different type");
78
79 Block::element_const_iterator val = m_wire.elements_begin();
80 if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) {
81 throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)");
82 }
83
84 m_code = readNonNegativeInteger(*val);
85 ++val;
86
87 if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) {
88 throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)");
89 }
90 m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
91 ++val;
92
93 if (val != m_wire.elements_end())
94 m_body = *val;
95 else
96 m_body = Block();
97}
98
99std::ostream&
100operator<<(std::ostream& os, const ControlResponse& response)
101{
102 os << response.getCode() << " " << response.getText();
103 return os;
104}
105
106} // namespace mgmt
107} // namespace ndn