nexthop.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, The University of Memphis,
4  * Regents of the University of California
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/>.
19  */
20 
21 #ifndef NLSR_ROUTE_NEXTHOP_HPP
22 #define NLSR_ROUTE_NEXTHOP_HPP
23 
24 #include "test-access-control.hpp"
25 
26 #include <ndn-cxx/encoding/block.hpp>
27 #include <ndn-cxx/encoding/encoding-buffer.hpp>
28 #include <ndn-cxx/encoding/tlv.hpp>
29 
30 #include <cmath>
31 #include <ostream>
32 
33 namespace nlsr {
34 
43 class NextHop
44 {
45 public:
46  using Error = ndn::tlv::Error;
47 
49  : m_connectingFaceUri()
50  , m_routeCost(0)
51  , m_isHyperbolic(false)
52  {
53  }
54 
55  NextHop(const std::string& cfu, double rc)
56  : m_connectingFaceUri(cfu)
57  , m_routeCost(rc)
58  , m_isHyperbolic(false)
59  {
60  }
61 
62  NextHop(const ndn::Block& block)
63  {
64  wireDecode(block);
65  }
66 
67  const std::string&
69  {
70  return m_connectingFaceUri;
71  }
72 
73  void
74  setConnectingFaceUri(const std::string& cfu)
75  {
76  m_connectingFaceUri = cfu;
77  }
78 
79  uint64_t
81  {
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  }
91  }
92 
93  double
94  getRouteCost() const
95  {
96  return m_routeCost;
97  }
98 
99  void
100  setRouteCost(const double rc)
101  {
102  m_routeCost = rc;
103  }
104 
105  void
107  {
108  m_isHyperbolic = b;
109  }
110 
111  bool
112  isHyperbolic() const
113  {
114  return m_isHyperbolic;
115  }
116 
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 
127 private:
128  std::string m_connectingFaceUri;
129  double m_routeCost;
130  bool m_isHyperbolic;
131 
132  mutable ndn::Block m_wire;
133 
145  static constexpr uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000;
146 };
147 
149 
150 bool
151 operator==(const NextHop& lhs, const NextHop& rhs);
152 
153 std::ostream&
154 operator<<(std::ostream& os, const NextHop& hop);
155 
156 } // namespace nlsr
157 
158 #endif // NLSR_ROUTE_NEXTHOP_HPP
Data abstraction for Nexthop.
Definition: nexthop.hpp:44
ndn::tlv::Error Error
Definition: nexthop.hpp:46
void setRouteCost(const double rc)
Definition: nexthop.hpp:100
NextHop(const std::string &cfu, double rc)
Definition: nexthop.hpp:55
void setConnectingFaceUri(const std::string &cfu)
Definition: nexthop.hpp:74
const ndn::Block & wireEncode() const
Definition: nexthop.cpp:46
void wireDecode(const ndn::Block &wire)
Definition: nexthop.cpp:64
const std::string & getConnectingFaceUri() const
Definition: nexthop.hpp:68
double getRouteCost() const
Definition: nexthop.hpp:94
void setHyperbolic(bool b)
Definition: nexthop.hpp:106
uint64_t getRouteCostAsAdjustedInteger() const
Definition: nexthop.hpp:80
bool isHyperbolic() const
Definition: nexthop.hpp:112
NextHop(const ndn::Block &block)
Definition: nexthop.hpp:62
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176
bool operator==(const NamePrefixTableEntry &lhs, const NamePrefixTableEntry &rhs)
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE