blob: 7f21a80775b15bcad3202aa0bdbef124d1ecc285 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <algorithm>
3
akmhoquec8a10f72014-04-25 18:42:55 -05004#include "adjacency-list.hpp"
akmhoque53353462014-04-22 08:43:45 -05005#include "adjacent.hpp"
6#include "nlsr.hpp"
7
8
9namespace nlsr {
10
akmhoquec8a10f72014-04-25 18:42:55 -050011AdjacencyList::AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050012{
13}
14
akmhoquec8a10f72014-04-25 18:42:55 -050015AdjacencyList::~AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050016{
17}
18
19static bool
20adjacent_compare(Adjacent& adj1, Adjacent& adj2)
21{
22 return adj1.getName() == adj2.getName();
23}
24
25int
akmhoquec8a10f72014-04-25 18:42:55 -050026AdjacencyList::insert(Adjacent& adj)
akmhoque53353462014-04-22 08:43:45 -050027{
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
akmhoquec8a10f72014-04-25 18:42:55 -050038AdjacencyList::addAdjacentsFromAdl(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050039{
40 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
41 it != adl.getAdjList().end(); ++it)
42 {
43 insert((*it));
44 }
45}
46
47int
akmhoquec8a10f72014-04-25 18:42:55 -050048AdjacencyList::updateAdjacentStatus(const string& adjName, int s)
akmhoque53353462014-04-22 08:43:45 -050049{
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
akmhoquec8a10f72014-04-25 18:42:55 -050060AdjacencyList::getAdjacent(const string& adjName)
akmhoque53353462014-04-22 08:43:45 -050061{
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
akmhoquec8a10f72014-04-25 18:42:55 -050073AdjacencyList::isEqual(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050074{
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
akmhoquec8a10f72014-04-25 18:42:55 -050099AdjacencyList::updateAdjacentLinkCost(const string& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -0500100{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500111AdjacencyList::isNeighbor(const string& adjName)
akmhoque53353462014-04-22 08:43:45 -0500112{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500122AdjacencyList::incrementTimedOutInterestCount(const string& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500123{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500133AdjacencyList::setTimedOutInterestCount(const string& neighbor, int count)
akmhoque53353462014-04-22 08:43:45 -0500134{
135 std::list<Adjacent>::iterator it = find(neighbor);
136 if (it != m_adjList.end())
137 {
138 (*it).setInterestTimedOutNo(count);
139 }
140}
141
142int
akmhoquec8a10f72014-04-25 18:42:55 -0500143AdjacencyList::getTimedOutInterestCount(const string& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500144{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500154AdjacencyList::getStatusOfNeighbor(const string& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500155{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500165AdjacencyList::setStatusOfNeighbor(const string& neighbor, int status)
akmhoque53353462014-04-22 08:43:45 -0500166{
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>&
akmhoquec8a10f72014-04-25 18:42:55 -0500175AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500176{
177 return m_adjList;
178}
179
180bool
akmhoquec8a10f72014-04-25 18:42:55 -0500181AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500182{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500208AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500209{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500223AdjacencyList::find(std::string adjName)
akmhoque53353462014-04-22 08:43:45 -0500224{
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
akmhoquec8a10f72014-04-25 18:42:55 -0500234AdjacencyList::print()
akmhoque53353462014-04-22 08:43:45 -0500235{
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