blob: d5e6dfe0080b390abdee2c7a757613f995613129 [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
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
akmhoque53353462014-04-22 08:43:45 -050087bool
Nick Gordon49648702018-01-22 11:57:33 -060088AdjacencyList::operator==(const AdjacencyList& adl) const
akmhoque53353462014-04-22 08:43:45 -050089{
Nick Gordonff9a6272017-10-12 13:38:29 -050090 if (size() != adl.size()) {
akmhoque53353462014-04-22 08:43:45 -050091 return false;
92 }
Nick Gordon2a1ac612017-10-06 15:36:49 -050093
94 auto comparator =
95 [] (const Adjacent* lhs, const Adjacent* rhs) {
96 return *lhs < *rhs;
97 };
98
99 std::vector<const Adjacent*> ourList;
100 std::transform(m_adjList.begin(), m_adjList.end(),
101 std::back_inserter(ourList), std::pointer_traits<const Adjacent*>::pointer_to);
102
103 std::vector<const Adjacent*> theirList;
104 std::transform(adl.getAdjList().begin(), adl.getAdjList().end(),
105 std::back_inserter(theirList), std::pointer_traits<const Adjacent*>::pointer_to);
106
107 std::sort(ourList.begin(), ourList.end(), std::bind(comparator, _1, _2));
108 std::sort(theirList.begin(), theirList.end(), std::bind(comparator, _1, _2));
109
110 for (size_t i = 0; i < ourList.size(); i++) {
111 if (*(ourList[i]) != *(theirList[i])) {
Nick Gordon2a1ac612017-10-06 15:36:49 -0500112 return false;
akmhoque53353462014-04-22 08:43:45 -0500113 }
akmhoque53353462014-04-22 08:43:45 -0500114 }
Nick Gordon2a1ac612017-10-06 15:36:49 -0500115 return true;
akmhoque53353462014-04-22 08:43:45 -0500116}
117
akmhoquefdbddb12014-05-02 18:35:19 -0500118int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500119AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -0500120{
121 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -0500122 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500123 return -1;
124 }
125 (*it).setLinkCost(lc);
126 return 0;
127}
128
129bool
Nick Gordon49648702018-01-22 11:57:33 -0600130AdjacencyList::isNeighbor(const ndn::Name& adjName) const
akmhoque53353462014-04-22 08:43:45 -0500131{
Nick Gordon49648702018-01-22 11:57:33 -0600132 std::list<Adjacent>::const_iterator it = find(adjName);
akmhoque53353462014-04-22 08:43:45 -0500133 if (it == m_adjList.end())
134 {
135 return false;
136 }
137 return true;
138}
139
140void
akmhoque31d1d4b2014-05-05 22:08:14 -0500141AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
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()) {
akmhoque53353462014-04-22 08:43:45 -0500145 return ;
146 }
147 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
148}
149
150void
akmhoque31d1d4b2014-05-05 22:08:14 -0500151AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
152 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500153{
154 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500155 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500156 (*it).setInterestTimedOutNo(count);
157 }
158}
159
akmhoquefdbddb12014-05-02 18:35:19 -0500160int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600161AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500162{
Nick Gordon49648702018-01-22 11:57:33 -0600163 std::list<Adjacent>::const_iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500164 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500165 return -1;
166 }
167 return (*it).getInterestTimedOutNo();
168}
169
Vince Lehmancb76ade2014-08-28 21:24:41 -0500170Adjacent::Status
Nick Gordon49648702018-01-22 11:57:33 -0600171AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500172{
Nick Gordon49648702018-01-22 11:57:33 -0600173 std::list<Adjacent>::const_iterator it = find(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500174
akmhoque157b0a42014-05-13 00:26:37 -0500175 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500176 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500177 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500178 else {
179 return it->getStatus();
180 }
akmhoque53353462014-04-22 08:43:45 -0500181}
182
183void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500184AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500185{
186 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500187 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500188 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500189 }
190}
191
192std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500193AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500194{
195 return m_adjList;
196}
197
Nick Gordon22b5c952017-08-10 17:48:15 -0500198const std::list<Adjacent>&
199AdjacencyList::getAdjList() const
200{
201 return m_adjList;
202}
203
akmhoque53353462014-04-22 08:43:45 -0500204bool
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500205AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
akmhoque53353462014-04-22 08:43:45 -0500206{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500207 uint32_t nTimedOutNeighbors = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500208
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500209 for (const Adjacent& adjacency : m_adjList) {
210
211 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
212 return true;
akmhoque53353462014-04-22 08:43:45 -0500213 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500214 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
215 nTimedOutNeighbors++;
akmhoque53353462014-04-22 08:43:45 -0500216 }
217 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500218
219 if (nTimedOutNeighbors == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500220 return true;
221 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500222 else {
223 return false;
224 }
akmhoque53353462014-04-22 08:43:45 -0500225}
226
akmhoquefdbddb12014-05-02 18:35:19 -0500227int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600228AdjacencyList::getNumOfActiveNeighbor() const
akmhoque53353462014-04-22 08:43:45 -0500229{
akmhoquefdbddb12014-05-02 18:35:19 -0500230 int32_t actNbrCount = 0;
Nick Gordon49648702018-01-22 11:57:33 -0600231 for (std::list<Adjacent>::const_iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500232
233 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500234 actNbrCount++;
235 }
236 }
237 return actNbrCount;
238}
239
240std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500241AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500242{
akmhoque53353462014-04-22 08:43:45 -0500243 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
244 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600245 std::bind(&Adjacent::compare,
246 _1, std::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500247 return it;
248}
249
Nick Gordon49648702018-01-22 11:57:33 -0600250std::list<Adjacent>::const_iterator
251AdjacencyList::find(const ndn::Name& adjName) const
252{
253 std::list<Adjacent>::const_iterator it = std::find_if(m_adjList.cbegin(),
254 m_adjList.cend(),
255 std::bind(&Adjacent::compare,
256 _1, std::cref(adjName)));
257 return it;
258}
259
260
Nick Gordonc780a692017-04-27 18:03:02 -0500261AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500262AdjacencyList::findAdjacent(const ndn::Name& adjName)
263{
Nick Gordonc780a692017-04-27 18:03:02 -0500264 return std::find_if(m_adjList.begin(),
265 m_adjList.end(),
266 std::bind(&Adjacent::compare,
267 _1, std::cref(adjName)));
akmhoquec04e7272014-07-02 11:00:14 -0500268}
269
Nick Gordonc780a692017-04-27 18:03:02 -0500270AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500271AdjacencyList::findAdjacent(uint64_t faceId)
272{
Nick Gordonc780a692017-04-27 18:03:02 -0500273 return std::find_if(m_adjList.begin(),
274 m_adjList.end(),
275 std::bind(&Adjacent::compareFaceId,
276 _1, faceId));
akmhoquec04e7272014-07-02 11:00:14 -0500277}
278
Nick Gordone9733ed2017-04-26 10:48:39 -0500279AdjacencyList::iterator
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500280AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
Nick Gordone9733ed2017-04-26 10:48:39 -0500281{
282 return std::find_if(m_adjList.begin(),
283 m_adjList.end(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500284 std::bind(&Adjacent::compareFaceUri,
285 _1, faceUri));
Nick Gordone9733ed2017-04-26 10:48:39 -0500286}
287
akmhoque102aea42014-08-04 10:22:12 -0500288uint64_t
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500289AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
akmhoque102aea42014-08-04 10:22:12 -0500290{
291 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
292 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600293 std::bind(&Adjacent::compareFaceUri,
akmhoque102aea42014-08-04 10:22:12 -0500294 _1, faceUri));
295 if (it != m_adjList.end()) {
296 return it->getFaceId();
297 }
298
299 return 0;
300}
301
akmhoque674b0b12014-05-20 14:33:28 -0500302void
303AdjacencyList::writeLog()
304{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500305 NLSR_LOG_DEBUG("-------Adjacency List--------");
akmhoque674b0b12014-05-20 14:33:28 -0500306 for (std::list<Adjacent>::iterator it = m_adjList.begin();
307 it != m_adjList.end(); it++) {
308 (*it).writeLog();
309 }
310}
311
Nick Gordonfad8e252016-08-11 14:21:38 -0500312} // namespace nlsr