akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, The University of Memphis, |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 19 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 20 | #ifndef NLSR_NEXTHOP_HPP |
| 21 | #define NLSR_NEXTHOP_HPP |
| 22 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 23 | #include "test-access-control.hpp" |
| 24 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | #include <iostream> |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 26 | #include <cmath> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 27 | #include <boost/cstdint.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
| 30 | class NextHop |
| 31 | { |
| 32 | public: |
| 33 | NextHop() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 34 | : m_connectingFaceUri() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | , m_routeCost(0) |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 36 | , m_isHyperbolic(false) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 40 | NextHop(const std::string& cfu, double rc) |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 41 | : m_isHyperbolic(false) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 42 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 43 | m_connectingFaceUri = cfu; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | m_routeCost = rc; |
| 45 | } |
| 46 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 47 | const std::string& |
| 48 | getConnectingFaceUri() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 49 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 50 | return m_connectingFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 54 | setConnectingFaceUri(const std::string& cfu) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 56 | m_connectingFaceUri = cfu; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | } |
| 58 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 59 | uint64_t |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 60 | getRouteCostAsAdjustedInteger() const |
| 61 | { |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 62 | if (m_isHyperbolic) { |
| 63 | // Round the cost to better preserve decimal cost differences |
| 64 | // e.g. Without rounding: 12.3456 > 12.3454 -> 12345 = 12345 |
| 65 | // With rounding: 12.3456 > 12.3454 -> 12346 > 12345 |
| 66 | return static_cast<uint64_t>(round(m_routeCost*HYPERBOLIC_COST_ADJUSTMENT_FACTOR)); |
| 67 | } |
| 68 | else { |
| 69 | return static_cast<uint64_t>(m_routeCost); |
| 70 | } |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | double |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | getRouteCost() const |
| 75 | { |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 76 | return m_routeCost; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 80 | setRouteCost(const double rc) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | { |
| 82 | m_routeCost = rc; |
| 83 | } |
| 84 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 85 | void |
| 86 | setHyperbolic(bool b) |
| 87 | { |
| 88 | m_isHyperbolic = b; |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | isHyperbolic() const |
| 93 | { |
| 94 | return m_isHyperbolic; |
| 95 | } |
| 96 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | private: |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 98 | std::string m_connectingFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | double m_routeCost; |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 100 | bool m_isHyperbolic; |
| 101 | |
| 102 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 103 | /*! \brief Used to adjust floating point route costs to integers |
| 104 | Since NFD uses integer route costs in the FIB, hyperbolic paths with similar route costs |
| 105 | will be rounded to integers and installed with identical nexthop costs. |
| 106 | |
| 107 | e.g. costs 12.34 and 12.35 will be equal in NFD's FIB |
| 108 | |
| 109 | This multiplier is used to differentiate similar route costs in the FIB. |
| 110 | |
| 111 | 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] | 112 | */ |
| 113 | static const uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | }; |
| 115 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame^] | 116 | bool |
| 117 | operator==(const NextHop& lhs, const NextHop& rhs); |
| 118 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 119 | std::ostream& |
| 120 | operator<<(std::ostream& os, const NextHop& hop); |
| 121 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 122 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | |
| 124 | #endif //NLSR_NEXTHOP_HPP |