akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 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 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 32 | INIT_LOGGER("AdjacencyList"); |
| 33 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 34 | using namespace std; |
| 35 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 36 | AdjacencyList::AdjacencyList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 40 | AdjacencyList::~AdjacencyList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 44 | int32_t |
| 45 | AdjacencyList::insert(Adjacent& adjacent) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 47 | std::list<Adjacent>::iterator it = find(adjacent.getName()); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 48 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 49 | return -1; |
| 50 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 51 | m_adjList.push_back(adjacent); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 56 | AdjacencyList::addAdjacents(AdjacencyList& adl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | { |
| 58 | for (std::list<Adjacent>::iterator it = adl.getAdjList().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 59 | it != adl.getAdjList().end(); ++it) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | insert((*it)); |
| 61 | } |
| 62 | } |
| 63 | |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 64 | bool |
| 65 | AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, Adjacent::Status s) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | { |
| 67 | std::list<Adjacent>::iterator it = find(adjName); |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 68 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | if (it == m_adjList.end()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 70 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | } |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 72 | else { |
| 73 | it->setStatus(s); |
| 74 | return true; |
| 75 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Adjacent |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 79 | AdjacencyList::getAdjacent(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | { |
| 81 | Adjacent adj(adjName); |
| 82 | std::list<Adjacent>::iterator it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 83 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | return (*it); |
| 85 | } |
| 86 | return adj; |
| 87 | } |
| 88 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 89 | static bool |
| 90 | compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2) |
| 91 | { |
| 92 | return adjacent1.getName() < adjacent2.getName(); |
| 93 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | |
| 95 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 96 | AdjacencyList::operator==(AdjacencyList& adl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 98 | if (getSize() != adl.getSize()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | return false; |
| 100 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 101 | m_adjList.sort(compareAdjacent); |
| 102 | adl.getAdjList().sort(compareAdjacent); |
| 103 | uint32_t equalAdjCount = 0; |
| 104 | std::list<Adjacent>& adjList2 = adl.getAdjList(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 105 | std::list<Adjacent>::iterator it1; |
| 106 | std::list<Adjacent>::iterator it2; |
| 107 | for (it1 = m_adjList.begin(), it2 = adjList2.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 108 | it1 != m_adjList.end(); it1++, it2++) { |
| 109 | if (!((*it1) == (*it2))) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | equalAdjCount++; |
| 113 | } |
| 114 | return equalAdjCount == getSize(); |
| 115 | } |
| 116 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 117 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 118 | AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | { |
| 120 | std::list<Adjacent>::iterator it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 121 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 122 | return -1; |
| 123 | } |
| 124 | (*it).setLinkCost(lc); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 129 | AdjacencyList::isNeighbor(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | { |
| 131 | std::list<Adjacent>::iterator it = find(adjName); |
| 132 | if (it == m_adjList.end()) |
| 133 | { |
| 134 | return false; |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 140 | AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | { |
| 142 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 143 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 144 | return ; |
| 145 | } |
| 146 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1); |
| 147 | } |
| 148 | |
| 149 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 150 | AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, |
| 151 | uint32_t count) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 152 | { |
| 153 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 154 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 155 | (*it).setInterestTimedOutNo(count); |
| 156 | } |
| 157 | } |
| 158 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 159 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 160 | AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | { |
| 162 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 163 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | return -1; |
| 165 | } |
| 166 | return (*it).getInterestTimedOutNo(); |
| 167 | } |
| 168 | |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 169 | Adjacent::Status |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 170 | AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 171 | { |
| 172 | std::list<Adjacent>::iterator it = find(neighbor); |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 173 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 174 | if (it == m_adjList.end()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 175 | return Adjacent::STATUS_UNKNOWN; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 176 | } |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 177 | else { |
| 178 | return it->getStatus(); |
| 179 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 183 | AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | { |
| 185 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 186 | if (it != m_adjList.end()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 187 | it->setStatus(status); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | std::list<Adjacent>& |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 192 | AdjacencyList::getAdjList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 193 | { |
| 194 | return m_adjList; |
| 195 | } |
| 196 | |
| 197 | bool |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 198 | AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 199 | { |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 200 | uint32_t nTimedOutNeighbors = 0; |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 201 | |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 202 | for (const Adjacent& adjacency : m_adjList) { |
| 203 | |
| 204 | if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) { |
| 205 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 207 | else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) { |
| 208 | nTimedOutNeighbors++; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 209 | } |
| 210 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 211 | |
| 212 | if (nTimedOutNeighbors == m_adjList.size()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 213 | return true; |
| 214 | } |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame^] | 215 | else { |
| 216 | return false; |
| 217 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | } |
| 219 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 220 | int32_t |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 221 | AdjacencyList::getNumOfActiveNeighbor() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 222 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 223 | int32_t actNbrCount = 0; |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 224 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end(); it++) { |
| 225 | |
| 226 | if (it->getStatus() == Adjacent::STATUS_ACTIVE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | actNbrCount++; |
| 228 | } |
| 229 | } |
| 230 | return actNbrCount; |
| 231 | } |
| 232 | |
| 233 | std::list<Adjacent>::iterator |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 234 | AdjacencyList::find(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 236 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 237 | m_adjList.end(), |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 238 | ndn::bind(&Adjacent::compare, |
| 239 | _1, ndn::cref(adjName))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | return it; |
| 241 | } |
| 242 | |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 243 | Adjacent * |
| 244 | AdjacencyList::findAdjacent(const ndn::Name& adjName) |
| 245 | { |
| 246 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 247 | m_adjList.end(), |
| 248 | ndn::bind(&Adjacent::compare, |
| 249 | _1, ndn::cref(adjName))); |
| 250 | if (it != m_adjList.end()) { |
| 251 | return &(*it); |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | Adjacent * |
| 258 | AdjacencyList::findAdjacent(uint64_t faceId) |
| 259 | { |
| 260 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 261 | m_adjList.end(), |
| 262 | ndn::bind(&Adjacent::compareFaceId, |
| 263 | _1, faceId)); |
| 264 | if (it != m_adjList.end()) { |
| 265 | return &(*it); |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 271 | uint64_t |
| 272 | AdjacencyList::getFaceId(const std::string& faceUri) |
| 273 | { |
| 274 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 275 | m_adjList.end(), |
| 276 | ndn::bind(&Adjacent::compareFaceUri, |
| 277 | _1, faceUri)); |
| 278 | if (it != m_adjList.end()) { |
| 279 | return it->getFaceId(); |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 285 | void |
| 286 | AdjacencyList::writeLog() |
| 287 | { |
| 288 | _LOG_DEBUG("-------Adjacency List--------"); |
| 289 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
| 290 | it != m_adjList.end(); it++) { |
| 291 | (*it).writeLog(); |
| 292 | } |
| 293 | } |
| 294 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 295 | } //namespace nlsr |