blob: 7f91cfe7c4c2b42785327af360788fdec6ba9f70 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
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
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070026#include <ndn-cxx/encoding/block.hpp>
27#include <ndn-cxx/encoding/encoding-buffer.hpp>
28#include <ndn-cxx/encoding/tlv.hpp>
29
akmhoque53353462014-04-22 08:43:45 -050030#include <iostream>
akmhoque102aea42014-08-04 10:22:12 -050031#include <cmath>
akmhoquefdbddb12014-05-02 18:35:19 -050032#include <boost/cstdint.hpp>
akmhoque53353462014-04-22 08:43:45 -050033
34namespace nlsr {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080035
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070036/*! \brief Data abstraction for Nexthop
37 *
38 * NextHop := NEXTHOP-TYPE TLV-LENGTH
39 * Uri
40 * Cost
41 *
42 * \sa https://redmine.named-data.net/projects/nlsr/wiki/Routing_Table_Dataset
43 */
akmhoque53353462014-04-22 08:43:45 -050044class NextHop
45{
46public:
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070047 using Error = ndn::tlv::Error;
48
akmhoque53353462014-04-22 08:43:45 -050049 NextHop()
akmhoque157b0a42014-05-13 00:26:37 -050050 : m_connectingFaceUri()
akmhoque53353462014-04-22 08:43:45 -050051 , m_routeCost(0)
Vince Lehman20fe4a92014-09-09 15:57:59 -050052 , m_isHyperbolic(false)
akmhoque53353462014-04-22 08:43:45 -050053 {
54 }
55
akmhoque157b0a42014-05-13 00:26:37 -050056 NextHop(const std::string& cfu, double rc)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070057 : m_connectingFaceUri(cfu)
58 , m_routeCost(rc)
59 , m_isHyperbolic(false)
akmhoque53353462014-04-22 08:43:45 -050060 {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070061 }
62
63 NextHop(const ndn::Block& block)
64 {
65 wireDecode(block);
akmhoque53353462014-04-22 08:43:45 -050066 }
67
akmhoque157b0a42014-05-13 00:26:37 -050068 const std::string&
69 getConnectingFaceUri() const
akmhoque53353462014-04-22 08:43:45 -050070 {
akmhoque157b0a42014-05-13 00:26:37 -050071 return m_connectingFaceUri;
akmhoque53353462014-04-22 08:43:45 -050072 }
73
74 void
akmhoque157b0a42014-05-13 00:26:37 -050075 setConnectingFaceUri(const std::string& cfu)
akmhoque53353462014-04-22 08:43:45 -050076 {
akmhoque157b0a42014-05-13 00:26:37 -050077 m_connectingFaceUri = cfu;
akmhoque53353462014-04-22 08:43:45 -050078 }
79
akmhoque102aea42014-08-04 10:22:12 -050080 uint64_t
Vince Lehman145064a2014-08-23 11:44:16 -050081 getRouteCostAsAdjustedInteger() const
82 {
Vince Lehman20fe4a92014-09-09 15:57:59 -050083 if (m_isHyperbolic) {
84 // Round the cost to better preserve decimal cost differences
85 // e.g. Without rounding: 12.3456 > 12.3454 -> 12345 = 12345
86 // With rounding: 12.3456 > 12.3454 -> 12346 > 12345
87 return static_cast<uint64_t>(round(m_routeCost*HYPERBOLIC_COST_ADJUSTMENT_FACTOR));
88 }
89 else {
90 return static_cast<uint64_t>(m_routeCost);
91 }
Vince Lehman145064a2014-08-23 11:44:16 -050092 }
93
94 double
akmhoque53353462014-04-22 08:43:45 -050095 getRouteCost() const
96 {
Vince Lehman145064a2014-08-23 11:44:16 -050097 return m_routeCost;
akmhoque53353462014-04-22 08:43:45 -050098 }
99
100 void
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500101 setRouteCost(const double rc)
akmhoque53353462014-04-22 08:43:45 -0500102 {
103 m_routeCost = rc;
104 }
105
Vince Lehman20fe4a92014-09-09 15:57:59 -0500106 void
107 setHyperbolic(bool b)
108 {
109 m_isHyperbolic = b;
110 }
111
112 bool
113 isHyperbolic() const
114 {
115 return m_isHyperbolic;
116 }
117
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700118 template<ndn::encoding::Tag TAG>
119 size_t
120 wireEncode(ndn::EncodingImpl<TAG>& block) const;
121
122 const ndn::Block&
123 wireEncode() const;
124
125 void
126 wireDecode(const ndn::Block& wire);
127
akmhoque53353462014-04-22 08:43:45 -0500128private:
akmhoque157b0a42014-05-13 00:26:37 -0500129 std::string m_connectingFaceUri;
akmhoque53353462014-04-22 08:43:45 -0500130 double m_routeCost;
Vince Lehman20fe4a92014-09-09 15:57:59 -0500131 bool m_isHyperbolic;
132
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700133 mutable ndn::Block m_wire;
134
Vince Lehman20fe4a92014-09-09 15:57:59 -0500135PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick G97e34942016-07-11 14:46:27 -0500136 /*! \brief Used to adjust floating point route costs to integers
137 Since NFD uses integer route costs in the FIB, hyperbolic paths with similar route costs
138 will be rounded to integers and installed with identical nexthop costs.
139
140 e.g. costs 12.34 and 12.35 will be equal in NFD's FIB
141
142 This multiplier is used to differentiate similar route costs in the FIB.
143
144 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 -0500145 */
146 static const uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000;
akmhoque53353462014-04-22 08:43:45 -0500147};
148
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700149NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
150
Nick Gordonb50e51b2016-07-22 16:05:57 -0500151bool
152operator==(const NextHop& lhs, const NextHop& rhs);
153
Vince Lehman9a709032014-09-13 16:28:07 -0500154std::ostream&
155operator<<(std::ostream& os, const NextHop& hop);
156
Nick Gordonfad8e252016-08-11 14:21:38 -0500157} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500158
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800159#endif // NLSR_ROUTE_NEXTHOP_HPP