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 | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 2 | /** |
| 3 | * Copyright (c) 2014-2020, 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 | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 20 | **/ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #include "coordinate-lsa.hpp" |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 23 | #include "tlv/tlv-nlsr.hpp" |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 24 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 25 | #include <boost/lexical_cast.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 26 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 27 | namespace nlsr { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 28 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 29 | CoordinateLsa::CoordinateLsa(const ndn::Name& originRouter, uint32_t seqNo, |
| 30 | 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 Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | CoordinateLsa::CoordinateLsa(const ndn::Block& block) |
| 39 | { |
| 40 | wireDecode(block); |
| 41 | } |
| 42 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 43 | bool |
| 44 | CoordinateLsa::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 Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 61 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 62 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 63 | CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 64 | { |
| 65 | size_t totalLength = 0; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 66 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 67 | for (auto it = m_hyperbolicAngles.rbegin(); it != m_hyperbolicAngles.rend(); ++it) { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 68 | totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::HyperbolicAngle, *it); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 69 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 70 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 71 | totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::HyperbolicRadius, |
| 72 | m_hyperbolicRadius); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 73 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 74 | totalLength += Lsa::wireEncode(block); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 75 | |
| 76 | totalLength += block.prependVarNumber(totalLength); |
| 77 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa); |
| 78 | |
| 79 | return totalLength; |
| 80 | } |
| 81 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 82 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 83 | |
| 84 | const ndn::Block& |
| 85 | CoordinateLsa::wireEncode() const |
| 86 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 87 | if (m_wire.hasWire() && m_baseWire.hasWire()) { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 88 | return m_wire; |
| 89 | } |
| 90 | |
| 91 | ndn::EncodingEstimator estimator; |
| 92 | size_t estimatedSize = wireEncode(estimator); |
| 93 | |
| 94 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 95 | wireEncode(buffer); |
| 96 | |
| 97 | m_wire = buffer.block(); |
| 98 | |
| 99 | return m_wire; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | CoordinateLsa::wireDecode(const ndn::Block& wire) |
| 104 | { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 105 | m_wire = wire; |
| 106 | |
| 107 | if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) { |
| 108 | std::stringstream error; |
| 109 | error << "Expected CoordinateLsa Block, but Block is of a different type: #" |
| 110 | << m_wire.type(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 111 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | m_wire.parse(); |
| 115 | |
| 116 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 117 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 118 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) { |
| 119 | Lsa::wireDecode(*val); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 120 | ++val; |
| 121 | } |
| 122 | else { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 123 | BOOST_THROW_EXCEPTION(Error("Missing required Lsa field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 127 | m_hyperbolicRadius = ndn::encoding::readDouble(*val); |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame] | 128 | ++val; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 129 | } |
| 130 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 131 | BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 134 | std::vector<double> angles; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 135 | for (; val != m_wire.elements_end(); ++val) { |
| 136 | if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 137 | angles.push_back(ndn::encoding::readDouble(*val)); |
| 138 | } |
| 139 | else { |
| 140 | BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicAngle field")); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 141 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 142 | } |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 143 | m_hyperbolicAngles = angles; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | std::ostream& |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 147 | operator<<(std::ostream& os, const CoordinateLsa& lsa) |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 148 | { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 149 | os << lsa.toString(); |
| 150 | os << " Hyperbolic Radius : " << lsa.getCorRadius() << "\n"; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 151 | int i = 0; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 152 | for (const auto& value : lsa.getCorTheta()) { |
| 153 | os << " Hyperbolic Theta " << i++ << " : " << value << "\n"; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 154 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 155 | |
| 156 | return os; |
| 157 | } |
| 158 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 159 | } // namespace nlsr |