blob: 50d6303403436e227ee165ea115c1bf01ac0a582 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include "nhl.hpp"
3#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
37Nhl::addNextHop(NextHop& nh)
38{
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
58Nhl::removeNextHop(NextHop& nh)
59{
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
70Nhl::sort()
71{
72 m_nexthopList.sort(nextHopSortingComparator);
73}
74
75ostream&
76operator<<(ostream& os, Nhl& nhl)
77{
78 std::list<NextHop> nexthopList = nhl.getNextHopList();
79 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