akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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" |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 23 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | #include "adjacent.hpp" |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 25 | #include "common.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 26 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | namespace nlsr { |
| 31 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 32 | INIT_LOGGER(AdjacencyList); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 33 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 34 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 35 | AdjacencyList::insert(Adjacent& adjacent) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 37 | std::list<Adjacent>::iterator it = find(adjacent.getName()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 38 | if (it != m_adjList.end()) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 39 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 41 | m_adjList.push_back(adjacent); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 42 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | } |
| 44 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | Adjacent |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 46 | AdjacencyList::getAdjacent(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | { |
| 48 | Adjacent adj(adjName); |
| 49 | std::list<Adjacent>::iterator it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 50 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 51 | return (*it); |
| 52 | } |
| 53 | return adj; |
| 54 | } |
| 55 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 56 | bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 57 | AdjacencyList::operator==(const AdjacencyList& adl) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | { |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 59 | auto theirList = adl.getAdjList(); |
| 60 | if (m_adjList.size() != theirList.size()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | return false; |
| 62 | } |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 63 | |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 64 | std::set<Adjacent> ourSet(m_adjList.cbegin(), m_adjList.cend()); |
| 65 | std::set<Adjacent> theirSet(theirList.cbegin(), theirList.cend()); |
Nick Gordon | 2a1ac61 | 2017-10-06 15:36:49 -0500 | [diff] [blame] | 66 | |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 67 | return ourSet == theirSet; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | } |
| 69 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | bool |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 71 | AdjacencyList::isNeighbor(const ndn::Name& adjName) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | { |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 73 | std::list<Adjacent>::const_iterator it = find(adjName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | if (it == m_adjList.end()) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 82 | AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 83 | { |
| 84 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 85 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | return ; |
| 87 | } |
| 88 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1); |
| 89 | } |
| 90 | |
| 91 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 92 | AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, |
| 93 | uint32_t count) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | { |
| 95 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 96 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | (*it).setInterestTimedOutNo(count); |
| 98 | } |
| 99 | } |
| 100 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 101 | int32_t |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 102 | AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 103 | { |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 104 | std::list<Adjacent>::const_iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 105 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | return (*it).getInterestTimedOutNo(); |
| 109 | } |
| 110 | |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 111 | Adjacent::Status |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 112 | AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 113 | { |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 114 | std::list<Adjacent>::const_iterator it = find(neighbor); |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 115 | |
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 | return Adjacent::STATUS_UNKNOWN; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | } |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 119 | else { |
| 120 | return it->getStatus(); |
| 121 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 125 | AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 126 | { |
| 127 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 128 | if (it != m_adjList.end()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 129 | it->setStatus(status); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | std::list<Adjacent>& |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 134 | AdjacencyList::getAdjList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | { |
| 136 | return m_adjList; |
| 137 | } |
| 138 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 139 | const std::list<Adjacent>& |
| 140 | AdjacencyList::getAdjList() const |
| 141 | { |
| 142 | return m_adjList; |
| 143 | } |
| 144 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | bool |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 146 | AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | { |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 148 | uint32_t nTimedOutNeighbors = 0; |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 149 | |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 150 | for (const Adjacent& adjacency : m_adjList) { |
| 151 | |
| 152 | if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) { |
| 153 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 155 | else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) { |
| 156 | nTimedOutNeighbors++; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 159 | |
| 160 | if (nTimedOutNeighbors == m_adjList.size()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | return true; |
| 162 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 163 | else { |
| 164 | return false; |
| 165 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | } |
| 167 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 168 | int32_t |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 169 | AdjacencyList::getNumOfActiveNeighbor() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 171 | int32_t actNbrCount = 0; |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 172 | for (const auto& adjacent: m_adjList) { |
| 173 | if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | actNbrCount++; |
| 175 | } |
| 176 | } |
| 177 | return actNbrCount; |
| 178 | } |
| 179 | |
| 180 | std::list<Adjacent>::iterator |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 181 | AdjacencyList::find(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 183 | return std::find_if(m_adjList.begin(), |
| 184 | m_adjList.end(), |
| 185 | std::bind(&Adjacent::compare, _1, std::cref(adjName))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 188 | std::list<Adjacent>::const_iterator |
| 189 | AdjacencyList::find(const ndn::Name& adjName) const |
| 190 | { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 191 | return std::find_if(m_adjList.cbegin(), |
| 192 | m_adjList.cend(), |
| 193 | std::bind(&Adjacent::compare, _1, std::cref(adjName))); |
Nick Gordon | 4964870 | 2018-01-22 11:57:33 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 196 | AdjacencyList::iterator |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 197 | AdjacencyList::findAdjacent(const ndn::Name& adjName) |
| 198 | { |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 199 | return std::find_if(m_adjList.begin(), |
| 200 | m_adjList.end(), |
| 201 | std::bind(&Adjacent::compare, |
| 202 | _1, std::cref(adjName))); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 205 | AdjacencyList::iterator |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 206 | AdjacencyList::findAdjacent(uint64_t faceId) |
| 207 | { |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 208 | return std::find_if(m_adjList.begin(), |
| 209 | m_adjList.end(), |
| 210 | std::bind(&Adjacent::compareFaceId, |
| 211 | _1, faceId)); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 214 | AdjacencyList::iterator |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 215 | AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri) |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 216 | { |
| 217 | return std::find_if(m_adjList.begin(), |
| 218 | m_adjList.end(), |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 219 | std::bind(&Adjacent::compareFaceUri, |
| 220 | _1, faceUri)); |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 221 | } |
| 222 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 223 | uint64_t |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 224 | AdjacencyList::getFaceId(const ndn::FaceUri& faceUri) |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 225 | { |
| 226 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 227 | m_adjList.end(), |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 228 | std::bind(&Adjacent::compareFaceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 229 | _1, faceUri)); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 230 | |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 231 | return it != m_adjList.end() ? it->getFaceId() : 0; |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 232 | } |
| 233 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 234 | void |
| 235 | AdjacencyList::writeLog() |
| 236 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 237 | NLSR_LOG_DEBUG("-------Adjacency List--------"); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 238 | for (const auto& adjacent : m_adjList) { |
| 239 | NLSR_LOG_DEBUG(adjacent); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 243 | } // namespace nlsr |