blob: 2db9c3c8a5502d72b76a7be9d554e4fd891283f1 [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 "nexthop.hpp"
23#include "tlv-nlsr.hpp"
Ashlesh Gawande8df15982018-05-09 17:52:33 -050024#include "coordinate-lsa.hpp"
laqinfan35731852017-08-08 06:17:39 -050025
26#include <ndn-cxx/util/concepts.hpp>
27#include <ndn-cxx/encoding/block-helpers.hpp>
28
29namespace nlsr {
30namespace tlv {
31
32BOOST_CONCEPT_ASSERT((ndn::WireEncodable<NextHop>));
33BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NextHop>));
34static_assert(std::is_base_of<ndn::tlv::Error, NextHop::Error>::value,
35 "NextHop::Error must inherit from tlv::Error");
36
37NextHop::NextHop()
38 : m_cost(0)
39{
40}
41
42NextHop::NextHop(const ndn::Block& block)
43{
44 wireDecode(block);
45}
46
47template<ndn::encoding::Tag TAG>
48size_t
49NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
50{
51 size_t totalLength = 0;
52
Ashlesh Gawande8df15982018-05-09 17:52:33 -050053 totalLength += prependDouble(block, ndn::tlv::nlsr::Double, m_cost);
laqinfan35731852017-08-08 06:17:39 -050054
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
laqinfana073e2e2018-01-15 21:17:24 +000064NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
laqinfan35731852017-08-08 06:17:39 -050065
66const ndn::Block&
67NextHop::wireEncode() const
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
84void
85NextHop::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
Ashlesh Gawande8df15982018-05-09 17:52:33 -0500111 m_cost = ndn::tlv::nlsr::readDouble(*val);
laqinfan35731852017-08-08 06:17:39 -0500112}
113
114std::ostream&
115operator<<(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