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 | /* |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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; |
| 53 | size_t doubleLength = 10; |
| 54 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 55 | const uint8_t* doubleBytes1; |
| 56 | for (auto it = m_hyperbolicAngle.rbegin(); it != m_hyperbolicAngle.rend(); ++it) { |
| 57 | doubleBytes1 = reinterpret_cast<const uint8_t*>(&*it); |
| 58 | |
| 59 | totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes1, 8); |
| 60 | totalLength += block.prependVarNumber(doubleLength); |
| 61 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicAngle); |
| 62 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 63 | |
| 64 | const uint8_t* doubleBytes2 = reinterpret_cast<const uint8_t*>(&m_hyperbolicRadius); |
| 65 | totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes2, 8); |
| 66 | totalLength += block.prependVarNumber(doubleLength); |
| 67 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicRadius); |
| 68 | |
| 69 | totalLength += m_lsaInfo.wireEncode(block); |
| 70 | |
| 71 | totalLength += block.prependVarNumber(totalLength); |
| 72 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa); |
| 73 | |
| 74 | return totalLength; |
| 75 | } |
| 76 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 77 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 78 | |
| 79 | const ndn::Block& |
| 80 | CoordinateLsa::wireEncode() const |
| 81 | { |
| 82 | if (m_wire.hasWire()) { |
| 83 | return m_wire; |
| 84 | } |
| 85 | |
| 86 | ndn::EncodingEstimator estimator; |
| 87 | size_t estimatedSize = wireEncode(estimator); |
| 88 | |
| 89 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 90 | wireEncode(buffer); |
| 91 | |
| 92 | m_wire = buffer.block(); |
| 93 | |
| 94 | return m_wire; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | CoordinateLsa::wireDecode(const ndn::Block& wire) |
| 99 | { |
| 100 | m_hyperbolicRadius = 0.0; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 101 | m_hyperbolicAngle.clear(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 102 | |
| 103 | m_wire = wire; |
| 104 | |
| 105 | if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) { |
| 106 | std::stringstream error; |
| 107 | error << "Expected CoordinateLsa Block, but Block is of a different type: #" |
| 108 | << m_wire.type(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | m_wire.parse(); |
| 113 | |
| 114 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 115 | |
| 116 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) { |
| 117 | m_lsaInfo.wireDecode(*val); |
| 118 | ++val; |
| 119 | } |
| 120 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 121 | std::cout << "Missing required LsaInfo field" << std::endl; |
| 122 | BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) { |
| 126 | val->parse(); |
| 127 | ndn::Block::element_const_iterator it = val->elements_begin(); |
| 128 | if (it != val->elements_end() && it->type() == ndn::tlv::nlsr::Double) { |
| 129 | m_hyperbolicRadius = *reinterpret_cast<const double*>(it->value()); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 130 | |
| 131 | ++val; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 132 | } |
| 133 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 134 | std::cout << "HyperbolicRadius: Missing required Double field" << std::endl; |
| 135 | BOOST_THROW_EXCEPTION(Error("HyperbolicRadius: Missing required Double field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 136 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 137 | } |
| 138 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 139 | std::cout << "Missing required HyperbolicRadius field" << std::endl; |
| 140 | BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 143 | for (; val != m_wire.elements_end(); ++val) { |
| 144 | if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) { |
| 145 | val->parse(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 146 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 147 | for (auto it = val->elements_begin(); it != val->elements_end(); ++it) { |
| 148 | if (it->type() == ndn::tlv::nlsr::Double) { |
| 149 | m_hyperbolicAngle.push_back(*reinterpret_cast<const double*>(it->value())); |
| 150 | } |
| 151 | else { |
| 152 | std::cout << "HyperbolicAngle: Missing required Double field" << std::endl; |
| 153 | BOOST_THROW_EXCEPTION(Error("HyperbolicAngle: Missing required Double field")); |
| 154 | } |
| 155 | } |
| 156 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | std::ostream& |
| 161 | operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa) |
| 162 | { |
| 163 | os << "CoordinateLsa(" |
| 164 | << coordinateLsa.getLsaInfo() << ", " |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 165 | << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", "; |
| 166 | |
| 167 | os << "HyperbolicAngles: "; |
| 168 | int i = 0; |
| 169 | for (const auto& value: coordinateLsa.getHyperbolicAngle()) { |
| 170 | if (i == 0) { |
| 171 | os << value; |
| 172 | } |
| 173 | else { |
| 174 | os << ", " << value; |
| 175 | } |
| 176 | ++i; |
| 177 | } |
| 178 | os << ")"; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 179 | |
| 180 | return os; |
| 181 | } |
| 182 | |
| 183 | } // namespace tlv |
| 184 | } // namespace nlsr |