akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 19 | */ |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 20 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 21 | #ifndef NLSR_NEXTHOP_LIST_HPP |
| 22 | #define NLSR_NEXTHOP_LIST_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 24 | #include "nexthop.hpp" |
| 25 | #include "adjacent.hpp" |
| 26 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/face.hpp> |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 28 | #include <ndn-cxx/util/ostream-joiner.hpp> |
| 29 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 30 | #include <set> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 34 | struct 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 Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 49 | struct NextHopUriSortedComparator { |
| 50 | bool |
| 51 | operator() (const NextHop& nh1, const NextHop& nh2) const { |
| 52 | return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri(); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | static inline bool |
| 57 | nexthopAddCompare(const NextHop& nh1, const NextHop& nh2) |
| 58 | { |
| 59 | return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri(); |
| 60 | } |
| 61 | |
| 62 | static inline bool |
| 63 | nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2) |
| 64 | { |
| 65 | return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() && |
| 66 | nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ; |
| 67 | } |
| 68 | |
| 69 | template<typename T = NextHopComparator> |
| 70 | class NexthopListT |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | { |
| 72 | public: |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 73 | NexthopListT() = default; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 74 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 75 | /*! \brief Adds a next hop to the list. |
| 76 | \param nh The next hop. |
| 77 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 78 | 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 G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 81 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 82 | void |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 83 | 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 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 96 | /*! \brief Remove a next hop from the Next Hop list |
| 97 | \param nh The NextHop we want to remove. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 98 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 99 | The next hop gets removed only if both next hop face and route cost are same. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 100 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | void |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 102 | 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 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 110 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 111 | size_t |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 112 | size() const |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 113 | { |
| 114 | return m_nexthopList.size(); |
| 115 | } |
| 116 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | void |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 118 | clear() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 120 | m_nexthopList.clear(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 123 | const std::set<NextHop, T>& |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 124 | getNextHops() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | { |
| 126 | return m_nexthopList; |
| 127 | } |
| 128 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 129 | 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 Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 133 | |
| 134 | iterator |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 135 | begin() const |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 136 | { |
| 137 | return m_nexthopList.begin(); |
| 138 | } |
| 139 | |
| 140 | iterator |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 141 | end() const |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 142 | { |
| 143 | return m_nexthopList.end(); |
| 144 | } |
| 145 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 146 | 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 Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 158 | 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 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 169 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | private: |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 171 | std::set<NextHop, T> m_nexthopList; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | }; |
| 173 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 174 | typedef NexthopListT<> NexthopList; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 175 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 176 | template<typename T> |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 177 | bool |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 178 | operator==(const NexthopListT<T>& lhs, const NexthopListT<T>& rhs) |
| 179 | { |
| 180 | return lhs.getNextHops() == rhs.getNextHops(); |
| 181 | } |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 182 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 183 | template<typename T> |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 184 | bool |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 185 | operator!=(const NexthopListT<T>& lhs, const NexthopListT<T>& rhs) |
| 186 | { |
| 187 | return !(lhs == rhs); |
| 188 | } |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 189 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 190 | template<typename T> |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 191 | std::ostream& |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame^] | 192 | operator<<(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 Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 198 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 199 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 201 | #endif // NLSR_NEXTHOP_LIST_HPP |