blob: ab39fe879f660098651b1941a61a57de30abb6ac [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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 Chowdhuryf04f9892017-08-20 20:42:56 -050027
28#include <iostream>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080029
30namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060031namespace tlv {
32
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080033BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>));
34BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>));
35static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value,
36 "CoordinateLsa::Error must inherit from tlv::Error");
37
38CoordinateLsa::CoordinateLsa()
39 : m_hyperbolicRadius(0.0)
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080040{
41}
42
43CoordinateLsa::CoordinateLsa(const ndn::Block& block)
44{
45 wireDecode(block);
46}
47
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080048template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080049size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080050CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080051{
52 size_t totalLength = 0;
53 size_t doubleLength = 10;
54
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060055 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 Tan7a56d1c2015-01-26 23:26:51 -080063
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
77template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080078CoordinateLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080079
80template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080081CoordinateLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080082
83const ndn::Block&
84CoordinateLsa::wireEncode() const
85{
86 if (m_wire.hasWire()) {
87 return m_wire;
88 }
89
90 ndn::EncodingEstimator estimator;
91 size_t estimatedSize = wireEncode(estimator);
92
93 ndn::EncodingBuffer buffer(estimatedSize, 0);
94 wireEncode(buffer);
95
96 m_wire = buffer.block();
97
98 return m_wire;
99}
100
101void
102CoordinateLsa::wireDecode(const ndn::Block& wire)
103{
104 m_hyperbolicRadius = 0.0;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600105 m_hyperbolicAngle.clear();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800106
107 m_wire = wire;
108
109 if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) {
110 std::stringstream error;
111 error << "Expected CoordinateLsa Block, but Block is of a different type: #"
112 << m_wire.type();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600113 BOOST_THROW_EXCEPTION(Error(error.str()));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800114 }
115
116 m_wire.parse();
117
118 ndn::Block::element_const_iterator val = m_wire.elements_begin();
119
120 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) {
121 m_lsaInfo.wireDecode(*val);
122 ++val;
123 }
124 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600125 std::cout << "Missing required LsaInfo field" << std::endl;
126 BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800127 }
128
129 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) {
130 val->parse();
131 ndn::Block::element_const_iterator it = val->elements_begin();
132 if (it != val->elements_end() && it->type() == ndn::tlv::nlsr::Double) {
133 m_hyperbolicRadius = *reinterpret_cast<const double*>(it->value());
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600134
135 ++val;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800136 }
137 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600138 std::cout << "HyperbolicRadius: Missing required Double field" << std::endl;
139 BOOST_THROW_EXCEPTION(Error("HyperbolicRadius: Missing required Double field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800140 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800141 }
142 else {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600143 std::cout << "Missing required HyperbolicRadius field" << std::endl;
144 BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800145 }
146
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600147 for (; val != m_wire.elements_end(); ++val) {
148 if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
149 val->parse();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800150
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600151 for (auto it = val->elements_begin(); it != val->elements_end(); ++it) {
152 if (it->type() == ndn::tlv::nlsr::Double) {
153 m_hyperbolicAngle.push_back(*reinterpret_cast<const double*>(it->value()));
154 }
155 else {
156 std::cout << "HyperbolicAngle: Missing required Double field" << std::endl;
157 BOOST_THROW_EXCEPTION(Error("HyperbolicAngle: Missing required Double field"));
158 }
159 }
160 }
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800161 }
162}
163
164std::ostream&
165operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
166{
167 os << "CoordinateLsa("
168 << coordinateLsa.getLsaInfo() << ", "
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600169 << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", ";
170
171 os << "HyperbolicAngles: ";
172 int i = 0;
173 for (const auto& value: coordinateLsa.getHyperbolicAngle()) {
174 if (i == 0) {
175 os << value;
176 }
177 else {
178 os << ", " << value;
179 }
180 ++i;
181 }
182 os << ")";
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800183
184 return os;
185}
186
187} // namespace tlv
188} // namespace nlsr