blob: a529b179f212e5b8fa892a3c9ad3e7dc0cc40304 [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
akmhoquefdbddb12014-05-02 18:35:19 -050011using namespace std;
12
akmhoquec8a10f72014-04-25 18:42:55 -050013AdjacencyList::AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050014{
15}
16
akmhoquec8a10f72014-04-25 18:42:55 -050017AdjacencyList::~AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050018{
19}
20
akmhoquefdbddb12014-05-02 18:35:19 -050021int32_t
22AdjacencyList::insert(Adjacent& adjacent)
akmhoque53353462014-04-22 08:43:45 -050023{
akmhoquefdbddb12014-05-02 18:35:19 -050024 std::list<Adjacent>::iterator it = find(adjacent.getName());
akmhoque53353462014-04-22 08:43:45 -050025 if (it != m_adjList.end())
26 {
27 return -1;
28 }
akmhoquefdbddb12014-05-02 18:35:19 -050029 m_adjList.push_back(adjacent);
akmhoque53353462014-04-22 08:43:45 -050030 return 0;
31}
32
33void
akmhoquefdbddb12014-05-02 18:35:19 -050034AdjacencyList::addAdjacents(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050035{
36 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
37 it != adl.getAdjList().end(); ++it)
38 {
39 insert((*it));
40 }
41}
42
akmhoquefdbddb12014-05-02 18:35:19 -050043int32_t
44AdjacencyList::updateAdjacentStatus(const string& adjName, int32_t s)
akmhoque53353462014-04-22 08:43:45 -050045{
46 std::list<Adjacent>::iterator it = find(adjName);
47 if (it == m_adjList.end())
48 {
49 return -1;
50 }
51 (*it).setStatus(s);
52 return 0;
53}
54
55Adjacent
akmhoquec8a10f72014-04-25 18:42:55 -050056AdjacencyList::getAdjacent(const string& adjName)
akmhoque53353462014-04-22 08:43:45 -050057{
58 Adjacent adj(adjName);
59 std::list<Adjacent>::iterator it = find(adjName);
60 if (it != m_adjList.end())
61 {
62 return (*it);
63 }
64 return adj;
65}
66
akmhoquefdbddb12014-05-02 18:35:19 -050067static bool
68compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
69{
70 return adjacent1.getName() < adjacent2.getName();
71}
akmhoque53353462014-04-22 08:43:45 -050072
73bool
akmhoquefdbddb12014-05-02 18:35:19 -050074AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050075{
76 if (getSize() != adl.getSize())
77 {
78 return false;
79 }
akmhoquefdbddb12014-05-02 18:35:19 -050080 m_adjList.sort(compareAdjacent);
81 adl.getAdjList().sort(compareAdjacent);
82 uint32_t equalAdjCount = 0;
83 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -050084 std::list<Adjacent>::iterator it1;
85 std::list<Adjacent>::iterator it2;
86 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
87 it1 != m_adjList.end(); it1++, it2++)
88 {
akmhoquefdbddb12014-05-02 18:35:19 -050089 if (!((*it1) == (*it2)))
akmhoque53353462014-04-22 08:43:45 -050090 {
91 break;
92 }
93 equalAdjCount++;
94 }
95 return equalAdjCount == getSize();
96}
97
akmhoquefdbddb12014-05-02 18:35:19 -050098int32_t
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
akmhoquefdbddb12014-05-02 18:35:19 -0500133AdjacencyList::setTimedOutInterestCount(const string& neighbor, uint32_t 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
akmhoquefdbddb12014-05-02 18:35:19 -0500142int32_t
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
akmhoquefdbddb12014-05-02 18:35:19 -0500153uint32_t
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
akmhoquefdbddb12014-05-02 18:35:19 -0500165AdjacencyList::setStatusOfNeighbor(const string& neighbor, int32_t 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
akmhoquefdbddb12014-05-02 18:35:19 -0500207int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500208AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500209{
akmhoquefdbddb12014-05-02 18:35:19 -0500210 int32_t actNbrCount = 0;
akmhoque53353462014-04-22 08:43:45 -0500211 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(),
akmhoquefdbddb12014-05-02 18:35:19 -0500228 bind(&Adjacent::compareName, &adj, _1));
akmhoque53353462014-04-22 08:43:45 -0500229 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