blob: ab5db84dfaaf5b40b52f295ae3c8729be92971b5 [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
akmhoque5a44dd42014-03-12 18:11:32 -050011 Adl::Adl()
12 {
13 }
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque5a44dd42014-03-12 18:11:32 -050015 Adl::~Adl()
16 {
17 }
akmhoque298385a2014-02-13 14:13:09 -060018
akmhoque5a44dd42014-03-12 18:11:32 -050019 static bool
20 adjacent_compare(Adjacent& adj1, Adjacent& adj2)
21 {
22 return adj1.getAdjacentName()==adj2.getAdjacentName();
23 }
akmhoque298385a2014-02-13 14:13:09 -060024
akmhoque5a44dd42014-03-12 18:11:32 -050025 int
26 Adl::insert(Adjacent& adj)
27 {
28 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
29 adjList.end(),
30 bind(&adjacent_compare, _1, adj));
31 if ( it != adjList.end() )
akmhoque1fd8c1e2014-02-19 19:41:49 -060032 {
akmhoque5a44dd42014-03-12 18:11:32 -050033 return -1;
34 }
35 adjList.push_back(adj);
36 return 0;
37 }
38
39 void
40 Adl::addAdjacentsFromAdl(Adl& adl)
41 {
42 for(std::list<Adjacent >::iterator it=adl.getAdjList().begin();
43 it!=adl.getAdjList().end(); ++it)
44 {
45 insert((*it));
46 }
47 }
48
49 int
50 Adl::updateAdjacentStatus(string adjName, int s)
51 {
52 Adjacent adj(adjName);
53 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
54 adjList.end(),
55 bind(&adjacent_compare, _1, adj));
56 if( it == adjList.end())
57 {
58 return -1;
59 }
60 (*it).setStatus(s);
61 return 0;
62 }
63
64 Adjacent
65 Adl::getAdjacent(string adjName)
66 {
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 if( it != adjList.end())
72 {
73 return (*it);
74 }
75 return adj;
76 }
77
78
79 bool
80 Adl::isAdlEqual(Adl &adl)
81 {
82 if ( getAdlSize() != adl.getAdlSize() )
83 {
84 return false;
85 }
86 adjList.sort(adjacent_compare);
87 adl.getAdjList().sort(adjacent_compare);
88 int equalAdjCount=0;
89 std::list< Adjacent > adjList2=adl.getAdjList();
90 std::list<Adjacent>::iterator it1;
91 std::list<Adjacent>::iterator it2;
92 for(it1=adjList.begin() , it2=adjList2.begin() ;
93 it1!=adjList.end(); it1++,it2++)
94 {
95 if ( !(*it1).isAdjacentEqual((*it2)) )
96 {
97 break;
98 }
99 equalAdjCount++;
100 }
101 return equalAdjCount==getAdlSize();
102 }
103
104
105 int
106 Adl::updateAdjacentLinkCost(string adjName, double lc)
107 {
108 Adjacent adj(adjName);
109 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
110 adjList.end(),
111 bind(&adjacent_compare, _1, adj));
112 if( it == adjList.end())
113 {
114 return -1;
115 }
116 (*it).setLinkCost(lc);
117 return 0;
118 }
119
120 bool
121 Adl::isNeighbor(string adjName)
122 {
123 Adjacent adj(adjName);
124 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
125 adjList.end(),
126 bind(&adjacent_compare, _1, adj));
127 if( it == adjList.end())
128 {
129 return false;
130 }
131 return true;
132 }
133
134 void
135 Adl::incrementTimedOutInterestCount(string& neighbor)
136 {
137 Adjacent adj(neighbor);
138 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
139 adjList.end(),
140 bind(&adjacent_compare, _1, adj));
141 if( it == adjList.end())
142 {
143 return ;
144 }
145 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo()+1);
146 }
147
148 void
149 Adl::setTimedOutInterestCount(string& neighbor, int count)
150 {
151 Adjacent adj(neighbor);
152 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
153 adjList.end(),
154 bind(&adjacent_compare, _1, adj));
155 if( it != adjList.end())
156 {
157 (*it).setInterestTimedOutNo(count);
158 }
159 }
160
161 int
162 Adl::getTimedOutInterestCount(string& neighbor)
163 {
164 Adjacent adj(neighbor);
165 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
166 adjList.end(),
167 bind(&adjacent_compare, _1, adj));
168 if( it == adjList.end())
169 {
170 return -1;
171 }
172 return (*it).getInterestTimedOutNo();
173 }
174
175 int
176 Adl::getStatusOfNeighbor(string& neighbor)
177 {
178 Adjacent adj(neighbor);
179 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
180 adjList.end(),
181 bind(&adjacent_compare, _1, adj));
182 if( it == adjList.end())
183 {
184 return -1;
185 }
186 return (*it).getStatus();
187 }
188
189 void
190 Adl::setStatusOfNeighbor(string& neighbor, int status)
191 {
192 Adjacent adj(neighbor);
193 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
194 adjList.end(),
195 bind(&adjacent_compare, _1, adj));
196 if( it != adjList.end())
197 {
198 (*it).setStatus(status);
199 }
200 }
201
202 std::list<Adjacent>&
203 Adl::getAdjList()
204 {
205 return adjList;
206 }
207
208 bool
209 Adl::isAdjLsaBuildable(Nlsr& pnlsr)
210 {
211 int nbrCount=0;
212 for( std::list<Adjacent>::iterator it=adjList.begin();
213 it!= adjList.end() ; it++)
214 {
215 if ( ((*it).getStatus() == 1 ) )
216 {
217 nbrCount++;
218 }
219 else
220 {
221 if ( (*it).getInterestTimedOutNo() >=
222 pnlsr.getConfParameter().getInterestRetryNumber())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600223 {
akmhoque5a44dd42014-03-12 18:11:32 -0500224 nbrCount++;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600225 }
akmhoque5a44dd42014-03-12 18:11:32 -0500226 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600227 }
akmhoque5a44dd42014-03-12 18:11:32 -0500228 if( nbrCount == adjList.size())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600229 {
akmhoque5a44dd42014-03-12 18:11:32 -0500230 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600231 }
akmhoque5a44dd42014-03-12 18:11:32 -0500232 return false;
233 }
akmhoque298385a2014-02-13 14:13:09 -0600234
akmhoque5a44dd42014-03-12 18:11:32 -0500235 int
236 Adl::getNumOfActiveNeighbor()
237 {
238 int actNbrCount=0;
239 for( std::list<Adjacent>::iterator it=adjList.begin();
240 it!= adjList.end() ; it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600241 {
akmhoque5a44dd42014-03-12 18:11:32 -0500242 if ( ((*it).getStatus() == 1 ) )
243 {
244 actNbrCount++;
245 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600246 }
akmhoque5a44dd42014-03-12 18:11:32 -0500247 return actNbrCount;
248 }
akmhoque298385a2014-02-13 14:13:09 -0600249
250// used for debugging purpose
akmhoque5a44dd42014-03-12 18:11:32 -0500251 void
252 Adl::printAdl()
253 {
254 for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ;
255 it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600256 {
akmhoque5a44dd42014-03-12 18:11:32 -0500257 cout<< (*it) <<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600258 }
akmhoque5a44dd42014-03-12 18:11:32 -0500259 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600260
261} //namespace nlsr