akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, The University of Memphis, |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 21 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 22 | #include "adjacency-list.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 23 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | namespace nlsr { |
| 28 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 29 | INIT_LOGGER(AdjacencyList); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 30 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 31 | bool |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 32 | AdjacencyList::insert(const Adjacent& adjacent) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 34 | auto it = find(adjacent.getName()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 35 | if (it != m_adjList.end()) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 36 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 38 | m_adjList.push_back(adjacent); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 39 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | } |
| 41 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 42 | Adjacent |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 43 | AdjacencyList::getAdjacent(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | { |
| 45 | Adjacent adj(adjName); |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 46 | auto it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 47 | if (it != m_adjList.end()) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 48 | return *it; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 49 | } |
| 50 | return adj; |
| 51 | } |
| 52 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 53 | bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 54 | AdjacencyList::operator==(const AdjacencyList& adl) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 56 | auto theirList = adl.getAdjList(); |
| 57 | if (m_adjList.size() != theirList.size()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | return false; |
| 59 | } |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 60 | |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 61 | std::set<Adjacent> ourSet(m_adjList.cbegin(), m_adjList.cend()); |
| 62 | std::set<Adjacent> theirSet(theirList.cbegin(), theirList.cend()); |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 63 | return ourSet == theirSet; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 64 | } |
| 65 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 67 | AdjacencyList::isNeighbor(const ndn::Name& adjName) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 69 | return find(adjName) != m_adjList.end(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 73 | AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 75 | auto it = find(neighbor); |
| 76 | if (it != m_adjList.end()) { |
| 77 | it->setInterestTimedOutNo(it->getInterestTimedOutNo() + 1); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 82 | AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, uint32_t count) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 83 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 84 | auto it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 85 | if (it != m_adjList.end()) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 86 | it->setInterestTimedOutNo(count); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 90 | int32_t |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 91 | AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 93 | auto it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 94 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | return -1; |
| 96 | } |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 97 | return it->getInterestTimedOutNo(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 100 | Adjacent::Status |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 101 | AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 103 | auto it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 104 | if (it == m_adjList.end()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 105 | return Adjacent::STATUS_UNKNOWN; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | } |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 107 | else { |
| 108 | return it->getStatus(); |
| 109 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 113 | AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 115 | auto it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 116 | if (it != m_adjList.end()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 117 | it->setStatus(status); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | std::list<Adjacent>& |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 122 | AdjacencyList::getAdjList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | { |
| 124 | return m_adjList; |
| 125 | } |
| 126 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 127 | const std::list<Adjacent>& |
| 128 | AdjacencyList::getAdjList() const |
| 129 | { |
| 130 | return m_adjList; |
| 131 | } |
| 132 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | bool |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 134 | AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | { |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 136 | uint32_t nTimedOutNeighbors = 0; |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 137 | |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 138 | for (const auto& adjacency : m_adjList) { |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 139 | if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) { |
| 140 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 142 | else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) { |
| 143 | nTimedOutNeighbors++; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 144 | } |
| 145 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 146 | |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 147 | return nTimedOutNeighbors == m_adjList.size(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 148 | } |
| 149 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 150 | int32_t |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 151 | AdjacencyList::getNumOfActiveNeighbor() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 152 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 153 | int32_t actNbrCount = 0; |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 154 | for (const auto& adjacent: m_adjList) { |
| 155 | if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 156 | actNbrCount++; |
| 157 | } |
| 158 | } |
| 159 | return actNbrCount; |
| 160 | } |
| 161 | |
| 162 | std::list<Adjacent>::iterator |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 163 | AdjacencyList::find(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 165 | return std::find_if(m_adjList.begin(), m_adjList.end(), |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 166 | std::bind(&Adjacent::compare, _1, std::cref(adjName))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 169 | std::list<Adjacent>::const_iterator |
| 170 | AdjacencyList::find(const ndn::Name& adjName) const |
| 171 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 172 | return std::find_if(m_adjList.cbegin(), m_adjList.cend(), |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 173 | std::bind(&Adjacent::compare, _1, std::cref(adjName))); |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 174 | } |
| 175 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 176 | AdjacencyList::iterator |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 177 | AdjacencyList::findAdjacent(const ndn::Name& adjName) |
| 178 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 179 | return std::find_if(m_adjList.begin(), m_adjList.end(), |
| 180 | std::bind(&Adjacent::compare, _1, std::cref(adjName))); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 183 | AdjacencyList::iterator |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 184 | AdjacencyList::findAdjacent(uint64_t faceId) |
| 185 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 186 | return std::find_if(m_adjList.begin(), m_adjList.end(), |
| 187 | std::bind(&Adjacent::compareFaceId, _1, faceId)); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 190 | AdjacencyList::iterator |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 191 | AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri) |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 192 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 193 | return std::find_if(m_adjList.begin(), m_adjList.end(), |
| 194 | std::bind(&Adjacent::compareFaceUri, _1, faceUri)); |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 195 | } |
| 196 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 197 | uint64_t |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 198 | AdjacencyList::getFaceId(const ndn::FaceUri& faceUri) |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 199 | { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 200 | auto it = std::find_if(m_adjList.begin(), m_adjList.end(), |
| 201 | std::bind(&Adjacent::compareFaceUri, _1, faceUri)); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 202 | return it != m_adjList.end() ? it->getFaceId() : 0; |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 203 | } |
| 204 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 205 | void |
| 206 | AdjacencyList::writeLog() |
| 207 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 208 | NLSR_LOG_DEBUG("-------Adjacency List--------"); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 209 | for (const auto& adjacent : m_adjList) { |
| 210 | NLSR_LOG_DEBUG(adjacent); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 214 | } // namespace nlsr |