Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Junxiao Shi | b573484 | 2024-01-09 21:14:53 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #include "coordinate-lsa.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 23 | #include "tlv-nlsr.hpp" |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 24 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 25 | namespace nlsr { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 26 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 27 | CoordinateLsa::CoordinateLsa(const ndn::Name& originRouter, uint64_t seqNo, |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 28 | const ndn::time::system_clock::time_point& timepoint, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 29 | double radius, std::vector<double> angles) |
| 30 | : Lsa(originRouter, seqNo, timepoint) |
| 31 | , m_hyperbolicRadius(radius) |
| 32 | , m_hyperbolicAngles(angles) |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | CoordinateLsa::CoordinateLsa(const ndn::Block& block) |
| 37 | { |
| 38 | wireDecode(block); |
| 39 | } |
| 40 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 41 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 42 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 43 | CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 44 | { |
| 45 | size_t totalLength = 0; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 46 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 47 | for (auto it = m_hyperbolicAngles.rbegin(); it != m_hyperbolicAngles.rend(); ++it) { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 48 | totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicAngle, *it); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 49 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 50 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 51 | totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::HyperbolicRadius, m_hyperbolicRadius); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 52 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 53 | totalLength += Lsa::wireEncode(block); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 54 | |
| 55 | totalLength += block.prependVarNumber(totalLength); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 56 | totalLength += block.prependVarNumber(nlsr::tlv::CoordinateLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 57 | |
| 58 | return totalLength; |
| 59 | } |
| 60 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 61 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 62 | |
| 63 | const ndn::Block& |
| 64 | CoordinateLsa::wireEncode() const |
| 65 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 66 | if (m_wire.hasWire()) { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 67 | 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 | |
| 81 | void |
| 82 | CoordinateLsa::wireDecode(const ndn::Block& wire) |
| 83 | { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 84 | m_wire = wire; |
| 85 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 86 | if (m_wire.type() != nlsr::tlv::CoordinateLsa) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 87 | NDN_THROW(Error("CoordinateLsa", m_wire.type())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | m_wire.parse(); |
| 91 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 92 | auto val = m_wire.elements_begin(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 93 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 94 | if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 95 | Lsa::wireDecode(*val); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 96 | ++val; |
| 97 | } |
| 98 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 99 | NDN_THROW(Error("Missing required Lsa field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 102 | if (val != m_wire.elements_end() && val->type() == nlsr::tlv::HyperbolicRadius) { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 103 | m_hyperbolicRadius = ndn::encoding::readDouble(*val); |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame] | 104 | ++val; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 105 | } |
| 106 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 107 | NDN_THROW(Error("Missing required HyperbolicRadius field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 110 | std::vector<double> angles; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 111 | for (; val != m_wire.elements_end(); ++val) { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 112 | if (val->type() == nlsr::tlv::HyperbolicAngle) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 113 | angles.push_back(ndn::encoding::readDouble(*val)); |
| 114 | } |
| 115 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 116 | NDN_THROW(Error("Missing required HyperbolicAngle field")); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 117 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 118 | } |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 119 | m_hyperbolicAngles = angles; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Junxiao Shi | 153fbc1 | 2024-01-09 23:37:23 +0000 | [diff] [blame^] | 122 | void |
| 123 | CoordinateLsa::print(std::ostream& os) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 124 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 125 | os << " Hyperbolic Radius : " << m_hyperbolicRadius << "\n"; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 126 | int i = 0; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 127 | for (const auto& value : m_hyperbolicAngles) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 128 | os << " Hyperbolic Theta " << i++ << " : " << value << "\n"; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 129 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 132 | std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>> |
| 133 | CoordinateLsa::update(const std::shared_ptr<Lsa>& lsa) |
| 134 | { |
| 135 | auto clsa = std::static_pointer_cast<CoordinateLsa>(lsa); |
Junxiao Shi | b573484 | 2024-01-09 21:14:53 +0000 | [diff] [blame] | 136 | if (*this != *clsa) { |
| 137 | m_hyperbolicRadius = clsa->getRadius(); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 138 | m_hyperbolicAngles.clear(); |
Junxiao Shi | b573484 | 2024-01-09 21:14:53 +0000 | [diff] [blame] | 139 | for (const auto& angle : clsa->getTheta()) { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 140 | m_hyperbolicAngles.push_back(angle); |
| 141 | } |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 142 | return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}}; |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 143 | } |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 144 | return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}}; |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 147 | } // namespace nlsr |