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