nack-header.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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  * @author Eric Newberry <enewberry@email.arizona.edu>
22  */
23 
25 
26 namespace ndn {
27 namespace lp {
28 
29 std::ostream&
30 operator<<(std::ostream& os, NackReason reason)
31 {
32  switch (reason) {
34  return os << "Congestion";
36  return os << "Duplicate";
38  return os << "NoRoute";
39  default:
40  return os << "None";
41  }
42 }
43 
44 bool
46 {
47  if (x == lp::NackReason::NONE) {
48  return false;
49  }
50  if (y == lp::NackReason::NONE) {
51  return true;
52  }
53 
54  return to_underlying(x) < to_underlying(y);
55 }
56 
58  : m_reason(NackReason::NONE)
59 {
60 }
61 
63 {
64  wireDecode(block);
65 }
66 
67 template<encoding::Tag TAG>
68 size_t
70 {
71  size_t length = 0;
72  length += prependNonNegativeIntegerBlock(encoder, tlv::NackReason, static_cast<uint64_t>(m_reason));
73  length += encoder.prependVarNumber(length);
74  length += encoder.prependVarNumber(tlv::Nack);
75  return length;
76 }
77 
79 
80 const Block&
82 {
83  if (m_wire.hasWire()) {
84  return m_wire;
85  }
86 
87  EncodingEstimator estimator;
88  size_t estimatedSize = wireEncode(estimator);
89 
90  EncodingBuffer buffer(estimatedSize, 0);
91  wireEncode(buffer);
92 
93  m_wire = buffer.block();
94 
95  return m_wire;
96 }
97 
98 void
100 {
101  if (wire.type() != tlv::Nack) {
102  NDN_THROW(ndn::tlv::Error("Nack", wire.type()));
103  }
104 
105  m_wire = wire;
106  m_wire.parse();
107  m_reason = NackReason::NONE;
108 
109  if (m_wire.elements_size() > 0) {
110  auto it = m_wire.elements_begin();
111  if (it->type() == tlv::NackReason) {
112  m_reason = readNonNegativeIntegerAs<NackReason>(*it);
113  }
114  else {
115  NDN_THROW(ndn::tlv::Error("NackReason", it->type()));
116  }
117  }
118 }
119 
122 {
123  switch (m_reason) {
127  return m_reason;
128  default:
129  return NackReason::NONE;
130  }
131 }
132 
133 NackHeader&
135 {
136  m_reason = reason;
137  m_wire.reset();
138  return *this;
139 }
140 
141 } // namespace lp
142 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:426
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:410
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:265
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
represents a Network NACK header
Definition: nack-header.hpp:58
const Block & wireEncode() const
Definition: nack-header.cpp:81
NackHeader & setReason(NackReason reason)
set reason code
NackReason getReason() const
void wireDecode(const Block &wire)
Definition: nack-header.cpp:99
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
EncodingImpl< EncoderTag > EncodingBuffer
@ NackReason
Definition: tlv.hpp:40
NackReason
indicates the reason type of a network NACK
Definition: nack-header.hpp:37
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
bool isLessSevere(lp::NackReason x, lp::NackReason y)
compare NackReason for severity
Definition: nack-header.cpp:45
Definition: data.cpp:25
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:136