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