blob: aeeb641e609abbd0740bd585672f58a026ce11fb [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Junxiao Shi6593a432023-08-21 10:50:28 +00003 * Copyright (c) 2014-2023, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
Vince Lehman9a709032014-09-13 16:28:07 -05005 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
Vince Lehman9a709032014-09-13 16:28:07 -050020
21#include "nexthop.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070022#include "tlv-nlsr.hpp"
23
24#include <ndn-cxx/encoding/block-helpers.hpp>
Vince Lehman9a709032014-09-13 16:28:07 -050025
26namespace nlsr {
27
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070028template<ndn::encoding::Tag TAG>
29size_t
30NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
31{
32 size_t totalLength = 0;
33
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040034 totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::CostDouble, m_routeCost);
Junxiao Shi6593a432023-08-21 10:50:28 +000035 totalLength += ndn::encoding::prependStringBlock(block, nlsr::tlv::Uri, m_connectingFaceUri.toString());
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070036
37 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040038 totalLength += block.prependVarNumber(nlsr::tlv::NextHop);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070039
40 return totalLength;
41}
42
43NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
44
45const ndn::Block&
46NextHop::wireEncode() const
47{
48 if (m_wire.hasWire()) {
49 return m_wire;
50 }
51
52 ndn::EncodingEstimator estimator;
53 size_t estimatedSize = wireEncode(estimator);
54
55 ndn::EncodingBuffer buffer(estimatedSize, 0);
56 wireEncode(buffer);
57
58 m_wire = buffer.block();
59
60 return m_wire;
61}
62
63void
64NextHop::wireDecode(const ndn::Block& wire)
65{
Junxiao Shi6593a432023-08-21 10:50:28 +000066 m_connectingFaceUri = {};
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070067 m_routeCost = 0;
68
69 m_wire = wire;
70
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040071 if (m_wire.type() != nlsr::tlv::NextHop) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050072 NDN_THROW(Error("NextHop", m_wire.type()));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070073 }
74
75 m_wire.parse();
76
Davide Pesaventod90338d2021-01-07 17:50:05 -050077 auto val = m_wire.elements_begin();
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070078
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040079 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Uri) {
Junxiao Shi6593a432023-08-21 10:50:28 +000080 try {
81 m_connectingFaceUri = ndn::FaceUri(ndn::encoding::readString(*val));
82 }
83 catch (const ndn::FaceUri::Error& e) {
84 NDN_THROW_NESTED(Error("Invalid Uri"));
85 }
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070086 ++val;
87 }
88 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -050089 NDN_THROW(Error("Missing required Uri field"));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070090 }
91
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040092 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::CostDouble) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050093 m_routeCost = ndn::encoding::readDouble(*val);
94 ++val;
95 }
96 else {
97 NDN_THROW(Error("Missing required CostDouble field"));
98 }
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070099}
100
Vince Lehman9a709032014-09-13 16:28:07 -0500101std::ostream&
102operator<<(std::ostream& os, const NextHop& hop)
103{
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700104 os << "NextHop(Uri: " << hop.getConnectingFaceUri() << ", Cost: " << hop.getRouteCost() << ")";
Vince Lehman9a709032014-09-13 16:28:07 -0500105 return os;
106}
107
Nick Gordonfad8e252016-08-11 14:21:38 -0500108} // namespace nlsr