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 | |
| 49 | int |
| 50 | Adl::updateAdjacentLinkCost(string adjName, double lc){ |
| 51 | Adjacent adj(adjName); |
| 52 | |
| 53 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 54 | adjList.end(), |
| 55 | bind(&adjacent_compare, _1, adj)); |
| 56 | |
| 57 | if( it == adjList.end()){ |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | (*it).setLinkCost(lc); |
| 62 | return 0; |
| 63 | |
| 64 | } |
| 65 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 66 | bool |
| 67 | Adl::isNeighbor(string adjName){ |
| 68 | Adjacent adj(adjName); |
| 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 false; |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | Adl::incrementTimedOutInterestCount(string& neighbor){ |
| 82 | Adjacent adj(neighbor); |
| 83 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 84 | adjList.end(), |
| 85 | bind(&adjacent_compare, _1, adj)); |
| 86 | |
| 87 | if( it == adjList.end()){ |
| 88 | return ; |
| 89 | } |
| 90 | |
| 91 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo()+1); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | Adl::setTimedOutInterestCount(string& neighbor, int count){ |
| 97 | Adjacent adj(neighbor); |
| 98 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 99 | adjList.end(), |
| 100 | bind(&adjacent_compare, _1, adj)); |
| 101 | |
| 102 | if( it != adjList.end()){ |
| 103 | (*it).setInterestTimedOutNo(count); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int |
| 108 | Adl::getTimedOutInterestCount(string& neighbor) |
| 109 | { |
| 110 | Adjacent adj(neighbor); |
| 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 | return (*it).getInterestTimedOutNo(); |
| 120 | } |
| 121 | |
| 122 | int |
| 123 | Adl::getStatusOfNeighbor(string& neighbor) |
| 124 | { |
| 125 | Adjacent adj(neighbor); |
| 126 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 127 | adjList.end(), |
| 128 | bind(&adjacent_compare, _1, adj)); |
| 129 | |
| 130 | if( it == adjList.end()){ |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | return (*it).getStatus(); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | Adl::setStatusOfNeighbor(string& neighbor, int status) |
| 139 | { |
| 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 | (*it).setStatus(status); |
| 147 | } |
| 148 | } |
| 149 | |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 150 | std::list<Adjacent>& |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 151 | Adl::getAdjList(){ |
| 152 | return adjList; |
| 153 | } |
| 154 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 155 | bool |
| 156 | Adl::isAdjLsaBuildable(nlsr& pnlsr) |
| 157 | { |
| 158 | int nbrCount=0; |
| 159 | for( std::list<Adjacent>::iterator it=adjList.begin(); |
| 160 | it!= adjList.end() ; it++) |
| 161 | { |
| 162 | if ( ((*it).getStatus() == 1 ) ) |
| 163 | { |
| 164 | nbrCount++; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | if ( (*it).getInterestTimedOutNo() >= |
| 169 | pnlsr.getConfParameter().getInterestRetryNumber()) |
| 170 | { |
| 171 | nbrCount++; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if( nbrCount == adjList.size()) |
| 177 | { |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | Adl::getNumOfActiveNeighbor() |
| 186 | { |
| 187 | int actNbrCount=0; |
| 188 | for( std::list<Adjacent>::iterator it=adjList.begin(); |
| 189 | it!= adjList.end() ; it++) |
| 190 | { |
| 191 | if ( ((*it).getStatus() == 1 ) ) |
| 192 | { |
| 193 | actNbrCount++; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return actNbrCount; |
| 198 | } |
| 199 | |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 200 | // used for debugging purpose |
| 201 | void |
| 202 | Adl::printAdl(){ |
| 203 | for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ; it++){ |
| 204 | cout<< (*it) <<endl; |
| 205 | } |
| 206 | } |