blob: 64976d726555fbe47ae21d3f6c28f483d6ade0bf [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include<iostream>
2#include<algorithm>
3
4#include "nlsr_adl.hpp"
5#include "nlsr_adjacent.hpp"
6#include "nlsr.hpp"
7
akmhoque1fd8c1e2014-02-19 19:41:49 -06008namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -06009{
akmhoque298385a2014-02-13 14:13:09 -060010
akmhoque1fd8c1e2014-02-19 19:41:49 -060011 Adl::Adl()
12 {
13 }
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque1fd8c1e2014-02-19 19:41:49 -060015 Adl::~Adl()
16 {
akmhoque298385a2014-02-13 14:13:09 -060017
akmhoque1fd8c1e2014-02-19 19:41:49 -060018 }
akmhoque298385a2014-02-13 14:13:09 -060019
akmhoque1fd8c1e2014-02-19 19:41:49 -060020 static bool
21 adjacent_compare(Adjacent& adj1, Adjacent& adj2)
22 {
23 return adj1.getAdjacentName()==adj2.getAdjacentName();
24 }
akmhoque298385a2014-02-13 14:13:09 -060025
akmhoque1fd8c1e2014-02-19 19:41:49 -060026 int
27 Adl::insert(Adjacent& adj)
28 {
29 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
30 adjList.end(),
31 bind(&adjacent_compare, _1, adj));
32 if ( it != adjList.end() )
33 {
34 return -1;
35 }
36 adjList.push_back(adj);
37 return 0;
38 }
akmhoque298385a2014-02-13 14:13:09 -060039
akmhoque1fd8c1e2014-02-19 19:41:49 -060040 void
41 Adl::addAdjacentsFromAdl(Adl& adl)
42 {
43 for(std::list<Adjacent >::iterator it=adl.getAdjList().begin();
44 it!=adl.getAdjList().end(); ++it)
45 {
46 insert((*it));
47 }
48 }
akmhoque298385a2014-02-13 14:13:09 -060049
akmhoque1fd8c1e2014-02-19 19:41:49 -060050 int
51 Adl::updateAdjacentStatus(string adjName, int s)
52 {
53 Adjacent adj(adjName);
54
55 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
56 adjList.end(),
57 bind(&adjacent_compare, _1, adj));
58
59 if( it == adjList.end())
60 {
61 return -1;
62 }
63
64 (*it).setStatus(s);
65 return 0;
akmhoque298385a2014-02-13 14:13:09 -060066
67
akmhoque1fd8c1e2014-02-19 19:41:49 -060068 }
akmhoque298385a2014-02-13 14:13:09 -060069
akmhoque1fd8c1e2014-02-19 19:41:49 -060070 Adjacent
71 Adl::getAdjacent(string adjName)
72 {
73 Adjacent adj(adjName);
akmhoque298385a2014-02-13 14:13:09 -060074
akmhoque1fd8c1e2014-02-19 19:41:49 -060075 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
76 adjList.end(),
77 bind(&adjacent_compare, _1, adj));
akmhoque298385a2014-02-13 14:13:09 -060078
akmhoque1fd8c1e2014-02-19 19:41:49 -060079 if( it != adjList.end())
80 {
81 return (*it);
82 }
akmhoque298385a2014-02-13 14:13:09 -060083
akmhoque1fd8c1e2014-02-19 19:41:49 -060084 return adj;
85 }
akmhoque298385a2014-02-13 14:13:09 -060086
87
akmhoque1fd8c1e2014-02-19 19:41:49 -060088 bool
89 Adl::isAdlEqual(Adl &adl)
90 {
91 if ( getAdlSize() != adl.getAdlSize() )
92 {
93 return false;
94 }
akmhoque298385a2014-02-13 14:13:09 -060095
akmhoque1fd8c1e2014-02-19 19:41:49 -060096 adjList.sort(adjacent_compare);
97 adl.getAdjList().sort(adjacent_compare);
98 int equalAdjCount=0;
akmhoque298385a2014-02-13 14:13:09 -060099
akmhoque1fd8c1e2014-02-19 19:41:49 -0600100 std::list< Adjacent > adjList2=adl.getAdjList();
akmhoque298385a2014-02-13 14:13:09 -0600101
akmhoque1fd8c1e2014-02-19 19:41:49 -0600102 std::list<Adjacent>::iterator it1;
103 std::list<Adjacent>::iterator it2;
104 for(it1=adjList.begin() , it2=adjList2.begin() ;
105 it1!=adjList.end(); it1++,it2++)
106 {
107 if ( !(*it1).isAdjacentEqual((*it2)) )
108 {
109 break;
110 }
111 equalAdjCount++;
112 }
akmhoque298385a2014-02-13 14:13:09 -0600113
akmhoque1fd8c1e2014-02-19 19:41:49 -0600114 return equalAdjCount==getAdlSize();
115 }
akmhoque298385a2014-02-13 14:13:09 -0600116
akmhoque298385a2014-02-13 14:13:09 -0600117
akmhoque1fd8c1e2014-02-19 19:41:49 -0600118 int
119 Adl::updateAdjacentLinkCost(string adjName, double lc)
120 {
121 Adjacent adj(adjName);
akmhoque298385a2014-02-13 14:13:09 -0600122
akmhoque1fd8c1e2014-02-19 19:41:49 -0600123 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
124 adjList.end(),
125 bind(&adjacent_compare, _1, adj));
akmhoque298385a2014-02-13 14:13:09 -0600126
akmhoque1fd8c1e2014-02-19 19:41:49 -0600127 if( it == adjList.end())
128 {
129 return -1;
130 }
akmhoque298385a2014-02-13 14:13:09 -0600131
akmhoque1fd8c1e2014-02-19 19:41:49 -0600132 (*it).setLinkCost(lc);
133 return 0;
akmhoque298385a2014-02-13 14:13:09 -0600134
akmhoque1fd8c1e2014-02-19 19:41:49 -0600135 }
akmhoque298385a2014-02-13 14:13:09 -0600136
akmhoque1fd8c1e2014-02-19 19:41:49 -0600137 bool
138 Adl::isNeighbor(string adjName)
139 {
140 Adjacent adj(adjName);
141 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
142 adjList.end(),
143 bind(&adjacent_compare, _1, adj));
akmhoque298385a2014-02-13 14:13:09 -0600144
akmhoque1fd8c1e2014-02-19 19:41:49 -0600145 if( it == adjList.end())
146 {
147 return false;
148 }
akmhoque298385a2014-02-13 14:13:09 -0600149
akmhoque1fd8c1e2014-02-19 19:41:49 -0600150 return true;
151 }
akmhoque298385a2014-02-13 14:13:09 -0600152
akmhoque1fd8c1e2014-02-19 19:41:49 -0600153 void
154 Adl::incrementTimedOutInterestCount(string& neighbor)
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));
akmhoque298385a2014-02-13 14:13:09 -0600160
akmhoque1fd8c1e2014-02-19 19:41:49 -0600161 if( it == adjList.end())
162 {
163 return ;
164 }
akmhoque298385a2014-02-13 14:13:09 -0600165
akmhoque1fd8c1e2014-02-19 19:41:49 -0600166 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo()+1);
akmhoque298385a2014-02-13 14:13:09 -0600167
akmhoque1fd8c1e2014-02-19 19:41:49 -0600168 }
akmhoque298385a2014-02-13 14:13:09 -0600169
akmhoque1fd8c1e2014-02-19 19:41:49 -0600170 void
171 Adl::setTimedOutInterestCount(string& neighbor, int count)
172 {
173 Adjacent adj(neighbor);
174 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
175 adjList.end(),
176 bind(&adjacent_compare, _1, adj));
akmhoque298385a2014-02-13 14:13:09 -0600177
akmhoque1fd8c1e2014-02-19 19:41:49 -0600178 if( it != adjList.end())
179 {
180 (*it).setInterestTimedOutNo(count);
181 }
182 }
akmhoque298385a2014-02-13 14:13:09 -0600183
akmhoque1fd8c1e2014-02-19 19:41:49 -0600184 int
185 Adl::getTimedOutInterestCount(string& neighbor)
186 {
187 Adjacent adj(neighbor);
188 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
189 adjList.end(),
190 bind(&adjacent_compare, _1, adj));
akmhoque298385a2014-02-13 14:13:09 -0600191
akmhoque1fd8c1e2014-02-19 19:41:49 -0600192 if( it == adjList.end())
193 {
194 return -1;
195 }
akmhoque298385a2014-02-13 14:13:09 -0600196
akmhoque1fd8c1e2014-02-19 19:41:49 -0600197 return (*it).getInterestTimedOutNo();
198 }
akmhoque298385a2014-02-13 14:13:09 -0600199
akmhoque1fd8c1e2014-02-19 19:41:49 -0600200 int
201 Adl::getStatusOfNeighbor(string& neighbor)
202 {
203 Adjacent adj(neighbor);
204 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
205 adjList.end(),
206 bind(&adjacent_compare, _1, adj));
akmhoque298385a2014-02-13 14:13:09 -0600207
akmhoque1fd8c1e2014-02-19 19:41:49 -0600208 if( it == adjList.end())
209 {
210 return -1;
211 }
akmhoque298385a2014-02-13 14:13:09 -0600212
akmhoque1fd8c1e2014-02-19 19:41:49 -0600213 return (*it).getStatus();
214 }
akmhoque298385a2014-02-13 14:13:09 -0600215
akmhoque1fd8c1e2014-02-19 19:41:49 -0600216 void
217 Adl::setStatusOfNeighbor(string& neighbor, int status)
218 {
219 Adjacent adj(neighbor);
220 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
221 adjList.end(),
222 bind(&adjacent_compare, _1, adj));
223
224 if( it != adjList.end())
225 {
226 (*it).setStatus(status);
227 }
228 }
229
230 std::list<Adjacent>&
231 Adl::getAdjList()
232 {
233 return adjList;
234 }
235
236 bool
237 Adl::isAdjLsaBuildable(Nlsr& pnlsr)
238 {
239 int nbrCount=0;
240 for( std::list<Adjacent>::iterator it=adjList.begin();
241 it!= adjList.end() ; it++)
242 {
243 if ( ((*it).getStatus() == 1 ) )
244 {
245 nbrCount++;
246 }
247 else
248 {
249 if ( (*it).getInterestTimedOutNo() >=
250 pnlsr.getConfParameter().getInterestRetryNumber())
251 {
252 nbrCount++;
253 }
254 }
255 }
256
257 if( nbrCount == adjList.size())
258 {
259 return true;
260 }
261
262 return false;
263 }
264
265 int
266 Adl::getNumOfActiveNeighbor()
267 {
268 int actNbrCount=0;
269 for( std::list<Adjacent>::iterator it=adjList.begin();
270 it!= adjList.end() ; it++)
271 {
272 if ( ((*it).getStatus() == 1 ) )
273 {
274 actNbrCount++;
275 }
276 }
277
278 return actNbrCount;
279 }
akmhoque298385a2014-02-13 14:13:09 -0600280
281// used for debugging purpose
akmhoque1fd8c1e2014-02-19 19:41:49 -0600282 void
283 Adl::printAdl()
284 {
285 for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ;
286 it++)
287 {
288 cout<< (*it) <<endl;
289 }
290 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600291
292} //namespace nlsr