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