akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #include <iostream> |
| 2 | |
| 3 | #include "nlsr_nhl.hpp" |
| 4 | #include "nlsr_nexthop.hpp" |
| 5 | |
| 6 | namespace nlsr |
| 7 | { |
| 8 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 9 | using namespace std; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 10 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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(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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 45 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 46 | nexthopList.push_back(nh); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 47 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 48 | if ( (*it).getRouteCost() > nh.getRouteCost() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 49 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 50 | (*it).setRouteCost(nh.getRouteCost()); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 51 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 52 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 53 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 66 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 67 | nexthopList.erase(it); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 68 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 69 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 70 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 71 | void |
| 72 | Nhl::sortNhl() |
| 73 | { |
| 74 | nexthopList.sort(nextHopSortingComparator); |
| 75 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 76 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 84 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 85 | os << "Nexthop "<<i<<": "<<(*it)<<endl; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 86 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 87 | return os; |
| 88 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 89 | |
| 90 | }//namespace nlsr |