blob: cb32d048bebf4a8eb6db2a16ba1bf2caaaaaaace [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>
27
28namespace nlsr {
29namespace tlv {
30
31BOOST_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)
38 , m_hyperbolicAngle(0.0)
39{
40}
41
42CoordinateLsa::CoordinateLsa(const ndn::Block& block)
43{
44 wireDecode(block);
45}
46
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080047template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080048size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080049CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080050{
51 size_t totalLength = 0;
52 size_t doubleLength = 10;
53
54 const uint8_t* doubleBytes1 = reinterpret_cast<const uint8_t*>(&m_hyperbolicAngle);
55 totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes1, 8);
56 totalLength += block.prependVarNumber(doubleLength);
57 totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicAngle);
58
59 const uint8_t* doubleBytes2 = reinterpret_cast<const uint8_t*>(&m_hyperbolicRadius);
60 totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes2, 8);
61 totalLength += block.prependVarNumber(doubleLength);
62 totalLength += block.prependVarNumber(ndn::tlv::nlsr::HyperbolicRadius);
63
64 totalLength += m_lsaInfo.wireEncode(block);
65
66 totalLength += block.prependVarNumber(totalLength);
67 totalLength += block.prependVarNumber(ndn::tlv::nlsr::CoordinateLsa);
68
69 return totalLength;
70}
71
72template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080073CoordinateLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080074
75template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080076CoordinateLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080077
78const ndn::Block&
79CoordinateLsa::wireEncode() const
80{
81 if (m_wire.hasWire()) {
82 return m_wire;
83 }
84
85 ndn::EncodingEstimator estimator;
86 size_t estimatedSize = wireEncode(estimator);
87
88 ndn::EncodingBuffer buffer(estimatedSize, 0);
89 wireEncode(buffer);
90
91 m_wire = buffer.block();
92
93 return m_wire;
94}
95
96void
97CoordinateLsa::wireDecode(const ndn::Block& wire)
98{
99 m_hyperbolicRadius = 0.0;
100 m_hyperbolicAngle = 0.0;
101
102 m_wire = wire;
103
104 if (m_wire.type() != ndn::tlv::nlsr::CoordinateLsa) {
105 std::stringstream error;
106 error << "Expected CoordinateLsa Block, but Block is of a different type: #"
107 << m_wire.type();
108 throw Error(error.str());
109 }
110
111 m_wire.parse();
112
113 ndn::Block::element_const_iterator val = m_wire.elements_begin();
114
115 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) {
116 m_lsaInfo.wireDecode(*val);
117 ++val;
118 }
119 else {
120 throw Error("Missing required LsaInfo field");
121 }
122
123 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicRadius) {
124 val->parse();
125 ndn::Block::element_const_iterator it = val->elements_begin();
126 if (it != val->elements_end() && it->type() == ndn::tlv::nlsr::Double) {
127 m_hyperbolicRadius = *reinterpret_cast<const double*>(it->value());
128 }
129 else {
130 throw Error("HyperbolicRadius: Missing required Double field");
131 }
132
133 ++val;
134 }
135 else {
136 throw Error("Missing required HyperbolicRadius field");
137 }
138
139 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
140 val->parse();
141 ndn::Block::element_const_iterator it = val->elements_begin();
142 if (it != val->elements_end() && it->type() == ndn::tlv::nlsr::Double) {
143 m_hyperbolicAngle = *reinterpret_cast<const double*>(it->value());
144 }
145 else {
146 throw Error("HyperbolicAngle: Missing required Double field");
147 }
148
149 ++val;
150 }
151 else {
152 throw Error("Missing required HyperbolicAngle field");
153 }
154}
155
156std::ostream&
157operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
158{
159 os << "CoordinateLsa("
160 << coordinateLsa.getLsaInfo() << ", "
161 << "HyperbolicRadius: " << coordinateLsa.getHyperbolicRadius() << ", "
162 << "HyperbolicAngle: " << coordinateLsa.getHyperbolicAngle() << ")";
163
164 return os;
165}
166
167} // namespace tlv
168} // namespace nlsr