akmhoque | 3d06e79 | 2014-05-27 16:23:20 -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 | /* |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -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 | */ |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 20 | |
| 21 | #ifndef NLSR_ROUTE_NEXTHOP_HPP |
| 22 | #define NLSR_ROUTE_NEXTHOP_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 24 | #include "test-access-control.hpp" |
| 25 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/encoding/block.hpp> |
| 27 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 28 | #include <ndn-cxx/encoding/tlv.hpp> |
| 29 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 30 | #include <cmath> |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 31 | #include <ostream> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | |
| 33 | namespace nlsr { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 34 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 35 | /*! \brief Data abstraction for Nexthop |
| 36 | * |
| 37 | * NextHop := NEXTHOP-TYPE TLV-LENGTH |
| 38 | * Uri |
| 39 | * Cost |
| 40 | * |
| 41 | * \sa https://redmine.named-data.net/projects/nlsr/wiki/Routing_Table_Dataset |
| 42 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | class NextHop |
| 44 | { |
| 45 | public: |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 46 | using Error = ndn::tlv::Error; |
| 47 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | NextHop() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 49 | : m_connectingFaceUri() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | , m_routeCost(0) |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 51 | , m_isHyperbolic(false) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 55 | NextHop(const std::string& cfu, double rc) |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 56 | : m_connectingFaceUri(cfu) |
| 57 | , m_routeCost(rc) |
| 58 | , m_isHyperbolic(false) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | NextHop(const ndn::Block& block) |
| 63 | { |
| 64 | wireDecode(block); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | } |
| 66 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 67 | const std::string& |
| 68 | getConnectingFaceUri() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 70 | return m_connectingFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 74 | setConnectingFaceUri(const std::string& cfu) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 75 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 76 | m_connectingFaceUri = cfu; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 79 | uint64_t |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 80 | getRouteCostAsAdjustedInteger() const |
| 81 | { |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 82 | if (m_isHyperbolic) { |
| 83 | // Round the cost to better preserve decimal cost differences |
| 84 | // e.g. Without rounding: 12.3456 > 12.3454 -> 12345 = 12345 |
| 85 | // With rounding: 12.3456 > 12.3454 -> 12346 > 12345 |
| 86 | return static_cast<uint64_t>(round(m_routeCost*HYPERBOLIC_COST_ADJUSTMENT_FACTOR)); |
| 87 | } |
| 88 | else { |
| 89 | return static_cast<uint64_t>(m_routeCost); |
| 90 | } |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | double |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | getRouteCost() const |
| 95 | { |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 96 | return m_routeCost; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 100 | setRouteCost(const double rc) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | { |
| 102 | m_routeCost = rc; |
| 103 | } |
| 104 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 105 | void |
| 106 | setHyperbolic(bool b) |
| 107 | { |
| 108 | m_isHyperbolic = b; |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | isHyperbolic() const |
| 113 | { |
| 114 | return m_isHyperbolic; |
| 115 | } |
| 116 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 117 | template<ndn::encoding::Tag TAG> |
| 118 | size_t |
| 119 | wireEncode(ndn::EncodingImpl<TAG>& block) const; |
| 120 | |
| 121 | const ndn::Block& |
| 122 | wireEncode() const; |
| 123 | |
| 124 | void |
| 125 | wireDecode(const ndn::Block& wire); |
| 126 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | private: |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 128 | std::string m_connectingFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | double m_routeCost; |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 130 | bool m_isHyperbolic; |
| 131 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 132 | mutable ndn::Block m_wire; |
| 133 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 134 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 135 | /*! \brief Used to adjust floating point route costs to integers |
| 136 | Since NFD uses integer route costs in the FIB, hyperbolic paths with similar route costs |
| 137 | will be rounded to integers and installed with identical nexthop costs. |
| 138 | |
| 139 | e.g. costs 12.34 and 12.35 will be equal in NFD's FIB |
| 140 | |
| 141 | This multiplier is used to differentiate similar route costs in the FIB. |
| 142 | |
| 143 | e.g costs 12.34 and 12.35 will be installed into NFD's FIB as 12340 and 12350 |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 144 | */ |
| 145 | static const uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 146 | }; |
| 147 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 148 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(NextHop); |
| 149 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 150 | bool |
| 151 | operator==(const NextHop& lhs, const NextHop& rhs); |
| 152 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 153 | std::ostream& |
| 154 | operator<<(std::ostream& os, const NextHop& hop); |
| 155 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 156 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 158 | #endif // NLSR_ROUTE_NEXTHOP_HPP |