blob: 953a0b66a23b222c7243810e6d5dfe60c5ab447d [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include <iostream>
2
3#include "nlsr_nhl.hpp"
4#include "nlsr_nexthop.hpp"
5
6namespace nlsr
7{
8
akmhoque5a44dd42014-03-12 18:11:32 -05009 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060010
akmhoque5a44dd42014-03-12 18:11:32 -050011 static bool
12 nexthopCompare(NextHop& nh1, NextHop& nh2)
13 {
14 return nh1.getConnectingFace()==nh2.getConnectingFace();
15 }
16
17 static bool
18 nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
19 {
20 return (nh1.getConnectingFace()==nh2.getConnectingFace() &&
21 nh1.getRouteCost() == nh2.getRouteCost()) ;
22 }
23
24 static bool
25 nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
26 {
27 return nh1.getRouteCost() < nh2.getRouteCost();
28 }
29
30 /**
31 Add next hop to the Next Hop list
32 If next hop is new it is added
33 If next hop already exists in next
34 hop list then updates the route
35 cost with new next hop's route cost
36 */
37
38 void
39 Nhl::addNextHop(NextHop& nh)
40 {
41 std::list<NextHop >::iterator it = std::find_if( nexthopList.begin(),
42 nexthopList.end(),
43 bind(&nexthopCompare, _1, nh));
44 if ( it == nexthopList.end() )
akmhoqueba094742014-02-28 11:47:21 -060045 {
akmhoque5a44dd42014-03-12 18:11:32 -050046 nexthopList.push_back(nh);
akmhoqueba094742014-02-28 11:47:21 -060047 }
akmhoque5a44dd42014-03-12 18:11:32 -050048 if ( (*it).getRouteCost() > nh.getRouteCost() )
akmhoqueba094742014-02-28 11:47:21 -060049 {
akmhoque5a44dd42014-03-12 18:11:32 -050050 (*it).setRouteCost(nh.getRouteCost());
akmhoqueba094742014-02-28 11:47:21 -060051 }
akmhoque5a44dd42014-03-12 18:11:32 -050052 }
akmhoqueba094742014-02-28 11:47:21 -060053
akmhoque5a44dd42014-03-12 18:11:32 -050054 /**
55 Remove a next hop only if both next hop face and route cost are same
56
57 */
58
59 void
60 Nhl::removeNextHop(NextHop &nh)
61 {
62 std::list<NextHop >::iterator it = std::find_if( nexthopList.begin(),
63 nexthopList.end(),
64 bind(&nexthopRemoveCompare, _1, nh));
65 if ( it != nexthopList.end() )
akmhoqueba094742014-02-28 11:47:21 -060066 {
akmhoque5a44dd42014-03-12 18:11:32 -050067 nexthopList.erase(it);
akmhoqueba094742014-02-28 11:47:21 -060068 }
akmhoque5a44dd42014-03-12 18:11:32 -050069 }
akmhoqueba094742014-02-28 11:47:21 -060070
akmhoque5a44dd42014-03-12 18:11:32 -050071 void
72 Nhl::sortNhl()
73 {
74 nexthopList.sort(nextHopSortingComparator);
75 }
akmhoqueba094742014-02-28 11:47:21 -060076
akmhoque5a44dd42014-03-12 18:11:32 -050077 ostream&
78 operator<<(ostream& os, Nhl& nhl)
79 {
80 std::list< NextHop > nexthopList = nhl.getNextHopList();
81 int i=1;
82 for( std::list<NextHop>::iterator it=nexthopList.begin();
83 it!= nexthopList.end() ; it++,i++)
akmhoqueba094742014-02-28 11:47:21 -060084 {
akmhoque5a44dd42014-03-12 18:11:32 -050085 os << "Nexthop "<<i<<": "<<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -060086 }
akmhoque5a44dd42014-03-12 18:11:32 -050087 return os;
88 }
akmhoqueba094742014-02-28 11:47:21 -060089
90}//namespace nlsr