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