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 | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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-entry.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<RoutingTable>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<RoutingTable>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, RoutingTable::Error>::value, |
| 34 | "RoutingTable::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | RoutingTable::RoutingTable() |
| 37 | : m_hasNexthops(false) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | RoutingTable::RoutingTable(const ndn::Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
| 45 | |
| 46 | bool |
| 47 | RoutingTable::hasNexthops() const |
| 48 | { |
| 49 | return m_hasNexthops; |
| 50 | } |
| 51 | |
| 52 | RoutingTable& |
| 53 | RoutingTable::addNexthops(const NextHop& nexthop) |
| 54 | { |
| 55 | m_nexthops.push_back(nexthop); |
| 56 | m_wire.reset(); |
| 57 | m_hasNexthops = true; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | RoutingTable& |
| 62 | RoutingTable::clearNexthops() |
| 63 | { |
| 64 | m_nexthops.clear(); |
| 65 | m_hasNexthops = false; |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | template<ndn::encoding::Tag TAG> |
| 70 | size_t |
| 71 | RoutingTable::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 Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 83 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTableEntry); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 84 | |
| 85 | return totalLength; |
| 86 | } |
| 87 | |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 88 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTable); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 89 | |
| 90 | const ndn::Block& |
| 91 | RoutingTable::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 | |
| 108 | void |
| 109 | RoutingTable::wireDecode(const ndn::Block& wire) |
| 110 | { |
| 111 | m_hasNexthops = false; |
| 112 | m_nexthops.clear(); |
| 113 | |
| 114 | m_wire = wire; |
| 115 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 116 | if (m_wire.type() != ndn::tlv::nlsr::RoutingTableEntry) { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 117 | 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 | |
| 149 | std::ostream& |
| 150 | operator<<(std::ostream& os, const RoutingTable& routingtable) |
| 151 | { |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 152 | os << routingtable.getDestination() << std::endl; |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 153 | os << "NexthopList(" << std::endl; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 154 | |
| 155 | for (const auto& rtentry : routingtable) { |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 156 | os << rtentry; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | os << ")"; |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 160 | return os; |
| 161 | } |
| 162 | |
| 163 | } // namespace tlv |
| 164 | } // namespace nlsr |