Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, The University of Memphis, |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 19 | */ |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 20 | |
| 21 | #include "routing-table-entry.hpp" |
| 22 | #include "nexthop-list.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 23 | #include "tlv-nlsr.hpp" |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 24 | |
| 25 | namespace nlsr { |
| 26 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 27 | template<ndn::encoding::Tag TAG> |
| 28 | size_t |
| 29 | RoutingTableEntry::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 30 | { |
| 31 | size_t totalLength = 0; |
| 32 | |
| 33 | for (auto it = m_nexthopList.rbegin(); it != m_nexthopList.rend(); ++it) { |
| 34 | totalLength += it->wireEncode(block); |
| 35 | } |
| 36 | |
| 37 | totalLength += m_destination.wireEncode(block); |
| 38 | |
| 39 | totalLength += block.prependVarNumber(totalLength); |
| 40 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTableEntry); |
| 41 | |
| 42 | return totalLength; |
| 43 | } |
| 44 | |
| 45 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableEntry); |
| 46 | |
| 47 | const ndn::Block& |
| 48 | RoutingTableEntry::wireEncode() const |
| 49 | { |
| 50 | if (m_wire.hasWire()) { |
| 51 | return m_wire; |
| 52 | } |
| 53 | |
| 54 | ndn::EncodingEstimator estimator; |
| 55 | size_t estimatedSize = wireEncode(estimator); |
| 56 | |
| 57 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 58 | wireEncode(buffer); |
| 59 | |
| 60 | m_wire = buffer.block(); |
| 61 | |
| 62 | return m_wire; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | RoutingTableEntry::wireDecode(const ndn::Block& wire) |
| 67 | { |
| 68 | m_nexthopList.clear(); |
| 69 | |
| 70 | m_wire = wire; |
| 71 | |
| 72 | if (m_wire.type() != ndn::tlv::nlsr::RoutingTableEntry) { |
| 73 | std::stringstream error; |
| 74 | error << "Expected RoutingTable Block, but Block is of a different type: #" |
| 75 | << m_wire.type(); |
| 76 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 77 | } |
| 78 | |
| 79 | m_wire.parse(); |
| 80 | |
| 81 | auto val = m_wire.elements_begin(); |
| 82 | |
| 83 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) { |
| 84 | m_destination.wireDecode(*val); |
| 85 | ++val; |
| 86 | } |
| 87 | else { |
| 88 | BOOST_THROW_EXCEPTION(Error("Missing required destination field")); |
| 89 | } |
| 90 | |
| 91 | for (; val != m_wire.elements_end(); ++val) { |
| 92 | if (val->type() == ndn::tlv::nlsr::NextHop) { |
| 93 | m_nexthopList.addNextHop(NextHop(*val)); |
| 94 | } |
| 95 | else { |
| 96 | std::stringstream error; |
| 97 | error << "Expected NextHop Block, but Block is of a different type: #" |
| 98 | << m_wire.type(); |
| 99 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 104 | std::ostream& |
| 105 | operator<<(std::ostream& os, const RoutingTableEntry& rte) |
| 106 | { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 107 | os << " Destination: " << rte.getDestination() << "\n" |
| 108 | << rte.getNexthopList() << "\n"; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 109 | |
| 110 | return os; |
| 111 | } |
| 112 | |
| 113 | } // namespace nlsr |