blob: 0f4be8f9146f07fe219a66b07d50d3e69e97de75 [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
49int
50Adl::updateAdjacentLinkCost(string adjName, double lc){
51 Adjacent adj(adjName);
52
53 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
54 adjList.end(),
55 bind(&adjacent_compare, _1, adj));
56
57 if( it == adjList.end()){
58 return -1;
59 }
60
61 (*it).setLinkCost(lc);
62 return 0;
63
64}
65
akmhoquea8cd6b92014-01-31 20:13:26 -060066bool
67Adl::isNeighbor(string adjName){
68 Adjacent adj(adjName);
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 false;
75 }
76
77 return true;
78}
79
80void
81Adl::incrementTimedOutInterestCount(string& neighbor){
82 Adjacent adj(neighbor);
83 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
84 adjList.end(),
85 bind(&adjacent_compare, _1, adj));
86
87 if( it == adjList.end()){
88 return ;
89 }
90
91 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo()+1);
92
93}
94
95void
96Adl::setTimedOutInterestCount(string& neighbor, int count){
97 Adjacent adj(neighbor);
98 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
99 adjList.end(),
100 bind(&adjacent_compare, _1, adj));
101
102 if( it != adjList.end()){
103 (*it).setInterestTimedOutNo(count);
104 }
105}
106
107int
108Adl::getTimedOutInterestCount(string& neighbor)
109{
110 Adjacent adj(neighbor);
111 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
112 adjList.end(),
113 bind(&adjacent_compare, _1, adj));
114
115 if( it == adjList.end()){
116 return -1;
117 }
118
119 return (*it).getInterestTimedOutNo();
120}
121
122int
123Adl::getStatusOfNeighbor(string& neighbor)
124{
125 Adjacent adj(neighbor);
126 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
127 adjList.end(),
128 bind(&adjacent_compare, _1, adj));
129
130 if( it == adjList.end()){
131 return -1;
132 }
133
134 return (*it).getStatus();
135}
136
137void
138Adl::setStatusOfNeighbor(string& neighbor, int status)
139{
140 Adjacent adj(neighbor);
141 std::list<Adjacent >::iterator it = std::find_if( adjList.begin(),
142 adjList.end(),
143 bind(&adjacent_compare, _1, adj));
144
145 if( it != adjList.end()){
146 (*it).setStatus(status);
147 }
148}
149
akmhoque87347a32014-01-31 11:00:44 -0600150std::list<Adjacent>
151Adl::getAdjList(){
152 return adjList;
153}
154
akmhoquecd552472014-02-01 21:22:16 -0600155bool
156Adl::isAdjLsaBuildable(nlsr& pnlsr)
157{
158 int nbrCount=0;
159 for( std::list<Adjacent>::iterator it=adjList.begin();
160 it!= adjList.end() ; it++)
161 {
162 if ( ((*it).getStatus() == 1 ) )
163 {
164 nbrCount++;
165 }
166 else
167 {
168 if ( (*it).getInterestTimedOutNo() >=
169 pnlsr.getConfParameter().getInterestRetryNumber())
170 {
171 nbrCount++;
172 }
173 }
174 }
175
176 if( nbrCount == adjList.size())
177 {
178 return true;
179 }
180
181 return false;
182}
183
184int
185Adl::getNumOfActiveNeighbor()
186{
187 int actNbrCount=0;
188 for( std::list<Adjacent>::iterator it=adjList.begin();
189 it!= adjList.end() ; it++)
190 {
191 if ( ((*it).getStatus() == 1 ) )
192 {
193 actNbrCount++;
194 }
195 }
196
197 return actNbrCount;
198}
199
akmhoque87347a32014-01-31 11:00:44 -0600200// used for debugging purpose
201void
202Adl::printAdl(){
203 for( std::list<Adjacent>::iterator it=adjList.begin(); it!= adjList.end() ; it++){
204 cout<< (*it) <<endl;
205 }
206}