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