blob: 117ba761e1d408d9461af6b35de56092576d9810 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <algorithm>
akmhoque31d1d4b2014-05-05 22:08:14 -05003#include <ndn-cxx/common.hpp>
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
akmhoque31d1d4b2014-05-05 22:08:14 -050044AdjacencyList::updateAdjacentStatus(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -050056AdjacencyList::getAdjacent(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -050099AdjacencyList::updateAdjacentLinkCost(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -0500111AdjacencyList::isNeighbor(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -0500122AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -0500133AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
134 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500135{
136 std::list<Adjacent>::iterator it = find(neighbor);
137 if (it != m_adjList.end())
138 {
139 (*it).setInterestTimedOutNo(count);
140 }
141}
142
akmhoquefdbddb12014-05-02 18:35:19 -0500143int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500144AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500145{
146 std::list<Adjacent>::iterator it = find(neighbor);
147 if (it == m_adjList.end())
148 {
149 return -1;
150 }
151 return (*it).getInterestTimedOutNo();
152}
153
akmhoquefdbddb12014-05-02 18:35:19 -0500154uint32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500155AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500156{
157 std::list<Adjacent>::iterator it = find(neighbor);
158 if (it == m_adjList.end())
159 {
160 return -1;
161 }
162 return (*it).getStatus();
163}
164
165void
akmhoque31d1d4b2014-05-05 22:08:14 -0500166AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, int32_t status)
akmhoque53353462014-04-22 08:43:45 -0500167{
168 std::list<Adjacent>::iterator it = find(neighbor);
169 if (it != m_adjList.end())
170 {
171 (*it).setStatus(status);
172 }
173}
174
175std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500176AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500177{
178 return m_adjList;
179}
180
181bool
akmhoquec8a10f72014-04-25 18:42:55 -0500182AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500183{
184 uint32_t nbrCount = 0;
185 for (std::list<Adjacent>::iterator it = m_adjList.begin();
186 it != m_adjList.end() ; it++)
187 {
188 if (((*it).getStatus() == 1))
189 {
190 nbrCount++;
191 }
192 else
193 {
194 if ((*it).getInterestTimedOutNo() >=
195 pnlsr.getConfParameter().getInterestRetryNumber())
196 {
197 nbrCount++;
198 }
199 }
200 }
201 if (nbrCount == m_adjList.size())
202 {
203 return true;
204 }
205 return false;
206}
207
akmhoquefdbddb12014-05-02 18:35:19 -0500208int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500209AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500210{
akmhoquefdbddb12014-05-02 18:35:19 -0500211 int32_t actNbrCount = 0;
akmhoque53353462014-04-22 08:43:45 -0500212 for (std::list<Adjacent>::iterator it = m_adjList.begin();
213 it != m_adjList.end(); it++)
214 {
215 if (((*it).getStatus() == 1))
216 {
217 actNbrCount++;
218 }
219 }
220 return actNbrCount;
221}
222
223std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500224AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500225{
akmhoque53353462014-04-22 08:43:45 -0500226 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
227 m_adjList.end(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500228 ndn::bind(&Adjacent::compare,
229 _1, ndn::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500230 return it;
231}
232
233// used for debugging purpose
234void
akmhoquec8a10f72014-04-25 18:42:55 -0500235AdjacencyList::print()
akmhoque53353462014-04-22 08:43:45 -0500236{
237 for (std::list<Adjacent>::iterator it = m_adjList.begin();
238 it != m_adjList.end(); it++)
239 {
240 cout << (*it) << endl;
241 }
242}
243
244} //namespace nlsr