blob: 6f9f9183d968f5fe1529ace01d039bbd9896ef81 [file] [log] [blame]
laqinfan35731852017-08-08 06:17:39 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, The University of Memphis,
4 * 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 "destination.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<Destination>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<Destination>));
33static_assert(std::is_base_of<ndn::tlv::Error, Destination::Error>::value,
34 "Destination::Error must inherit from tlv::Error");
35
36Destination::Destination() = default;
37
38Destination::Destination(const ndn::Block& block)
39{
40 wireDecode(block);
41}
42
43template<ndn::encoding::Tag TAG>
44size_t
45Destination::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
57NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Destination);
58
59template size_t
60Destination::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
61
62template size_t
63Destination::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
64
65const ndn::Block&
66Destination::wireEncode() const
67{
68 if (m_wire.hasWire()) {
69 return m_wire;
70 }
71
72 ndn::EncodingEstimator estimator;
73 size_t estimatedSize = wireEncode(estimator);
74
75 ndn::EncodingBuffer buffer(estimatedSize, 0);
76 wireEncode(buffer);
77
78 m_wire = buffer.block();
79
80 return m_wire;
81}
82
83void
84Destination::wireDecode(const ndn::Block& wire)
85{
86 m_name.clear();
87
88 m_wire = wire;
89
90 if (m_wire.type() != ndn::tlv::nlsr::Destination) {
91 std::stringstream error;
92 error << "Expected Destination Block, but Block is of a different type: #"
93 << m_wire.type();
94 BOOST_THROW_EXCEPTION(Error(error.str()));
95 }
96
97 m_wire.parse();
98
99 ndn::Block::element_const_iterator val = m_wire.elements_begin();
100
101 if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
102 m_name.wireDecode(*val);
103 ++val;
104 }
105 else {
106 BOOST_THROW_EXCEPTION(Error("Missing required Name field"));
107 }
108}
109
110std::ostream&
111operator<<(std::ostream& os, const Destination& Destination)
112{
113 os << "Destination: " << Destination.getName();
114 return os;
115}
116
117std::shared_ptr<Destination>
118makeDes(const RoutingTableEntry& rte)
119{
120 std::shared_ptr<Destination> desInfo = std::make_shared<Destination>();
121
122 desInfo->setName(rte.getDestination());
123
124 return desInfo;
125}
126
127} // namespace tlv
128} // namespace nlsr