blob: dc111139b78f016ec6cd1f8d64f4edb2c1e0b948 [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
akmhoque53353462014-04-22 08:43:45 -050022#include <iostream>
Vince Lehman0a7da612014-10-29 14:39:29 -050023
24#include "common.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050025#include "nexthop-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050026#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050028
29namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("NexthopList");
32
akmhoque53353462014-04-22 08:43:45 -050033using namespace std;
34
35static bool
36nexthopCompare(NextHop& nh1, NextHop& nh2)
37{
akmhoque157b0a42014-05-13 00:26:37 -050038 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050039}
40
41static bool
42nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
43{
akmhoque157b0a42014-05-13 00:26:37 -050044 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
Vince Lehman145064a2014-08-23 11:44:16 -050045 nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ;
akmhoque53353462014-04-22 08:43:45 -050046}
47
48static bool
49nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
50{
Vince Lehmancec38852015-03-31 13:21:38 -050051 if (nh1.getRouteCostAsAdjustedInteger() < nh2.getRouteCostAsAdjustedInteger()) {
52 return true;
53 }
54 else if (nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) {
55 return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
56 }
57 else {
58 return false;
59 }
akmhoque53353462014-04-22 08:43:45 -050060}
61
62/**
63Add next hop to the Next Hop list
64If next hop is new it is added
65If next hop already exists in next
66hop list then updates the route
67cost with new next hop's route cost
68*/
69
70void
akmhoquec8a10f72014-04-25 18:42:55 -050071NexthopList::addNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050072{
73 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
74 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050075 ndn::bind(&nexthopCompare, _1, nh));
76 if (it == m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050077 m_nexthopList.push_back(nh);
Alexander Afanasyev1717b242014-07-15 13:53:17 -070078 return;
akmhoque53353462014-04-22 08:43:45 -050079 }
akmhoque157b0a42014-05-13 00:26:37 -050080 if ((*it).getRouteCost() > nh.getRouteCost()) {
akmhoque53353462014-04-22 08:43:45 -050081 (*it).setRouteCost(nh.getRouteCost());
82 }
83}
84
85/**
86Remove a next hop only if both next hop face and route cost are same
87
88*/
89
90void
akmhoquec8a10f72014-04-25 18:42:55 -050091NexthopList::removeNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050092{
93 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
94 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050095 ndn::bind(&nexthopRemoveCompare, _1, nh));
96 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050097 m_nexthopList.erase(it);
98 }
99}
100
101void
akmhoquec8a10f72014-04-25 18:42:55 -0500102NexthopList::sort()
akmhoque53353462014-04-22 08:43:45 -0500103{
104 m_nexthopList.sort(nextHopSortingComparator);
105}
106
akmhoque674b0b12014-05-20 14:33:28 -0500107void
108NexthopList::writeLog()
109{
110 int i = 1;
akmhoquedcee9362014-08-05 22:58:01 -0500111 sort();
akmhoque674b0b12014-05-20 14:33:28 -0500112 for (std::list<NextHop>::iterator it = m_nexthopList.begin();
113 it != m_nexthopList.end() ; it++, i++) {
114 _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
115 << " Route Cost: " << (*it).getRouteCost());
116 }
117}
118
Nick Gordonfad8e252016-08-11 14:21:38 -0500119} // namespace nlsr