NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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<uint32_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) {
111 
112  if (it->type() == tlv::NackReason) {
113  m_reason = static_cast<NackReason>(readNonNegativeInteger(*it));
114  }
115  else {
116  NDN_THROW(ndn::tlv::Error("NackReason", it->type()));
117  }
118  }
119 }
120 
123 {
124  switch (m_reason) {
128  return m_reason;
129  default:
130  return NackReason::NONE;
131  }
132 }
133 
134 NackHeader&
136 {
137  m_reason = reason;
138  m_wire.reset();
139  return *this;
140 }
141 
142 } // namespace lp
143 } // namespace ndn
ndn::lp::NackHeader::NackHeader
NackHeader()
Definition: nack-header.cpp:57
ndn::lp::tlv::Nack
@ Nack
Definition: tlv.hpp:42
ndn::lp::NackHeader::setReason
NackHeader & setReason(NackReason reason)
set reason code
Definition: nack-header.cpp:135
ndn::lp::NackHeader
represents a Network NACK header
Definition: nack-header.hpp:58
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::lp::tlv::NackReason
@ NackReason
Definition: tlv.hpp:43
ndn::lp::NackReason
NackReason
indicates the reason type of a network NACK
Definition: nack-header.hpp:37
ndn::lp::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CachePolicy)
ndn::lp::NackHeader::wireEncode
const Block & wireEncode() const
Definition: nack-header.cpp:81
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::Block::element_const_iterator
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
ndn::lp::NackReason::NO_ROUTE
@ NO_ROUTE
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::encoding::readNonNegativeInteger
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Definition: block-helpers.cpp:64
ndn::lp::operator<<
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
Definition: cache-policy.cpp:31
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::lp::isLessSevere
bool isLessSevere(lp::NackReason x, lp::NackReason y)
compare NackReason for severity
Definition: nack-header.cpp:45
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
nack-header.hpp
ndn::lp::NackReason::DUPLICATE
@ DUPLICATE
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::encoding::prependNonNegativeIntegerBlock
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Definition: block-helpers.cpp:35
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_size
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::lp::NackHeader::getReason
NackReason getReason() const
Definition: nack-header.cpp:122
ndn::lp::NackReason::NONE
@ NONE
ndn::lp
Definition: cache-policy.cpp:28
ndn::lp::NackReason::CONGESTION
@ CONGESTION
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::lp::NackHeader::wireDecode
void wireDecode(const Block &wire)
Definition: nack-header.cpp:99
ndn::to_underlying
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:141
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38