strategy-choice.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 
26 
27 namespace ndn::nfd {
28 
30 
32 {
33  this->wireDecode(payload);
34 }
35 
36 template<encoding::Tag TAG>
37 size_t
39 {
40  size_t totalLength = 0;
41 
42  totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
43  totalLength += m_name.wireEncode(encoder);
44 
45  totalLength += encoder.prependVarNumber(totalLength);
46  totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
47  return totalLength;
48 }
49 
51 
52 const Block&
54 {
55  if (m_wire.hasWire())
56  return m_wire;
57 
58  EncodingEstimator estimator;
59  size_t estimatedSize = wireEncode(estimator);
60 
61  EncodingBuffer buffer(estimatedSize, 0);
62  wireEncode(buffer);
63 
64  m_wire = buffer.block();
65  return m_wire;
66 }
67 
68 void
70 {
71  if (block.type() != tlv::nfd::StrategyChoice) {
72  NDN_THROW(Error("StrategyChoice", block.type()));
73  }
74 
75  m_wire = block;
76  m_wire.parse();
77  auto val = m_wire.elements_begin();
78 
79  if (val != m_wire.elements_end() && val->type() == tlv::Name) {
80  m_name.wireDecode(*val);
81  ++val;
82  }
83  else {
84  NDN_THROW(Error("missing required Name field"));
85  }
86 
87  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
88  val->parse();
89  if (val->elements().empty()) {
90  NDN_THROW(Error("expecting Strategy/Name"));
91  }
92  else {
93  m_strategy.wireDecode(*val->elements_begin());
94  }
95  ++val;
96  }
97  else {
98  NDN_THROW(Error("missing required Strategy field"));
99  }
100 }
101 
104 {
105  m_wire.reset();
106  m_name = name;
107  return *this;
108 }
109 
112 {
113  m_wire.reset();
114  m_strategy = strategy;
115  return *this;
116 }
117 
118 bool
120 {
121  return a.getName() == b.getName() && a.getStrategy() == b.getStrategy();
122 }
123 
124 std::ostream&
125 operator<<(std::ostream& os, const StrategyChoice& sc)
126 {
127  return os << "StrategyChoice("
128  << "Name: " << sc.getName() << ", "
129  << "Strategy: " << sc.getStrategy()
130  << ")";
131 }
132 
133 } // 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 absolute name.
Definition: name.hpp:45
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: name.cpp:92
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:125
Represents an item in NFD StrategyChoice dataset.
StrategyChoice & setName(const Name &name)
const Name & getName() const
const Block & wireEncode() const
const Name & getStrategy() const
void wireDecode(const Block &wire)
StrategyChoice & setStrategy(const Name &strategy)
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
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)
@ Name
Definition: tlv.hpp:71