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-2023, 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 #include <ndn-cxx/net/face-uri.hpp>
30 
31 #include <boost/operators.hpp>
32 
33 #include <cmath>
34 #include <ostream>
35 
36 namespace nlsr {
37 
46 class NextHop : private boost::totally_ordered<NextHop>
47 {
48 public:
49  using Error = ndn::tlv::Error;
50 
51  NextHop() = default;
52 
53  NextHop(const ndn::FaceUri& cfu, double rc)
54  : m_connectingFaceUri(cfu)
55  , m_routeCost(rc)
56  {
57  }
58 
59  explicit
60  NextHop(const ndn::Block& block)
61  {
62  wireDecode(block);
63  }
64 
65  const ndn::FaceUri&
67  {
68  return m_connectingFaceUri;
69  }
70 
71  void
72  setConnectingFaceUri(const ndn::FaceUri& cfu)
73  {
74  m_connectingFaceUri = cfu;
75  }
76 
77  uint64_t
79  {
80  if (m_isHyperbolic) {
81  // Round the cost to better preserve decimal cost differences
82  // e.g. Without rounding: 12.3456 > 12.3454 -> 12345 = 12345
83  // With rounding: 12.3456 > 12.3454 -> 12346 > 12345
84  return static_cast<uint64_t>(round(m_routeCost*HYPERBOLIC_COST_ADJUSTMENT_FACTOR));
85  }
86  else {
87  return static_cast<uint64_t>(m_routeCost);
88  }
89  }
90 
91  double
92  getRouteCost() const
93  {
94  return m_routeCost;
95  }
96 
97  void
98  setRouteCost(const double rc)
99  {
100  m_routeCost = rc;
101  }
102 
103  void
105  {
106  m_isHyperbolic = b;
107  }
108 
109  bool
110  isHyperbolic() const
111  {
112  return m_isHyperbolic;
113  }
114 
115  template<ndn::encoding::Tag TAG>
116  size_t
117  wireEncode(ndn::EncodingImpl<TAG>& block) const;
118 
119  const ndn::Block&
120  wireEncode() const;
121 
122  void
123  wireDecode(const ndn::Block& wire);
124 
125 private: // non-member operators
126  // NOTE: the following "hidden friend" operators are available via
127  // argument-dependent lookup only and must be defined inline.
128  // boost::totally_ordered provides !=, <=, >=, and > operators.
129 
130  friend bool
131  operator==(const NextHop& lhs, const NextHop& rhs)
132  {
135  }
136 
137  friend bool
138  operator<(const NextHop& lhs, const NextHop& rhs)
139  {
140  return std::forward_as_tuple(lhs.getRouteCostAsAdjustedInteger(), lhs.getConnectingFaceUri()) <
141  std::forward_as_tuple(rhs.getRouteCostAsAdjustedInteger(), rhs.getConnectingFaceUri());
142  }
143 
144 private:
145  ndn::FaceUri m_connectingFaceUri;
146  double m_routeCost = 0.0;
147  bool m_isHyperbolic = false;
148 
149  mutable ndn::Block m_wire;
150 
162  static constexpr uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000;
163 };
164 
166 
167 std::ostream&
168 operator<<(std::ostream& os, const NextHop& hop);
169 
170 } // namespace nlsr
171 
172 #endif // NLSR_ROUTE_NEXTHOP_HPP
Data abstraction for Nexthop.
Definition: nexthop.hpp:47
friend bool operator==(const NextHop &lhs, const NextHop &rhs)
Definition: nexthop.hpp:131
ndn::tlv::Error Error
Definition: nexthop.hpp:49
void setRouteCost(const double rc)
Definition: nexthop.hpp:98
friend bool operator<(const NextHop &lhs, const NextHop &rhs)
Definition: nexthop.hpp:138
const ndn::Block & wireEncode() const
Definition: nexthop.cpp:46
void setConnectingFaceUri(const ndn::FaceUri &cfu)
Definition: nexthop.hpp:72
const ndn::FaceUri & getConnectingFaceUri() const
Definition: nexthop.hpp:66
void wireDecode(const ndn::Block &wire)
Definition: nexthop.cpp:64
double getRouteCost() const
Definition: nexthop.hpp:92
void setHyperbolic(bool b)
Definition: nexthop.hpp:104
NextHop(const ndn::FaceUri &cfu, double rc)
Definition: nexthop.hpp:53
NextHop()=default
uint64_t getRouteCostAsAdjustedInteger() const
Definition: nexthop.hpp:78
bool isHyperbolic() const
Definition: nexthop.hpp:110
NextHop(const ndn::Block &block)
Definition: nexthop.hpp:60
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
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE