ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
control-response.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2025 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
25
26namespace ndn::mgmt {
27
29
30ControlResponse::ControlResponse(uint32_t code, const std::string& text)
31 : m_code(code)
32 , m_text(text)
33{
34}
35
37{
38 wireDecode(block);
39}
40
43{
44 m_code = code;
45 m_wire.reset();
46 return *this;
47}
48
50ControlResponse::setText(const std::string& text)
51{
52 m_text = text;
53 m_wire.reset();
54 return *this;
55}
56
59{
60 m_body = body;
61 m_body.encode(); // will do nothing if already encoded
62 m_wire.reset();
63 return *this;
64}
65
66const Block&
68{
69 if (m_wire.hasWire()) {
70 return m_wire;
71 }
72
74 m_wire.push_back(makeNonNegativeIntegerBlock(tlv::nfd::StatusCode, m_code));
76 if (m_body.hasWire()) {
78 }
79
80 m_wire.encode();
81 return m_wire;
82}
83
84void
86{
87 if (wire.type() != tlv::nfd::ControlResponse) {
88 NDN_THROW(Error("ControlResponse", wire.type()));
89 }
90
91 *this = {};
92 m_wire = wire;
93 m_wire.parse();
94
95 auto it = m_wire.elements_begin();
96 if (it != m_wire.elements_end() && it->type() == tlv::nfd::StatusCode) {
97 m_code = readNonNegativeIntegerAs<uint32_t>(*it);
98 ++it;
99 }
100 else {
101 NDN_THROW(Error("StatusCode is missing or out of order"));
102 }
103
104 if (it != m_wire.elements_end() && it->type() == tlv::nfd::StatusText) {
105 m_text = readString(*it);
106 ++it;
107 }
108 else {
109 NDN_THROW(Error("StatusText is missing or out of order"));
110 }
111
112 if (it != m_wire.elements_end()) {
113 m_body = *it;
114 }
115}
116
117} // namespace ndn::mgmt
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition block.hpp:433
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition block.hpp:442
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition block.hpp:205
void push_back(const Block &element)
Append a sub-element.
Definition block.cpp:456
void encode()
Encode sub-elements into TLV-VALUE.
Definition block.cpp:353
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
void reset() noexcept
Reset the Block to a default-constructed state.
Definition block.cpp:293
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
ControlCommand response.
void wireDecode(const Block &block)
const Block & wireEncode() const
ControlResponse & setBody(const Block &body)
ControlResponse & setText(const std::string &text)
ControlResponse & setCode(uint32_t code)
#define NDN_THROW(e)
Definition exception.hpp:56