blob: 508bf1acd27b7d8e6bb09c4187e1cd284eba2ab9 [file] [log] [blame]
laqinfan35731852017-08-08 06:17:39 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Tianxing Ma9ea36392018-10-05 14:32:55 -05002/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
laqinfan35731852017-08-08 06:17:39 -05004 * 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"
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<NextHop>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NextHop>));
33static_assert(std::is_base_of<ndn::tlv::Error, NextHop::Error>::value,
34 "NextHop::Error must inherit from tlv::Error");
35
36NextHop::NextHop()
37 : m_cost(0)
38{
39}
40
41NextHop::NextHop(const ndn::Block& block)
42{
43 wireDecode(block);
44}
45
46template<ndn::encoding::Tag TAG>
47size_t
48NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
49{
50 size_t totalLength = 0;
51
Tianxing Ma9ea36392018-10-05 14:32:55 -050052 totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::CostDouble, m_cost);
laqinfan35731852017-08-08 06:17:39 -050053
54 totalLength += block.prependByteArrayBlock(
55 ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
56
57 totalLength += block.prependVarNumber(totalLength);
58 totalLength += block.prependVarNumber(ndn::tlv::nlsr::NextHop);
59
60 return totalLength;
61}
62
laqinfana073e2e2018-01-15 21:17:24 +000063NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
laqinfan35731852017-08-08 06:17:39 -050064
65const ndn::Block&
66NextHop::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
84NextHop::wireDecode(const ndn::Block& wire)
85{
86 m_uri = "";
87 m_cost = 0;
88
89 m_wire = wire;
90
91 if (m_wire.type() != ndn::tlv::nlsr::NextHop) {
92 std::stringstream error;
93 error << "Expected NextHop Block, but Block is of a different type: #"
94 << m_wire.type();
95 BOOST_THROW_EXCEPTION(Error(error.str()));
96 }
97
98 m_wire.parse();
99
100 ndn::Block::element_const_iterator val = m_wire.elements_begin();
101
102 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
103 m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
104 ++val;
105 }
106 else {
107 BOOST_THROW_EXCEPTION(Error("Missing required Uri field"));
108 }
109
Tianxing Ma9ea36392018-10-05 14:32:55 -0500110 m_cost = ndn::encoding::readDouble(*val);
laqinfan35731852017-08-08 06:17:39 -0500111}
112
113std::ostream&
114operator<<(std::ostream& os, const NextHop& nexthop)
115{
116 os << "NextHop("
117 << "Uri: " << nexthop.getUri() << ", "<< "Cost: " << nexthop.getCost() << ")" << std::endl;
118
119 return os;
120}
121
122} // namespace tlv
123} // namespace nlsr