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-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  * @author Eric Newberry <enewberry@email.arizona.edu>
22  */
23 
25 
26 namespace ndn::lp {
27 
28 std::ostream&
29 operator<<(std::ostream& os, NackReason reason)
30 {
31  switch (reason) {
33  return os << "Congestion";
35  return os << "Duplicate";
37  return os << "NoRoute";
38  default:
39  return os << "None";
40  }
41 }
42 
43 bool
45 {
46  if (x == lp::NackReason::NONE) {
47  return false;
48  }
49  if (y == lp::NackReason::NONE) {
50  return true;
51  }
52 
53  return to_underlying(x) < to_underlying(y);
54 }
55 
57  : m_reason(NackReason::NONE)
58 {
59 }
60 
62 {
63  wireDecode(block);
64 }
65 
66 template<encoding::Tag TAG>
67 size_t
69 {
70  size_t length = 0;
71  length += prependNonNegativeIntegerBlock(encoder, tlv::NackReason, static_cast<uint64_t>(m_reason));
72  length += encoder.prependVarNumber(length);
73  length += encoder.prependVarNumber(tlv::Nack);
74  return length;
75 }
76 
78 
79 const Block&
81 {
82  if (m_wire.hasWire()) {
83  return m_wire;
84  }
85 
86  EncodingEstimator estimator;
87  size_t estimatedSize = wireEncode(estimator);
88 
89  EncodingBuffer buffer(estimatedSize, 0);
90  wireEncode(buffer);
91 
92  m_wire = buffer.block();
93 
94  return m_wire;
95 }
96 
97 void
99 {
100  if (wire.type() != tlv::Nack) {
101  NDN_THROW(ndn::tlv::Error("Nack", wire.type()));
102  }
103 
104  m_wire = wire;
105  m_wire.parse();
106  m_reason = NackReason::NONE;
107 
108  if (m_wire.elements_size() > 0) {
109  auto it = m_wire.elements_begin();
110  if (it->type() == tlv::NackReason) {
111  m_reason = readNonNegativeIntegerAs<NackReason>(*it);
112  }
113  else {
114  NDN_THROW(ndn::tlv::Error("NackReason", it->type()));
115  }
116  }
117 }
118 
121 {
122  switch (m_reason) {
126  return m_reason;
127  default:
128  return NackReason::NONE;
129  }
130 }
131 
132 NackHeader&
134 {
135  m_reason = reason;
136  m_wire.reset();
137  return *this;
138 }
139 
140 } // namespace ndn::lp
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
size_t elements_size() const noexcept
Equivalent to elements().size().
Definition: block.hpp:451
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 a Network Nack header.
Definition: nack-header.hpp:58
const Block & wireEncode() const
Definition: nack-header.cpp:80
NackHeader & setReason(NackReason reason)
Set reason code.
NackReason getReason() const
Get reason code.
void wireDecode(const Block &wire)
Definition: nack-header.cpp:98
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
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:44
Contains classes and functions related to NDNLPv2.
NackReason
Indicates the reason type of a Network Nack.
Definition: nack-header.hpp:36
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
bool isLessSevere(lp::NackReason x, lp::NackReason y)
Compare NackReason according to severity.
Definition: nack-header.cpp:44
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:44