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> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 25 | #include "adjacency-list.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 26 | #include "adjacent.hpp" |
| 27 | #include "nlsr.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 28 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
| 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 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 64 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 65 | AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, int32_t s) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | { |
| 67 | std::list<Adjacent>::iterator it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 68 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | (*it).setStatus(s); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | Adjacent |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 76 | AdjacencyList::getAdjacent(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | { |
| 78 | Adjacent adj(adjName); |
| 79 | std::list<Adjacent>::iterator it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 80 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | return (*it); |
| 82 | } |
| 83 | return adj; |
| 84 | } |
| 85 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 86 | static bool |
| 87 | compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2) |
| 88 | { |
| 89 | return adjacent1.getName() < adjacent2.getName(); |
| 90 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 91 | |
| 92 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 93 | AdjacencyList::operator==(AdjacencyList& adl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 95 | if (getSize() != adl.getSize()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 96 | return false; |
| 97 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 98 | m_adjList.sort(compareAdjacent); |
| 99 | adl.getAdjList().sort(compareAdjacent); |
| 100 | uint32_t equalAdjCount = 0; |
| 101 | std::list<Adjacent>& adjList2 = adl.getAdjList(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | std::list<Adjacent>::iterator it1; |
| 103 | std::list<Adjacent>::iterator it2; |
| 104 | for (it1 = m_adjList.begin(), it2 = adjList2.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 105 | it1 != m_adjList.end(); it1++, it2++) { |
| 106 | if (!((*it1) == (*it2))) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | equalAdjCount++; |
| 110 | } |
| 111 | return equalAdjCount == getSize(); |
| 112 | } |
| 113 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 114 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 115 | AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 116 | { |
| 117 | std::list<Adjacent>::iterator it = find(adjName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 118 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | return -1; |
| 120 | } |
| 121 | (*it).setLinkCost(lc); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 126 | AdjacencyList::isNeighbor(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | { |
| 128 | std::list<Adjacent>::iterator it = find(adjName); |
| 129 | if (it == m_adjList.end()) |
| 130 | { |
| 131 | return false; |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 137 | AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | { |
| 139 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 140 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | return ; |
| 142 | } |
| 143 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1); |
| 144 | } |
| 145 | |
| 146 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 147 | AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, |
| 148 | uint32_t count) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 149 | { |
| 150 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 151 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 152 | (*it).setInterestTimedOutNo(count); |
| 153 | } |
| 154 | } |
| 155 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 156 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 157 | AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 158 | { |
| 159 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 160 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | return (*it).getInterestTimedOutNo(); |
| 164 | } |
| 165 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 166 | uint32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 167 | AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | { |
| 169 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 170 | if (it == m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 171 | return -1; |
| 172 | } |
| 173 | return (*it).getStatus(); |
| 174 | } |
| 175 | |
| 176 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 177 | AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, int32_t status) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 178 | { |
| 179 | std::list<Adjacent>::iterator it = find(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 180 | if (it != m_adjList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | (*it).setStatus(status); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | std::list<Adjacent>& |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 186 | AdjacencyList::getAdjList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 187 | { |
| 188 | return m_adjList; |
| 189 | } |
| 190 | |
| 191 | bool |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 192 | AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 193 | { |
| 194 | uint32_t nbrCount = 0; |
| 195 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 196 | it != m_adjList.end() ; it++) { |
| 197 | if (((*it).getStatus() == 1)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | nbrCount++; |
| 199 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 200 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 201 | if ((*it).getInterestTimedOutNo() >= |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 202 | pnlsr.getConfParameter().getInterestRetryNumber()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 203 | nbrCount++; |
| 204 | } |
| 205 | } |
| 206 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 207 | if (nbrCount == m_adjList.size()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 213 | int32_t |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 214 | AdjacencyList::getNumOfActiveNeighbor() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 216 | int32_t actNbrCount = 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 217 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 218 | it != m_adjList.end(); it++) { |
| 219 | if (((*it).getStatus() == 1)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 220 | actNbrCount++; |
| 221 | } |
| 222 | } |
| 223 | return actNbrCount; |
| 224 | } |
| 225 | |
| 226 | std::list<Adjacent>::iterator |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 227 | AdjacencyList::find(const ndn::Name& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 228 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 230 | m_adjList.end(), |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 231 | ndn::bind(&Adjacent::compare, |
| 232 | _1, ndn::cref(adjName))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 233 | return it; |
| 234 | } |
| 235 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 236 | void |
| 237 | AdjacencyList::writeLog() |
| 238 | { |
| 239 | _LOG_DEBUG("-------Adjacency List--------"); |
| 240 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
| 241 | it != m_adjList.end(); it++) { |
| 242 | (*it).writeLog(); |
| 243 | } |
| 244 | } |
| 245 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | // used for debugging purpose |
| 247 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 248 | AdjacencyList::print() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 249 | { |
| 250 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 251 | it != m_adjList.end(); it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 252 | cout << (*it) << endl; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | } //namespace nlsr |