tlv/nexthop.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "nexthop.hpp"
23 #include "tlv-nlsr.hpp"
24 #include "coordinate-lsa.hpp"
25 
26 #include <ndn-cxx/util/concepts.hpp>
27 #include <ndn-cxx/encoding/block-helpers.hpp>
28 
29 namespace nlsr {
30 namespace tlv {
31 
32 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<NextHop>));
33 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NextHop>));
34 static_assert(std::is_base_of<ndn::tlv::Error, NextHop::Error>::value,
35  "NextHop::Error must inherit from tlv::Error");
36 
38  : m_cost(0)
39 {
40 }
41 
42 NextHop::NextHop(const ndn::Block& block)
43 {
44  wireDecode(block);
45 }
46 
47 template<ndn::encoding::Tag TAG>
48 size_t
49 NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
50 {
51  size_t totalLength = 0;
52 
53  totalLength += prependDouble(block, ndn::tlv::nlsr::Double, m_cost);
54 
55  totalLength += block.prependByteArrayBlock(
56  ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
57 
58  totalLength += block.prependVarNumber(totalLength);
59  totalLength += block.prependVarNumber(ndn::tlv::nlsr::NextHop);
60 
61  return totalLength;
62 }
63 
65 
66 const ndn::Block&
68 {
69  if (m_wire.hasWire()) {
70  return m_wire;
71  }
72 
73  ndn::EncodingEstimator estimator;
74  size_t estimatedSize = wireEncode(estimator);
75 
76  ndn::EncodingBuffer buffer(estimatedSize, 0);
77  wireEncode(buffer);
78 
79  m_wire = buffer.block();
80 
81  return m_wire;
82 }
83 
84 void
85 NextHop::wireDecode(const ndn::Block& wire)
86 {
87  m_uri = "";
88  m_cost = 0;
89 
90  m_wire = wire;
91 
92  if (m_wire.type() != ndn::tlv::nlsr::NextHop) {
93  std::stringstream error;
94  error << "Expected NextHop Block, but Block is of a different type: #"
95  << m_wire.type();
96  BOOST_THROW_EXCEPTION(Error(error.str()));
97  }
98 
99  m_wire.parse();
100 
101  ndn::Block::element_const_iterator val = m_wire.elements_begin();
102 
103  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
104  m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
105  ++val;
106  }
107  else {
108  BOOST_THROW_EXCEPTION(Error("Missing required Uri field"));
109  }
110 
111  m_cost = ndn::tlv::nlsr::readDouble(*val);
112 }
113 
114 std::ostream&
115 operator<<(std::ostream& os, const NextHop& nexthop)
116 {
117  os << "NextHop("
118  << "Uri: " << nexthop.getUri() << ", "<< "Cost: " << nexthop.getCost() << ")" << std::endl;
119 
120  return os;
121 }
122 
123 } // namespace tlv
124 } // namespace nlsr
const std::string & getUri() const
Definition: tlv/nexthop.hpp:62
Data abstraction for Nexthop.
Definition: tlv/nexthop.hpp:43
double readDouble(const ndn::Block &block)
Read a double from a TLV element.
Definition: tlv-nlsr.hpp:64
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa)
size_t prependDouble(ndn::EncodingImpl< TAG > &encoder, uint32_t type, double value)
Prepend a TLV element containing a double.
Definition: tlv-nlsr.hpp:85
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
const ndn::Block & wireEncode() const
Definition: tlv/nexthop.cpp:67
void wireDecode(const ndn::Block &wire)
Definition: tlv/nexthop.cpp:85
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
double getCost() const
Definition: tlv/nexthop.hpp:76