blob: 02509a002d8d32bc002801b28f9a38588ed4cff3 [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"
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050024#include "logger.hpp"
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080025
26#include <ndn-cxx/util/concepts.hpp>
27#include <ndn-cxx/encoding/block-helpers.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050028
29#include <iostream>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080030
31namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060032namespace tlv {
33
34INIT_LOGGER("CoordinateLsa");
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080035
36BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>));
37BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>));
38static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value,
39 "CoordinateLsa::Error must inherit from tlv::Error");
40
41CoordinateLsa::CoordinateLsa()
42 : m_hyperbolicRadius(0.0)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080043{
44}
45
46CoordinateLsa::CoordinateLsa(const ndn::Block& block)
47{
48 wireDecode(block);
49}
50
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080051template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080052size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080053CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080054{
55 size_t totalLength = 0;
56 size_t doubleLength = 10;
57
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060058 const uint8_t* doubleBytes1;
59 for (auto it = m_hyperbolicAngle.rbegin(); it != m_hyperbolicAngle.rend(); ++it) {
60 doubleBytes1 = reinterpret_cast<const uint8_t*>(&*it);
61
62 totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes1, 8);
63 totalLength += block.prependVarNumber(doubleLength);
64 totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicAngle);
65 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080066
67 const uint8_t* doubleBytes2 = reinterpret_cast<const uint8_t*>(&m_hyperbolicRadius);
68 totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes2, 8);
69 totalLength += block.prependVarNumber(doubleLength);
70 totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicRadius);
71
72 totalLength += m_lsaInfo.wireEncode(block);
73
74 totalLength += block.prependVarNumber(totalLength);
75 totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa);
76
77 return totalLength;
78}
79
80template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080081CoordinateLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080082
83template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080084CoordinateLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080085
86const ndn::Block&
87CoordinateLsa::wireEncode() const
88{
89 if (m_wire.hasWire()) {
90 return m_wire;
91 }
92
93 ndn::EncodingEstimator estimator;
94 size_t estimatedSize = wireEncode(estimator);
95
96 ndn::EncodingBuffer buffer(estimatedSize, 0);
97 wireEncode(buffer);
98
99 m_wire = buffer.block();
100
101 return m_wire;
102}
103
104void
105CoordinateLsa::wireDecode(const ndn::Block& wire)
106{
107 m_hyperbolicRadius = 0.0;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600108 m_hyperbolicAngle.clear();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800109
110 m_wire = wire;
111
112 if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) {
113 std::stringstream error;
114 error << "Expected CoordinateLsa Block, but Block is of a different type: #"
115 << m_wire.type();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600116 BOOST_THROW_EXCEPTION(Error(error.str()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800117 }
118
119 m_wire.parse();
120
121 ndn::Block::element_const_iterator val = m_wire.elements_begin();
122
123 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) {
124 m_lsaInfo.wireDecode(*val);
125 ++val;
126 }
127 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600128 std::cout << "Missing required LsaInfo field" << std::endl;
129 BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800130 }
131
132 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) {
133 val->parse();
134 ndn::Block::element_const_iterator it = val->elements_begin();
135 if (it != val->elements_end() && it->type() == ndn::tlv::nlsr::Double) {
136 m_hyperbolicRadius = *reinterpret_cast<const double*>(it->value());
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600137
138 ++val;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800139 }
140 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600141 std::cout << "HyperbolicRadius: Missing required Double field" << std::endl;
142 BOOST_THROW_EXCEPTION(Error("HyperbolicRadius: Missing required Double field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800143 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800144 }
145 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600146 std::cout << "Missing required HyperbolicRadius field" << std::endl;
147 BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800148 }
149
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600150 for (; val != m_wire.elements_end(); ++val) {
151 if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
152 val->parse();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800153
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600154 for (auto it = val->elements_begin(); it != val->elements_end(); ++it) {
155 if (it->type() == ndn::tlv::nlsr::Double) {
156 m_hyperbolicAngle.push_back(*reinterpret_cast<const double*>(it->value()));
157 }
158 else {
159 std::cout << "HyperbolicAngle: Missing required Double field" << std::endl;
160 BOOST_THROW_EXCEPTION(Error("HyperbolicAngle: Missing required Double field"));
161 }
162 }
163 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800164 }
165}
166
167std::ostream&
168operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
169{
170 os << "CoordinateLsa("
171 << coordinateLsa.getLsaInfo() << ", "
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600172 << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", ";
173
174 os << "HyperbolicAngles: ";
175 int i = 0;
176 for (const auto& value: coordinateLsa.getHyperbolicAngle()) {
177 if (i == 0) {
178 os << value;
179 }
180 else {
181 os << ", " << value;
182 }
183 ++i;
184 }
185 os << ")";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800186
187 return os;
188}
189
190} // namespace tlv
191} // namespace nlsr