blob: 11428f809a59c74b18848ec554b5e5d57839622b [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/*
Junxiao Shib5734842024-01-09 21:14:53 +00003 * Copyright (c) 2014-2024, 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
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080025namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060026
Ashlesh Gawande57a87172020-05-09 19:47:06 -070027CoordinateLsa::CoordinateLsa(const ndn::Name& originRouter, uint64_t seqNo,
Davide Pesavento658fd852023-05-10 22:15:03 -040028 const ndn::time::system_clock::time_point& timepoint,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080029 double radius, std::vector<double> angles)
30 : Lsa(originRouter, seqNo, timepoint)
31 , m_hyperbolicRadius(radius)
32 , m_hyperbolicAngles(angles)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080033{
34}
35
36CoordinateLsa::CoordinateLsa(const ndn::Block& block)
37{
38 wireDecode(block);
39}
40
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080041template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080042size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080043CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080044{
45 size_t totalLength = 0;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080046
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080047 for (auto it = m_hyperbolicAngles.rbegin(); it != m_hyperbolicAngles.rend(); ++it) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040048 totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicAngle, *it);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060049 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080050
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040051 totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicRadius, m_hyperbolicRadius);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080052
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080053 totalLength += Lsa::wireEncode(block);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080054
55 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040056 totalLength += block.prependVarNumber(nlsr::tlv::CoordinateLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080057
58 return totalLength;
59}
60
Alexander Afanasyev67758b12018-03-06 18:36:44 -050061NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080062
63const ndn::Block&
64CoordinateLsa::wireEncode() const
65{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070066 if (m_wire.hasWire()) {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080067 return m_wire;
68 }
69
70 ndn::EncodingEstimator estimator;
71 size_t estimatedSize = wireEncode(estimator);
72
73 ndn::EncodingBuffer buffer(estimatedSize, 0);
74 wireEncode(buffer);
75
76 m_wire = buffer.block();
77
78 return m_wire;
79}
80
81void
82CoordinateLsa::wireDecode(const ndn::Block& wire)
83{
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080084 m_wire = wire;
85
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040086 if (m_wire.type() != nlsr::tlv::CoordinateLsa) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050087 NDN_THROW(Error("CoordinateLsa", m_wire.type()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080088 }
89
90 m_wire.parse();
91
Ashlesh Gawande57a87172020-05-09 19:47:06 -070092 auto val = m_wire.elements_begin();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080093
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040094 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080095 Lsa::wireDecode(*val);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080096 ++val;
97 }
98 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050099 NDN_THROW(Error("Missing required Lsa field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800100 }
101
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400102 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::HyperbolicRadius) {
Tianxing Ma9ea36392018-10-05 14:32:55 -0500103 m_hyperbolicRadius = ndn::encoding::readDouble(*val);
Ashlesh Gawande8df15982018-05-09 17:52:33 -0500104 ++val;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800105 }
106 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500107 NDN_THROW(Error("Missing required HyperbolicRadius field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800108 }
109
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800110 std::vector<double> angles;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600111 for (; val != m_wire.elements_end(); ++val) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400112 if (val->type() == nlsr::tlv::HyperbolicAngle) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800113 angles.push_back(ndn::encoding::readDouble(*val));
114 }
115 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500116 NDN_THROW(Error("Missing required HyperbolicAngle field"));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600117 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800118 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800119 m_hyperbolicAngles = angles;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800120}
121
Junxiao Shi153fbc12024-01-09 23:37:23 +0000122void
123CoordinateLsa::print(std::ostream& os) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800124{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700125 os << " Hyperbolic Radius : " << m_hyperbolicRadius << "\n";
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600126 int i = 0;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700127 for (const auto& value : m_hyperbolicAngles) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800128 os << " Hyperbolic Theta " << i++ << " : " << value << "\n";
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600129 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700130}
131
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700132std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
133CoordinateLsa::update(const std::shared_ptr<Lsa>& lsa)
134{
135 auto clsa = std::static_pointer_cast<CoordinateLsa>(lsa);
Junxiao Shib5734842024-01-09 21:14:53 +0000136 if (*this != *clsa) {
137 m_hyperbolicRadius = clsa->getRadius();
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700138 m_hyperbolicAngles.clear();
Junxiao Shib5734842024-01-09 21:14:53 +0000139 for (const auto& angle : clsa->getTheta()) {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700140 m_hyperbolicAngles.push_back(angle);
141 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400142 return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700143 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400144 return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700145}
146
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700147} // namespace nlsr