blob: fb268b2f34befdff5a2fbf7bd24d6e37edce0259 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * 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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080021
22#include "coordinate-lsa.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070023#include "tlv-nlsr.hpp"
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080024
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080025#include <boost/lexical_cast.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050026
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080027namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060028
Ashlesh Gawande57a87172020-05-09 19:47:06 -070029CoordinateLsa::CoordinateLsa(const ndn::Name& originRouter, uint64_t seqNo,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080030 const ndn::time::system_clock::TimePoint& timepoint,
31 double radius, std::vector<double> angles)
32 : Lsa(originRouter, seqNo, timepoint)
33 , m_hyperbolicRadius(radius)
34 , m_hyperbolicAngles(angles)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080035{
36}
37
38CoordinateLsa::CoordinateLsa(const ndn::Block& block)
39{
40 wireDecode(block);
41}
42
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080043bool
44CoordinateLsa::isEqualContent(const CoordinateLsa& clsa) const
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
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080061template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080062size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080063CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080064{
65 size_t totalLength = 0;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080066
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080067 for (auto it = m_hyperbolicAngles.rbegin(); it != m_hyperbolicAngles.rend(); ++it) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040068 totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicAngle, *it);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060069 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080070
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040071 totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicRadius, m_hyperbolicRadius);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080072
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080073 totalLength += Lsa::wireEncode(block);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080074
75 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040076 totalLength += block.prependVarNumber(nlsr::tlv::CoordinateLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080077
78 return totalLength;
79}
80
Alexander Afanasyev67758b12018-03-06 18:36:44 -050081NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080082
83const ndn::Block&
84CoordinateLsa::wireEncode() const
85{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070086 if (m_wire.hasWire()) {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080087 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
101void
102CoordinateLsa::wireDecode(const ndn::Block& wire)
103{
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800104 m_wire = wire;
105
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400106 if (m_wire.type() != nlsr::tlv::CoordinateLsa) {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500107 NDN_THROW(Error("CoordinateLsa", m_wire.type()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800108 }
109
110 m_wire.parse();
111
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700112 auto val = m_wire.elements_begin();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800113
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400114 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800115 Lsa::wireDecode(*val);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800116 ++val;
117 }
118 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500119 NDN_THROW(Error("Missing required Lsa field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800120 }
121
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400122 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::HyperbolicRadius) {
Tianxing Ma9ea36392018-10-05 14:32:55 -0500123 m_hyperbolicRadius = ndn::encoding::readDouble(*val);
Ashlesh Gawande8df15982018-05-09 17:52:33 -0500124 ++val;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800125 }
126 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500127 NDN_THROW(Error("Missing required HyperbolicRadius field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800128 }
129
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800130 std::vector<double> angles;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600131 for (; val != m_wire.elements_end(); ++val) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400132 if (val->type() == nlsr::tlv::HyperbolicAngle) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800133 angles.push_back(ndn::encoding::readDouble(*val));
134 }
135 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500136 NDN_THROW(Error("Missing required HyperbolicAngle field"));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600137 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800138 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800139 m_hyperbolicAngles = angles;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800140}
141
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700142std::string
143CoordinateLsa::toString() const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800144{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700145 std::ostringstream os;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700146 os << getString();
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700147 os << " Hyperbolic Radius : " << m_hyperbolicRadius << "\n";
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600148 int i = 0;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700149 for (const auto& value : m_hyperbolicAngles) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800150 os << " Hyperbolic Theta " << i++ << " : " << value << "\n";
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600151 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800152
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700153 return os.str();
154}
155
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700156std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
157CoordinateLsa::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 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400166 return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700167 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400168 return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700169}
170
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700171std::ostream&
172operator<<(std::ostream& os, const CoordinateLsa& lsa)
173{
174 return os << lsa.toString();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800175}
176
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700177} // namespace nlsr