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 | |
| 6 | using namespace std; |
| 7 | |
| 8 | static bool |
| 9 | nexthopCompare(NextHop& nh1, NextHop& nh2){ |
| 10 | return nh1.getConnectingFace()==nh2.getConnectingFace(); |
| 11 | } |
| 12 | |
| 13 | static bool |
| 14 | nexthopRemoveCompare(NextHop& nh1, NextHop& nh2){ |
| 15 | return (nh1.getConnectingFace()==nh2.getConnectingFace() && |
| 16 | nh1.getRouteCost() == nh2.getRouteCost()) ; |
| 17 | } |
| 18 | |
| 19 | static bool |
| 20 | nextHopSortingComparator(NextHop& nh1, NextHop& nh2) |
| 21 | { |
| 22 | return nh1.getRouteCost() < nh2.getRouteCost(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | Add next hop to the Next Hop list |
| 27 | If next hop is new it is added |
| 28 | If next hop already exists in next |
| 29 | hop list then updates the route |
| 30 | cost with new next hop's route cost |
| 31 | */ |
| 32 | |
| 33 | void |
| 34 | Nhl::addNextHop(NextHop& nh) |
| 35 | { |
| 36 | std::list<NextHop >::iterator it = std::find_if( nexthopList.begin(), |
| 37 | nexthopList.end(), |
| 38 | bind(&nexthopCompare, _1, nh)); |
| 39 | if ( it == nexthopList.end() ){ |
| 40 | nexthopList.push_back(nh); |
| 41 | } |
| 42 | if ( (*it).getRouteCost() > nh.getRouteCost() ) |
| 43 | { |
| 44 | (*it).setRouteCost(nh.getRouteCost()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | Remove a next hop only if both next hop face and route cost are same |
| 50 | |
| 51 | */ |
| 52 | |
| 53 | void |
| 54 | Nhl::removeNextHop(NextHop &nh) |
| 55 | { |
| 56 | std::list<NextHop >::iterator it = std::find_if( nexthopList.begin(), |
| 57 | nexthopList.end(), |
| 58 | bind(&nexthopRemoveCompare, _1, nh)); |
| 59 | if ( it != nexthopList.end() ){ |
| 60 | nexthopList.erase(it); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | Nhl::sortNhl() |
| 66 | { |
| 67 | nexthopList.sort(nextHopSortingComparator); |
| 68 | } |
| 69 | |
| 70 | ostream& |
| 71 | operator<<(ostream& os, Nhl& nhl) |
| 72 | { |
| 73 | std::list< NextHop > nexthopList = nhl.getNextHopList(); |
| 74 | int i=1; |
| 75 | for( std::list<NextHop>::iterator it=nexthopList.begin(); |
| 76 | it!= nexthopList.end() ; it++,i++) |
| 77 | { |
| 78 | os << "Nexthop "<<i<<": "<<(*it)<<endl; |
| 79 | } |
| 80 | return os; |
| 81 | } |