Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 2 | /* |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, 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/>. |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 20 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #include "coordinate-lsa.hpp" |
| 23 | #include "tlv-nlsr.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/util/concepts.hpp> |
| 26 | #include <ndn-cxx/encoding/block-helpers.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 27 | |
| 28 | #include <iostream> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 29 | |
| 30 | namespace nlsr { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 31 | namespace tlv { |
| 32 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 33 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>)); |
| 34 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>)); |
| 35 | static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value, |
| 36 | "CoordinateLsa::Error must inherit from tlv::Error"); |
| 37 | |
| 38 | CoordinateLsa::CoordinateLsa() |
| 39 | : m_hyperbolicRadius(0.0) |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | CoordinateLsa::CoordinateLsa(const ndn::Block& block) |
| 44 | { |
| 45 | wireDecode(block); |
| 46 | } |
| 47 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 48 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 49 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 50 | CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 51 | { |
| 52 | size_t totalLength = 0; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 53 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 54 | for (auto it = m_hyperbolicAngle.rbegin(); it != m_hyperbolicAngle.rend(); ++it) { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame^] | 55 | totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::HyperbolicAngle, *it); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 56 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 57 | |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame^] | 58 | totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::HyperbolicRadius, m_hyperbolicRadius); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 59 | |
| 60 | totalLength += m_lsaInfo.wireEncode(block); |
| 61 | |
| 62 | totalLength += block.prependVarNumber(totalLength); |
| 63 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa); |
| 64 | |
| 65 | return totalLength; |
| 66 | } |
| 67 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 68 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 69 | |
| 70 | const ndn::Block& |
| 71 | CoordinateLsa::wireEncode() const |
| 72 | { |
| 73 | if (m_wire.hasWire()) { |
| 74 | return m_wire; |
| 75 | } |
| 76 | |
| 77 | ndn::EncodingEstimator estimator; |
| 78 | size_t estimatedSize = wireEncode(estimator); |
| 79 | |
| 80 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 81 | wireEncode(buffer); |
| 82 | |
| 83 | m_wire = buffer.block(); |
| 84 | |
| 85 | return m_wire; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | CoordinateLsa::wireDecode(const ndn::Block& wire) |
| 90 | { |
| 91 | m_hyperbolicRadius = 0.0; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 92 | m_hyperbolicAngle.clear(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 93 | |
| 94 | m_wire = wire; |
| 95 | |
| 96 | if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) { |
| 97 | std::stringstream error; |
| 98 | error << "Expected CoordinateLsa Block, but Block is of a different type: #" |
| 99 | << m_wire.type(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 100 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | m_wire.parse(); |
| 104 | |
| 105 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 106 | |
| 107 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) { |
| 108 | m_lsaInfo.wireDecode(*val); |
| 109 | ++val; |
| 110 | } |
| 111 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 112 | BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame^] | 116 | m_hyperbolicRadius = ndn::encoding::readDouble(*val); |
Ashlesh Gawande | 8df1598 | 2018-05-09 17:52:33 -0500 | [diff] [blame] | 117 | ++val; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 118 | } |
| 119 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 120 | BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 123 | for (; val != m_wire.elements_end(); ++val) { |
| 124 | if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) { |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame^] | 125 | m_hyperbolicAngle.push_back(ndn::encoding::readDouble(*val)); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 126 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | std::ostream& |
| 131 | operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa) |
| 132 | { |
| 133 | os << "CoordinateLsa(" |
| 134 | << coordinateLsa.getLsaInfo() << ", " |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 135 | << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", "; |
| 136 | |
| 137 | os << "HyperbolicAngles: "; |
| 138 | int i = 0; |
| 139 | for (const auto& value: coordinateLsa.getHyperbolicAngle()) { |
| 140 | if (i == 0) { |
| 141 | os << value; |
| 142 | } |
| 143 | else { |
| 144 | os << ", " << value; |
| 145 | } |
| 146 | ++i; |
| 147 | } |
| 148 | os << ")"; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 149 | |
| 150 | return os; |
| 151 | } |
| 152 | |
| 153 | } // namespace tlv |
| 154 | } // namespace nlsr |