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