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-2023 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 
26 namespace ndn::mgmt {
27 
29 
30 ControlResponse::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 
50 ControlResponse::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 
66 const Block&
68 {
69  if (m_wire.hasWire()) {
70  return m_wire;
71  }
72 
76  if (m_body.hasWire()) {
78  }
79 
80  m_wire.encode();
81  return m_wire;
82 }
83 
84 void
86 {
87  if (wire.type() != tlv::nfd::ControlResponse) {
88  NDN_THROW(Error("ControlResponse", wire.type()));
89  }
90  m_wire = wire;
91  m_wire.parse();
92 
93  auto val = m_wire.elements_begin();
94  if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusCode) {
95  NDN_THROW(Error("missing StatusCode sub-element"));
96  }
97  m_code = readNonNegativeIntegerAs<uint32_t>(*val);
98  ++val;
99 
100  if (val == m_wire.elements_end() || val->type() != tlv::nfd::StatusText) {
101  NDN_THROW(Error("missing StatusText sub-element"));
102  }
103  m_text = readString(*val);
104  ++val;
105 
106  if (val != m_wire.elements_end())
107  m_body = *val;
108  else
109  m_body = {};
110 }
111 
112 } // 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
Block makeStringBlock(uint32_t type, std::string_view value)
Create a TLV block containing a string.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
std::string readString(const Block &block)
Read the TLV-VALUE of a TLV element as a string.