blob: 98e5760b4e4ec20de6c32a52170f78ca35f4e498 [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 Gawande6f0f35d2021-08-21 23:52:14 -07003 * Copyright (c) 2014-2021, 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
Vince Lehmanef21d8e2015-04-01 15:59:39 -050034struct NextHopComparator {
35 bool
36 operator() (const NextHop& nh1, const NextHop& nh2) const {
37 if (nh1.getRouteCostAsAdjustedInteger() < nh2.getRouteCostAsAdjustedInteger()) {
38 return true;
39 }
40 else if (nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) {
41 return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
42 }
43 else {
44 return false;
45 }
46 }
47};
48
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070049struct NextHopUriSortedComparator {
50 bool
51 operator() (const NextHop& nh1, const NextHop& nh2) const {
52 return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
53 }
54};
55
56static inline bool
57nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
58{
59 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
60}
61
62static inline bool
63nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
64{
65 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
66 nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ;
67}
68
69template<typename T = NextHopComparator>
70class NexthopListT
akmhoque53353462014-04-22 08:43:45 -050071{
72public:
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070073 NexthopListT() = default;
akmhoquefdbddb12014-05-02 18:35:19 -050074
Nick G97e34942016-07-11 14:46:27 -050075 /*! \brief Adds a next hop to the list.
76 \param nh The next hop.
77
Nick Gordonb50e51b2016-07-22 16:05:57 -050078 Adds a next hop to this object. If the next hop is new it is
79 added. If the next hop already exists in the list then that next
80 hop's route cost is updated.
Nick G97e34942016-07-11 14:46:27 -050081 */
akmhoque53353462014-04-22 08:43:45 -050082 void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070083 addNextHop(const NextHop& nh)
84 {
85 auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
86 std::bind(&nexthopAddCompare, _1, nh));
87 if (it == m_nexthopList.end()) {
88 m_nexthopList.insert(nh);
89 }
90 else if (it->getRouteCost() > nh.getRouteCost()) {
91 removeNextHop(*it);
92 m_nexthopList.insert(nh);
93 }
94 }
akmhoque53353462014-04-22 08:43:45 -050095
Vince Lehmanef21d8e2015-04-01 15:59:39 -050096 /*! \brief Remove a next hop from the Next Hop list
97 \param nh The NextHop we want to remove.
Nick G97e34942016-07-11 14:46:27 -050098
Vince Lehmanef21d8e2015-04-01 15:59:39 -050099 The next hop gets removed only if both next hop face and route cost are same.
Nick G97e34942016-07-11 14:46:27 -0500100 */
akmhoque53353462014-04-22 08:43:45 -0500101 void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700102 removeNextHop(const NextHop& nh)
103 {
104 auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
105 std::bind(&nexthopRemoveCompare, _1, nh));
106 if (it != m_nexthopList.end()) {
107 m_nexthopList.erase(it);
108 }
109 }
akmhoque53353462014-04-22 08:43:45 -0500110
akmhoquefdbddb12014-05-02 18:35:19 -0500111 size_t
Nick Gordonff9a6272017-10-12 13:38:29 -0500112 size() const
Nick Gordonb50e51b2016-07-22 16:05:57 -0500113 {
114 return m_nexthopList.size();
115 }
116
akmhoque53353462014-04-22 08:43:45 -0500117 void
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700118 clear()
akmhoque53353462014-04-22 08:43:45 -0500119 {
akmhoquefdbddb12014-05-02 18:35:19 -0500120 m_nexthopList.clear();
akmhoque53353462014-04-22 08:43:45 -0500121 }
122
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700123 const std::set<NextHop, T>&
laqinfan35731852017-08-08 06:17:39 -0500124 getNextHops() const
akmhoque53353462014-04-22 08:43:45 -0500125 {
126 return m_nexthopList;
127 }
128
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700129 typedef T value_type;
130 typedef typename std::set<NextHop, T>::iterator iterator;
131 typedef typename std::set<NextHop, T>::const_iterator const_iterator;
132 typedef typename std::set<NextHop, T>::reverse_iterator reverse_iterator;
Vince Lehman18841082014-08-19 17:15:24 -0500133
134 iterator
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700135 begin() const
Vince Lehman18841082014-08-19 17:15:24 -0500136 {
137 return m_nexthopList.begin();
138 }
139
140 iterator
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700141 end() const
Vince Lehman18841082014-08-19 17:15:24 -0500142 {
143 return m_nexthopList.end();
144 }
145
Vince Lehman942eb7b2014-10-02 10:09:27 -0500146 const_iterator
147 cbegin() const
148 {
149 return m_nexthopList.begin();
150 }
151
152 const_iterator
153 cend() const
154 {
155 return m_nexthopList.end();
156 }
157
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700158 reverse_iterator
159 rbegin() const
160 {
161 return m_nexthopList.rbegin();
162 }
163
164 reverse_iterator
165 rend() const
166 {
167 return m_nexthopList.rend();
168 }
akmhoque674b0b12014-05-20 14:33:28 -0500169
akmhoque53353462014-04-22 08:43:45 -0500170private:
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700171 std::set<NextHop, T> m_nexthopList;
akmhoque53353462014-04-22 08:43:45 -0500172};
173
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700174typedef NexthopListT<> NexthopList;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500175
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700176template<typename T>
Nick Gordonb50e51b2016-07-22 16:05:57 -0500177bool
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700178operator==(const NexthopListT<T>& lhs, const NexthopListT<T>& rhs)
179{
180 return lhs.getNextHops() == rhs.getNextHops();
181}
Nick Gordonb50e51b2016-07-22 16:05:57 -0500182
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700183template<typename T>
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500184bool
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700185operator!=(const NexthopListT<T>& lhs, const NexthopListT<T>& rhs)
186{
187 return !(lhs == rhs);
188}
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500189
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700190template<typename T>
Nick Gordonb50e51b2016-07-22 16:05:57 -0500191std::ostream&
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700192operator<<(std::ostream& os, const NexthopListT<T>& nhl)
193{
194 os << " ";
195 std::copy(nhl.cbegin(), nhl.cend(), ndn::make_ostream_joiner(os, "\n "));
196 return os;
197}
Nick Gordonb50e51b2016-07-22 16:05:57 -0500198
Nick Gordonfad8e252016-08-11 14:21:38 -0500199} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500200
Nick Gordond0a7df32017-05-30 16:44:34 -0500201#endif // NLSR_NEXTHOP_LIST_HPP