blob: a35f88cd7ad28649b61c1a1f0067f3dbde90132e [file] [log] [blame]
laqinfan35731852017-08-08 06:17:39 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -05003 * Copyright (c) 2014-2019, 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-status.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<RoutingTableStatus>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<RoutingTableStatus>));
33static_assert(std::is_base_of<ndn::tlv::Error, RoutingTableStatus::Error>::value,
laqinfana073e2e2018-01-15 21:17:24 +000034 "RoutingTableStatus::Error must inherit from tlv::Error");
laqinfan35731852017-08-08 06:17:39 -050035
36RoutingTableStatus::RoutingTableStatus()
37 : m_hasRoutingtable(false)
38{
39}
40
41RoutingTableStatus::RoutingTableStatus(const ndn::Block& block)
42{
43 wireDecode(block);
44}
45
46RoutingTableStatus&
47RoutingTableStatus::addRoutingTable(const RoutingTable& routetable)
48{
49 m_routingtables.push_back(routetable);
50 m_wire.reset();
51 m_hasRoutingtable = true;
52 return *this;
53}
54
55RoutingTableStatus&
56RoutingTableStatus::clearRoutingTable()
57{
58 m_routingtables.clear();
59 m_hasRoutingtable = false;
60 return *this;
61}
62
63bool
64RoutingTableStatus::hasRoutingTable()
65{
66 return m_hasRoutingtable;
67}
68
69template<ndn::encoding::Tag TAG>
70size_t
71RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
72{
73 size_t totalLength = 0;
74
75 for (std::list<RoutingTable>::const_reverse_iterator it = m_routingtables.rbegin();
76 it != m_routingtables.rend(); ++it) {
77 totalLength += it->wireEncode(block);
78 }
79
80 totalLength += block.prependVarNumber(totalLength);
81 totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTable);
82
83 return totalLength;
84}
85
laqinfana073e2e2018-01-15 21:17:24 +000086NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus);
laqinfan35731852017-08-08 06:17:39 -050087
88const ndn::Block&
89RoutingTableStatus::wireEncode() const
90{
91 if (m_wire.hasWire()) {
92 return m_wire;
93 }
94
95 ndn::EncodingEstimator estimator;
96 size_t estimatedSize = wireEncode(estimator);
97
98 ndn::EncodingBuffer buffer(estimatedSize, 0);
99 wireEncode(buffer);
100
101 m_wire = buffer.block();
102
103 return m_wire;
104}
105
106void
107RoutingTableStatus::wireDecode(const ndn::Block& wire)
108{
109 m_routingtables.clear();
110
111 m_hasRoutingtable = false;
112
113 m_wire = wire;
114
115 if (m_wire.type() != ndn::tlv::nlsr::RoutingTable) {
116 std::stringstream error;
117 error << "Expected RoutingTableStatus Block, but Block is of a different type: #"
118 << m_wire.type();
119 BOOST_THROW_EXCEPTION(Error(error.str()));
120 }
121
122 m_wire.parse();
123
124 ndn::Block::element_const_iterator val = m_wire.elements_begin();
125
126 for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::RouteTableEntry; ++val) {
127 m_routingtables.push_back(RoutingTable(*val));
128 m_hasRoutingtable = true;
129 }
130
131 if (val != m_wire.elements_end()) {
132 std::stringstream error;
133 error << "Expected the end of elements, but Block is of a different type: #"
134 << val->type();
135 BOOST_THROW_EXCEPTION(Error(error.str()));
136 }
137}
138
139std::ostream&
140operator<<(std::ostream& os, const RoutingTableStatus& rtStatus)
141{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500142 os << "Routing Table Status: " << std::endl;
laqinfan35731852017-08-08 06:17:39 -0500143
144 bool isFirst = true;
145
146 for (const auto& routingtable : rtStatus.getRoutingtable()) {
147 if (isFirst) {
148 isFirst = false;
149 }
150 else {
151 os << ", ";
152 }
153
154 os << routingtable;
155 }
156
157 return os;
158}
159
160} // namespace tlv
161} // namespace nlsr