blob: b82346f17216576241ce88cb238683856c79698e [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
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
Vince Lehmancb76ade2014-08-28 21:24:41 -050064bool
65AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, Adjacent::Status s)
akmhoque53353462014-04-22 08:43:45 -050066{
67 std::list<Adjacent>::iterator it = find(adjName);
Vince Lehmancb76ade2014-08-28 21:24:41 -050068
akmhoque157b0a42014-05-13 00:26:37 -050069 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -050070 return false;
akmhoque53353462014-04-22 08:43:45 -050071 }
Vince Lehmancb76ade2014-08-28 21:24:41 -050072 else {
73 it->setStatus(s);
74 return true;
75 }
akmhoque53353462014-04-22 08:43:45 -050076}
77
78Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050079AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050080{
81 Adjacent adj(adjName);
82 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050083 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050084 return (*it);
85 }
86 return adj;
87}
88
akmhoquefdbddb12014-05-02 18:35:19 -050089static bool
90compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
91{
92 return adjacent1.getName() < adjacent2.getName();
93}
akmhoque53353462014-04-22 08:43:45 -050094
95bool
akmhoquefdbddb12014-05-02 18:35:19 -050096AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050097{
akmhoque157b0a42014-05-13 00:26:37 -050098 if (getSize() != adl.getSize()) {
akmhoque53353462014-04-22 08:43:45 -050099 return false;
100 }
akmhoquefdbddb12014-05-02 18:35:19 -0500101 m_adjList.sort(compareAdjacent);
102 adl.getAdjList().sort(compareAdjacent);
103 uint32_t equalAdjCount = 0;
104 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500105 std::list<Adjacent>::iterator it1;
106 std::list<Adjacent>::iterator it2;
107 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500108 it1 != m_adjList.end(); it1++, it2++) {
109 if (!((*it1) == (*it2))) {
akmhoque53353462014-04-22 08:43:45 -0500110 break;
111 }
112 equalAdjCount++;
113 }
114 return equalAdjCount == getSize();
115}
116
akmhoquefdbddb12014-05-02 18:35:19 -0500117int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500118AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -0500119{
120 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -0500121 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500122 return -1;
123 }
124 (*it).setLinkCost(lc);
125 return 0;
126}
127
128bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500129AdjacencyList::isNeighbor(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500130{
131 std::list<Adjacent>::iterator it = find(adjName);
132 if (it == m_adjList.end())
133 {
134 return false;
135 }
136 return true;
137}
138
139void
akmhoque31d1d4b2014-05-05 22:08:14 -0500140AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500141{
142 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500143 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500144 return ;
145 }
146 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
147}
148
149void
akmhoque31d1d4b2014-05-05 22:08:14 -0500150AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
151 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500152{
153 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500154 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500155 (*it).setInterestTimedOutNo(count);
156 }
157}
158
akmhoquefdbddb12014-05-02 18:35:19 -0500159int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500160AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500161{
162 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500163 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500164 return -1;
165 }
166 return (*it).getInterestTimedOutNo();
167}
168
Vince Lehmancb76ade2014-08-28 21:24:41 -0500169Adjacent::Status
akmhoque31d1d4b2014-05-05 22:08:14 -0500170AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500171{
172 std::list<Adjacent>::iterator it = find(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500173
akmhoque157b0a42014-05-13 00:26:37 -0500174 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500175 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500176 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500177 else {
178 return it->getStatus();
179 }
akmhoque53353462014-04-22 08:43:45 -0500180}
181
182void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500183AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500184{
185 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500186 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500187 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500188 }
189}
190
191std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500192AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500193{
194 return m_adjList;
195}
196
Nick Gordon22b5c952017-08-10 17:48:15 -0500197const std::list<Adjacent>&
198AdjacencyList::getAdjList() const
199{
200 return m_adjList;
201}
202
akmhoque53353462014-04-22 08:43:45 -0500203bool
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500204AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
akmhoque53353462014-04-22 08:43:45 -0500205{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500206 uint32_t nTimedOutNeighbors = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500207
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500208 for (const Adjacent& adjacency : m_adjList) {
209
210 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
211 return true;
akmhoque53353462014-04-22 08:43:45 -0500212 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500213 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
214 nTimedOutNeighbors++;
akmhoque53353462014-04-22 08:43:45 -0500215 }
216 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500217
218 if (nTimedOutNeighbors == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500219 return true;
220 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500221 else {
222 return false;
223 }
akmhoque53353462014-04-22 08:43:45 -0500224}
225
akmhoquefdbddb12014-05-02 18:35:19 -0500226int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500227AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500228{
akmhoquefdbddb12014-05-02 18:35:19 -0500229 int32_t actNbrCount = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500230 for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
231
232 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500233 actNbrCount++;
234 }
235 }
236 return actNbrCount;
237}
238
239std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500240AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500241{
akmhoque53353462014-04-22 08:43:45 -0500242 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
243 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600244 std::bind(&Adjacent::compare,
245 _1, std::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500246 return it;
247}
248
Nick Gordonc780a692017-04-27 18:03:02 -0500249AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500250AdjacencyList::findAdjacent(const ndn::Name& adjName)
251{
Nick Gordonc780a692017-04-27 18:03:02 -0500252 return std::find_if(m_adjList.begin(),
253 m_adjList.end(),
254 std::bind(&Adjacent::compare,
255 _1, std::cref(adjName)));
akmhoquec04e7272014-07-02 11:00:14 -0500256}
257
Nick Gordonc780a692017-04-27 18:03:02 -0500258AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500259AdjacencyList::findAdjacent(uint64_t faceId)
260{
Nick Gordonc780a692017-04-27 18:03:02 -0500261 return std::find_if(m_adjList.begin(),
262 m_adjList.end(),
263 std::bind(&Adjacent::compareFaceId,
264 _1, faceId));
akmhoquec04e7272014-07-02 11:00:14 -0500265}
266
Nick Gordone9733ed2017-04-26 10:48:39 -0500267AdjacencyList::iterator
268AdjacencyList::findAdjacent(const ndn::util::FaceUri& faceUri)
269{
270 return std::find_if(m_adjList.begin(),
271 m_adjList.end(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500272 std::bind(&Adjacent::compareFaceUri,
273 _1, faceUri));
Nick Gordone9733ed2017-04-26 10:48:39 -0500274}
275
akmhoque102aea42014-08-04 10:22:12 -0500276uint64_t
Nick Gordone9733ed2017-04-26 10:48:39 -0500277AdjacencyList::getFaceId(const ndn::util::FaceUri& faceUri)
akmhoque102aea42014-08-04 10:22:12 -0500278{
279 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
280 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600281 std::bind(&Adjacent::compareFaceUri,
akmhoque102aea42014-08-04 10:22:12 -0500282 _1, faceUri));
283 if (it != m_adjList.end()) {
284 return it->getFaceId();
285 }
286
287 return 0;
288}
289
akmhoque674b0b12014-05-20 14:33:28 -0500290void
291AdjacencyList::writeLog()
292{
293 _LOG_DEBUG("-------Adjacency List--------");
294 for (std::list<Adjacent>::iterator it = m_adjList.begin();
295 it != m_adjList.end(); it++) {
296 (*it).writeLog();
297 }
298}
299
Nick Gordonfad8e252016-08-11 14:21:38 -0500300} // namespace nlsr