laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, The University of Memphis, |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 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 "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 | |
| 28 | namespace nlsr { |
| 29 | namespace tlv { |
| 30 | |
| 31 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<RoutingTableStatus>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<RoutingTableStatus>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, RoutingTableStatus::Error>::value, |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 34 | "RoutingTableStatus::Error must inherit from tlv::Error"); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 35 | |
| 36 | RoutingTableStatus::RoutingTableStatus() |
| 37 | : m_hasRoutingtable(false) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | RoutingTableStatus::RoutingTableStatus(const ndn::Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
| 45 | |
| 46 | RoutingTableStatus& |
| 47 | RoutingTableStatus::addRoutingTable(const RoutingTable& routetable) |
| 48 | { |
| 49 | m_routingtables.push_back(routetable); |
| 50 | m_wire.reset(); |
| 51 | m_hasRoutingtable = true; |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | RoutingTableStatus& |
| 56 | RoutingTableStatus::clearRoutingTable() |
| 57 | { |
| 58 | m_routingtables.clear(); |
| 59 | m_hasRoutingtable = false; |
| 60 | return *this; |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | RoutingTableStatus::hasRoutingTable() |
| 65 | { |
| 66 | return m_hasRoutingtable; |
| 67 | } |
| 68 | |
| 69 | template<ndn::encoding::Tag TAG> |
| 70 | size_t |
| 71 | RoutingTableStatus::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 | |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 86 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 87 | |
| 88 | const ndn::Block& |
| 89 | RoutingTableStatus::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 | |
| 106 | void |
| 107 | RoutingTableStatus::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 | |
| 139 | std::ostream& |
| 140 | operator<<(std::ostream& os, const RoutingTableStatus& rtStatus) |
| 141 | { |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame^] | 142 | os << "Routing Table Status: " << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 143 | |
| 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 |