blob: 22046fd30a12858139a28110ed102ae2c69c3871 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Muktadir R Chowdhury800833b2016-07-29 13:43:59 -05003 * Copyright (c) 2014-2016, The University of Memphis,
Vince Lehmancec38852015-03-31 13:21:38 -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 Lehmancec38852015-03-31 13:21:38 -050021
Vince Lehman0a7da612014-10-29 14:39:29 -050022#include "common.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050023#include "nexthop-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050024#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050025#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050026
27namespace nlsr {
28
akmhoque674b0b12014-05-20 14:33:28 -050029INIT_LOGGER("NexthopList");
30
akmhoque53353462014-04-22 08:43:45 -050031using namespace std;
32
33static bool
Vince Lehmanef21d8e2015-04-01 15:59:39 -050034nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
akmhoque53353462014-04-22 08:43:45 -050035{
akmhoque157b0a42014-05-13 00:26:37 -050036 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050037}
38
39static bool
Vince Lehmanef21d8e2015-04-01 15:59:39 -050040nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
akmhoque53353462014-04-22 08:43:45 -050041{
akmhoque157b0a42014-05-13 00:26:37 -050042 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
Vince Lehman145064a2014-08-23 11:44:16 -050043 nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ;
akmhoque53353462014-04-22 08:43:45 -050044}
45
akmhoque53353462014-04-22 08:43:45 -050046void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050047NexthopList::addNextHop(const NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050048{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050049 std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
akmhoque53353462014-04-22 08:43:45 -050050 m_nexthopList.end(),
Vince Lehmanef21d8e2015-04-01 15:59:39 -050051 ndn::bind(&nexthopAddCompare, _1, nh));
akmhoque157b0a42014-05-13 00:26:37 -050052 if (it == m_nexthopList.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050053 m_nexthopList.insert(nh);
akmhoque53353462014-04-22 08:43:45 -050054 }
Vince Lehmanef21d8e2015-04-01 15:59:39 -050055 else if (it->getRouteCost() > nh.getRouteCost()) {
56 removeNextHop(*it);
57 m_nexthopList.insert(nh);
akmhoque53353462014-04-22 08:43:45 -050058 }
59}
60
akmhoque53353462014-04-22 08:43:45 -050061void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050062NexthopList::removeNextHop(const NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050063{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050064 std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
akmhoque53353462014-04-22 08:43:45 -050065 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050066 ndn::bind(&nexthopRemoveCompare, _1, nh));
67 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050068 m_nexthopList.erase(it);
69 }
70}
71
72void
akmhoque674b0b12014-05-20 14:33:28 -050073NexthopList::writeLog()
74{
75 int i = 1;
Vince Lehmanef21d8e2015-04-01 15:59:39 -050076
77 for (std::set<NextHop, NextHopComparator>::iterator it = m_nexthopList.begin();
akmhoque674b0b12014-05-20 14:33:28 -050078 it != m_nexthopList.end() ; it++, i++) {
79 _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
80 << " Route Cost: " << (*it).getRouteCost());
81 }
82}
83
Nick Gordonfad8e252016-08-11 14:21:38 -050084} // namespace nlsr