Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -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 | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 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 | */ |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 20 | |
| 21 | #include "nexthop.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 22 | #include "tlv-nlsr.hpp" |
| 23 | |
| 24 | #include <ndn-cxx/encoding/block-helpers.hpp> |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 25 | |
| 26 | namespace nlsr { |
| 27 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 28 | template<ndn::encoding::Tag TAG> |
| 29 | size_t |
| 30 | NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 31 | { |
| 32 | size_t totalLength = 0; |
| 33 | |
| 34 | totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::CostDouble, m_routeCost); |
| 35 | totalLength += ndn::encoding::prependStringBlock(block, ndn::tlv::nlsr::Uri, m_connectingFaceUri); |
| 36 | |
| 37 | totalLength += block.prependVarNumber(totalLength); |
| 38 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::NextHop); |
| 39 | |
| 40 | return totalLength; |
| 41 | } |
| 42 | |
| 43 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHop); |
| 44 | |
| 45 | const ndn::Block& |
| 46 | NextHop::wireEncode() const |
| 47 | { |
| 48 | if (m_wire.hasWire()) { |
| 49 | return m_wire; |
| 50 | } |
| 51 | |
| 52 | ndn::EncodingEstimator estimator; |
| 53 | size_t estimatedSize = wireEncode(estimator); |
| 54 | |
| 55 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 56 | wireEncode(buffer); |
| 57 | |
| 58 | m_wire = buffer.block(); |
| 59 | |
| 60 | return m_wire; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | NextHop::wireDecode(const ndn::Block& wire) |
| 65 | { |
| 66 | m_connectingFaceUri = ""; |
| 67 | m_routeCost = 0; |
| 68 | |
| 69 | m_wire = wire; |
| 70 | |
| 71 | if (m_wire.type() != ndn::tlv::nlsr::NextHop) { |
| 72 | std::stringstream error; |
| 73 | error << "Expected NextHop Block, but Block is of a different type: #" |
| 74 | << m_wire.type(); |
| 75 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 76 | } |
| 77 | |
| 78 | m_wire.parse(); |
| 79 | |
| 80 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 81 | |
| 82 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) { |
| 83 | m_connectingFaceUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 84 | ++val; |
| 85 | } |
| 86 | else { |
| 87 | BOOST_THROW_EXCEPTION(Error("Missing required Uri field")); |
| 88 | } |
| 89 | |
| 90 | m_routeCost = ndn::encoding::readDouble(*val); |
| 91 | } |
| 92 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 93 | bool |
| 94 | operator==(const NextHop& lhs, const NextHop& rhs) |
| 95 | { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 96 | return (lhs.getRouteCostAsAdjustedInteger() == rhs.getRouteCostAsAdjustedInteger()) && |
| 97 | (lhs.getConnectingFaceUri() == rhs.getConnectingFaceUri()); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 100 | std::ostream& |
| 101 | operator<<(std::ostream& os, const NextHop& hop) |
| 102 | { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 103 | os << "NextHop(Uri: " << hop.getConnectingFaceUri() << ", Cost: " << hop.getRouteCost() << ")"; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 104 | return os; |
| 105 | } |
| 106 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 107 | } // namespace nlsr |