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