akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include <iostream> |
| 2 | |
| 3 | #include "nlsr_nhl.hpp" |
| 4 | #include "nlsr_nexthop.hpp" |
| 5 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame^] | 6 | namespace nlsr { |
| 7 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 8 | using namespace std; |
| 9 | |
| 10 | static bool |
| 11 | nexthopCompare(NextHop& nh1, NextHop& nh2){ |
| 12 | return nh1.getConnectingFace()==nh2.getConnectingFace(); |
| 13 | } |
| 14 | |
| 15 | static bool |
| 16 | nexthopRemoveCompare(NextHop& nh1, NextHop& nh2){ |
| 17 | return (nh1.getConnectingFace()==nh2.getConnectingFace() && |
| 18 | nh1.getRouteCost() == nh2.getRouteCost()) ; |
| 19 | } |
| 20 | |
| 21 | static bool |
| 22 | nextHopSortingComparator(NextHop& nh1, NextHop& nh2) |
| 23 | { |
| 24 | return nh1.getRouteCost() < nh2.getRouteCost(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | Add next hop to the Next Hop list |
| 29 | If next hop is new it is added |
| 30 | If next hop already exists in next |
| 31 | hop list then updates the route |
| 32 | cost with new next hop's route cost |
| 33 | */ |
| 34 | |
| 35 | void |
| 36 | Nhl::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 | /** |
| 51 | Remove a next hop only if both next hop face and route cost are same |
| 52 | |
| 53 | */ |
| 54 | |
| 55 | void |
| 56 | Nhl::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 | |
| 66 | void |
| 67 | Nhl::sortNhl() |
| 68 | { |
| 69 | nexthopList.sort(nextHopSortingComparator); |
| 70 | } |
| 71 | |
| 72 | ostream& |
| 73 | operator<<(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 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame^] | 84 | |
| 85 | }//namespace nlsr |