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