blob: 0cf8ff3679c6e48efbebd59a69cbc605e24b657b [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{
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050076 auto theirList = adl.getAdjList();
77 if (m_adjList.size() != theirList.size()) {
akmhoque53353462014-04-22 08:43:45 -050078 return false;
79 }
Nick Gordon2a1ac612017-10-06 15:36:49 -050080
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050081 std::set<Adjacent> ourSet(m_adjList.cbegin(), m_adjList.cend());
82 std::set<Adjacent> theirSet(theirList.cbegin(), theirList.cend());
Nick Gordon2a1ac612017-10-06 15:36:49 -050083
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050084 return ourSet == theirSet;
akmhoque53353462014-04-22 08:43:45 -050085}
86
akmhoque53353462014-04-22 08:43:45 -050087bool
Nick Gordon49648702018-01-22 11:57:33 -060088AdjacencyList::isNeighbor(const ndn::Name& adjName) const
akmhoque53353462014-04-22 08:43:45 -050089{
Nick Gordon49648702018-01-22 11:57:33 -060090 std::list<Adjacent>::const_iterator it = find(adjName);
akmhoque53353462014-04-22 08:43:45 -050091 if (it == m_adjList.end())
92 {
93 return false;
94 }
95 return true;
96}
97
98void
akmhoque31d1d4b2014-05-05 22:08:14 -050099AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500100{
101 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500102 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500103 return ;
104 }
105 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
106}
107
108void
akmhoque31d1d4b2014-05-05 22:08:14 -0500109AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
110 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500111{
112 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500113 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500114 (*it).setInterestTimedOutNo(count);
115 }
116}
117
akmhoquefdbddb12014-05-02 18:35:19 -0500118int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600119AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500120{
Nick Gordon49648702018-01-22 11:57:33 -0600121 std::list<Adjacent>::const_iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500122 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500123 return -1;
124 }
125 return (*it).getInterestTimedOutNo();
126}
127
Vince Lehmancb76ade2014-08-28 21:24:41 -0500128Adjacent::Status
Nick Gordon49648702018-01-22 11:57:33 -0600129AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500130{
Nick Gordon49648702018-01-22 11:57:33 -0600131 std::list<Adjacent>::const_iterator it = find(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500132
akmhoque157b0a42014-05-13 00:26:37 -0500133 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500134 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500135 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500136 else {
137 return it->getStatus();
138 }
akmhoque53353462014-04-22 08:43:45 -0500139}
140
141void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500142AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500143{
144 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500145 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500146 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500147 }
148}
149
150std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500151AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500152{
153 return m_adjList;
154}
155
Nick Gordon22b5c952017-08-10 17:48:15 -0500156const std::list<Adjacent>&
157AdjacencyList::getAdjList() const
158{
159 return m_adjList;
160}
161
akmhoque53353462014-04-22 08:43:45 -0500162bool
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500163AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
akmhoque53353462014-04-22 08:43:45 -0500164{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500165 uint32_t nTimedOutNeighbors = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500166
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500167 for (const Adjacent& adjacency : m_adjList) {
168
169 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
170 return true;
akmhoque53353462014-04-22 08:43:45 -0500171 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500172 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
173 nTimedOutNeighbors++;
akmhoque53353462014-04-22 08:43:45 -0500174 }
175 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500176
177 if (nTimedOutNeighbors == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500178 return true;
179 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500180 else {
181 return false;
182 }
akmhoque53353462014-04-22 08:43:45 -0500183}
184
akmhoquefdbddb12014-05-02 18:35:19 -0500185int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600186AdjacencyList::getNumOfActiveNeighbor() const
akmhoque53353462014-04-22 08:43:45 -0500187{
akmhoquefdbddb12014-05-02 18:35:19 -0500188 int32_t actNbrCount = 0;
Nick Gordon49648702018-01-22 11:57:33 -0600189 for (std::list<Adjacent>::const_iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500190
191 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500192 actNbrCount++;
193 }
194 }
195 return actNbrCount;
196}
197
198std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500199AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500200{
akmhoque53353462014-04-22 08:43:45 -0500201 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
202 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600203 std::bind(&Adjacent::compare,
204 _1, std::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500205 return it;
206}
207
Nick Gordon49648702018-01-22 11:57:33 -0600208std::list<Adjacent>::const_iterator
209AdjacencyList::find(const ndn::Name& adjName) const
210{
211 std::list<Adjacent>::const_iterator it = std::find_if(m_adjList.cbegin(),
212 m_adjList.cend(),
213 std::bind(&Adjacent::compare,
214 _1, std::cref(adjName)));
215 return it;
216}
217
218
Nick Gordonc780a692017-04-27 18:03:02 -0500219AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500220AdjacencyList::findAdjacent(const ndn::Name& adjName)
221{
Nick Gordonc780a692017-04-27 18:03:02 -0500222 return std::find_if(m_adjList.begin(),
223 m_adjList.end(),
224 std::bind(&Adjacent::compare,
225 _1, std::cref(adjName)));
akmhoquec04e7272014-07-02 11:00:14 -0500226}
227
Nick Gordonc780a692017-04-27 18:03:02 -0500228AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500229AdjacencyList::findAdjacent(uint64_t faceId)
230{
Nick Gordonc780a692017-04-27 18:03:02 -0500231 return std::find_if(m_adjList.begin(),
232 m_adjList.end(),
233 std::bind(&Adjacent::compareFaceId,
234 _1, faceId));
akmhoquec04e7272014-07-02 11:00:14 -0500235}
236
Nick Gordone9733ed2017-04-26 10:48:39 -0500237AdjacencyList::iterator
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500238AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
Nick Gordone9733ed2017-04-26 10:48:39 -0500239{
240 return std::find_if(m_adjList.begin(),
241 m_adjList.end(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500242 std::bind(&Adjacent::compareFaceUri,
243 _1, faceUri));
Nick Gordone9733ed2017-04-26 10:48:39 -0500244}
245
akmhoque102aea42014-08-04 10:22:12 -0500246uint64_t
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500247AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
akmhoque102aea42014-08-04 10:22:12 -0500248{
249 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
250 m_adjList.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600251 std::bind(&Adjacent::compareFaceUri,
akmhoque102aea42014-08-04 10:22:12 -0500252 _1, faceUri));
253 if (it != m_adjList.end()) {
254 return it->getFaceId();
255 }
256
257 return 0;
258}
259
akmhoque674b0b12014-05-20 14:33:28 -0500260void
261AdjacencyList::writeLog()
262{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500263 NLSR_LOG_DEBUG("-------Adjacency List--------");
akmhoque674b0b12014-05-20 14:33:28 -0500264 for (std::list<Adjacent>::iterator it = m_adjList.begin();
265 it != m_adjList.end(); it++) {
266 (*it).writeLog();
267 }
268}
269
Nick Gordonfad8e252016-08-11 14:21:38 -0500270} // namespace nlsr