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-2023, 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 <boost/lexical_cast.hpp>
26 
27 namespace nlsr {
28 
29 CoordinateLsa::CoordinateLsa(const ndn::Name& originRouter, uint64_t seqNo,
30  const ndn::time::system_clock::time_point& timepoint,
31  double radius, std::vector<double> angles)
32  : Lsa(originRouter, seqNo, timepoint)
33  , m_hyperbolicRadius(radius)
34  , m_hyperbolicAngles(angles)
35 {
36 }
37 
38 CoordinateLsa::CoordinateLsa(const ndn::Block& block)
39 {
40  wireDecode(block);
41 }
42 
43 bool
45 {
46  if (clsa.getCorTheta().size() != m_hyperbolicAngles.size()) {
47  return false;
48  }
49 
50  std::vector<double> m_angles2 = clsa.getCorTheta();
51  for (unsigned int i = 0; i < clsa.getCorTheta().size(); i++) {
52  if (std::abs(m_hyperbolicAngles[i] - m_angles2[i]) > std::numeric_limits<double>::epsilon()) {
53  return false;
54  }
55  }
56 
57  return (std::abs(m_hyperbolicRadius - clsa.getCorRadius()) <
58  std::numeric_limits<double>::epsilon());
59 }
60 
61 template<ndn::encoding::Tag TAG>
62 size_t
63 CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
64 {
65  size_t totalLength = 0;
66 
67  for (auto it = m_hyperbolicAngles.rbegin(); it != m_hyperbolicAngles.rend(); ++it) {
68  totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicAngle, *it);
69  }
70 
71  totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicRadius, m_hyperbolicRadius);
72 
73  totalLength += Lsa::wireEncode(block);
74 
75  totalLength += block.prependVarNumber(totalLength);
76  totalLength += block.prependVarNumber(nlsr::tlv::CoordinateLsa);
77 
78  return totalLength;
79 }
80 
82 
83 const ndn::Block&
85 {
86  if (m_wire.hasWire()) {
87  return m_wire;
88  }
89 
90  ndn::EncodingEstimator estimator;
91  size_t estimatedSize = wireEncode(estimator);
92 
93  ndn::EncodingBuffer buffer(estimatedSize, 0);
94  wireEncode(buffer);
95 
96  m_wire = buffer.block();
97 
98  return m_wire;
99 }
100 
101 void
102 CoordinateLsa::wireDecode(const ndn::Block& wire)
103 {
104  m_wire = wire;
105 
106  if (m_wire.type() != nlsr::tlv::CoordinateLsa) {
107  NDN_THROW(Error("CoordinateLsa", m_wire.type()));
108  }
109 
110  m_wire.parse();
111 
112  auto val = m_wire.elements_begin();
113 
114  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
115  Lsa::wireDecode(*val);
116  ++val;
117  }
118  else {
119  NDN_THROW(Error("Missing required Lsa field"));
120  }
121 
122  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::HyperbolicRadius) {
123  m_hyperbolicRadius = ndn::encoding::readDouble(*val);
124  ++val;
125  }
126  else {
127  NDN_THROW(Error("Missing required HyperbolicRadius field"));
128  }
129 
130  std::vector<double> angles;
131  for (; val != m_wire.elements_end(); ++val) {
132  if (val->type() == nlsr::tlv::HyperbolicAngle) {
133  angles.push_back(ndn::encoding::readDouble(*val));
134  }
135  else {
136  NDN_THROW(Error("Missing required HyperbolicAngle field"));
137  }
138  }
139  m_hyperbolicAngles = angles;
140 }
141 
142 std::string
144 {
145  std::ostringstream os;
146  os << getString();
147  os << " Hyperbolic Radius : " << m_hyperbolicRadius << "\n";
148  int i = 0;
149  for (const auto& value : m_hyperbolicAngles) {
150  os << " Hyperbolic Theta " << i++ << " : " << value << "\n";
151  }
152 
153  return os.str();
154 }
155 
156 std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
157 CoordinateLsa::update(const std::shared_ptr<Lsa>& lsa)
158 {
159  auto clsa = std::static_pointer_cast<CoordinateLsa>(lsa);
160  if (!isEqualContent(*clsa)) {
161  m_hyperbolicRadius = clsa->getCorRadius();
162  m_hyperbolicAngles.clear();
163  for (const auto& angle : clsa->getCorTheta()) {
164  m_hyperbolicAngles.push_back(angle);
165  }
166  return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
167  }
168  return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
169 }
170 
171 std::ostream&
172 operator<<(std::ostream& os, const CoordinateLsa& lsa)
173 {
174  return os << lsa.toString();
175 }
176 
177 } // namespace nlsr
Data abstraction for CoordinateLsa CoordinateLsa := COORDINATE-LSA-TYPE TLV-LENGTH Lsa HyperbolicRadi...
const ndn::Block & wireEncode() const override
double getCorRadius() const
void wireDecode(const ndn::Block &wire)
const std::vector< double > getCorTheta() const
bool isEqualContent(const CoordinateLsa &clsa) const
CoordinateLsa()=default
std::tuple< bool, std::list< ndn::Name >, std::list< ndn::Name > > update(const std::shared_ptr< Lsa > &lsa) override
std::string toString() const override
Data abstraction for Lsa Lsa := LSA-TYPE TLV-LENGTH Name SequenceNumber ExpirationTimePoint.
Definition: lsa.hpp:42
virtual const ndn::Block & wireEncode() const =0
std::string getString() const
Definition: lsa.cpp:144
ndn::Block m_wire
Definition: lsa.hpp:144
void wireDecode(const ndn::Block &wire)
Definition: lsa.cpp:68
@ HyperbolicAngle
Definition: tlv-nlsr.hpp:40
@ CoordinateLsa
Definition: tlv-nlsr.hpp:37
@ HyperbolicRadius
Definition: tlv-nlsr.hpp:39
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)