blob: 7056162591da817d36a8568a3e3ca718c61a8ee8 [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Junxiao Shi70911652014-08-12 10:14:24 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
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.
10 *
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.
14 *
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.
20 */
21
22#include "nfd-control-response.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
Junxiao Shi70911652014-08-12 10:14:24 -070025
26namespace ndn {
27namespace nfd {
28
Junxiao Shi65f1a712014-11-20 14:59:36 -070029//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
Junxiao Shi70911652014-08-12 10:14:24 -070035ControlResponse::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);
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070058 m_wire.push_back(makeNonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code));
Junxiao Shi70911652014-08-12 10:14:24 -070059
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070060 m_wire.push_back(makeBinaryBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size()));
Junxiao Shi70911652014-08-12 10:14:24 -070061
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070062 if (m_body.hasWire()) {
63 m_wire.push_back(m_body);
64 }
Junxiao Shi70911652014-08-12 10:14:24 -070065
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)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070077 BOOST_THROW_EXCEPTION(Error("Requested decoding of ControlResponse, but Block is of different "
78 "type"));
Junxiao Shi70911652014-08-12 10:14:24 -070079
80 Block::element_const_iterator val = m_wire.elements_begin();
81 if (val == m_wire.elements_end() ||
82 val->type() != tlv::nfd::StatusCode)
83 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070084 BOOST_THROW_EXCEPTION(Error("Incorrect ControlResponse format (StatusCode missing or not the "
85 "first item)"));
Junxiao Shi70911652014-08-12 10:14:24 -070086 }
87
88 m_code = readNonNegativeInteger(*val);
89 ++val;
90
91 if (val == m_wire.elements_end() ||
92 val->type() != tlv::nfd::StatusText)
93 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070094 BOOST_THROW_EXCEPTION(Error("Incorrect ControlResponse format (StatusText missing or not the "
95 "second item)"));
Junxiao Shi70911652014-08-12 10:14:24 -070096 }
97 m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
98 ++val;
99
100 if (val != m_wire.elements_end())
101 m_body = *val;
102 else
103 m_body = Block();
104}
105
106std::ostream&
107operator<<(std::ostream& os, const ControlResponse& response)
108{
109 os << response.getCode() << " " << response.getText();
110 return os;
111}
112
113} // namespace nfd
114} // namespace ndn