blob: 5468b408522ee2a7d179af2b9f06da9caa90f325 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
akmhoquec8a10f72014-04-25 18:42:55 -05002#include "nexthop-list.hpp"
akmhoque53353462014-04-22 08:43:45 -05003#include "nexthop.hpp"
4
5namespace nlsr {
6
7using namespace std;
8
9static bool
10nexthopCompare(NextHop& nh1, NextHop& nh2)
11{
12 return nh1.getConnectingFace() == nh2.getConnectingFace();
13}
14
15static bool
16nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
17{
18 return (nh1.getConnectingFace() == nh2.getConnectingFace() &&
19 nh1.getRouteCost() == nh2.getRouteCost()) ;
20}
21
22static bool
23nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
24{
25 return nh1.getRouteCost() < nh2.getRouteCost();
26}
27
28/**
29Add next hop to the Next Hop list
30If next hop is new it is added
31If next hop already exists in next
32hop list then updates the route
33cost with new next hop's route cost
34*/
35
36void
akmhoquec8a10f72014-04-25 18:42:55 -050037NexthopList::addNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050038{
39 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
40 m_nexthopList.end(),
41 bind(&nexthopCompare, _1, nh));
42 if (it == m_nexthopList.end())
43 {
44 m_nexthopList.push_back(nh);
45 }
46 if ((*it).getRouteCost() > nh.getRouteCost())
47 {
48 (*it).setRouteCost(nh.getRouteCost());
49 }
50}
51
52/**
53Remove a next hop only if both next hop face and route cost are same
54
55*/
56
57void
akmhoquec8a10f72014-04-25 18:42:55 -050058NexthopList::removeNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050059{
60 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
61 m_nexthopList.end(),
62 bind(&nexthopRemoveCompare, _1, nh));
63 if (it != m_nexthopList.end())
64 {
65 m_nexthopList.erase(it);
66 }
67}
68
69void
akmhoquec8a10f72014-04-25 18:42:55 -050070NexthopList::sort()
akmhoque53353462014-04-22 08:43:45 -050071{
72 m_nexthopList.sort(nextHopSortingComparator);
73}
74
75ostream&
akmhoquec8a10f72014-04-25 18:42:55 -050076operator<<(ostream& os, NexthopList& nhl)
akmhoque53353462014-04-22 08:43:45 -050077{
akmhoquefdbddb12014-05-02 18:35:19 -050078 std::list<NextHop> nexthopList = nhl.getNextHops();
akmhoque53353462014-04-22 08:43:45 -050079 int i = 1;
80 for (std::list<NextHop>::iterator it = nexthopList.begin();
81 it != nexthopList.end() ; it++, i++)
82 {
83 os << "Nexthop " << i << ": " << (*it) << endl;
84 }
85 return os;
86}
87
88}//namespace nlsr