blob: 255d41806fb41e37f19b90301a1cc4affce4d445 [file] [log] [blame]
laqinfan35731852017-08-08 06:17:39 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 "routing-table-entry.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<RoutingTable>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<RoutingTable>));
33static_assert(std::is_base_of<ndn::tlv::Error, RoutingTable::Error>::value,
34 "RoutingTable::Error must inherit from tlv::Error");
35
36RoutingTable::RoutingTable()
37 : m_hasNexthops(false)
38{
39}
40
41RoutingTable::RoutingTable(const ndn::Block& block)
42{
43 wireDecode(block);
44}
45
46bool
47RoutingTable::hasNexthops() const
48{
49 return m_hasNexthops;
50}
51
52RoutingTable&
53RoutingTable::addNexthops(const NextHop& nexthop)
54{
55 m_nexthops.push_back(nexthop);
56 m_wire.reset();
57 m_hasNexthops = true;
58 return *this;
59}
60
61RoutingTable&
62RoutingTable::clearNexthops()
63{
64 m_nexthops.clear();
65 m_hasNexthops = false;
66 return *this;
67}
68
69template<ndn::encoding::Tag TAG>
70size_t
71RoutingTable::wireEncode(ndn::EncodingImpl<TAG>& block) const
72{
73 size_t totalLength = 0;
74
75 for (std::list<NextHop>::const_reverse_iterator it = m_nexthops.rbegin();
76 it != m_nexthops.rend(); ++it) {
77 totalLength += it->wireEncode(block);
78 }
79
80 totalLength += m_des.wireEncode(block);
81
82 totalLength += block.prependVarNumber(totalLength);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080083 totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTableEntry);
laqinfan35731852017-08-08 06:17:39 -050084
85 return totalLength;
86}
87
laqinfana073e2e2018-01-15 21:17:24 +000088NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTable);
laqinfan35731852017-08-08 06:17:39 -050089
90const ndn::Block&
91RoutingTable::wireEncode() const
92{
93 if (m_wire.hasWire()) {
94 return m_wire;
95 }
96
97 ndn::EncodingEstimator estimator;
98 size_t estimatedSize = wireEncode(estimator);
99
100 ndn::EncodingBuffer buffer(estimatedSize, 0);
101 wireEncode(buffer);
102
103 m_wire = buffer.block();
104
105 return m_wire;
106}
107
108void
109RoutingTable::wireDecode(const ndn::Block& wire)
110{
111 m_hasNexthops = false;
112 m_nexthops.clear();
113
114 m_wire = wire;
115
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800116 if (m_wire.type() != ndn::tlv::nlsr::RoutingTableEntry) {
laqinfan35731852017-08-08 06:17:39 -0500117 std::stringstream error;
118 error << "Expected RoutingTable Block, but Block is of a different type: #"
119 << m_wire.type();
120 BOOST_THROW_EXCEPTION(Error(error.str()));
121 }
122
123 m_wire.parse();
124
125 ndn::Block::element_const_iterator val = m_wire.elements_begin();
126
127 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Destination) {
128 m_des.wireDecode(*val);
129 ++val;
130 }
131 else {
132 BOOST_THROW_EXCEPTION(Error("Missing required destination field"));
133 }
134
135 for (; val != m_wire.elements_end(); ++val) {
136 if (val->type() == ndn::tlv::nlsr::NextHop) {
137 m_nexthops.push_back(NextHop(*val));
138 m_hasNexthops = true;
139 }
140 else {
141 std::stringstream error;
142 error << "Expected NextHop Block, but Block is of a different type: #"
143 << m_wire.type();
144 BOOST_THROW_EXCEPTION(Error(error.str()));
145 }
146 }
147}
148
149std::ostream&
150operator<<(std::ostream& os, const RoutingTable& routingtable)
151{
laqinfan35731852017-08-08 06:17:39 -0500152 os << routingtable.getDestination() << std::endl;
laqinfana073e2e2018-01-15 21:17:24 +0000153 os << "NexthopList(" << std::endl;
laqinfan35731852017-08-08 06:17:39 -0500154
155 for (const auto& rtentry : routingtable) {
laqinfana073e2e2018-01-15 21:17:24 +0000156 os << rtentry;
laqinfan35731852017-08-08 06:17:39 -0500157 }
158
159 os << ")";
laqinfan35731852017-08-08 06:17:39 -0500160 return os;
161}
162
163} // namespace tlv
164} // namespace nlsr