blob: 07b7d1f9edd12cd30b74bdafdddae0bc5af2b77c [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 <iostream>
akmhoquec8a10f72014-04-25 18:42:55 -050024#include "nexthop-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050025#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050026#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050027
28namespace nlsr {
29
akmhoque674b0b12014-05-20 14:33:28 -050030INIT_LOGGER("NexthopList");
31
akmhoque53353462014-04-22 08:43:45 -050032using namespace std;
33
34static bool
35nexthopCompare(NextHop& nh1, NextHop& nh2)
36{
akmhoque157b0a42014-05-13 00:26:37 -050037 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050038}
39
40static bool
41nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
42{
akmhoque157b0a42014-05-13 00:26:37 -050043 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
akmhoque53353462014-04-22 08:43:45 -050044 nh1.getRouteCost() == nh2.getRouteCost()) ;
45}
46
47static bool
48nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
49{
50 return nh1.getRouteCost() < nh2.getRouteCost();
51}
52
53/**
54Add next hop to the Next Hop list
55If next hop is new it is added
56If next hop already exists in next
57hop list then updates the route
58cost with new next hop's route cost
59*/
60
61void
akmhoquec8a10f72014-04-25 18:42:55 -050062NexthopList::addNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050063{
64 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
65 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050066 ndn::bind(&nexthopCompare, _1, nh));
67 if (it == m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050068 m_nexthopList.push_back(nh);
69 }
akmhoque157b0a42014-05-13 00:26:37 -050070 if ((*it).getRouteCost() > nh.getRouteCost()) {
akmhoque53353462014-04-22 08:43:45 -050071 (*it).setRouteCost(nh.getRouteCost());
72 }
73}
74
75/**
76Remove a next hop only if both next hop face and route cost are same
77
78*/
79
80void
akmhoquec8a10f72014-04-25 18:42:55 -050081NexthopList::removeNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050082{
83 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
84 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050085 ndn::bind(&nexthopRemoveCompare, _1, nh));
86 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050087 m_nexthopList.erase(it);
88 }
89}
90
91void
akmhoquec8a10f72014-04-25 18:42:55 -050092NexthopList::sort()
akmhoque53353462014-04-22 08:43:45 -050093{
94 m_nexthopList.sort(nextHopSortingComparator);
95}
96
akmhoque674b0b12014-05-20 14:33:28 -050097void
98NexthopList::writeLog()
99{
100 int i = 1;
101 for (std::list<NextHop>::iterator it = m_nexthopList.begin();
102 it != m_nexthopList.end() ; it++, i++) {
103 _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
104 << " Route Cost: " << (*it).getRouteCost());
105 }
106}
107
akmhoque53353462014-04-22 08:43:45 -0500108ostream&
akmhoquec8a10f72014-04-25 18:42:55 -0500109operator<<(ostream& os, NexthopList& nhl)
akmhoque53353462014-04-22 08:43:45 -0500110{
akmhoquefdbddb12014-05-02 18:35:19 -0500111 std::list<NextHop> nexthopList = nhl.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500112 int i = 1;
113 for (std::list<NextHop>::iterator it = nexthopList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500114 it != nexthopList.end() ; it++, i++) {
akmhoque53353462014-04-22 08:43:45 -0500115 os << "Nexthop " << i << ": " << (*it) << endl;
116 }
117 return os;
118}
119
120}//namespace nlsr