blob: 4fe50aa8c488c7857849e8967c82853ae63f25c2 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <iostream>
2
3#include "nlsr_nhl.hpp"
4#include "nlsr_nexthop.hpp"
5
6using namespace std;
7
8static bool
9nexthopCompare(NextHop& nh1, NextHop& nh2){
10 return nh1.getConnectingFace()==nh2.getConnectingFace();
11}
12
13static bool
14nexthopRemoveCompare(NextHop& nh1, NextHop& nh2){
15 return (nh1.getConnectingFace()==nh2.getConnectingFace() &&
16 nh1.getRouteCost() == nh2.getRouteCost()) ;
17}
18
19static bool
20nextHopSortingComparator(NextHop& nh1, NextHop& nh2)
21{
22 return nh1.getRouteCost() < nh2.getRouteCost();
23}
24
25/**
26Add next hop to the Next Hop list
27If next hop is new it is added
28If next hop already exists in next
29hop list then updates the route
30cost with new next hop's route cost
31*/
32
33void
34Nhl::addNextHop(NextHop& nh)
35{
36 std::list<NextHop >::iterator it = std::find_if( nexthopList.begin(),
37 nexthopList.end(),
38 bind(&nexthopCompare, _1, nh));
39 if ( it == nexthopList.end() ){
40 nexthopList.push_back(nh);
41 }
42 if ( (*it).getRouteCost() > nh.getRouteCost() )
43 {
44 (*it).setRouteCost(nh.getRouteCost());
45 }
46}
47
48/**
49Remove a next hop only if both next hop face and route cost are same
50
51*/
52
53void
54Nhl::removeNextHop(NextHop &nh)
55{
56 std::list<NextHop >::iterator it = std::find_if( nexthopList.begin(),
57 nexthopList.end(),
58 bind(&nexthopRemoveCompare, _1, nh));
59 if ( it != nexthopList.end() ){
60 nexthopList.erase(it);
61 }
62}
63
64void
65Nhl::sortNhl()
66{
67 nexthopList.sort(nextHopSortingComparator);
68}
69
70ostream&
71operator<<(ostream& os, Nhl& nhl)
72{
73 std::list< NextHop > nexthopList = nhl.getNextHopList();
74 int i=1;
75 for( std::list<NextHop>::iterator it=nexthopList.begin();
76 it!= nexthopList.end() ; it++,i++)
77 {
78 os << "Nexthop "<<i<<": "<<(*it)<<endl;
79 }
80 return os;
81}