blob: f05b682de79aa804f90c53cd826787a2c155e5bd [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002/*
Tianxing Ma9ea36392018-10-05 14:32:55 -05003 * Copyright (c) 2014-2019, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * 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 Afanasyev67758b12018-03-06 18:36:44 -050020 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080021
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 Chowdhuryf04f9892017-08-20 20:42:56 -050027
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080028namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060029namespace tlv {
30
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080031BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>));
33static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value,
34 "CoordinateLsa::Error must inherit from tlv::Error");
35
36CoordinateLsa::CoordinateLsa()
37 : m_hyperbolicRadius(0.0)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080038{
39}
40
41CoordinateLsa::CoordinateLsa(const ndn::Block& block)
42{
43 wireDecode(block);
44}
45
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080046template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080047size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080048CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080049{
50 size_t totalLength = 0;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080051
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060052 for (auto it = m_hyperbolicAngle.rbegin(); it != m_hyperbolicAngle.rend(); ++it) {
Tianxing Ma9ea36392018-10-05 14:32:55 -050053 totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::HyperbolicAngle, *it);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060054 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080055
Tianxing Ma9ea36392018-10-05 14:32:55 -050056 totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::HyperbolicRadius, m_hyperbolicRadius);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080057
58 totalLength += m_lsaInfo.wireEncode(block);
59
60 totalLength += block.prependVarNumber(totalLength);
61 totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa);
62
63 return totalLength;
64}
65
Alexander Afanasyev67758b12018-03-06 18:36:44 -050066NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080067
68const ndn::Block&
69CoordinateLsa::wireEncode() const
70{
71 if (m_wire.hasWire()) {
72 return m_wire;
73 }
74
75 ndn::EncodingEstimator estimator;
76 size_t estimatedSize = wireEncode(estimator);
77
78 ndn::EncodingBuffer buffer(estimatedSize, 0);
79 wireEncode(buffer);
80
81 m_wire = buffer.block();
82
83 return m_wire;
84}
85
86void
87CoordinateLsa::wireDecode(const ndn::Block& wire)
88{
89 m_hyperbolicRadius = 0.0;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060090 m_hyperbolicAngle.clear();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080091
92 m_wire = wire;
93
94 if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) {
95 std::stringstream error;
96 error << "Expected CoordinateLsa Block, but Block is of a different type: #"
97 << m_wire.type();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060098 BOOST_THROW_EXCEPTION(Error(error.str()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080099 }
100
101 m_wire.parse();
102
103 ndn::Block::element_const_iterator val = m_wire.elements_begin();
104
105 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) {
106 m_lsaInfo.wireDecode(*val);
107 ++val;
108 }
109 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600110 BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800111 }
112
113 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) {
Tianxing Ma9ea36392018-10-05 14:32:55 -0500114 m_hyperbolicRadius = ndn::encoding::readDouble(*val);
Ashlesh Gawande8df15982018-05-09 17:52:33 -0500115 ++val;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800116 }
117 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600118 BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800119 }
120
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600121 for (; val != m_wire.elements_end(); ++val) {
122 if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
Tianxing Ma9ea36392018-10-05 14:32:55 -0500123 m_hyperbolicAngle.push_back(ndn::encoding::readDouble(*val));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600124 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800125 }
126}
127
128std::ostream&
129operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
130{
131 os << "CoordinateLsa("
132 << coordinateLsa.getLsaInfo() << ", "
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600133 << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", ";
134
135 os << "HyperbolicAngles: ";
136 int i = 0;
137 for (const auto& value: coordinateLsa.getHyperbolicAngle()) {
138 if (i == 0) {
139 os << value;
140 }
141 else {
142 os << ", " << value;
143 }
144 ++i;
145 }
146 os << ")";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800147
148 return os;
149}
150
151} // namespace tlv
152} // namespace nlsr