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