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