akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #include <iostream> |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 2 | #include "utility/nlsr_logger.hpp" |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 3 | #include "nlsr_nhl.hpp" |
| 4 | #include "nlsr_nexthop.hpp" |
| 5 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 6 | #define THIS_FILE "nlsr_nhl.cpp" |
| 7 | |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 8 | namespace nlsr |
| 9 | { |
| 10 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 11 | using namespace std; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 12 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 13 | static bool |
| 14 | nexthopCompare(NextHop& nh1, NextHop& nh2) |
| 15 | { |
| 16 | return nh1.getConnectingFace()==nh2.getConnectingFace(); |
| 17 | } |
| 18 | |
| 19 | static bool |
| 20 | nexthopRemoveCompare(NextHop& nh1, NextHop& nh2) |
| 21 | { |
| 22 | return (nh1.getConnectingFace()==nh2.getConnectingFace() && |
| 23 | nh1.getRouteCost() == nh2.getRouteCost()) ; |
| 24 | } |
| 25 | |
| 26 | static bool |
| 27 | nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2) |
| 28 | { |
| 29 | return nh1.getRouteCost() < nh2.getRouteCost(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | Add next hop to the Next Hop list |
| 34 | If next hop is new it is added |
| 35 | If next hop already exists in next |
| 36 | hop list then updates the route |
| 37 | cost with new next hop's route cost |
| 38 | */ |
| 39 | |
| 40 | void |
| 41 | Nhl::addNextHop(NextHop& nh) |
| 42 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 43 | std::list<NextHop >::iterator it = std::find_if( m_nexthopList.begin(), |
| 44 | m_nexthopList.end(), |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 45 | bind(&nexthopCompare, _1, nh)); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 46 | if ( it == m_nexthopList.end() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 47 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 48 | m_nexthopList.push_back(nh); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 49 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 50 | if ( (*it).getRouteCost() > 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 | (*it).setRouteCost(nh.getRouteCost()); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 53 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 54 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 55 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 56 | /** |
| 57 | Remove a next hop only if both next hop face and route cost are same |
| 58 | |
| 59 | */ |
| 60 | |
| 61 | void |
| 62 | Nhl::removeNextHop(NextHop &nh) |
| 63 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 64 | std::list<NextHop >::iterator it = std::find_if( m_nexthopList.begin(), |
| 65 | m_nexthopList.end(), |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 66 | bind(&nexthopRemoveCompare, _1, nh)); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 67 | if ( it != m_nexthopList.end() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 68 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 69 | m_nexthopList.erase(it); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 70 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 71 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 72 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 73 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 74 | Nhl::sort() |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 75 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 76 | m_nexthopList.sort(nextHopSortingComparator); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 77 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 78 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 79 | ostream& |
| 80 | operator<<(ostream& os, Nhl& nhl) |
| 81 | { |
| 82 | std::list< NextHop > nexthopList = nhl.getNextHopList(); |
| 83 | int i=1; |
| 84 | for( std::list<NextHop>::iterator it=nexthopList.begin(); |
| 85 | it!= nexthopList.end() ; it++,i++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 86 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 87 | os << "Nexthop "<<i<<": "<<(*it)<<endl; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 88 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 89 | return os; |
| 90 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 91 | |
| 92 | }//namespace nlsr |