laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, The University of Memphis, |
| 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); |
| 83 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::RouteTableEntry); |
| 84 | |
| 85 | return totalLength; |
| 86 | } |
| 87 | |
| 88 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(RoutingTable); |
| 89 | |
| 90 | template size_t |
| 91 | RoutingTable::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& encoder) const; |
| 92 | |
| 93 | template size_t |
| 94 | RoutingTable::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& encoder) const; |
| 95 | |
| 96 | const ndn::Block& |
| 97 | RoutingTable::wireEncode() const |
| 98 | { |
| 99 | if (m_wire.hasWire()) { |
| 100 | return m_wire; |
| 101 | } |
| 102 | |
| 103 | ndn::EncodingEstimator estimator; |
| 104 | size_t estimatedSize = wireEncode(estimator); |
| 105 | |
| 106 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 107 | wireEncode(buffer); |
| 108 | |
| 109 | m_wire = buffer.block(); |
| 110 | |
| 111 | return m_wire; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | RoutingTable::wireDecode(const ndn::Block& wire) |
| 116 | { |
| 117 | m_hasNexthops = false; |
| 118 | m_nexthops.clear(); |
| 119 | |
| 120 | m_wire = wire; |
| 121 | |
| 122 | if (m_wire.type() != ndn::tlv::nlsr::RouteTableEntry) { |
| 123 | std::stringstream error; |
| 124 | error << "Expected RoutingTable Block, but Block is of a different type: #" |
| 125 | << m_wire.type(); |
| 126 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 127 | } |
| 128 | |
| 129 | m_wire.parse(); |
| 130 | |
| 131 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 132 | |
| 133 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Destination) { |
| 134 | m_des.wireDecode(*val); |
| 135 | ++val; |
| 136 | } |
| 137 | else { |
| 138 | BOOST_THROW_EXCEPTION(Error("Missing required destination field")); |
| 139 | } |
| 140 | |
| 141 | for (; val != m_wire.elements_end(); ++val) { |
| 142 | if (val->type() == ndn::tlv::nlsr::NextHop) { |
| 143 | m_nexthops.push_back(NextHop(*val)); |
| 144 | m_hasNexthops = true; |
| 145 | } |
| 146 | else { |
| 147 | std::stringstream error; |
| 148 | error << "Expected NextHop Block, but Block is of a different type: #" |
| 149 | << m_wire.type(); |
| 150 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | std::ostream& |
| 156 | operator<<(std::ostream& os, const RoutingTable& routingtable) |
| 157 | { |
| 158 | os << "Routing Table: " << std::endl; |
| 159 | os << routingtable.getDestination() << std::endl; |
| 160 | os << "Nexthops: NexthopList(" << std::endl; |
| 161 | |
| 162 | for (const auto& rtentry : routingtable) { |
| 163 | os << rtentry << std::endl; |
| 164 | } |
| 165 | |
| 166 | os << ")"; |
| 167 | |
| 168 | return os; |
| 169 | } |
| 170 | |
| 171 | } // namespace tlv |
| 172 | } // namespace nlsr |