blob: f2a83021903de01306055ec3a860bda998abac24 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanf7eec4f2015-05-08 19:02:31 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehman0a7da612014-10-29 14:39:29 -050021
akmhoquec8a10f72014-04-25 18:42:55 -050022#include "adjacency-list.hpp"
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050023
akmhoque53353462014-04-22 08:43:45 -050024#include "adjacent.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050025#include "common.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050026#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050027
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050028#include <algorithm>
29
akmhoque53353462014-04-22 08:43:45 -050030namespace nlsr {
31
akmhoque674b0b12014-05-20 14:33:28 -050032INIT_LOGGER("AdjacencyList");
33
akmhoquec8a10f72014-04-25 18:42:55 -050034AdjacencyList::AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050035{
36}
37
akmhoquec8a10f72014-04-25 18:42:55 -050038AdjacencyList::~AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050039{
40}
41
akmhoquefdbddb12014-05-02 18:35:19 -050042int32_t
43AdjacencyList::insert(Adjacent& adjacent)
akmhoque53353462014-04-22 08:43:45 -050044{
akmhoquefdbddb12014-05-02 18:35:19 -050045 std::list<Adjacent>::iterator it = find(adjacent.getName());
akmhoque157b0a42014-05-13 00:26:37 -050046 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050047 return -1;
48 }
akmhoquefdbddb12014-05-02 18:35:19 -050049 m_adjList.push_back(adjacent);
akmhoque53353462014-04-22 08:43:45 -050050 return 0;
51}
52
53void
akmhoquefdbddb12014-05-02 18:35:19 -050054AdjacencyList::addAdjacents(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050055{
56 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
akmhoque157b0a42014-05-13 00:26:37 -050057 it != adl.getAdjList().end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -050058 insert((*it));
59 }
60}
61
Vince Lehmancb76ade2014-08-28 21:24:41 -050062bool
63AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, Adjacent::Status s)
akmhoque53353462014-04-22 08:43:45 -050064{
65 std::list<Adjacent>::iterator it = find(adjName);
Vince Lehmancb76ade2014-08-28 21:24:41 -050066
akmhoque157b0a42014-05-13 00:26:37 -050067 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -050068 return false;
akmhoque53353462014-04-22 08:43:45 -050069 }
Vince Lehmancb76ade2014-08-28 21:24:41 -050070 else {
71 it->setStatus(s);
72 return true;
73 }
akmhoque53353462014-04-22 08:43:45 -050074}
75
76Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050077AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050078{
79 Adjacent adj(adjName);
80 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050081 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050082 return (*it);
83 }
84 return adj;
85}
86
akmhoquefdbddb12014-05-02 18:35:19 -050087static bool
88compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
89{
90 return adjacent1.getName() < adjacent2.getName();
91}
akmhoque53353462014-04-22 08:43:45 -050092
93bool
akmhoquefdbddb12014-05-02 18:35:19 -050094AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050095{
akmhoque157b0a42014-05-13 00:26:37 -050096 if (getSize() != adl.getSize()) {
akmhoque53353462014-04-22 08:43:45 -050097 return false;
98 }
akmhoquefdbddb12014-05-02 18:35:19 -050099 m_adjList.sort(compareAdjacent);
100 adl.getAdjList().sort(compareAdjacent);
101 uint32_t equalAdjCount = 0;
102 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500103 std::list<Adjacent>::iterator it1;
104 std::list<Adjacent>::iterator it2;
105 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500106 it1 != m_adjList.end(); it1++, it2++) {
107 if (!((*it1) == (*it2))) {
akmhoque53353462014-04-22 08:43:45 -0500108 break;
109 }
110 equalAdjCount++;
111 }
112 return equalAdjCount == getSize();
113}
114
akmhoquefdbddb12014-05-02 18:35:19 -0500115int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500116AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -0500117{
118 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -0500119 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500120 return -1;
121 }
122 (*it).setLinkCost(lc);
123 return 0;
124}
125
126bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500127AdjacencyList::isNeighbor(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500128{
129 std::list<Adjacent>::iterator it = find(adjName);
130 if (it == m_adjList.end())
131 {
132 return false;
133 }
134 return true;
135}
136
137void
akmhoque31d1d4b2014-05-05 22:08:14 -0500138AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500139{
140 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500141 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500142 return ;
143 }
144 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
145}
146
147void
akmhoque31d1d4b2014-05-05 22:08:14 -0500148AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
149 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500150{
151 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500152 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500153 (*it).setInterestTimedOutNo(count);
154 }
155}
156
akmhoquefdbddb12014-05-02 18:35:19 -0500157int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500158AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500159{
160 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500161 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500162 return -1;
163 }
164 return (*it).getInterestTimedOutNo();
165}
166
Vince Lehmancb76ade2014-08-28 21:24:41 -0500167Adjacent::Status
akmhoque31d1d4b2014-05-05 22:08:14 -0500168AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500169{
170 std::list<Adjacent>::iterator it = find(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500171
akmhoque157b0a42014-05-13 00:26:37 -0500172 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500173 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500174 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500175 else {
176 return it->getStatus();
177 }
akmhoque53353462014-04-22 08:43:45 -0500178}
179
180void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500181AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500182{
183 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500184 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500185 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500186 }
187}
188
189std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500190AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500191{
192 return m_adjList;
193}
194
Nick Gordon22b5c952017-08-10 17:48:15 -0500195const std::list<Adjacent>&
196AdjacencyList::getAdjList() const
197{
198 return m_adjList;
199}
200
akmhoque53353462014-04-22 08:43:45 -0500201bool
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500202AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
akmhoque53353462014-04-22 08:43:45 -0500203{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500204 uint32_t nTimedOutNeighbors = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500205
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500206 for (const Adjacent& adjacency : m_adjList) {
207
208 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
209 return true;
akmhoque53353462014-04-22 08:43:45 -0500210 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500211 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
212 nTimedOutNeighbors++;
akmhoque53353462014-04-22 08:43:45 -0500213 }
214 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500215
216 if (nTimedOutNeighbors == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500217 return true;
218 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500219 else {
220 return false;
221 }
akmhoque53353462014-04-22 08:43:45 -0500222}
223
akmhoquefdbddb12014-05-02 18:35:19 -0500224int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500225AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500226{
akmhoquefdbddb12014-05-02 18:35:19 -0500227 int32_t actNbrCount = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500228 for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
229
230 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500231 actNbrCount++;
232 }
233 }
234 return actNbrCount;
235}
236
237std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500238AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500239{
akmhoque53353462014-04-22 08:43:45 -0500240 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
241 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600242 std::bind(&Adjacent::compare,
243 _1, std::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500244 return it;
245}
246
Nick Gordonc780a692017-04-27 18:03:02 -0500247AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500248AdjacencyList::findAdjacent(const ndn::Name& adjName)
249{
Nick Gordonc780a692017-04-27 18:03:02 -0500250 return std::find_if(m_adjList.begin(),
251 m_adjList.end(),
252 std::bind(&Adjacent::compare,
253 _1, std::cref(adjName)));
akmhoquec04e7272014-07-02 11:00:14 -0500254}
255
Nick Gordonc780a692017-04-27 18:03:02 -0500256AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500257AdjacencyList::findAdjacent(uint64_t faceId)
258{
Nick Gordonc780a692017-04-27 18:03:02 -0500259 return std::find_if(m_adjList.begin(),
260 m_adjList.end(),
261 std::bind(&Adjacent::compareFaceId,
262 _1, faceId));
akmhoquec04e7272014-07-02 11:00:14 -0500263}
264
Nick Gordone9733ed2017-04-26 10:48:39 -0500265AdjacencyList::iterator
266AdjacencyList::findAdjacent(const ndn::util::FaceUri& faceUri)
267{
268 return std::find_if(m_adjList.begin(),
269 m_adjList.end(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500270 std::bind(&Adjacent::compareFaceUri,
271 _1, faceUri));
Nick Gordone9733ed2017-04-26 10:48:39 -0500272}
273
akmhoque102aea42014-08-04 10:22:12 -0500274uint64_t
Nick Gordone9733ed2017-04-26 10:48:39 -0500275AdjacencyList::getFaceId(const ndn::util::FaceUri& faceUri)
akmhoque102aea42014-08-04 10:22:12 -0500276{
277 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
278 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600279 std::bind(&Adjacent::compareFaceUri,
akmhoque102aea42014-08-04 10:22:12 -0500280 _1, faceUri));
281 if (it != m_adjList.end()) {
282 return it->getFaceId();
283 }
284
285 return 0;
286}
287
akmhoque674b0b12014-05-20 14:33:28 -0500288void
289AdjacencyList::writeLog()
290{
291 _LOG_DEBUG("-------Adjacency List--------");
292 for (std::list<Adjacent>::iterator it = m_adjList.begin();
293 it != m_adjList.end(); it++) {
294 (*it).writeLog();
295 }
296}
297
Nick Gordonfad8e252016-08-11 14:21:38 -0500298} // namespace nlsr