akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | #include <algorithm> |
| 3 | |
| 4 | #include "adl.hpp" |
| 5 | #include "adjacent.hpp" |
| 6 | #include "nlsr.hpp" |
| 7 | |
| 8 | |
| 9 | namespace nlsr { |
| 10 | |
| 11 | Adl::Adl() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | Adl::~Adl() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | static bool |
| 20 | adjacent_compare(Adjacent& adj1, Adjacent& adj2) |
| 21 | { |
| 22 | return adj1.getName() == adj2.getName(); |
| 23 | } |
| 24 | |
| 25 | int |
| 26 | Adl::insert(Adjacent& adj) |
| 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 |
| 38 | Adl::addAdjacentsFromAdl(Adl& adl) |
| 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 |
| 48 | Adl::updateAdjacentStatus(string adjName, int s) |
| 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 |
| 60 | Adl::getAdjacent(string adjName) |
| 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 |
| 73 | Adl::isEqual(Adl& adl) |
| 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 |
| 99 | Adl::updateAdjacentLinkCost(string adjName, double lc) |
| 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 |
| 111 | Adl::isNeighbor(string adjName) |
| 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 |
| 122 | Adl::incrementTimedOutInterestCount(string& neighbor) |
| 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 |
| 133 | Adl::setTimedOutInterestCount(string& neighbor, int count) |
| 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 |
| 143 | Adl::getTimedOutInterestCount(string& neighbor) |
| 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 |
| 154 | Adl::getStatusOfNeighbor(string& neighbor) |
| 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 |
| 165 | Adl::setStatusOfNeighbor(string& neighbor, int status) |
| 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>& |
| 175 | Adl::getAdjList() |
| 176 | { |
| 177 | return m_adjList; |
| 178 | } |
| 179 | |
| 180 | bool |
| 181 | Adl::isAdjLsaBuildable(Nlsr& pnlsr) |
| 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 |
| 208 | Adl::getNumOfActiveNeighbor() |
| 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 |
| 223 | Adl::find(std::string adjName) |
| 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 |
| 234 | Adl::printAdl() |
| 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 |