blob: 055a7923c75417714b5964328ad96a0d98886666 [file] [log] [blame]
akmhoque87347a32014-01-31 11:00:44 -06001#include<iostream>
2#include<algorithm>
3
akmhoque204e7542014-01-31 16:08:25 -06004#include "nlsr_adl.hpp"
5#include "nlsr_adjacent.hpp"
akmhoquecd552472014-02-01 21:22:16 -06006#include "nlsr.hpp"
akmhoque87347a32014-01-31 11:00:44 -06007
8Adl::Adl(){
9}
10
11Adl::~Adl(){
12
13}
14
15static bool
16adjacent_compare(Adjacent& adj1, Adjacent& adj2){
17 return adj1.getAdjacentName()==adj2.getAdjacentName();
18}
19
20int
21Adl::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}
31int
32Adl::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
akmhoquef7c2c7c2014-02-06 11:32:43 -060049Adjacent
50Adl::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
akmhoque87347a32014-01-31 11:00:44 -060065int
66Adl::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
akmhoquea8cd6b92014-01-31 20:13:26 -060082bool
83Adl::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
96void
97Adl::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
111void
112Adl::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
123int
124Adl::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
138int
139Adl::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
153void
154Adl::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
akmhoque79d355f2014-02-04 15:11:16 -0600166std::list<Adjacent>&
akmhoque87347a32014-01-31 11:00:44 -0600167Adl::getAdjList(){
168 return adjList;
169}
170
akmhoquecd552472014-02-01 21:22:16 -0600171bool
172Adl::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
200int
201Adl::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
akmhoque87347a32014-01-31 11:00:44 -0600216// used for debugging purpose
217void
218Adl::printAdl(){
219 for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ; it++){
220 cout<< (*it) <<endl;
221 }
222}