blob: 57b33077f26c6c69cfa97749626b09d562af3459 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <algorithm>
3
4#include "adl.hpp"
5#include "adjacent.hpp"
6#include "nlsr.hpp"
7
8
9namespace nlsr {
10
11Adl::Adl()
12{
13}
14
15Adl::~Adl()
16{
17}
18
19static bool
20adjacent_compare(Adjacent& adj1, Adjacent& adj2)
21{
22 return adj1.getName() == adj2.getName();
23}
24
25int
26Adl::insert(Adjacent& adj)
27{
28 std::list<Adjacent>::iterator it = find(adj.getName());
29 if (it != m_adjList.end())
30 {
31 return -1;
32 }
33 m_adjList.push_back(adj);
34 return 0;
35}
36
37void
38Adl::addAdjacentsFromAdl(Adl& adl)
39{
40 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
41 it != adl.getAdjList().end(); ++it)
42 {
43 insert((*it));
44 }
45}
46
47int
48Adl::updateAdjacentStatus(string adjName, int s)
49{
50 std::list<Adjacent>::iterator it = find(adjName);
51 if (it == m_adjList.end())
52 {
53 return -1;
54 }
55 (*it).setStatus(s);
56 return 0;
57}
58
59Adjacent
60Adl::getAdjacent(string adjName)
61{
62 Adjacent adj(adjName);
63 std::list<Adjacent>::iterator it = find(adjName);
64 if (it != m_adjList.end())
65 {
66 return (*it);
67 }
68 return adj;
69}
70
71
72bool
73Adl::isEqual(Adl& adl)
74{
75 if (getSize() != adl.getSize())
76 {
77 return false;
78 }
79 m_adjList.sort(adjacent_compare);
80 adl.getAdjList().sort(adjacent_compare);
81 int equalAdjCount = 0;
82 std::list<Adjacent> adjList2 = adl.getAdjList();
83 std::list<Adjacent>::iterator it1;
84 std::list<Adjacent>::iterator it2;
85 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
86 it1 != m_adjList.end(); it1++, it2++)
87 {
88 if (!(*it1).isEqual((*it2)))
89 {
90 break;
91 }
92 equalAdjCount++;
93 }
94 return equalAdjCount == getSize();
95}
96
97
98int
99Adl::updateAdjacentLinkCost(string adjName, double lc)
100{
101 std::list<Adjacent>::iterator it = find(adjName);
102 if (it == m_adjList.end())
103 {
104 return -1;
105 }
106 (*it).setLinkCost(lc);
107 return 0;
108}
109
110bool
111Adl::isNeighbor(string adjName)
112{
113 std::list<Adjacent>::iterator it = find(adjName);
114 if (it == m_adjList.end())
115 {
116 return false;
117 }
118 return true;
119}
120
121void
122Adl::incrementTimedOutInterestCount(string& neighbor)
123{
124 std::list<Adjacent>::iterator it = find(neighbor);
125 if (it == m_adjList.end())
126 {
127 return ;
128 }
129 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
130}
131
132void
133Adl::setTimedOutInterestCount(string& neighbor, int count)
134{
135 std::list<Adjacent>::iterator it = find(neighbor);
136 if (it != m_adjList.end())
137 {
138 (*it).setInterestTimedOutNo(count);
139 }
140}
141
142int
143Adl::getTimedOutInterestCount(string& neighbor)
144{
145 std::list<Adjacent>::iterator it = find(neighbor);
146 if (it == m_adjList.end())
147 {
148 return -1;
149 }
150 return (*it).getInterestTimedOutNo();
151}
152
153int
154Adl::getStatusOfNeighbor(string& neighbor)
155{
156 std::list<Adjacent>::iterator it = find(neighbor);
157 if (it == m_adjList.end())
158 {
159 return -1;
160 }
161 return (*it).getStatus();
162}
163
164void
165Adl::setStatusOfNeighbor(string& neighbor, int status)
166{
167 std::list<Adjacent>::iterator it = find(neighbor);
168 if (it != m_adjList.end())
169 {
170 (*it).setStatus(status);
171 }
172}
173
174std::list<Adjacent>&
175Adl::getAdjList()
176{
177 return m_adjList;
178}
179
180bool
181Adl::isAdjLsaBuildable(Nlsr& pnlsr)
182{
183 uint32_t nbrCount = 0;
184 for (std::list<Adjacent>::iterator it = m_adjList.begin();
185 it != m_adjList.end() ; it++)
186 {
187 if (((*it).getStatus() == 1))
188 {
189 nbrCount++;
190 }
191 else
192 {
193 if ((*it).getInterestTimedOutNo() >=
194 pnlsr.getConfParameter().getInterestRetryNumber())
195 {
196 nbrCount++;
197 }
198 }
199 }
200 if (nbrCount == m_adjList.size())
201 {
202 return true;
203 }
204 return false;
205}
206
207int
208Adl::getNumOfActiveNeighbor()
209{
210 int actNbrCount = 0;
211 for (std::list<Adjacent>::iterator it = m_adjList.begin();
212 it != m_adjList.end(); it++)
213 {
214 if (((*it).getStatus() == 1))
215 {
216 actNbrCount++;
217 }
218 }
219 return actNbrCount;
220}
221
222std::list<Adjacent>::iterator
223Adl::find(std::string adjName)
224{
225 Adjacent adj(adjName);
226 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
227 m_adjList.end(),
228 bind(&adjacent_compare, _1, adj));
229 return it;
230}
231
232// used for debugging purpose
233void
234Adl::printAdl()
235{
236 for (std::list<Adjacent>::iterator it = m_adjList.begin();
237 it != m_adjList.end(); it++)
238 {
239 cout << (*it) << endl;
240 }
241}
242
243} //namespace nlsr