Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
| 20 | **/ |
| 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 R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 27 | #include "logger.hpp" |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 30 | namespace tlv { |
| 31 | |
| 32 | INIT_LOGGER("CoordinateLsa"); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 33 | |
| 34 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>)); |
| 35 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>)); |
| 36 | static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value, |
| 37 | "CoordinateLsa::Error must inherit from tlv::Error"); |
| 38 | |
| 39 | CoordinateLsa::CoordinateLsa() |
| 40 | : m_hyperbolicRadius(0.0) |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | CoordinateLsa::CoordinateLsa(const ndn::Block& block) |
| 45 | { |
| 46 | wireDecode(block); |
| 47 | } |
| 48 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 49 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 50 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 51 | CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 52 | { |
| 53 | size_t totalLength = 0; |
| 54 | size_t doubleLength = 10; |
| 55 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 56 | const uint8_t* doubleBytes1; |
| 57 | for (auto it = m_hyperbolicAngle.rbegin(); it != m_hyperbolicAngle.rend(); ++it) { |
| 58 | doubleBytes1 = reinterpret_cast<const uint8_t*>(&*it); |
| 59 | |
| 60 | totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes1, 8); |
| 61 | totalLength += block.prependVarNumber(doubleLength); |
| 62 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicAngle); |
| 63 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 64 | |
| 65 | const uint8_t* doubleBytes2 = reinterpret_cast<const uint8_t*>(&m_hyperbolicRadius); |
| 66 | totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes2, 8); |
| 67 | totalLength += block.prependVarNumber(doubleLength); |
| 68 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicRadius); |
| 69 | |
| 70 | totalLength += m_lsaInfo.wireEncode(block); |
| 71 | |
| 72 | totalLength += block.prependVarNumber(totalLength); |
| 73 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa); |
| 74 | |
| 75 | return totalLength; |
| 76 | } |
| 77 | |
| 78 | template size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 79 | CoordinateLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 80 | |
| 81 | template size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 82 | CoordinateLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 83 | |
| 84 | const ndn::Block& |
| 85 | CoordinateLsa::wireEncode() const |
| 86 | { |
| 87 | if (m_wire.hasWire()) { |
| 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 | { |
| 105 | m_hyperbolicRadius = 0.0; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 106 | m_hyperbolicAngle.clear(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 107 | |
| 108 | m_wire = wire; |
| 109 | |
| 110 | if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) { |
| 111 | std::stringstream error; |
| 112 | error << "Expected CoordinateLsa Block, but Block is of a different type: #" |
| 113 | << m_wire.type(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 114 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | m_wire.parse(); |
| 118 | |
| 119 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 120 | |
| 121 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) { |
| 122 | m_lsaInfo.wireDecode(*val); |
| 123 | ++val; |
| 124 | } |
| 125 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 126 | std::cout << "Missing required LsaInfo field" << std::endl; |
| 127 | BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) { |
| 131 | val->parse(); |
| 132 | ndn::Block::element_const_iterator it = val->elements_begin(); |
| 133 | if (it != val->elements_end() && it->type() == ndn::tlv::nlsr::Double) { |
| 134 | m_hyperbolicRadius = *reinterpret_cast<const double*>(it->value()); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 135 | |
| 136 | ++val; |
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 << "HyperbolicRadius: Missing required Double field" << std::endl; |
| 140 | BOOST_THROW_EXCEPTION(Error("HyperbolicRadius: Missing required Double field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 141 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 142 | } |
| 143 | else { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 144 | std::cout << "Missing required HyperbolicRadius field" << std::endl; |
| 145 | BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 148 | for (; val != m_wire.elements_end(); ++val) { |
| 149 | if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) { |
| 150 | val->parse(); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 151 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 152 | for (auto it = val->elements_begin(); it != val->elements_end(); ++it) { |
| 153 | if (it->type() == ndn::tlv::nlsr::Double) { |
| 154 | m_hyperbolicAngle.push_back(*reinterpret_cast<const double*>(it->value())); |
| 155 | } |
| 156 | else { |
| 157 | std::cout << "HyperbolicAngle: Missing required Double field" << std::endl; |
| 158 | BOOST_THROW_EXCEPTION(Error("HyperbolicAngle: Missing required Double field")); |
| 159 | } |
| 160 | } |
| 161 | } |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | std::ostream& |
| 166 | operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa) |
| 167 | { |
| 168 | os << "CoordinateLsa(" |
| 169 | << coordinateLsa.getLsaInfo() << ", " |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 170 | << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", "; |
| 171 | |
| 172 | os << "HyperbolicAngles: "; |
| 173 | int i = 0; |
| 174 | for (const auto& value: coordinateLsa.getHyperbolicAngle()) { |
| 175 | if (i == 0) { |
| 176 | os << value; |
| 177 | } |
| 178 | else { |
| 179 | os << ", " << value; |
| 180 | } |
| 181 | ++i; |
| 182 | } |
| 183 | os << ")"; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 184 | |
| 185 | return os; |
| 186 | } |
| 187 | |
| 188 | } // namespace tlv |
| 189 | } // namespace nlsr |