coordinate-lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "coordinate-lsa.hpp"
23 #include "tlv-nlsr.hpp"
24 #include "logger.hpp"
25 
26 #include <ndn-cxx/util/concepts.hpp>
27 #include <ndn-cxx/encoding/block-helpers.hpp>
28 
29 #include <iostream>
30 
31 namespace nlsr {
32 namespace tlv {
33 
34 INIT_LOGGER("CoordinateLsa");
35 
36 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<CoordinateLsa>));
37 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<CoordinateLsa>));
38 static_assert(std::is_base_of<ndn::tlv::Error, CoordinateLsa::Error>::value,
39  "CoordinateLsa::Error must inherit from tlv::Error");
40 
42  : m_hyperbolicRadius(0.0)
43 {
44 }
45 
46 CoordinateLsa::CoordinateLsa(const ndn::Block& block)
47 {
48  wireDecode(block);
49 }
50 
51 template<ndn::encoding::Tag TAG>
52 size_t
53 CoordinateLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
54 {
55  size_t totalLength = 0;
56  size_t doubleLength = 10;
57 
58  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  }
66 
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 
80 template size_t
81 CoordinateLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
82 
83 template size_t
84 CoordinateLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
85 
86 const ndn::Block&
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 
104 void
105 CoordinateLsa::wireDecode(const ndn::Block& wire)
106 {
107  m_hyperbolicRadius = 0.0;
108  m_hyperbolicAngle.clear();
109 
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();
116  BOOST_THROW_EXCEPTION(Error(error.str()));
117  }
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 {
128  std::cout << "Missing required LsaInfo field" << std::endl;
129  BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field"));
130  }
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());
137 
138  ++val;
139  }
140  else {
141  std::cout << "HyperbolicRadius: Missing required Double field" << std::endl;
142  BOOST_THROW_EXCEPTION(Error("HyperbolicRadius: Missing required Double field"));
143  }
144  }
145  else {
146  std::cout << "Missing required HyperbolicRadius field" << std::endl;
147  BOOST_THROW_EXCEPTION(Error("Missing required HyperbolicRadius field"));
148  }
149 
150  for (; val != m_wire.elements_end(); ++val) {
151  if (val->type() == ndn::tlv::nlsr::HyperbolicAngle) {
152  val->parse();
153 
154  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  }
164  }
165 }
166 
167 std::ostream&
168 operator<<(std::ostream& os, const CoordinateLsa& coordinateLsa)
169 {
170  os << "CoordinateLsa("
171  << coordinateLsa.getLsaInfo() << ", "
172  << "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 << ")";
186 
187  return os;
188 }
189 
190 } // namespace tlv
191 } // namespace nlsr
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Encodes LSA info using the method in TAG.
Definition: lsa-info.cpp:52
const LsaInfo & getLsaInfo() const
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
Data abstraction for CoordinateLsa.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
const std::vector< double > getHyperbolicAngle() const
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
double getHyperbolicRadius() const
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
Definition: lsa-info.cpp:100