blob: 9c66f2a06475f8a208962c62d25b7863e59d6519 [file] [log] [blame]
Yanbiao Li8ee37ed2015-05-19 12:44:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "control-response.hpp"
27#include "../encoding/block-helpers.hpp"
28#include "../encoding/tlv-nfd.hpp"
29
30namespace ndn {
31namespace mgmt {
32
33// BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlResponse>));
34BOOST_CONCEPT_ASSERT((WireEncodable<ControlResponse>));
35BOOST_CONCEPT_ASSERT((WireDecodable<ControlResponse>));
36static_assert(std::is_base_of<tlv::Error, ControlResponse::Error>::value,
37 "ControlResponse::Error must inherit from tlv::Error");
38
39ControlResponse::ControlResponse()
40 : m_code(200)
41{
42}
43
44ControlResponse::ControlResponse(uint32_t code, const std::string& text)
45 : m_code(code)
46 , m_text(text)
47{
48}
49
50ControlResponse::ControlResponse(const Block& block)
51{
52 wireDecode(block);
53}
54
55const Block&
56ControlResponse::wireEncode() const
57{
58 if (m_wire.hasWire())
59 return m_wire;
60
61 m_wire = Block(tlv::nfd::ControlResponse);
62 m_wire.push_back(nonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code));
63
64 m_wire.push_back(dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size()));
65
66 if (m_body.hasWire()) {
67 m_wire.push_back(m_body);
68 }
69
70 m_wire.encode();
71 return m_wire;
72}
73
74void
75ControlResponse::wireDecode(const Block& wire)
76{
77 m_wire = wire;
78 m_wire.parse();
79
80 if (m_wire.type() != tlv::nfd::ControlResponse)
81 throw Error("Requested decoding of ControlResponse, but Block is of different type");
82
83 Block::element_const_iterator val = m_wire.elements_begin();
84 if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) {
85 throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)");
86 }
87
88 m_code = readNonNegativeInteger(*val);
89 ++val;
90
91 if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) {
92 throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)");
93 }
94 m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
95 ++val;
96
97 if (val != m_wire.elements_end())
98 m_body = *val;
99 else
100 m_body = Block();
101}
102
103std::ostream&
104operator<<(std::ostream& os, const ControlResponse& response)
105{
106 os << response.getCode() << " " << response.getText();
107 return os;
108}
109
110} // namespace mgmt
111} // namespace ndn