blob: 48e35cf60001c05a4a5c5b7c89da96ec9dd24d8a [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());
akmhoque157b0a42014-05-13 00:26:37 -050025 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050026 return -1;
27 }
akmhoquefdbddb12014-05-02 18:35:19 -050028 m_adjList.push_back(adjacent);
akmhoque53353462014-04-22 08:43:45 -050029 return 0;
30}
31
32void
akmhoquefdbddb12014-05-02 18:35:19 -050033AdjacencyList::addAdjacents(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050034{
35 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
akmhoque157b0a42014-05-13 00:26:37 -050036 it != adl.getAdjList().end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -050037 insert((*it));
38 }
39}
40
akmhoquefdbddb12014-05-02 18:35:19 -050041int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050042AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, int32_t s)
akmhoque53353462014-04-22 08:43:45 -050043{
44 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050045 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050046 return -1;
47 }
48 (*it).setStatus(s);
49 return 0;
50}
51
52Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050053AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050054{
55 Adjacent adj(adjName);
56 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050057 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050058 return (*it);
59 }
60 return adj;
61}
62
akmhoquefdbddb12014-05-02 18:35:19 -050063static bool
64compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
65{
66 return adjacent1.getName() < adjacent2.getName();
67}
akmhoque53353462014-04-22 08:43:45 -050068
69bool
akmhoquefdbddb12014-05-02 18:35:19 -050070AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050071{
akmhoque157b0a42014-05-13 00:26:37 -050072 if (getSize() != adl.getSize()) {
akmhoque53353462014-04-22 08:43:45 -050073 return false;
74 }
akmhoquefdbddb12014-05-02 18:35:19 -050075 m_adjList.sort(compareAdjacent);
76 adl.getAdjList().sort(compareAdjacent);
77 uint32_t equalAdjCount = 0;
78 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -050079 std::list<Adjacent>::iterator it1;
80 std::list<Adjacent>::iterator it2;
81 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
akmhoque157b0a42014-05-13 00:26:37 -050082 it1 != m_adjList.end(); it1++, it2++) {
83 if (!((*it1) == (*it2))) {
akmhoque53353462014-04-22 08:43:45 -050084 break;
85 }
86 equalAdjCount++;
87 }
88 return equalAdjCount == getSize();
89}
90
akmhoquefdbddb12014-05-02 18:35:19 -050091int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050092AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -050093{
94 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050095 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050096 return -1;
97 }
98 (*it).setLinkCost(lc);
99 return 0;
100}
101
102bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500103AdjacencyList::isNeighbor(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500104{
105 std::list<Adjacent>::iterator it = find(adjName);
106 if (it == m_adjList.end())
107 {
108 return false;
109 }
110 return true;
111}
112
113void
akmhoque31d1d4b2014-05-05 22:08:14 -0500114AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500115{
116 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500117 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500118 return ;
119 }
120 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
121}
122
123void
akmhoque31d1d4b2014-05-05 22:08:14 -0500124AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
125 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500126{
127 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500128 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500129 (*it).setInterestTimedOutNo(count);
130 }
131}
132
akmhoquefdbddb12014-05-02 18:35:19 -0500133int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500134AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500135{
136 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500137 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500138 return -1;
139 }
140 return (*it).getInterestTimedOutNo();
141}
142
akmhoquefdbddb12014-05-02 18:35:19 -0500143uint32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500144AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500145{
146 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500147 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500148 return -1;
149 }
150 return (*it).getStatus();
151}
152
153void
akmhoque31d1d4b2014-05-05 22:08:14 -0500154AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, int32_t status)
akmhoque53353462014-04-22 08:43:45 -0500155{
156 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500157 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500158 (*it).setStatus(status);
159 }
160}
161
162std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500163AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500164{
165 return m_adjList;
166}
167
168bool
akmhoquec8a10f72014-04-25 18:42:55 -0500169AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500170{
171 uint32_t nbrCount = 0;
172 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500173 it != m_adjList.end() ; it++) {
174 if (((*it).getStatus() == 1)) {
akmhoque53353462014-04-22 08:43:45 -0500175 nbrCount++;
176 }
akmhoque157b0a42014-05-13 00:26:37 -0500177 else {
akmhoque53353462014-04-22 08:43:45 -0500178 if ((*it).getInterestTimedOutNo() >=
akmhoque157b0a42014-05-13 00:26:37 -0500179 pnlsr.getConfParameter().getInterestRetryNumber()) {
akmhoque53353462014-04-22 08:43:45 -0500180 nbrCount++;
181 }
182 }
183 }
akmhoque157b0a42014-05-13 00:26:37 -0500184 if (nbrCount == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500185 return true;
186 }
187 return false;
188}
189
akmhoquefdbddb12014-05-02 18:35:19 -0500190int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500191AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500192{
akmhoquefdbddb12014-05-02 18:35:19 -0500193 int32_t actNbrCount = 0;
akmhoque53353462014-04-22 08:43:45 -0500194 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500195 it != m_adjList.end(); it++) {
196 if (((*it).getStatus() == 1)) {
akmhoque53353462014-04-22 08:43:45 -0500197 actNbrCount++;
198 }
199 }
200 return actNbrCount;
201}
202
203std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500204AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500205{
akmhoque53353462014-04-22 08:43:45 -0500206 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
207 m_adjList.end(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500208 ndn::bind(&Adjacent::compare,
209 _1, ndn::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500210 return it;
211}
212
213// used for debugging purpose
214void
akmhoquec8a10f72014-04-25 18:42:55 -0500215AdjacencyList::print()
akmhoque53353462014-04-22 08:43:45 -0500216{
217 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500218 it != m_adjList.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500219 cout << (*it) << endl;
220 }
221}
222
223} //namespace nlsr