blob: 92809fea2958c8154f2e7266f28cecb80c828ed7 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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/>.
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 Chowdhuryb00dc2a2016-11-05 10:48:58 -060027#include "logger.hpp"
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080028
29namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060030namespace tlv {
31
32INIT_LOGGER("CoordinateLsa");
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080033
34BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>));
35BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>));
36static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value,
37 "CoordinateLsa::Error must inherit from tlv::Error");
38
39CoordinateLsa::CoordinateLsa()
40 : m_hyperbolicRadius(0.0)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080041{
42}
43
44CoordinateLsa::CoordinateLsa(const ndn::Block& block)
45{
46 wireDecode(block);
47}
48
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080049template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080050size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080051CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080052{
53 size_t totalLength = 0;
54 size_t doubleLength = 10;
55
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060056 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 Tan7a56d1c2015-01-26 23:26:51 -080064
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
78template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080079CoordinateLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080080
81template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080082CoordinateLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080083
84const ndn::Block&
85CoordinateLsa::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
102void
103CoordinateLsa::wireDecode(const ndn::Block& wire)
104{
105 m_hyperbolicRadius = 0.0;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600106 m_hyperbolicAngle.clear();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800107
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 Chowdhuryb00dc2a2016-11-05 10:48:58 -0600114 BOOST_THROW_EXCEPTION(Error(error.str()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800115 }
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 Chowdhuryb00dc2a2016-11-05 10:48:58 -0600126 std::cout << "Missing required LsaInfo field" << std::endl;
127 BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800128 }
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 Chowdhuryb00dc2a2016-11-05 10:48:58 -0600135
136 ++val;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800137 }
138 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600139 std::cout << "HyperbolicRadius: Missing required Double field" << std::endl;
140 BOOST_THROW_EXCEPTION(Error("HyperbolicRadius: Missing required Double field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800141 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800142 }
143 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600144 std::cout << "Missing required HyperbolicRadius field" << std::endl;
145 BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800146 }
147
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600148 for (; val != m_wire.elements_end(); ++val) {
149 if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
150 val->parse();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800151
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600152 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 Tan7a56d1c2015-01-26 23:26:51 -0800162 }
163}
164
165std::ostream&
166operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
167{
168 os << "CoordinateLsa("
169 << coordinateLsa.getLsaInfo() << ", "
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600170 << "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 Tan7a56d1c2015-01-26 23:26:51 -0800184
185 return os;
186}
187
188} // namespace tlv
189} // namespace nlsr