blob: 3e535ee68835501b08519652726d3c0ebccb6e69 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <algorithm>
akmhoque31d1d4b2014-05-05 22:08:14 -050024#include <ndn-cxx/common.hpp>
Vince Lehman0a7da612014-10-29 14:39:29 -050025
akmhoquec8a10f72014-04-25 18:42:55 -050026#include "adjacency-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050027#include "adjacent.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050028#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050029#include "nlsr.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050031
32namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("AdjacencyList");
35
akmhoquefdbddb12014-05-02 18:35:19 -050036using namespace std;
37
akmhoquec8a10f72014-04-25 18:42:55 -050038AdjacencyList::AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050039{
40}
41
akmhoquec8a10f72014-04-25 18:42:55 -050042AdjacencyList::~AdjacencyList()
akmhoque53353462014-04-22 08:43:45 -050043{
44}
45
akmhoquefdbddb12014-05-02 18:35:19 -050046int32_t
47AdjacencyList::insert(Adjacent& adjacent)
akmhoque53353462014-04-22 08:43:45 -050048{
akmhoquefdbddb12014-05-02 18:35:19 -050049 std::list<Adjacent>::iterator it = find(adjacent.getName());
akmhoque157b0a42014-05-13 00:26:37 -050050 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050051 return -1;
52 }
akmhoquefdbddb12014-05-02 18:35:19 -050053 m_adjList.push_back(adjacent);
akmhoque53353462014-04-22 08:43:45 -050054 return 0;
55}
56
57void
akmhoquefdbddb12014-05-02 18:35:19 -050058AdjacencyList::addAdjacents(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050059{
60 for (std::list<Adjacent>::iterator it = adl.getAdjList().begin();
akmhoque157b0a42014-05-13 00:26:37 -050061 it != adl.getAdjList().end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -050062 insert((*it));
63 }
64}
65
Vince Lehmancb76ade2014-08-28 21:24:41 -050066bool
67AdjacencyList::updateAdjacentStatus(const ndn::Name& adjName, Adjacent::Status s)
akmhoque53353462014-04-22 08:43:45 -050068{
69 std::list<Adjacent>::iterator it = find(adjName);
Vince Lehmancb76ade2014-08-28 21:24:41 -050070
akmhoque157b0a42014-05-13 00:26:37 -050071 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -050072 return false;
akmhoque53353462014-04-22 08:43:45 -050073 }
Vince Lehmancb76ade2014-08-28 21:24:41 -050074 else {
75 it->setStatus(s);
76 return true;
77 }
akmhoque53353462014-04-22 08:43:45 -050078}
79
80Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050081AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050082{
83 Adjacent adj(adjName);
84 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050085 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050086 return (*it);
87 }
88 return adj;
89}
90
akmhoquefdbddb12014-05-02 18:35:19 -050091static bool
92compareAdjacent(const Adjacent& adjacent1, const Adjacent& adjacent2)
93{
94 return adjacent1.getName() < adjacent2.getName();
95}
akmhoque53353462014-04-22 08:43:45 -050096
97bool
akmhoquefdbddb12014-05-02 18:35:19 -050098AdjacencyList::operator==(AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -050099{
akmhoque157b0a42014-05-13 00:26:37 -0500100 if (getSize() != adl.getSize()) {
akmhoque53353462014-04-22 08:43:45 -0500101 return false;
102 }
akmhoquefdbddb12014-05-02 18:35:19 -0500103 m_adjList.sort(compareAdjacent);
104 adl.getAdjList().sort(compareAdjacent);
105 uint32_t equalAdjCount = 0;
106 std::list<Adjacent>& adjList2 = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500107 std::list<Adjacent>::iterator it1;
108 std::list<Adjacent>::iterator it2;
109 for (it1 = m_adjList.begin(), it2 = adjList2.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500110 it1 != m_adjList.end(); it1++, it2++) {
111 if (!((*it1) == (*it2))) {
akmhoque53353462014-04-22 08:43:45 -0500112 break;
113 }
114 equalAdjCount++;
115 }
116 return equalAdjCount == getSize();
117}
118
akmhoquefdbddb12014-05-02 18:35:19 -0500119int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500120AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
akmhoque53353462014-04-22 08:43:45 -0500121{
122 std::list<Adjacent>::iterator it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -0500123 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500124 return -1;
125 }
126 (*it).setLinkCost(lc);
127 return 0;
128}
129
130bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500131AdjacencyList::isNeighbor(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500132{
133 std::list<Adjacent>::iterator it = find(adjName);
134 if (it == m_adjList.end())
135 {
136 return false;
137 }
138 return true;
139}
140
141void
akmhoque31d1d4b2014-05-05 22:08:14 -0500142AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
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()) {
akmhoque53353462014-04-22 08:43:45 -0500146 return ;
147 }
148 (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
149}
150
151void
akmhoque31d1d4b2014-05-05 22:08:14 -0500152AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
153 uint32_t count)
akmhoque53353462014-04-22 08:43:45 -0500154{
155 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500156 if (it != m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500157 (*it).setInterestTimedOutNo(count);
158 }
159}
160
akmhoquefdbddb12014-05-02 18:35:19 -0500161int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -0500162AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500163{
164 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500165 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -0500166 return -1;
167 }
168 return (*it).getInterestTimedOutNo();
169}
170
Vince Lehmancb76ade2014-08-28 21:24:41 -0500171Adjacent::Status
akmhoque31d1d4b2014-05-05 22:08:14 -0500172AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -0500173{
174 std::list<Adjacent>::iterator it = find(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500175
akmhoque157b0a42014-05-13 00:26:37 -0500176 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500177 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500178 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500179 else {
180 return it->getStatus();
181 }
akmhoque53353462014-04-22 08:43:45 -0500182}
183
184void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500185AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500186{
187 std::list<Adjacent>::iterator it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500188 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500189 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500190 }
191}
192
193std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500194AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500195{
196 return m_adjList;
197}
198
199bool
akmhoquec8a10f72014-04-25 18:42:55 -0500200AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500201{
202 uint32_t nbrCount = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500203 for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end() ; it++) {
204
205 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500206 nbrCount++;
207 }
akmhoque157b0a42014-05-13 00:26:37 -0500208 else {
akmhoque53353462014-04-22 08:43:45 -0500209 if ((*it).getInterestTimedOutNo() >=
akmhoque157b0a42014-05-13 00:26:37 -0500210 pnlsr.getConfParameter().getInterestRetryNumber()) {
akmhoque53353462014-04-22 08:43:45 -0500211 nbrCount++;
212 }
213 }
214 }
akmhoque157b0a42014-05-13 00:26:37 -0500215 if (nbrCount == m_adjList.size()) {
akmhoque53353462014-04-22 08:43:45 -0500216 return true;
217 }
218 return false;
219}
220
akmhoquefdbddb12014-05-02 18:35:19 -0500221int32_t
akmhoquec8a10f72014-04-25 18:42:55 -0500222AdjacencyList::getNumOfActiveNeighbor()
akmhoque53353462014-04-22 08:43:45 -0500223{
akmhoquefdbddb12014-05-02 18:35:19 -0500224 int32_t actNbrCount = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500225 for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
226
227 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500228 actNbrCount++;
229 }
230 }
231 return actNbrCount;
232}
233
234std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500235AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500236{
akmhoque53353462014-04-22 08:43:45 -0500237 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
238 m_adjList.end(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500239 ndn::bind(&Adjacent::compare,
240 _1, ndn::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500241 return it;
242}
243
akmhoquec04e7272014-07-02 11:00:14 -0500244Adjacent *
245AdjacencyList::findAdjacent(const ndn::Name& adjName)
246{
247 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
248 m_adjList.end(),
249 ndn::bind(&Adjacent::compare,
250 _1, ndn::cref(adjName)));
251 if (it != m_adjList.end()) {
252 return &(*it);
253 }
254
255 return 0;
256}
257
258Adjacent *
259AdjacencyList::findAdjacent(uint64_t faceId)
260{
261 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
262 m_adjList.end(),
263 ndn::bind(&Adjacent::compareFaceId,
264 _1, faceId));
265 if (it != m_adjList.end()) {
266 return &(*it);
267 }
268
269 return 0;
270}
271
akmhoque102aea42014-08-04 10:22:12 -0500272uint64_t
273AdjacencyList::getFaceId(const std::string& faceUri)
274{
275 std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
276 m_adjList.end(),
277 ndn::bind(&Adjacent::compareFaceUri,
278 _1, faceUri));
279 if (it != m_adjList.end()) {
280 return it->getFaceId();
281 }
282
283 return 0;
284}
285
akmhoque674b0b12014-05-20 14:33:28 -0500286void
287AdjacencyList::writeLog()
288{
289 _LOG_DEBUG("-------Adjacency List--------");
290 for (std::list<Adjacent>::iterator it = m_adjList.begin();
291 it != m_adjList.end(); it++) {
292 (*it).writeLog();
293 }
294}
295
akmhoque53353462014-04-22 08:43:45 -0500296} //namespace nlsr