NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
24 #include "nack-header.hpp"
25 
26 namespace ndn {
27 namespace lp {
28 
29 std::ostream&
30 operator<<(std::ostream& os, NackReason reason)
31 {
32  switch (reason) {
34  os << "Congestion";
35  break;
37  os << "Duplicate";
38  break;
40  os << "NoRoute";
41  break;
42  default:
43  os << "None";
44  break;
45  }
46  return os;
47 }
48 
50  : m_reason(NackReason::NONE)
51 {
52 }
53 
55 {
56  wireDecode(block);
57 }
58 
59 template<encoding::Tag TAG>
60 size_t
62 {
63  size_t length = 0;
65  static_cast<uint32_t>(m_reason));
66  length += encoder.prependVarNumber(length);
67  length += encoder.prependVarNumber(tlv::Nack);
68  return length;
69 }
70 
71 template size_t
72 NackHeader::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
73 
74 template size_t
75 NackHeader::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
76 
77 const Block&
79 {
80  if (m_wire.hasWire()) {
81  return m_wire;
82  }
83 
84  EncodingEstimator estimator;
85  size_t estimatedSize = wireEncode(estimator);
86 
87  EncodingBuffer buffer(estimatedSize, 0);
88  wireEncode(buffer);
89 
90  m_wire = buffer.block();
91 
92  return m_wire;
93 }
94 
95 void
97 {
98  if (wire.type() != tlv::Nack) {
99  BOOST_THROW_EXCEPTION(ndn::tlv::Error("expecting Nack block"));
100  }
101 
102  m_wire = wire;
103  m_wire.parse();
104  m_reason = NackReason::NONE;
105 
106  if (m_wire.elements_size() > 0) {
108 
109  if (it->type() == tlv::NackReason) {
110  m_reason = static_cast<NackReason>(readNonNegativeInteger(*it));
111  }
112  else {
113  BOOST_THROW_EXCEPTION(ndn::tlv::Error("expecting NackReason block"));
114  }
115  }
116 }
117 
120 {
121  switch (m_reason) {
125  return m_reason;
126  default:
127  return NackReason::NONE;
128  }
129 }
130 
131 NackHeader&
133 {
134  m_reason = reason;
135  m_wire.reset();
136  return *this;
137 }
138 
139 } // namespace lp
140 } // namespace ndn
element_const_iterator elements_begin() const
Definition: block.cpp:589
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
NackHeader & setReason(NackReason reason)
set reason code
const Block & wireEncode() const
Definition: nack-header.cpp:78
EncodingImpl< EstimatorTag > EncodingEstimator
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void wireDecode(const Block &wire)
Definition: nack-header.cpp:96
NackReason
indicates the reason type of a network NACK
Definition: nack-header.hpp:39
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
NackReason getReason() const
EncodingImpl< EncoderTag > EncodingBuffer
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
size_t elements_size() const
Definition: block.cpp:601
std::ostream & operator<<(std::ostream &os, CachePolicyType policy)
represents a Network NACK header
Definition: nack-header.hpp:52
uint32_t type() const
Definition: block.hpp:346
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50