blob: fd84f9ecc46c1f6801fa2ee3e147d3742d755ca0 [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"
akmhoque05d5fcf2014-04-15 14:58:45 -05007#include "utility/nlsr_logger.hpp"
8
9#define THIS_FILE "nlsr_adl.cpp"
akmhoque298385a2014-02-13 14:13:09 -060010
akmhoque1fd8c1e2014-02-19 19:41:49 -060011namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060012{
akmhoque298385a2014-02-13 14:13:09 -060013
akmhoque5a44dd42014-03-12 18:11:32 -050014 Adl::Adl()
15 {
16 }
akmhoque298385a2014-02-13 14:13:09 -060017
akmhoque5a44dd42014-03-12 18:11:32 -050018 Adl::~Adl()
19 {
20 }
akmhoque298385a2014-02-13 14:13:09 -060021
akmhoque5a44dd42014-03-12 18:11:32 -050022 static bool
23 adjacent_compare(Adjacent& adj1, Adjacent& adj2)
24 {
akmhoque05d5fcf2014-04-15 14:58:45 -050025 return adj1.getName()==adj2.getName();
akmhoque5a44dd42014-03-12 18:11:32 -050026 }
akmhoque298385a2014-02-13 14:13:09 -060027
akmhoque5a44dd42014-03-12 18:11:32 -050028 int
29 Adl::insert(Adjacent& adj)
30 {
akmhoque05d5fcf2014-04-15 14:58:45 -050031 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
32 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050033 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -050034 if ( it != m_adjList.end() )
akmhoque1fd8c1e2014-02-19 19:41:49 -060035 {
akmhoque5a44dd42014-03-12 18:11:32 -050036 return -1;
37 }
akmhoque05d5fcf2014-04-15 14:58:45 -050038 m_adjList.push_back(adj);
akmhoque5a44dd42014-03-12 18:11:32 -050039 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);
akmhoque05d5fcf2014-04-15 14:58:45 -050056 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
57 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050058 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -050059 if( it == m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -050060 {
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);
akmhoque05d5fcf2014-04-15 14:58:45 -050071 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
72 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050073 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -050074 if( it != m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -050075 {
76 return (*it);
77 }
78 return adj;
79 }
80
81
82 bool
akmhoque05d5fcf2014-04-15 14:58:45 -050083 Adl::isEqual(Adl& adl)
akmhoque5a44dd42014-03-12 18:11:32 -050084 {
akmhoque05d5fcf2014-04-15 14:58:45 -050085 if ( getSize() != adl.getSize() )
akmhoque5a44dd42014-03-12 18:11:32 -050086 {
87 return false;
88 }
akmhoque05d5fcf2014-04-15 14:58:45 -050089 m_adjList.sort(adjacent_compare);
akmhoque5a44dd42014-03-12 18:11:32 -050090 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;
akmhoque05d5fcf2014-04-15 14:58:45 -050095 for(it1=m_adjList.begin() , it2=adjList2.begin() ;
96 it1!=m_adjList.end(); it1++,it2++)
akmhoque5a44dd42014-03-12 18:11:32 -050097 {
akmhoque05d5fcf2014-04-15 14:58:45 -050098 if ( !(*it1).isEqual((*it2)) )
akmhoque5a44dd42014-03-12 18:11:32 -050099 {
100 break;
101 }
102 equalAdjCount++;
103 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500104 return equalAdjCount==getSize();
akmhoque5a44dd42014-03-12 18:11:32 -0500105 }
106
107
108 int
109 Adl::updateAdjacentLinkCost(string adjName, double lc)
110 {
111 Adjacent adj(adjName);
akmhoque05d5fcf2014-04-15 14:58:45 -0500112 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
113 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500114 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500115 if( it == m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500116 {
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);
akmhoque05d5fcf2014-04-15 14:58:45 -0500127 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
128 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500129 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500130 if( it == m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500131 {
132 return false;
133 }
134 return true;
135 }
136
137 void
138 Adl::incrementTimedOutInterestCount(string& neighbor)
139 {
140 Adjacent adj(neighbor);
akmhoque05d5fcf2014-04-15 14:58:45 -0500141 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
142 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500143 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500144 if( it == m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500145 {
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);
akmhoque05d5fcf2014-04-15 14:58:45 -0500155 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
156 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500157 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500158 if( it != m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500159 {
160 (*it).setInterestTimedOutNo(count);
161 }
162 }
163
164 int
165 Adl::getTimedOutInterestCount(string& neighbor)
166 {
167 Adjacent adj(neighbor);
akmhoque05d5fcf2014-04-15 14:58:45 -0500168 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
169 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500170 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500171 if( it == m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500172 {
173 return -1;
174 }
175 return (*it).getInterestTimedOutNo();
176 }
177
178 int
179 Adl::getStatusOfNeighbor(string& neighbor)
180 {
181 Adjacent adj(neighbor);
akmhoque05d5fcf2014-04-15 14:58:45 -0500182 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
183 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500184 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500185 if( it == m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500186 {
187 return -1;
188 }
189 return (*it).getStatus();
190 }
191
192 void
193 Adl::setStatusOfNeighbor(string& neighbor, int status)
194 {
195 Adjacent adj(neighbor);
akmhoque05d5fcf2014-04-15 14:58:45 -0500196 std::list<Adjacent >::iterator it = std::find_if( m_adjList.begin(),
197 m_adjList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500198 bind(&adjacent_compare, _1, adj));
akmhoque05d5fcf2014-04-15 14:58:45 -0500199 if( it != m_adjList.end())
akmhoque5a44dd42014-03-12 18:11:32 -0500200 {
201 (*it).setStatus(status);
202 }
203 }
204
205 std::list<Adjacent>&
206 Adl::getAdjList()
207 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500208 return m_adjList;
akmhoque5a44dd42014-03-12 18:11:32 -0500209 }
210
211 bool
212 Adl::isAdjLsaBuildable(Nlsr& pnlsr)
213 {
214 int nbrCount=0;
akmhoque05d5fcf2014-04-15 14:58:45 -0500215 for( std::list<Adjacent>::iterator it=m_adjList.begin();
216 it!= m_adjList.end() ; it++)
akmhoque5a44dd42014-03-12 18:11:32 -0500217 {
218 if ( ((*it).getStatus() == 1 ) )
219 {
220 nbrCount++;
221 }
222 else
223 {
224 if ( (*it).getInterestTimedOutNo() >=
225 pnlsr.getConfParameter().getInterestRetryNumber())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600226 {
akmhoque5a44dd42014-03-12 18:11:32 -0500227 nbrCount++;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600228 }
akmhoque5a44dd42014-03-12 18:11:32 -0500229 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600230 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500231 if( nbrCount == m_adjList.size())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600232 {
akmhoque5a44dd42014-03-12 18:11:32 -0500233 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600234 }
akmhoque5a44dd42014-03-12 18:11:32 -0500235 return false;
236 }
akmhoque298385a2014-02-13 14:13:09 -0600237
akmhoque5a44dd42014-03-12 18:11:32 -0500238 int
239 Adl::getNumOfActiveNeighbor()
240 {
241 int actNbrCount=0;
akmhoque05d5fcf2014-04-15 14:58:45 -0500242 for( std::list<Adjacent>::iterator it=m_adjList.begin();
243 it!= m_adjList.end() ; it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600244 {
akmhoque5a44dd42014-03-12 18:11:32 -0500245 if ( ((*it).getStatus() == 1 ) )
246 {
247 actNbrCount++;
248 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600249 }
akmhoque5a44dd42014-03-12 18:11:32 -0500250 return actNbrCount;
251 }
akmhoque298385a2014-02-13 14:13:09 -0600252
253// used for debugging purpose
akmhoque5a44dd42014-03-12 18:11:32 -0500254 void
255 Adl::printAdl()
256 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500257 for( std::list<Adjacent>::iterator it=m_adjList.begin(); it!= m_adjList.end() ;
akmhoque5a44dd42014-03-12 18:11:32 -0500258 it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600259 {
akmhoque5a44dd42014-03-12 18:11:32 -0500260 cout<< (*it) <<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600261 }
akmhoque5a44dd42014-03-12 18:11:32 -0500262 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600263
264} //namespace nlsr