blob: 0aa56406d7cbb2da451e09f43bbdcfe11feb413f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05003 * Copyright (c) 2014-2016, The University of Memphis,
4 * 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 **/
akmhoque53353462014-04-22 08:43:45 -050020#ifndef NLSR_NEXTHOP_HPP
21#define NLSR_NEXTHOP_HPP
22
Vince Lehman20fe4a92014-09-09 15:57:59 -050023#include "test-access-control.hpp"
24
akmhoque53353462014-04-22 08:43:45 -050025#include <iostream>
akmhoque102aea42014-08-04 10:22:12 -050026#include <cmath>
akmhoquefdbddb12014-05-02 18:35:19 -050027#include <boost/cstdint.hpp>
akmhoque53353462014-04-22 08:43:45 -050028
29namespace nlsr {
30class NextHop
31{
32public:
33 NextHop()
akmhoque157b0a42014-05-13 00:26:37 -050034 : m_connectingFaceUri()
akmhoque53353462014-04-22 08:43:45 -050035 , m_routeCost(0)
Vince Lehman20fe4a92014-09-09 15:57:59 -050036 , m_isHyperbolic(false)
akmhoque53353462014-04-22 08:43:45 -050037 {
38 }
39
akmhoque157b0a42014-05-13 00:26:37 -050040 NextHop(const std::string& cfu, double rc)
Vince Lehman20fe4a92014-09-09 15:57:59 -050041 : m_isHyperbolic(false)
akmhoque53353462014-04-22 08:43:45 -050042 {
akmhoque157b0a42014-05-13 00:26:37 -050043 m_connectingFaceUri = cfu;
akmhoque53353462014-04-22 08:43:45 -050044 m_routeCost = rc;
45 }
46
akmhoque157b0a42014-05-13 00:26:37 -050047 const std::string&
48 getConnectingFaceUri() const
akmhoque53353462014-04-22 08:43:45 -050049 {
akmhoque157b0a42014-05-13 00:26:37 -050050 return m_connectingFaceUri;
akmhoque53353462014-04-22 08:43:45 -050051 }
52
53 void
akmhoque157b0a42014-05-13 00:26:37 -050054 setConnectingFaceUri(const std::string& cfu)
akmhoque53353462014-04-22 08:43:45 -050055 {
akmhoque157b0a42014-05-13 00:26:37 -050056 m_connectingFaceUri = cfu;
akmhoque53353462014-04-22 08:43:45 -050057 }
58
akmhoque102aea42014-08-04 10:22:12 -050059 uint64_t
Vince Lehman145064a2014-08-23 11:44:16 -050060 getRouteCostAsAdjustedInteger() const
61 {
Vince Lehman20fe4a92014-09-09 15:57:59 -050062 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 Lehman145064a2014-08-23 11:44:16 -050071 }
72
73 double
akmhoque53353462014-04-22 08:43:45 -050074 getRouteCost() const
75 {
Vince Lehman145064a2014-08-23 11:44:16 -050076 return m_routeCost;
akmhoque53353462014-04-22 08:43:45 -050077 }
78
79 void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050080 setRouteCost(const double rc)
akmhoque53353462014-04-22 08:43:45 -050081 {
82 m_routeCost = rc;
83 }
84
Vince Lehman20fe4a92014-09-09 15:57:59 -050085 void
86 setHyperbolic(bool b)
87 {
88 m_isHyperbolic = b;
89 }
90
91 bool
92 isHyperbolic() const
93 {
94 return m_isHyperbolic;
95 }
96
akmhoque53353462014-04-22 08:43:45 -050097private:
akmhoque157b0a42014-05-13 00:26:37 -050098 std::string m_connectingFaceUri;
akmhoque53353462014-04-22 08:43:45 -050099 double m_routeCost;
Vince Lehman20fe4a92014-09-09 15:57:59 -0500100 bool m_isHyperbolic;
101
102PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick G97e34942016-07-11 14:46:27 -0500103 /*! \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 Lehman20fe4a92014-09-09 15:57:59 -0500112 */
113 static const uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000;
akmhoque53353462014-04-22 08:43:45 -0500114};
115
Vince Lehman9a709032014-09-13 16:28:07 -0500116std::ostream&
117operator<<(std::ostream& os, const NextHop& hop);
118
Nick Gordonfad8e252016-08-11 14:21:38 -0500119} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500120
121#endif //NLSR_NEXTHOP_HPP