channel-status.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::nfd {
27 
29 
31 {
32  this->wireDecode(payload);
33 }
34 
35 template<encoding::Tag TAG>
36 size_t
38 {
39  size_t totalLength = 0;
40  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
41  totalLength += encoder.prependVarNumber(totalLength);
42  totalLength += encoder.prependVarNumber(tlv::nfd::ChannelStatus);
43  return totalLength;
44 }
45 
47 
48 const Block&
50 {
51  if (m_wire.hasWire())
52  return m_wire;
53 
54  EncodingEstimator estimator;
55  size_t estimatedSize = wireEncode(estimator);
56 
57  EncodingBuffer buffer(estimatedSize, 0);
58  wireEncode(buffer);
59 
60  m_wire = buffer.block();
61  return m_wire;
62 }
63 
64 void
66 {
67  if (block.type() != tlv::nfd::ChannelStatus) {
68  NDN_THROW(Error("ChannelStatus", block.type()));
69  }
70 
71  m_wire = block;
72  m_wire.parse();
73  auto val = m_wire.elements_begin();
74 
75  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
76  m_localUri = readString(*val);
77  ++val;
78  }
79  else {
80  NDN_THROW(Error("Missing required LocalUri field"));
81  }
82 }
83 
85 ChannelStatus::setLocalUri(const std::string& localUri)
86 {
87  m_wire.reset();
88  m_localUri = localUri;
89  return *this;
90 }
91 
92 bool
94 {
95  return a.getLocalUri() == b.getLocalUri();
96 }
97 
98 std::ostream&
99 operator<<(std::ostream& os, const ChannelStatus& status)
100 {
101  return os << "Channel(LocalUri: " << status.getLocalUri() << ")";
102 }
103 
104 } // namespace ndn::nfd
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
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
Represents an item in NFD Channel dataset.
const std::string & getLocalUri() const
ChannelStatus & setLocalUri(const std::string &localUri)
void wireDecode(const Block &wire)
const Block & wireEncode() const
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, std::string_view value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read the TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
Contains classes and functions related to the NFD Management protocol.
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)