blob: 17f6537d9b9a86cb2c805854a497a3730d0042e8 [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"
akmhoque674b0b12014-05-20 14:33:28 -05007#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -05008
9namespace nlsr {
10
akmhoque674b0b12014-05-20 14:33:28 -050011INIT_LOGGER("AdjacencyList");
12
akmhoquefdbddb12014-05-02 18:35:19 -050013using namespace std;
14
akmhoquec8a10f72014-04-25 18:42:55 -050015AdjacencyList::AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050016{
17}
18
akmhoquec8a10f72014-04-25 18:42:55 -050019AdjacencyList::~AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050020{
21}
22
akmhoquefdbddb12014-05-02 18:35:19 -050023int32_t
24AdjacencyList::insert(Adjacent& adjacent)
akmhoque53353462014-04-22 08:43:45 -050025{
akmhoquefdbddb12014-05-02 18:35:19 -050026 std::list<Adjacent>::iterator it = find(adjacent.getName());
akmhoque157b0a42014-05-13 00:26:37 -050027 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050028 return -1;
29 }
akmhoquefdbddb12014-05-02 18:35:19 -050030 m_adjList.push_back(adjacent);
akmhoque53353462014-04-22 08:43:45 -050031 return 0;
32}
33
34void
akmhoquefdbddb12014-05-02 18:35:19 -050035AdjacencyList::addAdjacents(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050036{
37 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
akmhoque157b0a42014-05-13 00:26:37 -050038 it != adl.getAdjList().end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -050039 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);
akmhoque157b0a42014-05-13 00:26:37 -050047 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050048 return -1;
49 }
50 (*it).setStatus(s);
51 return 0;
52}
53
54Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050055AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050056{
57 Adjacent adj(adjName);
58 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050059 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050060 return (*it);
61 }
62 return adj;
63}
64
akmhoquefdbddb12014-05-02 18:35:19 -050065static bool
66compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
67{
68 return adjacent1.getName() < adjacent2.getName();
69}
akmhoque53353462014-04-22 08:43:45 -050070
71bool
akmhoquefdbddb12014-05-02 18:35:19 -050072AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050073{
akmhoque157b0a42014-05-13 00:26:37 -050074 if (getSize() != adl.getSize()) {
akmhoque53353462014-04-22 08:43:45 -050075 return false;
76 }
akmhoquefdbddb12014-05-02 18:35:19 -050077 m_adjList.sort(compareAdjacent);
78 adl.getAdjList().sort(compareAdjacent);
79 uint32_t equalAdjCount = 0;
80 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -050081 std::list<Adjacent>::iterator it1;
82 std::list<Adjacent>::iterator it2;
83 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
akmhoque157b0a42014-05-13 00:26:37 -050084 it1 != m_adjList.end(); it1++, it2++) {
85 if (!((*it1) == (*it2))) {
akmhoque53353462014-04-22 08:43:45 -050086 break;
87 }
88 equalAdjCount++;
89 }
90 return equalAdjCount == getSize();
91}
92
akmhoquefdbddb12014-05-02 18:35:19 -050093int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050094AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -050095{
96 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050097 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050098 return -1;
99 }
100 (*it).setLinkCost(lc);
101 return 0;
102}
103
104bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500105AdjacencyList::isNeighbor(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500106{
107 std::list<Adjacent>::iterator it = find(adjName);
108 if (it == m_adjList.end())
109 {
110 return false;
111 }
112 return true;
113}
114
115void
akmhoque31d1d4b2014-05-05 22:08:14 -0500116AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500117{
118 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500119 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500120 return ;
121 }
122 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
123}
124
125void
akmhoque31d1d4b2014-05-05 22:08:14 -0500126AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
127 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500128{
129 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500130 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500131 (*it).setInterestTimedOutNo(count);
132 }
133}
134
akmhoquefdbddb12014-05-02 18:35:19 -0500135int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500136AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500137{
138 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500139 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500140 return -1;
141 }
142 return (*it).getInterestTimedOutNo();
143}
144
akmhoquefdbddb12014-05-02 18:35:19 -0500145uint32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500146AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500147{
148 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500149 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500150 return -1;
151 }
152 return (*it).getStatus();
153}
154
155void
akmhoque31d1d4b2014-05-05 22:08:14 -0500156AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, int32_t status)
akmhoque53353462014-04-22 08:43:45 -0500157{
158 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500159 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500160 (*it).setStatus(status);
161 }
162}
163
164std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500165AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500166{
167 return m_adjList;
168}
169
170bool
akmhoquec8a10f72014-04-25 18:42:55 -0500171AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500172{
173 uint32_t nbrCount = 0;
174 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500175 it != m_adjList.end() ; it++) {
176 if (((*it).getStatus() == 1)) {
akmhoque53353462014-04-22 08:43:45 -0500177 nbrCount++;
178 }
akmhoque157b0a42014-05-13 00:26:37 -0500179 else {
akmhoque53353462014-04-22 08:43:45 -0500180 if ((*it).getInterestTimedOutNo() >=
akmhoque157b0a42014-05-13 00:26:37 -0500181 pnlsr.getConfParameter().getInterestRetryNumber()) {
akmhoque53353462014-04-22 08:43:45 -0500182 nbrCount++;
183 }
184 }
185 }
akmhoque157b0a42014-05-13 00:26:37 -0500186 if (nbrCount == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500187 return true;
188 }
189 return false;
190}
191
akmhoquefdbddb12014-05-02 18:35:19 -0500192int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500193AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500194{
akmhoquefdbddb12014-05-02 18:35:19 -0500195 int32_t actNbrCount = 0;
akmhoque53353462014-04-22 08:43:45 -0500196 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500197 it != m_adjList.end(); it++) {
198 if (((*it).getStatus() == 1)) {
akmhoque53353462014-04-22 08:43:45 -0500199 actNbrCount++;
200 }
201 }
202 return actNbrCount;
203}
204
205std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500206AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500207{
akmhoque53353462014-04-22 08:43:45 -0500208 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
209 m_adjList.end(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500210 ndn::bind(&Adjacent::compare,
211 _1, ndn::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500212 return it;
213}
214
akmhoque674b0b12014-05-20 14:33:28 -0500215void
216AdjacencyList::writeLog()
217{
218 _LOG_DEBUG("-------Adjacency List--------");
219 for (std::list<Adjacent>::iterator it = m_adjList.begin();
220 it != m_adjList.end(); it++) {
221 (*it).writeLog();
222 }
223}
224
akmhoque53353462014-04-22 08:43:45 -0500225// used for debugging purpose
226void
akmhoquec8a10f72014-04-25 18:42:55 -0500227AdjacencyList::print()
akmhoque53353462014-04-22 08:43:45 -0500228{
229 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500230 it != m_adjList.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500231 cout << (*it) << endl;
232 }
233}
234
235} //namespace nlsr