laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 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 "nexthop.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<NextHop>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NextHop>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, NextHop::Error>::value, |
| 34 | "NextHop::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | NextHop::NextHop() |
| 37 | : m_cost(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | NextHop::NextHop(const ndn::Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
| 45 | |
| 46 | template<ndn::encoding::Tag TAG> |
| 47 | size_t |
| 48 | NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 49 | { |
| 50 | size_t totalLength = 0; |
| 51 | |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 52 | totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::CostDouble, m_cost); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 53 | |
| 54 | totalLength += block.prependByteArrayBlock( |
| 55 | ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size()); |
| 56 | |
| 57 | totalLength += block.prependVarNumber(totalLength); |
| 58 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::NextHop); |
| 59 | |
| 60 | return totalLength; |
| 61 | } |
| 62 | |
laqinfan | a073e2e | 2018-01-15 21:17:24 +0000 | [diff] [blame] | 63 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHop); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 64 | |
| 65 | const ndn::Block& |
| 66 | NextHop::wireEncode() const |
| 67 | { |
| 68 | if (m_wire.hasWire()) { |
| 69 | return m_wire; |
| 70 | } |
| 71 | |
| 72 | ndn::EncodingEstimator estimator; |
| 73 | size_t estimatedSize = wireEncode(estimator); |
| 74 | |
| 75 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 76 | wireEncode(buffer); |
| 77 | |
| 78 | m_wire = buffer.block(); |
| 79 | |
| 80 | return m_wire; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | NextHop::wireDecode(const ndn::Block& wire) |
| 85 | { |
| 86 | m_uri = ""; |
| 87 | m_cost = 0; |
| 88 | |
| 89 | m_wire = wire; |
| 90 | |
| 91 | if (m_wire.type() != ndn::tlv::nlsr::NextHop) { |
| 92 | std::stringstream error; |
| 93 | error << "Expected NextHop Block, but Block is of a different type: #" |
| 94 | << m_wire.type(); |
| 95 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 96 | } |
| 97 | |
| 98 | m_wire.parse(); |
| 99 | |
| 100 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 101 | |
| 102 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) { |
| 103 | m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 104 | ++val; |
| 105 | } |
| 106 | else { |
| 107 | BOOST_THROW_EXCEPTION(Error("Missing required Uri field")); |
| 108 | } |
| 109 | |
Tianxing Ma | 9ea3639 | 2018-10-05 14:32:55 -0500 | [diff] [blame] | 110 | m_cost = ndn::encoding::readDouble(*val); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | std::ostream& |
| 114 | operator<<(std::ostream& os, const NextHop& nexthop) |
| 115 | { |
| 116 | os << "NextHop(" |
| 117 | << "Uri: " << nexthop.getUri() << ", "<< "Cost: " << nexthop.getCost() << ")" << std::endl; |
| 118 | |
| 119 | return os; |
| 120 | } |
| 121 | |
| 122 | } // namespace tlv |
| 123 | } // namespace nlsr |