lsdb-status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "lsdb-status.hpp"
23 #include "tlv-nlsr.hpp"
24 
25 #include <ndn-cxx/util/concepts.hpp>
26 #include <ndn-cxx/encoding/block-helpers.hpp>
27 
28 namespace nlsr {
29 namespace tlv {
30 
31 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<LsdbStatus>));
32 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<LsdbStatus>));
33 static_assert(std::is_base_of<ndn::tlv::Error, LsdbStatus::Error>::value,
34  "LsdbStatus::Error must inherit from tlv::Error");
35 
37  : m_hasAdjacencyLsas(false)
38  , m_hasCoordinateLsas(false)
39  , m_hasNameLsas(false)
40 {
41 }
42 
43 LsdbStatus::LsdbStatus(const ndn::Block& block)
44 {
45  wireDecode(block);
46 }
47 
48 template<ndn::encoding::Tag TAG>
49 size_t
50 LsdbStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
51 {
52  size_t totalLength = 0;
53 
54  for (std::list<NameLsa>::const_reverse_iterator it = m_nameLsas.rbegin();
55  it != m_nameLsas.rend(); ++it) {
56  totalLength += it->wireEncode(block);
57  }
58 
59  for (std::list<CoordinateLsa>::const_reverse_iterator it = m_coordinateLsas.rbegin();
60  it != m_coordinateLsas.rend(); ++it) {
61  totalLength += it->wireEncode(block);
62  }
63 
64  for (std::list<AdjacencyLsa>::const_reverse_iterator it = m_adjacencyLsas.rbegin();
65  it != m_adjacencyLsas.rend(); ++it) {
66  totalLength += it->wireEncode(block);
67  }
68 
69  totalLength += block.prependVarNumber(totalLength);
70  totalLength += block.prependVarNumber(ndn::tlv::nlsr::LsdbStatus);
71 
72  return totalLength;
73 }
74 
75 template size_t
76 LsdbStatus::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
77 
78 template size_t
79 LsdbStatus::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
80 
81 const ndn::Block&
83 {
84  if (m_wire.hasWire()) {
85  return m_wire;
86  }
87 
88  ndn::EncodingEstimator estimator;
89  size_t estimatedSize = wireEncode(estimator);
90 
91  ndn::EncodingBuffer buffer(estimatedSize, 0);
92  wireEncode(buffer);
93 
94  m_wire = buffer.block();
95 
96  return m_wire;
97 }
98 
99 void
100 LsdbStatus::wireDecode(const ndn::Block& wire)
101 {
102  m_adjacencyLsas.clear();
103  m_coordinateLsas.clear();
104  m_nameLsas.clear();
105 
106  m_hasAdjacencyLsas = false;
107  m_hasCoordinateLsas = false;
108  m_hasNameLsas = false;
109 
110  m_wire = wire;
111 
112  if (m_wire.type() != ndn::tlv::nlsr::LsdbStatus) {
113  std::stringstream error;
114  error << "Expected LsdbStatus Block, but Block is of a different type: #"
115  << m_wire.type();
116  throw Error(error.str());
117  }
118 
119  m_wire.parse();
120 
121  ndn::Block::element_const_iterator val = m_wire.elements_begin();
122 
123  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::AdjacencyLsa; ++val) {
124  m_adjacencyLsas.push_back(AdjacencyLsa(*val));
125  m_hasAdjacencyLsas = true;
126  }
127 
128  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::CoordinateLsa; ++val) {
129  m_coordinateLsas.push_back(CoordinateLsa(*val));
130  m_hasCoordinateLsas = true;
131  }
132 
133  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::NameLsa; ++val) {
134  m_nameLsas.push_back(NameLsa(*val));
135  m_hasNameLsas = true;
136  }
137 
138  if (val != m_wire.elements_end()) {
139  std::stringstream error;
140  error << "Expected the end of elements, but Block is of a different type: #"
141  << val->type();
142  throw Error(error.str());
143  }
144 }
145 
146 std::ostream&
147 operator<<(std::ostream& os, const LsdbStatus& lsdbStatus)
148 {
149  os << "LsdbStatus(";
150 
151  bool isFirst = true;
152 
153  for (const auto& adjacencyLsa : lsdbStatus.getAdjacencyLsas()) {
154  if (isFirst) {
155  isFirst = false;
156  }
157  else {
158  os << ", ";
159  }
160 
161  os << adjacencyLsa;
162  }
163 
164  for (const auto& coordinateLsa : lsdbStatus.getCoordinateLsas()) {
165  if (isFirst) {
166  isFirst = false;
167  }
168  else {
169  os << ", ";
170  }
171 
172  os << coordinateLsa;
173  }
174 
175  for (const auto& nameLsa : lsdbStatus.getNameLsas()) {
176  if (isFirst) {
177  isFirst = false;
178  }
179  else {
180  os << ", ";
181  }
182 
183  os << nameLsa;
184  }
185 
186  os << ")";
187 
188  return os;
189 }
190 
191 } // namespace tlv
192 } // namespace nlsr
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
const std::list< CoordinateLsa > & getCoordinateLsas() const
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
Definition: lsdb-status.cpp:82
const std::list< AdjacencyLsa > & getAdjacencyLsas() const
Definition: lsdb-status.hpp:73
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Data abstraction for LsdbStatus.
Definition: lsdb-status.hpp:50
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
const std::list< NameLsa > & getNameLsas() const