akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 1 | #include<iostream> |
| 2 | #include<algorithm> |
| 3 | |
akmhoque | 204e754 | 2014-01-31 16:08:25 -0600 | [diff] [blame] | 4 | #include "nlsr_adl.hpp" |
| 5 | #include "nlsr_adjacent.hpp" |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 6 | #include "nlsr.hpp" |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | int |
| 32 | Adl::updateAdjacentStatus(string adjName, int s){ |
| 33 | Adjacent adj(adjName); |
| 34 | |
| 35 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 36 | adjList.end(), |
| 37 | bind(&adjacent_compare, _1, adj)); |
| 38 | |
| 39 | if( it == adjList.end()){ |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | (*it).setStatus(s); |
| 44 | return 0; |
| 45 | |
| 46 | |
| 47 | } |
| 48 | |
akmhoque | f7c2c7c | 2014-02-06 11:32:43 -0600 | [diff] [blame^] | 49 | Adjacent |
| 50 | Adl::getAdjacent(string adjName) |
| 51 | { |
| 52 | Adjacent adj(adjName); |
| 53 | |
| 54 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 55 | adjList.end(), |
| 56 | bind(&adjacent_compare, _1, adj)); |
| 57 | |
| 58 | if( it != adjList.end()){ |
| 59 | return (*it); |
| 60 | } |
| 61 | |
| 62 | return adj; |
| 63 | } |
| 64 | |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 65 | int |
| 66 | Adl::updateAdjacentLinkCost(string adjName, double lc){ |
| 67 | Adjacent adj(adjName); |
| 68 | |
| 69 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 70 | adjList.end(), |
| 71 | bind(&adjacent_compare, _1, adj)); |
| 72 | |
| 73 | if( it == adjList.end()){ |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | (*it).setLinkCost(lc); |
| 78 | return 0; |
| 79 | |
| 80 | } |
| 81 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 82 | bool |
| 83 | Adl::isNeighbor(string adjName){ |
| 84 | Adjacent adj(adjName); |
| 85 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 86 | adjList.end(), |
| 87 | bind(&adjacent_compare, _1, adj)); |
| 88 | |
| 89 | if( it == adjList.end()){ |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | Adl::incrementTimedOutInterestCount(string& neighbor){ |
| 98 | Adjacent adj(neighbor); |
| 99 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 100 | adjList.end(), |
| 101 | bind(&adjacent_compare, _1, adj)); |
| 102 | |
| 103 | if( it == adjList.end()){ |
| 104 | return ; |
| 105 | } |
| 106 | |
| 107 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo()+1); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | Adl::setTimedOutInterestCount(string& neighbor, int count){ |
| 113 | Adjacent adj(neighbor); |
| 114 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 115 | adjList.end(), |
| 116 | bind(&adjacent_compare, _1, adj)); |
| 117 | |
| 118 | if( it != adjList.end()){ |
| 119 | (*it).setInterestTimedOutNo(count); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | int |
| 124 | Adl::getTimedOutInterestCount(string& neighbor) |
| 125 | { |
| 126 | Adjacent adj(neighbor); |
| 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 -1; |
| 133 | } |
| 134 | |
| 135 | return (*it).getInterestTimedOutNo(); |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | Adl::getStatusOfNeighbor(string& neighbor) |
| 140 | { |
| 141 | Adjacent adj(neighbor); |
| 142 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 143 | adjList.end(), |
| 144 | bind(&adjacent_compare, _1, adj)); |
| 145 | |
| 146 | if( it == adjList.end()){ |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | return (*it).getStatus(); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | Adl::setStatusOfNeighbor(string& neighbor, int status) |
| 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)); |
| 160 | |
| 161 | if( it != adjList.end()){ |
| 162 | (*it).setStatus(status); |
| 163 | } |
| 164 | } |
| 165 | |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 166 | std::list<Adjacent>& |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 167 | Adl::getAdjList(){ |
| 168 | return adjList; |
| 169 | } |
| 170 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 171 | bool |
| 172 | Adl::isAdjLsaBuildable(nlsr& pnlsr) |
| 173 | { |
| 174 | int nbrCount=0; |
| 175 | for( std::list<Adjacent>::iterator it=adjList.begin(); |
| 176 | it!= adjList.end() ; it++) |
| 177 | { |
| 178 | if ( ((*it).getStatus() == 1 ) ) |
| 179 | { |
| 180 | nbrCount++; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | if ( (*it).getInterestTimedOutNo() >= |
| 185 | pnlsr.getConfParameter().getInterestRetryNumber()) |
| 186 | { |
| 187 | nbrCount++; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if( nbrCount == adjList.size()) |
| 193 | { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | int |
| 201 | Adl::getNumOfActiveNeighbor() |
| 202 | { |
| 203 | int actNbrCount=0; |
| 204 | for( std::list<Adjacent>::iterator it=adjList.begin(); |
| 205 | it!= adjList.end() ; it++) |
| 206 | { |
| 207 | if ( ((*it).getStatus() == 1 ) ) |
| 208 | { |
| 209 | actNbrCount++; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return actNbrCount; |
| 214 | } |
| 215 | |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 216 | // used for debugging purpose |
| 217 | void |
| 218 | Adl::printAdl(){ |
| 219 | for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ; it++){ |
| 220 | cout<< (*it) <<endl; |
| 221 | } |
| 222 | } |