blob: 2aaa2a6fa44a852eb626ce23751f4a965cf652fd [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <iostream>
2
3#include "nlsr_nhl.hpp"
4#include "nlsr_nexthop.hpp"
5
akmhoque1fd8c1e2014-02-19 19:41:49 -06006namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -06007{
akmhoque298385a2014-02-13 14:13:09 -06008
akmhoque1fd8c1e2014-02-19 19:41:49 -06009 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060010
akmhoque1fd8c1e2014-02-19 19:41:49 -060011 static bool
12 nexthopCompare(NextHop& nh1, NextHop& nh2)
13 {
14 return nh1.getConnectingFace()==nh2.getConnectingFace();
15 }
akmhoque298385a2014-02-13 14:13:09 -060016
akmhoque1fd8c1e2014-02-19 19:41:49 -060017 static bool
18 nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
19 {
20 return (nh1.getConnectingFace()==nh2.getConnectingFace() &&
21 nh1.getRouteCost() == nh2.getRouteCost()) ;
22 }
akmhoque298385a2014-02-13 14:13:09 -060023
akmhoque1fd8c1e2014-02-19 19:41:49 -060024 static bool
25 nextHopSortingComparator(NextHop& nh1, NextHop& nh2)
26 {
27 return nh1.getRouteCost() < nh2.getRouteCost();
28 }
akmhoque298385a2014-02-13 14:13:09 -060029
akmhoque1fd8c1e2014-02-19 19:41:49 -060030 /**
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 */
akmhoque298385a2014-02-13 14:13:09 -060037
akmhoque1fd8c1e2014-02-19 19:41:49 -060038 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 }
akmhoque298385a2014-02-13 14:13:09 -060053
akmhoque1fd8c1e2014-02-19 19:41:49 -060054 /**
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 }
akmhoqueb1710aa2014-02-19 17:13:36 -060089
90}//namespace nlsr