All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2025 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 
26 
27 namespace ndn::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 {
59  wireDecode(block);
60 }
61 
62 template<encoding::Tag TAG>
63 size_t
65 {
66  size_t length = 0;
67  length += prependNonNegativeIntegerBlock(encoder, tlv::NackReason, static_cast<uint64_t>(m_reason));
68  length += encoder.prependVarNumber(length);
69  length += encoder.prependVarNumber(tlv::Nack);
70  return length;
71 }
72 
74 
75 const Block&
77 {
78  if (m_wire.hasWire()) {
79  return m_wire;
80  }
81 
82  EncodingEstimator estimator;
83  size_t estimatedSize = wireEncode(estimator);
84 
85  EncodingBuffer buffer(estimatedSize, 0);
86  wireEncode(buffer);
87 
88  m_wire = buffer.block();
89  return m_wire;
90 }
91 
92 void
94 {
95  if (wire.type() != tlv::Nack) {
96  NDN_THROW(ndn::tlv::Error("Nack", wire.type()));
97  }
98  m_wire = wire;
99  m_wire.parse();
100 
101  m_reason = NackReason::NONE;
102 
103  auto it = m_wire.elements_begin();
104  if (it != m_wire.elements_end()) {
105  if (it->type() == tlv::NackReason) {
106  m_reason = readNonNegativeIntegerAs<NackReason>(*it);
107  }
108  else {
109  NDN_THROW(ndn::tlv::Error("NackReason", it->type()));
110  }
111  }
112 }
113 
116 {
117  switch (m_reason) {
121  return m_reason;
122  default:
123  return NackReason::NONE;
124  }
125 }
126 
127 NackHeader&
129 {
130  m_reason = reason;
131  m_wire.reset();
132  return *this;
133 }
134 
135 } // 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
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 a Network Nack header.
Definition: nack-header.hpp:57
const Block & wireEncode() const
Definition: nack-header.cpp:76
NackHeader & setReason(NackReason reason)
Set reason code.
NackReason getReason() const
Get reason code.
void wireDecode(const Block &wire)
Definition: nack-header.cpp:93
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:35
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:45
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:44