blob: f2e0aaf47be8f0d7142d2255ff99c12101ed3430 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <algorithm>
akmhoque31d1d4b2014-05-05 22:08:14 -050024#include <ndn-cxx/common.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050025#include "adjacency-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050026#include "adjacent.hpp"
27#include "nlsr.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050028#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050029
30namespace nlsr {
31
akmhoque674b0b12014-05-20 14:33:28 -050032INIT_LOGGER("AdjacencyList");
33
akmhoquefdbddb12014-05-02 18:35:19 -050034using namespace std;
35
akmhoquec8a10f72014-04-25 18:42:55 -050036AdjacencyList::AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050037{
38}
39
akmhoquec8a10f72014-04-25 18:42:55 -050040AdjacencyList::~AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050041{
42}
43
akmhoquefdbddb12014-05-02 18:35:19 -050044int32_t
45AdjacencyList::insert(Adjacent& adjacent)
akmhoque53353462014-04-22 08:43:45 -050046{
akmhoquefdbddb12014-05-02 18:35:19 -050047 std::list<Adjacent>::iterator it = find(adjacent.getName());
akmhoque157b0a42014-05-13 00:26:37 -050048 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050049 return -1;
50 }
akmhoquefdbddb12014-05-02 18:35:19 -050051 m_adjList.push_back(adjacent);
akmhoque53353462014-04-22 08:43:45 -050052 return 0;
53}
54
55void
akmhoquefdbddb12014-05-02 18:35:19 -050056AdjacencyList::addAdjacents(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050057{
58 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
akmhoque157b0a42014-05-13 00:26:37 -050059 it != adl.getAdjList().end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -050060 insert((*it));
61 }
62}
63
akmhoquefdbddb12014-05-02 18:35:19 -050064int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050065AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, int32_t s)
akmhoque53353462014-04-22 08:43:45 -050066{
67 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050068 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050069 return -1;
70 }
71 (*it).setStatus(s);
72 return 0;
73}
74
75Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050076AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050077{
78 Adjacent adj(adjName);
79 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050080 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050081 return (*it);
82 }
83 return adj;
84}
85
akmhoquefdbddb12014-05-02 18:35:19 -050086static bool
87compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
88{
89 return adjacent1.getName() < adjacent2.getName();
90}
akmhoque53353462014-04-22 08:43:45 -050091
92bool
akmhoquefdbddb12014-05-02 18:35:19 -050093AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050094{
akmhoque157b0a42014-05-13 00:26:37 -050095 if (getSize() != adl.getSize()) {
akmhoque53353462014-04-22 08:43:45 -050096 return false;
97 }
akmhoquefdbddb12014-05-02 18:35:19 -050098 m_adjList.sort(compareAdjacent);
99 adl.getAdjList().sort(compareAdjacent);
100 uint32_t equalAdjCount = 0;
101 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500102 std::list<Adjacent>::iterator it1;
103 std::list<Adjacent>::iterator it2;
104 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500105 it1 != m_adjList.end(); it1++, it2++) {
106 if (!((*it1) == (*it2))) {
akmhoque53353462014-04-22 08:43:45 -0500107 break;
108 }
109 equalAdjCount++;
110 }
111 return equalAdjCount == getSize();
112}
113
akmhoquefdbddb12014-05-02 18:35:19 -0500114int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500115AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -0500116{
117 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -0500118 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500119 return -1;
120 }
121 (*it).setLinkCost(lc);
122 return 0;
123}
124
125bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500126AdjacencyList::isNeighbor(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500127{
128 std::list<Adjacent>::iterator it = find(adjName);
129 if (it == m_adjList.end())
130 {
131 return false;
132 }
133 return true;
134}
135
136void
akmhoque31d1d4b2014-05-05 22:08:14 -0500137AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500138{
139 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500140 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500141 return ;
142 }
143 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
144}
145
146void
akmhoque31d1d4b2014-05-05 22:08:14 -0500147AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
148 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500149{
150 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500151 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500152 (*it).setInterestTimedOutNo(count);
153 }
154}
155
akmhoquefdbddb12014-05-02 18:35:19 -0500156int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500157AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500158{
159 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500160 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500161 return -1;
162 }
163 return (*it).getInterestTimedOutNo();
164}
165
akmhoquefdbddb12014-05-02 18:35:19 -0500166uint32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500167AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500168{
169 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500170 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500171 return -1;
172 }
173 return (*it).getStatus();
174}
175
176void
akmhoque31d1d4b2014-05-05 22:08:14 -0500177AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, int32_t status)
akmhoque53353462014-04-22 08:43:45 -0500178{
179 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500180 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500181 (*it).setStatus(status);
182 }
183}
184
185std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500186AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500187{
188 return m_adjList;
189}
190
191bool
akmhoquec8a10f72014-04-25 18:42:55 -0500192AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500193{
194 uint32_t nbrCount = 0;
195 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500196 it != m_adjList.end() ; it++) {
197 if (((*it).getStatus() == 1)) {
akmhoque53353462014-04-22 08:43:45 -0500198 nbrCount++;
199 }
akmhoque157b0a42014-05-13 00:26:37 -0500200 else {
akmhoque53353462014-04-22 08:43:45 -0500201 if ((*it).getInterestTimedOutNo() >=
akmhoque157b0a42014-05-13 00:26:37 -0500202 pnlsr.getConfParameter().getInterestRetryNumber()) {
akmhoque53353462014-04-22 08:43:45 -0500203 nbrCount++;
204 }
205 }
206 }
akmhoque157b0a42014-05-13 00:26:37 -0500207 if (nbrCount == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500208 return true;
209 }
210 return false;
211}
212
akmhoquefdbddb12014-05-02 18:35:19 -0500213int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500214AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500215{
akmhoquefdbddb12014-05-02 18:35:19 -0500216 int32_t actNbrCount = 0;
akmhoque53353462014-04-22 08:43:45 -0500217 for (std::list<Adjacent>::iterator it = m_adjList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500218 it != m_adjList.end(); it++) {
219 if (((*it).getStatus() == 1)) {
akmhoque53353462014-04-22 08:43:45 -0500220 actNbrCount++;
221 }
222 }
223 return actNbrCount;
224}
225
226std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500227AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500228{
akmhoque53353462014-04-22 08:43:45 -0500229 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
230 m_adjList.end(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500231 ndn::bind(&Adjacent::compare,
232 _1, ndn::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500233 return it;
234}
235
akmhoquec04e7272014-07-02 11:00:14 -0500236Adjacent *
237AdjacencyList::findAdjacent(const ndn::Name& adjName)
238{
239 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
240 m_adjList.end(),
241 ndn::bind(&Adjacent::compare,
242 _1, ndn::cref(adjName)));
243 if (it != m_adjList.end()) {
244 return &(*it);
245 }
246
247 return 0;
248}
249
250Adjacent *
251AdjacencyList::findAdjacent(uint64_t faceId)
252{
253 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
254 m_adjList.end(),
255 ndn::bind(&Adjacent::compareFaceId,
256 _1, faceId));
257 if (it != m_adjList.end()) {
258 return &(*it);
259 }
260
261 return 0;
262}
263
akmhoque102aea42014-08-04 10:22:12 -0500264uint64_t
265AdjacencyList::getFaceId(const std::string& faceUri)
266{
267 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
268 m_adjList.end(),
269 ndn::bind(&Adjacent::compareFaceUri,
270 _1, faceUri));
271 if (it != m_adjList.end()) {
272 return it->getFaceId();
273 }
274
275 return 0;
276}
277
akmhoque674b0b12014-05-20 14:33:28 -0500278void
279AdjacencyList::writeLog()
280{
281 _LOG_DEBUG("-------Adjacency List--------");
282 for (std::list<Adjacent>::iterator it = m_adjList.begin();
283 it != m_adjList.end(); it++) {
284 (*it).writeLog();
285 }
286}
287
akmhoque53353462014-04-22 08:43:45 -0500288} //namespace nlsr