coordinate-lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
7  * This file is part of NLSR (Named-data Link State Routing).
8  * See AUTHORS.md for complete list of NLSR authors and contributors.
9  *
10  * NLSR is free software: you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation,
12  * either version 3 of the License, or (at your option) any later version.
13  *
14  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "coordinate-lsa.hpp"
23 #include "tlv-nlsr.hpp"
24 
25 #include <ndn-cxx/util/concepts.hpp>
26 #include <ndn-cxx/encoding/block-helpers.hpp>
27 
28 #include <iostream>
29 
30 namespace nlsr {
31 namespace tlv {
32 
33 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>));
34 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>));
35 static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value,
36  "CoordinateLsa::Error must inherit from tlv::Error");
37 
39  : m_hyperbolicRadius(0.0)
40 {
41 }
42 
43 CoordinateLsa::CoordinateLsa(const ndn::Block& block)
44 {
45  wireDecode(block);
46 }
47 
48 template<ndn::encoding::Tag TAG>
49 size_t
50 CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
51 {
52  size_t totalLength = 0;
53 
54  for (auto it = m_hyperbolicAngle.rbegin(); it != m_hyperbolicAngle.rend(); ++it) {
55  totalLength += prependDouble(block, ndn::tlv::nlsr::HyperbolicAngle, *it);
56  }
57 
58  totalLength += prependDouble(block, ndn::tlv::nlsr::HyperbolicRadius, m_hyperbolicRadius);
59 
60  totalLength += m_lsaInfo.wireEncode(block);
61 
62  totalLength += block.prependVarNumber(totalLength);
63  totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa);
64 
65  return totalLength;
66 }
67 
69 
70 const ndn::Block&
72 {
73  if (m_wire.hasWire()) {
74  return m_wire;
75  }
76 
77  ndn::EncodingEstimator estimator;
78  size_t estimatedSize = wireEncode(estimator);
79 
80  ndn::EncodingBuffer buffer(estimatedSize, 0);
81  wireEncode(buffer);
82 
83  m_wire = buffer.block();
84 
85  return m_wire;
86 }
87 
88 void
89 CoordinateLsa::wireDecode(const ndn::Block& wire)
90 {
91  m_hyperbolicRadius = 0.0;
92  m_hyperbolicAngle.clear();
93 
94  m_wire = wire;
95 
96  if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) {
97  std::stringstream error;
98  error << "Expected CoordinateLsa Block, but Block is of a different type: #"
99  << m_wire.type();
100  BOOST_THROW_EXCEPTION(Error(error.str()));
101  }
102 
103  m_wire.parse();
104 
105  ndn::Block::element_const_iterator val = m_wire.elements_begin();
106 
107  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) {
108  m_lsaInfo.wireDecode(*val);
109  ++val;
110  }
111  else {
112  std::cout << "Missing required LsaInfo field" << std::endl;
113  BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field"));
114  }
115 
116  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) {
117  m_hyperbolicRadius = ndn::tlv::nlsr::readDouble(*val);
118  ++val;
119  }
120  else {
121  std::cout << "Missing required HyperbolicRadius field" << std::endl;
122  BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field"));
123  }
124 
125  for (; val != m_wire.elements_end(); ++val) {
126  if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
127  m_hyperbolicAngle.push_back(ndn::tlv::nlsr::readDouble(*val));
128  }
129  }
130 }
131 
132 std::ostream&
133 operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
134 {
135  os << "CoordinateLsa("
136  << coordinateLsa.getLsaInfo() << ", "
137  << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", ";
138 
139  os << "HyperbolicAngles: ";
140  int i = 0;
141  for (const auto& value: coordinateLsa.getHyperbolicAngle()) {
142  if (i == 0) {
143  os << value;
144  }
145  else {
146  os << ", " << value;
147  }
148  ++i;
149  }
150  os << ")";
151 
152  return os;
153 }
154 
155 } // namespace tlv
156 } // namespace nlsr
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Encodes LSA info using the method in TAG.
Definition: lsa-info.cpp:52
const LsaInfo & getLsaInfo() const
Data abstraction for CoordinateLsa.
double readDouble(const ndn::Block &block)
Read a double from a TLV element.
Definition: tlv-nlsr.hpp:64
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa)
size_t prependDouble(ndn::EncodingImpl< TAG > &encoder, uint32_t type, double value)
Prepend a TLV element containing a double.
Definition: tlv-nlsr.hpp:85
const std::vector< double > getHyperbolicAngle() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
double getHyperbolicRadius() const
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
Definition: lsa-info.cpp:96