blob: 7fa3e6e0739d70b133974032dffea441f95e55d7 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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
dmcoomescf8d0ed2017-02-21 11:39:01 -060032INIT_LOGGER(AdjacencyList);
akmhoque674b0b12014-05-20 14:33:28 -050033
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
akmhoque53353462014-04-22 08:43:45 -050062Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050063AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050064{
65 Adjacent adj(adjName);
66 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050067 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050068 return (*it);
69 }
70 return adj;
71}
72
akmhoque53353462014-04-22 08:43:45 -050073bool
Nick Gordon49648702018-01-22 11:57:33 -060074AdjacencyList::operator==(const AdjacencyList& adl) const
akmhoque53353462014-04-22 08:43:45 -050075{
Nick Gordonaf875752018-01-22 17:01:07 -060076 if (m_adjList.size() != adl.getAdjList().size()) {
akmhoque53353462014-04-22 08:43:45 -050077 return false;
78 }
Nick Gordon2a1ac612017-10-06 15:36:49 -050079
Nick Gordonaf875752018-01-22 17:01:07 -060080 std::set<Adjacent> ourList(m_adjList.cbegin(), m_adjList.cend());
81 std::set<Adjacent> theirList(adl.getAdjList().cbegin(), adl.getAdjList().cend());
Nick Gordon2a1ac612017-10-06 15:36:49 -050082
Nick Gordonaf875752018-01-22 17:01:07 -060083 return ourList == theirList;
akmhoque53353462014-04-22 08:43:45 -050084}
85
akmhoque53353462014-04-22 08:43:45 -050086bool
Nick Gordon49648702018-01-22 11:57:33 -060087AdjacencyList::isNeighbor(const ndn::Name& adjName) const
akmhoque53353462014-04-22 08:43:45 -050088{
Nick Gordon49648702018-01-22 11:57:33 -060089 std::list<Adjacent>::const_iterator it = find(adjName);
akmhoque53353462014-04-22 08:43:45 -050090 if (it == m_adjList.end())
91 {
92 return false;
93 }
94 return true;
95}
96
97void
akmhoque31d1d4b2014-05-05 22:08:14 -050098AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -050099{
100 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500101 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500102 return ;
103 }
104 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
105}
106
107void
akmhoque31d1d4b2014-05-05 22:08:14 -0500108AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
109 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500110{
111 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500112 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500113 (*it).setInterestTimedOutNo(count);
114 }
115}
116
akmhoquefdbddb12014-05-02 18:35:19 -0500117int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600118AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500119{
Nick Gordon49648702018-01-22 11:57:33 -0600120 std::list<Adjacent>::const_iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500121 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500122 return -1;
123 }
124 return (*it).getInterestTimedOutNo();
125}
126
Vince Lehmancb76ade2014-08-28 21:24:41 -0500127Adjacent::Status
Nick Gordon49648702018-01-22 11:57:33 -0600128AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500129{
Nick Gordon49648702018-01-22 11:57:33 -0600130 std::list<Adjacent>::const_iterator it = find(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500131
akmhoque157b0a42014-05-13 00:26:37 -0500132 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500133 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500134 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500135 else {
136 return it->getStatus();
137 }
akmhoque53353462014-04-22 08:43:45 -0500138}
139
140void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500141AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500142{
143 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500144 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500145 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500146 }
147}
148
149std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500150AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500151{
152 return m_adjList;
153}
154
Nick Gordon22b5c952017-08-10 17:48:15 -0500155const std::list<Adjacent>&
156AdjacencyList::getAdjList() const
157{
158 return m_adjList;
159}
160
akmhoque53353462014-04-22 08:43:45 -0500161bool
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500162AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
akmhoque53353462014-04-22 08:43:45 -0500163{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500164 uint32_t nTimedOutNeighbors = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500165
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500166 for (const Adjacent& adjacency : m_adjList) {
167
168 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
169 return true;
akmhoque53353462014-04-22 08:43:45 -0500170 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500171 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
172 nTimedOutNeighbors++;
akmhoque53353462014-04-22 08:43:45 -0500173 }
174 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500175
176 if (nTimedOutNeighbors == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500177 return true;
178 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500179 else {
180 return false;
181 }
akmhoque53353462014-04-22 08:43:45 -0500182}
183
akmhoquefdbddb12014-05-02 18:35:19 -0500184int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600185AdjacencyList::getNumOfActiveNeighbor() const
akmhoque53353462014-04-22 08:43:45 -0500186{
akmhoquefdbddb12014-05-02 18:35:19 -0500187 int32_t actNbrCount = 0;
Nick Gordon49648702018-01-22 11:57:33 -0600188 for (std::list<Adjacent>::const_iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500189
190 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500191 actNbrCount++;
192 }
193 }
194 return actNbrCount;
195}
196
197std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500198AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500199{
akmhoque53353462014-04-22 08:43:45 -0500200 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
201 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600202 std::bind(&Adjacent::compare,
203 _1, std::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500204 return it;
205}
206
Nick Gordon49648702018-01-22 11:57:33 -0600207std::list<Adjacent>::const_iterator
208AdjacencyList::find(const ndn::Name& adjName) const
209{
210 std::list<Adjacent>::const_iterator it = std::find_if(m_adjList.cbegin(),
211 m_adjList.cend(),
212 std::bind(&Adjacent::compare,
213 _1, std::cref(adjName)));
214 return it;
215}
216
217
Nick Gordonc780a692017-04-27 18:03:02 -0500218AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500219AdjacencyList::findAdjacent(const ndn::Name& adjName)
220{
Nick Gordonc780a692017-04-27 18:03:02 -0500221 return std::find_if(m_adjList.begin(),
222 m_adjList.end(),
223 std::bind(&Adjacent::compare,
224 _1, std::cref(adjName)));
akmhoquec04e7272014-07-02 11:00:14 -0500225}
226
Nick Gordonc780a692017-04-27 18:03:02 -0500227AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500228AdjacencyList::findAdjacent(uint64_t faceId)
229{
Nick Gordonc780a692017-04-27 18:03:02 -0500230 return std::find_if(m_adjList.begin(),
231 m_adjList.end(),
232 std::bind(&Adjacent::compareFaceId,
233 _1, faceId));
akmhoquec04e7272014-07-02 11:00:14 -0500234}
235
Nick Gordone9733ed2017-04-26 10:48:39 -0500236AdjacencyList::iterator
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500237AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
Nick Gordone9733ed2017-04-26 10:48:39 -0500238{
239 return std::find_if(m_adjList.begin(),
240 m_adjList.end(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500241 std::bind(&Adjacent::compareFaceUri,
242 _1, faceUri));
Nick Gordone9733ed2017-04-26 10:48:39 -0500243}
244
akmhoque102aea42014-08-04 10:22:12 -0500245uint64_t
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500246AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
akmhoque102aea42014-08-04 10:22:12 -0500247{
248 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
249 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600250 std::bind(&Adjacent::compareFaceUri,
akmhoque102aea42014-08-04 10:22:12 -0500251 _1, faceUri));
252 if (it != m_adjList.end()) {
253 return it->getFaceId();
254 }
255
256 return 0;
257}
258
akmhoque674b0b12014-05-20 14:33:28 -0500259void
260AdjacencyList::writeLog()
261{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500262 NLSR_LOG_DEBUG("-------Adjacency List--------");
akmhoque674b0b12014-05-20 14:33:28 -0500263 for (std::list<Adjacent>::iterator it = m_adjList.begin();
264 it != m_adjList.end(); it++) {
265 (*it).writeLog();
266 }
267}
268
Nick Gordonfad8e252016-08-11 14:21:38 -0500269} // namespace nlsr