blob: 2aaa2a6fa44a852eb626ce23751f4a965cf652fd [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
9 using namespace std;
10
11 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(NextHop& nh1, 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() )
45 {
46 nexthopList.push_back(nh);
47 }
48 if ( (*it).getRouteCost() > nh.getRouteCost() )
49 {
50 (*it).setRouteCost(nh.getRouteCost());
51 }
52 }
53
54 /**
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() )
66 {
67 nexthopList.erase(it);
68 }
69 }
70
71 void
72 Nhl::sortNhl()
73 {
74 nexthopList.sort(nextHopSortingComparator);
75 }
76
77 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++)
84 {
85 os << "Nexthop "<<i<<": "<<(*it)<<endl;
86 }
87 return os;
88 }
89
90}//namespace nlsr