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