blob: e24df7b718cd34caec04e275c669cda20faa6c27 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080020
21#ifndef NLSR_ROUTE_NEXTHOP_HPP
22#define NLSR_ROUTE_NEXTHOP_HPP
akmhoque53353462014-04-22 08:43:45 -050023
Vince Lehman20fe4a92014-09-09 15:57:59 -050024#include "test-access-control.hpp"
25
akmhoque53353462014-04-22 08:43:45 -050026#include <iostream>
akmhoque102aea42014-08-04 10:22:12 -050027#include <cmath>
akmhoquefdbddb12014-05-02 18:35:19 -050028#include <boost/cstdint.hpp>
akmhoque53353462014-04-22 08:43:45 -050029
30namespace nlsr {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080031
akmhoque53353462014-04-22 08:43:45 -050032class NextHop
33{
34public:
35 NextHop()
akmhoque157b0a42014-05-13 00:26:37 -050036 : m_connectingFaceUri()
akmhoque53353462014-04-22 08:43:45 -050037 , m_routeCost(0)
Vince Lehman20fe4a92014-09-09 15:57:59 -050038 , m_isHyperbolic(false)
akmhoque53353462014-04-22 08:43:45 -050039 {
40 }
41
akmhoque157b0a42014-05-13 00:26:37 -050042 NextHop(const std::string& cfu, double rc)
Vince Lehman20fe4a92014-09-09 15:57:59 -050043 : m_isHyperbolic(false)
akmhoque53353462014-04-22 08:43:45 -050044 {
akmhoque157b0a42014-05-13 00:26:37 -050045 m_connectingFaceUri = cfu;
akmhoque53353462014-04-22 08:43:45 -050046 m_routeCost = rc;
47 }
48
akmhoque157b0a42014-05-13 00:26:37 -050049 const std::string&
50 getConnectingFaceUri() const
akmhoque53353462014-04-22 08:43:45 -050051 {
akmhoque157b0a42014-05-13 00:26:37 -050052 return m_connectingFaceUri;
akmhoque53353462014-04-22 08:43:45 -050053 }
54
55 void
akmhoque157b0a42014-05-13 00:26:37 -050056 setConnectingFaceUri(const std::string& cfu)
akmhoque53353462014-04-22 08:43:45 -050057 {
akmhoque157b0a42014-05-13 00:26:37 -050058 m_connectingFaceUri = cfu;
akmhoque53353462014-04-22 08:43:45 -050059 }
60
akmhoque102aea42014-08-04 10:22:12 -050061 uint64_t
Vince Lehman145064a2014-08-23 11:44:16 -050062 getRouteCostAsAdjustedInteger() const
63 {
Vince Lehman20fe4a92014-09-09 15:57:59 -050064 if (m_isHyperbolic) {
65 // Round the cost to better preserve decimal cost differences
66 // e.g. Without rounding: 12.3456 > 12.3454 -> 12345 = 12345
67 // With rounding: 12.3456 > 12.3454 -> 12346 > 12345
68 return static_cast<uint64_t>(round(m_routeCost*HYPERBOLIC_COST_ADJUSTMENT_FACTOR));
69 }
70 else {
71 return static_cast<uint64_t>(m_routeCost);
72 }
Vince Lehman145064a2014-08-23 11:44:16 -050073 }
74
75 double
akmhoque53353462014-04-22 08:43:45 -050076 getRouteCost() const
77 {
Vince Lehman145064a2014-08-23 11:44:16 -050078 return m_routeCost;
akmhoque53353462014-04-22 08:43:45 -050079 }
80
81 void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050082 setRouteCost(const double rc)
akmhoque53353462014-04-22 08:43:45 -050083 {
84 m_routeCost = rc;
85 }
86
Vince Lehman20fe4a92014-09-09 15:57:59 -050087 void
88 setHyperbolic(bool b)
89 {
90 m_isHyperbolic = b;
91 }
92
93 bool
94 isHyperbolic() const
95 {
96 return m_isHyperbolic;
97 }
98
akmhoque53353462014-04-22 08:43:45 -050099private:
akmhoque157b0a42014-05-13 00:26:37 -0500100 std::string m_connectingFaceUri;
akmhoque53353462014-04-22 08:43:45 -0500101 double m_routeCost;
Vince Lehman20fe4a92014-09-09 15:57:59 -0500102 bool m_isHyperbolic;
103
104PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick G97e34942016-07-11 14:46:27 -0500105 /*! \brief Used to adjust floating point route costs to integers
106 Since NFD uses integer route costs in the FIB, hyperbolic paths with similar route costs
107 will be rounded to integers and installed with identical nexthop costs.
108
109 e.g. costs 12.34 and 12.35 will be equal in NFD's FIB
110
111 This multiplier is used to differentiate similar route costs in the FIB.
112
113 e.g costs 12.34 and 12.35 will be installed into NFD's FIB as 12340 and 12350
Vince Lehman20fe4a92014-09-09 15:57:59 -0500114 */
115 static const uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000;
akmhoque53353462014-04-22 08:43:45 -0500116};
117
Nick Gordonb50e51b2016-07-22 16:05:57 -0500118bool
119operator==(const NextHop& lhs, const NextHop& rhs);
120
Vince Lehman9a709032014-09-13 16:28:07 -0500121std::ostream&
122operator<<(std::ostream& os, const NextHop& hop);
123
Nick Gordonfad8e252016-08-11 14:21:38 -0500124} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500125
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800126#endif // NLSR_ROUTE_NEXTHOP_HPP