Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 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 Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame^] | 23 | #include "encoding/tlv-nfd.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/concepts.hpp" |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame^] | 30 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ControlResponse>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<ControlResponse>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<ControlResponse>)); |
| 33 | static_assert(std::is_base_of<tlv::Error, ControlResponse::Error>::value, |
| 34 | "ControlResponse::Error must inherit from tlv::Error"); |
| 35 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 36 | ControlResponse::ControlResponse() |
| 37 | : m_code(200) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | ControlResponse::ControlResponse(uint32_t code, const std::string& text) |
| 42 | : m_code(code) |
| 43 | , m_text(text) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | ControlResponse::ControlResponse(const Block& block) |
| 48 | { |
| 49 | wireDecode(block); |
| 50 | } |
| 51 | |
| 52 | const Block& |
| 53 | ControlResponse::wireEncode() const |
| 54 | { |
| 55 | if (m_wire.hasWire()) |
| 56 | return m_wire; |
| 57 | |
| 58 | m_wire = Block(tlv::nfd::ControlResponse); |
| 59 | m_wire.push_back |
| 60 | (nonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code)); |
| 61 | |
| 62 | m_wire.push_back |
| 63 | (dataBlock(tlv::nfd::StatusText, m_text.c_str(), m_text.size())); |
| 64 | |
| 65 | if (m_body.hasWire()) |
| 66 | { |
| 67 | m_wire.push_back(m_body); |
| 68 | } |
| 69 | |
| 70 | m_wire.encode(); |
| 71 | return m_wire; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | ControlResponse::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() || |
| 85 | val->type() != tlv::nfd::StatusCode) |
| 86 | { |
| 87 | throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)"); |
| 88 | } |
| 89 | |
| 90 | m_code = readNonNegativeInteger(*val); |
| 91 | ++val; |
| 92 | |
| 93 | if (val == m_wire.elements_end() || |
| 94 | val->type() != tlv::nfd::StatusText) |
| 95 | { |
| 96 | throw Error("Incorrect ControlResponse format (StatusText missing or not the second item)"); |
| 97 | } |
| 98 | m_text.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 99 | ++val; |
| 100 | |
| 101 | if (val != m_wire.elements_end()) |
| 102 | m_body = *val; |
| 103 | else |
| 104 | m_body = Block(); |
| 105 | } |
| 106 | |
| 107 | std::ostream& |
| 108 | operator<<(std::ostream& os, const ControlResponse& response) |
| 109 | { |
| 110 | os << response.getCode() << " " << response.getText(); |
| 111 | return os; |
| 112 | } |
| 113 | |
| 114 | } // namespace nfd |
| 115 | } // namespace ndn |