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 | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 6 | |
| 7 | Adl::Adl(){ |
| 8 | } |
| 9 | |
| 10 | Adl::~Adl(){ |
| 11 | |
| 12 | } |
| 13 | |
| 14 | static bool |
| 15 | adjacent_compare(Adjacent& adj1, Adjacent& adj2){ |
| 16 | return adj1.getAdjacentName()==adj2.getAdjacentName(); |
| 17 | } |
| 18 | |
| 19 | int |
| 20 | Adl::insert(Adjacent& adj){ |
| 21 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 22 | adjList.end(), |
| 23 | bind(&adjacent_compare, _1, adj)); |
| 24 | if ( it != adjList.end() ){ |
| 25 | return -1; |
| 26 | } |
| 27 | adjList.push_back(adj); |
| 28 | return 0; |
| 29 | } |
| 30 | int |
| 31 | Adl::updateAdjacentStatus(string adjName, int s){ |
| 32 | Adjacent adj(adjName); |
| 33 | |
| 34 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 35 | adjList.end(), |
| 36 | bind(&adjacent_compare, _1, adj)); |
| 37 | |
| 38 | if( it == adjList.end()){ |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | (*it).setStatus(s); |
| 43 | return 0; |
| 44 | |
| 45 | |
| 46 | } |
| 47 | |
| 48 | int |
| 49 | Adl::updateAdjacentLinkCost(string adjName, double lc){ |
| 50 | Adjacent adj(adjName); |
| 51 | |
| 52 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 53 | adjList.end(), |
| 54 | bind(&adjacent_compare, _1, adj)); |
| 55 | |
| 56 | if( it == adjList.end()){ |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | (*it).setLinkCost(lc); |
| 61 | return 0; |
| 62 | |
| 63 | } |
| 64 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 65 | bool |
| 66 | Adl::isNeighbor(string adjName){ |
| 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 | |
| 72 | if( it == adjList.end()){ |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Adl::incrementTimedOutInterestCount(string& neighbor){ |
| 81 | Adjacent adj(neighbor); |
| 82 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 83 | adjList.end(), |
| 84 | bind(&adjacent_compare, _1, adj)); |
| 85 | |
| 86 | if( it == adjList.end()){ |
| 87 | return ; |
| 88 | } |
| 89 | |
| 90 | (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo()+1); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | Adl::setTimedOutInterestCount(string& neighbor, int count){ |
| 96 | Adjacent adj(neighbor); |
| 97 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 98 | adjList.end(), |
| 99 | bind(&adjacent_compare, _1, adj)); |
| 100 | |
| 101 | if( it != adjList.end()){ |
| 102 | (*it).setInterestTimedOutNo(count); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | Adl::getTimedOutInterestCount(string& neighbor) |
| 108 | { |
| 109 | Adjacent adj(neighbor); |
| 110 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 111 | adjList.end(), |
| 112 | bind(&adjacent_compare, _1, adj)); |
| 113 | |
| 114 | if( it == adjList.end()){ |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | return (*it).getInterestTimedOutNo(); |
| 119 | } |
| 120 | |
| 121 | int |
| 122 | Adl::getStatusOfNeighbor(string& neighbor) |
| 123 | { |
| 124 | Adjacent adj(neighbor); |
| 125 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 126 | adjList.end(), |
| 127 | bind(&adjacent_compare, _1, adj)); |
| 128 | |
| 129 | if( it == adjList.end()){ |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | return (*it).getStatus(); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | Adl::setStatusOfNeighbor(string& neighbor, int status) |
| 138 | { |
| 139 | Adjacent adj(neighbor); |
| 140 | std::list<Adjacent >::iterator it = std::find_if( adjList.begin(), |
| 141 | adjList.end(), |
| 142 | bind(&adjacent_compare, _1, adj)); |
| 143 | |
| 144 | if( it != adjList.end()){ |
| 145 | (*it).setStatus(status); |
| 146 | } |
| 147 | } |
| 148 | |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 149 | std::list<Adjacent> |
| 150 | Adl::getAdjList(){ |
| 151 | return adjList; |
| 152 | } |
| 153 | |
| 154 | // used for debugging purpose |
| 155 | void |
| 156 | Adl::printAdl(){ |
| 157 | for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ; it++){ |
| 158 | cout<< (*it) <<endl; |
| 159 | } |
| 160 | } |