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 
50 {
51  m_adjacencyLsas.push_back(adjacencyLsa);
52  m_wire.reset();
53  m_hasAdjacencyLsas = true;
54  return *this;
55 }
56 
59 {
60  m_adjacencyLsas.clear();
61  m_hasAdjacencyLsas = false;
62  return *this;
63 }
64 
67 {
68  m_coordinateLsas.push_back(coordinateLsa);
69  m_wire.reset();
70  m_hasCoordinateLsas = true;
71  return *this;
72 }
73 
76 {
77  m_coordinateLsas.clear();
78  m_hasCoordinateLsas = false;
79  return *this;
80 }
81 
84 {
85  m_nameLsas.push_back(nameLsa);
86  m_wire.reset();
87  m_hasNameLsas = true;
88  return *this;
89 }
90 
93 {
94  m_nameLsas.clear();
95  m_hasNameLsas = false;
96  return *this;
97 }
98 
99 template<ndn::encoding::Tag TAG>
100 size_t
101 LsdbStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
102 {
103  size_t totalLength = 0;
104 
105  for (std::list<NameLsa>::const_reverse_iterator it = m_nameLsas.rbegin();
106  it != m_nameLsas.rend(); ++it) {
107  totalLength += it->wireEncode(block);
108  }
109 
110  for (std::list<CoordinateLsa>::const_reverse_iterator it = m_coordinateLsas.rbegin();
111  it != m_coordinateLsas.rend(); ++it) {
112  totalLength += it->wireEncode(block);
113  }
114 
115  for (std::list<AdjacencyLsa>::const_reverse_iterator it = m_adjacencyLsas.rbegin();
116  it != m_adjacencyLsas.rend(); ++it) {
117  totalLength += it->wireEncode(block);
118  }
119 
120  totalLength += block.prependVarNumber(totalLength);
121  totalLength += block.prependVarNumber(ndn::tlv::nlsr::LsdbStatus);
122 
123  return totalLength;
124 }
125 
127 
128 template size_t
129 LsdbStatus::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
130 
131 template size_t
132 LsdbStatus::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
133 
134 const ndn::Block&
136 {
137  if (m_wire.hasWire()) {
138  return m_wire;
139  }
140 
141  ndn::EncodingEstimator estimator;
142  size_t estimatedSize = wireEncode(estimator);
143 
144  ndn::EncodingBuffer buffer(estimatedSize, 0);
145  wireEncode(buffer);
146 
147  m_wire = buffer.block();
148 
149  return m_wire;
150 }
151 
152 void
153 LsdbStatus::wireDecode(const ndn::Block& wire)
154 {
155  m_adjacencyLsas.clear();
156  m_coordinateLsas.clear();
157  m_nameLsas.clear();
158 
159  m_hasAdjacencyLsas = false;
160  m_hasCoordinateLsas = false;
161  m_hasNameLsas = false;
162 
163  m_wire = wire;
164 
165  if (m_wire.type() != ndn::tlv::nlsr::LsdbStatus) {
166  std::stringstream error;
167  error << "Expected LsdbStatus Block, but Block is of a different type: #"
168  << m_wire.type();
169  BOOST_THROW_EXCEPTION(Error(error.str()));
170  }
171 
172  m_wire.parse();
173 
174  ndn::Block::element_const_iterator val = m_wire.elements_begin();
175 
176  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::AdjacencyLsa; ++val) {
177  m_adjacencyLsas.push_back(AdjacencyLsa(*val));
178  m_hasAdjacencyLsas = true;
179  }
180 
181  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::CoordinateLsa; ++val) {
182  m_coordinateLsas.push_back(CoordinateLsa(*val));
183  m_hasCoordinateLsas = true;
184  }
185 
186  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::NameLsa; ++val) {
187  m_nameLsas.push_back(NameLsa(*val));
188  m_hasNameLsas = true;
189  }
190 
191  if (val != m_wire.elements_end()) {
192  std::stringstream error;
193  error << "Expected the end of elements, but Block is of a different type: #"
194  << val->type();
195  BOOST_THROW_EXCEPTION(Error(error.str()));
196  }
197 }
198 
199 std::ostream&
200 operator<<(std::ostream& os, const LsdbStatus& lsdbStatus)
201 {
202  os << "LsdbStatus(";
203 
204  bool isFirst = true;
205 
206  for (const auto& adjacencyLsa : lsdbStatus.getAdjacencyLsas()) {
207  if (isFirst) {
208  isFirst = false;
209  }
210  else {
211  os << ", ";
212  }
213 
214  os << adjacencyLsa;
215  }
216 
217  for (const auto& coordinateLsa : lsdbStatus.getCoordinateLsas()) {
218  if (isFirst) {
219  isFirst = false;
220  }
221  else {
222  os << ", ";
223  }
224 
225  os << coordinateLsa;
226  }
227 
228  for (const auto& nameLsa : lsdbStatus.getNameLsas()) {
229  if (isFirst) {
230  isFirst = false;
231  }
232  else {
233  os << ", ";
234  }
235 
236  os << nameLsa;
237  }
238 
239  os << ")";
240 
241  return os;
242 }
243 
244 } // namespace tlv
245 } // namespace nlsr
LsdbStatus & clearAdjacencyLsas()
Definition: lsdb-status.cpp:58
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
const std::list< CoordinateLsa > & getCoordinateLsas() const
Definition: lsdb-status.hpp:90
Data abstraction for CoordinateLsa.
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
LsdbStatus & clearNameLsas()
Definition: lsdb-status.cpp:92
LsdbStatus & addCoordinateLsa(const CoordinateLsa &coordinateLsa)
Definition: lsdb-status.cpp:66
LsdbStatus & clearCoordinateLsas()
Definition: lsdb-status.cpp:75
LsdbStatus & addAdjacencyLsa(const AdjacencyLsa &adjacencyLsa)
Definition: lsdb-status.cpp:49
Data abstraction for NameLsa.
Definition: name-lsa.hpp:47
const std::list< AdjacencyLsa > & getAdjacencyLsas() const
Definition: lsdb-status.hpp:72
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Data abstraction for AdjacencyLsa.
Data abstraction for LsdbStatus.
Definition: lsdb-status.hpp:49
LsdbStatus & addNameLsa(const NameLsa &nameLsa)
Definition: lsdb-status.cpp:83
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Destination)
const std::list< NameLsa > & getNameLsas() const