blob: 1ed1a5aaf9536f0497ba3d49126e7a8610f687aa [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/*
Junxiao Shi6593a432023-08-21 10:50:28 +00003 * Copyright (c) 2014-2023, 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 */
Nick Gordond0a7df32017-05-30 16:44:34 -050020
akmhoquefdbddb12014-05-02 18:35:19 -050021#ifndef NLSR_NEXTHOP_LIST_HPP
22#define NLSR_NEXTHOP_LIST_HPP
akmhoque53353462014-04-22 08:43:45 -050023
Nick Gordond0a7df32017-05-30 16:44:34 -050024#include "nexthop.hpp"
25#include "adjacent.hpp"
26
akmhoquefdbddb12014-05-02 18:35:19 -050027#include <ndn-cxx/face.hpp>
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070028#include <ndn-cxx/util/ostream-joiner.hpp>
29
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080030#include <set>
akmhoque53353462014-04-22 08:43:45 -050031
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
Junxiao Shi6593a432023-08-21 10:50:28 +000034struct NextHopUriSortedComparator
35{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050036 bool
Junxiao Shi6593a432023-08-21 10:50:28 +000037 operator()(const NextHop& lhs, const NextHop& rhs) const
38 {
39 return lhs.getConnectingFaceUri() < rhs.getConnectingFaceUri();
Vince Lehmanef21d8e2015-04-01 15:59:39 -050040 }
41};
42
Junxiao Shi6593a432023-08-21 10:50:28 +000043template<typename T = std::less<NextHop>>
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070044class NexthopListT
akmhoque53353462014-04-22 08:43:45 -050045{
46public:
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070047 NexthopListT() = default;
akmhoquefdbddb12014-05-02 18:35:19 -050048
Nick G97e34942016-07-11 14:46:27 -050049 /*! \brief Adds a next hop to the list.
50 \param nh The next hop.
51
Nick Gordonb50e51b2016-07-22 16:05:57 -050052 Adds a next hop to this object. If the next hop is new it is
Junxiao Shi6593a432023-08-21 10:50:28 +000053 added. If the next hop already exists but has a higher cost then
54 its route cost is updated.
Nick G97e34942016-07-11 14:46:27 -050055 */
akmhoque53353462014-04-22 08:43:45 -050056 void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070057 addNextHop(const NextHop& nh)
58 {
59 auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
Junxiao Shi6593a432023-08-21 10:50:28 +000060 [&nh] (const auto& item) {
61 return item.getConnectingFaceUri() == nh.getConnectingFaceUri();
62 });
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070063 if (it == m_nexthopList.end()) {
64 m_nexthopList.insert(nh);
65 }
66 else if (it->getRouteCost() > nh.getRouteCost()) {
Junxiao Shi6593a432023-08-21 10:50:28 +000067 m_nexthopList.erase(it);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070068 m_nexthopList.insert(nh);
69 }
70 }
akmhoque53353462014-04-22 08:43:45 -050071
Vince Lehmanef21d8e2015-04-01 15:59:39 -050072 /*! \brief Remove a next hop from the Next Hop list
73 \param nh The NextHop we want to remove.
Nick G97e34942016-07-11 14:46:27 -050074
Vince Lehmanef21d8e2015-04-01 15:59:39 -050075 The next hop gets removed only if both next hop face and route cost are same.
Nick G97e34942016-07-11 14:46:27 -050076 */
akmhoque53353462014-04-22 08:43:45 -050077 void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070078 removeNextHop(const NextHop& nh)
79 {
Junxiao Shi6593a432023-08-21 10:50:28 +000080 auto it = std::find(m_nexthopList.begin(), m_nexthopList.end(), nh);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070081 if (it != m_nexthopList.end()) {
82 m_nexthopList.erase(it);
83 }
84 }
akmhoque53353462014-04-22 08:43:45 -050085
akmhoquefdbddb12014-05-02 18:35:19 -050086 size_t
Nick Gordonff9a6272017-10-12 13:38:29 -050087 size() const
Nick Gordonb50e51b2016-07-22 16:05:57 -050088 {
89 return m_nexthopList.size();
90 }
91
akmhoque53353462014-04-22 08:43:45 -050092 void
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070093 clear()
akmhoque53353462014-04-22 08:43:45 -050094 {
akmhoquefdbddb12014-05-02 18:35:19 -050095 m_nexthopList.clear();
akmhoque53353462014-04-22 08:43:45 -050096 }
97
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070098 const std::set<NextHop, T>&
laqinfan35731852017-08-08 06:17:39 -050099 getNextHops() const
akmhoque53353462014-04-22 08:43:45 -0500100 {
101 return m_nexthopList;
102 }
103
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700104 typedef T value_type;
105 typedef typename std::set<NextHop, T>::iterator iterator;
106 typedef typename std::set<NextHop, T>::const_iterator const_iterator;
107 typedef typename std::set<NextHop, T>::reverse_iterator reverse_iterator;
Vince Lehman18841082014-08-19 17:15:24 -0500108
109 iterator
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700110 begin() const
Vince Lehman18841082014-08-19 17:15:24 -0500111 {
112 return m_nexthopList.begin();
113 }
114
115 iterator
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700116 end() const
Vince Lehman18841082014-08-19 17:15:24 -0500117 {
118 return m_nexthopList.end();
119 }
120
Vince Lehman942eb7b2014-10-02 10:09:27 -0500121 const_iterator
122 cbegin() const
123 {
124 return m_nexthopList.begin();
125 }
126
127 const_iterator
128 cend() const
129 {
130 return m_nexthopList.end();
131 }
132
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700133 reverse_iterator
134 rbegin() const
135 {
136 return m_nexthopList.rbegin();
137 }
138
139 reverse_iterator
140 rend() const
141 {
142 return m_nexthopList.rend();
143 }
akmhoque674b0b12014-05-20 14:33:28 -0500144
akmhoque53353462014-04-22 08:43:45 -0500145private:
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700146 std::set<NextHop, T> m_nexthopList;
akmhoque53353462014-04-22 08:43:45 -0500147};
148
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700149typedef NexthopListT<> NexthopList;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500150
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700151template<typename T>
Nick Gordonb50e51b2016-07-22 16:05:57 -0500152bool
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700153operator==(const NexthopListT<T>& lhs, const NexthopListT<T>& rhs)
154{
155 return lhs.getNextHops() == rhs.getNextHops();
156}
Nick Gordonb50e51b2016-07-22 16:05:57 -0500157
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700158template<typename T>
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500159bool
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700160operator!=(const NexthopListT<T>& lhs, const NexthopListT<T>& rhs)
161{
162 return !(lhs == rhs);
163}
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500164
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700165template<typename T>
Nick Gordonb50e51b2016-07-22 16:05:57 -0500166std::ostream&
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700167operator<<(std::ostream& os, const NexthopListT<T>& nhl)
168{
169 os << " ";
170 std::copy(nhl.cbegin(), nhl.cend(), ndn::make_ostream_joiner(os, "\n "));
171 return os;
172}
Nick Gordonb50e51b2016-07-22 16:05:57 -0500173
Nick Gordonfad8e252016-08-11 14:21:38 -0500174} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500175
Nick Gordond0a7df32017-05-30 16:44:34 -0500176#endif // NLSR_NEXTHOP_LIST_HPP