blob: 3bbc9854c9a3489d595def5df3459a26f3720456 [file] [log] [blame]
Eric Newberry38982622015-08-06 21:39:55 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Eric Newberry38982622015-08-06 21:39:55 -07004 *
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
24#include "nack-header.hpp"
25
26namespace ndn {
27namespace lp {
28
29std::ostream&
30operator<<(std::ostream& os, NackReason reason)
31{
32 switch (reason) {
33 case NackReason::CONGESTION:
34 os << "Congestion";
35 break;
36 case NackReason::DUPLICATE:
37 os << "Duplicate";
38 break;
39 case NackReason::NO_ROUTE:
40 os << "NoRoute";
41 break;
42 default:
43 os << "None";
44 break;
45 }
46 return os;
47}
48
49NackHeader::NackHeader()
50 : m_reason(NackReason::NONE)
51{
52}
53
54NackHeader::NackHeader(const Block& block)
55{
56 wireDecode(block);
57}
58
59template<encoding::Tag TAG>
60size_t
61NackHeader::wireEncode(EncodingImpl<TAG>& encoder) const
62{
63 size_t length = 0;
Davide Pesavento88a0d812017-08-19 21:31:42 -040064 length += prependNonNegativeIntegerBlock(encoder, tlv::NackReason, static_cast<uint32_t>(m_reason));
Eric Newberry38982622015-08-06 21:39:55 -070065 length += encoder.prependVarNumber(length);
66 length += encoder.prependVarNumber(tlv::Nack);
67 return length;
68}
69
Davide Pesavento88a0d812017-08-19 21:31:42 -040070NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NackHeader);
Eric Newberry38982622015-08-06 21:39:55 -070071
72const Block&
73NackHeader::wireEncode() const
74{
75 if (m_wire.hasWire()) {
76 return m_wire;
77 }
78
79 EncodingEstimator estimator;
80 size_t estimatedSize = wireEncode(estimator);
81
82 EncodingBuffer buffer(estimatedSize, 0);
83 wireEncode(buffer);
84
85 m_wire = buffer.block();
86
87 return m_wire;
88}
89
90void
91NackHeader::wireDecode(const Block& wire)
92{
93 if (wire.type() != tlv::Nack) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070094 BOOST_THROW_EXCEPTION(ndn::tlv::Error("expecting Nack block"));
Eric Newberry38982622015-08-06 21:39:55 -070095 }
96
97 m_wire = wire;
98 m_wire.parse();
99 m_reason = NackReason::NONE;
100
101 if (m_wire.elements_size() > 0) {
102 Block::element_const_iterator it = m_wire.elements_begin();
103
104 if (it->type() == tlv::NackReason) {
105 m_reason = static_cast<NackReason>(readNonNegativeInteger(*it));
106 }
107 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700108 BOOST_THROW_EXCEPTION(ndn::tlv::Error("expecting NackReason block"));
Eric Newberry38982622015-08-06 21:39:55 -0700109 }
110 }
111}
112
113NackReason
114NackHeader::getReason() const
115{
116 switch (m_reason) {
117 case NackReason::CONGESTION:
118 case NackReason::DUPLICATE:
119 case NackReason::NO_ROUTE:
120 return m_reason;
121 default:
122 return NackReason::NONE;
123 }
124}
125
126NackHeader&
127NackHeader::setReason(NackReason reason)
128{
129 m_reason = reason;
130 m_wire.reset();
131 return *this;
132}
133
134} // namespace lp
135} // namespace ndn