akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <algorithm> |
| 3 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 4 | #include "adjacency-list.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 5 | #include "adjacent.hpp" |
| 6 | #include "nlsr.hpp" |
| 7 | |
| 8 | |
| 9 | namespace nlsr { |
| 10 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 11 | AdjacencyList::AdjacencyList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 12 | { |
| 13 | } |
| 14 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 15 | AdjacencyList::~AdjacencyList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 16 | { |
| 17 | } |
| 18 | |
| 19 | static bool |
| 20 | adjacent_compare(Adjacent& adj1, Adjacent& adj2) |
| 21 | { |
| 22 | return adj1.getName() == adj2.getName(); |
| 23 | } |
| 24 | |
| 25 | int |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 26 | AdjacencyList::insert(Adjacent& adj) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | { |
| 28 | std::list<Adjacent>::iterator it = find(adj.getName()); |
| 29 | if (it != m_adjList.end()) |
| 30 | { |
| 31 | return -1; |
| 32 | } |
| 33 | m_adjList.push_back(adj); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 38 | AdjacencyList::addAdjacentsFromAdl(AdjacencyList& adl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | { |
| 40 | for (std::list<Adjacent>::iterator it = adl.getAdjList().begin(); |
| 41 | it != adl.getAdjList().end(); ++it) |
| 42 | { |
| 43 | insert((*it)); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | int |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 48 | AdjacencyList::updateAdjacentStatus(const string& adjName, int s) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 49 | { |
| 50 | std::list<Adjacent>::iterator it = find(adjName); |
| 51 | if (it == m_adjList.end()) |
| 52 | { |
| 53 | return -1; |
| 54 | } |
| 55 | (*it).setStatus(s); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | Adjacent |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 60 | AdjacencyList::getAdjacent(const string& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | { |
| 62 | Adjacent adj(adjName); |
| 63 | std::list<Adjacent>::iterator it = find(adjName); |
| 64 | if (it != m_adjList.end()) |
| 65 | { |
| 66 | return (*it); |
| 67 | } |
| 68 | return adj; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | bool |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 73 | AdjacencyList::isEqual(AdjacencyList& adl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | { |
| 75 | if (getSize() != adl.getSize()) |
| 76 | { |
| 77 | return false; |
| 78 | } |
| 79 | m_adjList.sort(adjacent_compare); |
| 80 | adl.getAdjList().sort(adjacent_compare); |
| 81 | int equalAdjCount = 0; |
| 82 | std::list<Adjacent> adjList2 = adl.getAdjList(); |
| 83 | std::list<Adjacent>::iterator it1; |
| 84 | std::list<Adjacent>::iterator it2; |
| 85 | for (it1 = m_adjList.begin(), it2 = adjList2.begin(); |
| 86 | it1 != m_adjList.end(); it1++, it2++) |
| 87 | { |
| 88 | if (!(*it1).isEqual((*it2))) |
| 89 | { |
| 90 | break; |
| 91 | } |
| 92 | equalAdjCount++; |
| 93 | } |
| 94 | return equalAdjCount == getSize(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | int |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 99 | AdjacencyList::updateAdjacentLinkCost(const string& adjName, double lc) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 100 | { |
| 101 | std::list<Adjacent>::iterator it = find(adjName); |
| 102 | if (it == m_adjList.end()) |
| 103 | { |
| 104 | return -1; |
| 105 | } |
| 106 | (*it).setLinkCost(lc); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | bool |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 111 | AdjacencyList::isNeighbor(const string& adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 112 | { |
| 113 | std::list<Adjacent>::iterator it = find(adjName); |
| 114 | if (it == m_adjList.end()) |
| 115 | { |
| 116 | return false; |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 122 | AdjacencyList::incrementTimedOutInterestCount(const string& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | { |
| 124 | std::list<Adjacent>::iterator it = find(neighbor); |
| 125 | if (it == m_adjList.end()) |
| 126 | { |
| 127 | return ; |
| 128 | } |
| 129 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1); |
| 130 | } |
| 131 | |
| 132 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 133 | AdjacencyList::setTimedOutInterestCount(const string& neighbor, int count) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | { |
| 135 | std::list<Adjacent>::iterator it = find(neighbor); |
| 136 | if (it != m_adjList.end()) |
| 137 | { |
| 138 | (*it).setInterestTimedOutNo(count); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | int |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 143 | AdjacencyList::getTimedOutInterestCount(const string& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 144 | { |
| 145 | std::list<Adjacent>::iterator it = find(neighbor); |
| 146 | if (it == m_adjList.end()) |
| 147 | { |
| 148 | return -1; |
| 149 | } |
| 150 | return (*it).getInterestTimedOutNo(); |
| 151 | } |
| 152 | |
| 153 | int |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 154 | AdjacencyList::getStatusOfNeighbor(const string& neighbor) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 155 | { |
| 156 | std::list<Adjacent>::iterator it = find(neighbor); |
| 157 | if (it == m_adjList.end()) |
| 158 | { |
| 159 | return -1; |
| 160 | } |
| 161 | return (*it).getStatus(); |
| 162 | } |
| 163 | |
| 164 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 165 | AdjacencyList::setStatusOfNeighbor(const string& neighbor, int status) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | { |
| 167 | std::list<Adjacent>::iterator it = find(neighbor); |
| 168 | if (it != m_adjList.end()) |
| 169 | { |
| 170 | (*it).setStatus(status); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | std::list<Adjacent>& |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 175 | AdjacencyList::getAdjList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 176 | { |
| 177 | return m_adjList; |
| 178 | } |
| 179 | |
| 180 | bool |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 181 | AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | { |
| 183 | uint32_t nbrCount = 0; |
| 184 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
| 185 | it != m_adjList.end() ; it++) |
| 186 | { |
| 187 | if (((*it).getStatus() == 1)) |
| 188 | { |
| 189 | nbrCount++; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | if ((*it).getInterestTimedOutNo() >= |
| 194 | pnlsr.getConfParameter().getInterestRetryNumber()) |
| 195 | { |
| 196 | nbrCount++; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | if (nbrCount == m_adjList.size()) |
| 201 | { |
| 202 | return true; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | int |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 208 | AdjacencyList::getNumOfActiveNeighbor() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 209 | { |
| 210 | int actNbrCount = 0; |
| 211 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
| 212 | it != m_adjList.end(); it++) |
| 213 | { |
| 214 | if (((*it).getStatus() == 1)) |
| 215 | { |
| 216 | actNbrCount++; |
| 217 | } |
| 218 | } |
| 219 | return actNbrCount; |
| 220 | } |
| 221 | |
| 222 | std::list<Adjacent>::iterator |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 223 | AdjacencyList::find(std::string adjName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | { |
| 225 | Adjacent adj(adjName); |
| 226 | std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(), |
| 227 | m_adjList.end(), |
| 228 | bind(&adjacent_compare, _1, adj)); |
| 229 | return it; |
| 230 | } |
| 231 | |
| 232 | // used for debugging purpose |
| 233 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 234 | AdjacencyList::print() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | { |
| 236 | for (std::list<Adjacent>::iterator it = m_adjList.begin(); |
| 237 | it != m_adjList.end(); it++) |
| 238 | { |
| 239 | cout << (*it) << endl; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | } //namespace nlsr |