akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame^] | 1 | #include <list> |
| 2 | #include <utility> |
| 3 | #include "npte.hpp" |
| 4 | #include "routing-table-entry.hpp" |
| 5 | #include "nexthop.hpp" |
| 6 | |
| 7 | namespace nlsr { |
| 8 | |
| 9 | using namespace std; |
| 10 | |
| 11 | void |
| 12 | Npte::generateNhlfromRteList() |
| 13 | { |
| 14 | m_nhl.reset(); |
| 15 | for (std::list<RoutingTableEntry>::iterator it = m_rteList.begin(); |
| 16 | it != m_rteList.end(); ++it) |
| 17 | { |
| 18 | for (std::list<NextHop>::iterator nhit = |
| 19 | (*it).getNhl().getNextHopList().begin(); |
| 20 | nhit != (*it).getNhl().getNextHopList().end(); ++nhit) |
| 21 | { |
| 22 | m_nhl.addNextHop((*nhit)); |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | static bool |
| 30 | rteCompare(RoutingTableEntry& rte, string& destRouter) |
| 31 | { |
| 32 | return rte.getDestination() == destRouter; |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | Npte::removeRoutingTableEntry(RoutingTableEntry& rte) |
| 37 | { |
| 38 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_rteList.begin(), |
| 39 | m_rteList.end(), |
| 40 | bind(&rteCompare, _1, rte.getDestination())); |
| 41 | if (it != m_rteList.end()) |
| 42 | { |
| 43 | m_rteList.erase(it); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | Npte::addRoutingTableEntry(RoutingTableEntry& rte) |
| 49 | { |
| 50 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_rteList.begin(), |
| 51 | m_rteList.end(), |
| 52 | bind(&rteCompare, _1, rte.getDestination())); |
| 53 | if (it == m_rteList.end()) |
| 54 | { |
| 55 | m_rteList.push_back(rte); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | (*it).getNhl().reset(); // reseting existing routing table's next hop |
| 60 | for (std::list<NextHop>::iterator nhit = rte.getNhl().getNextHopList().begin(); |
| 61 | nhit != rte.getNhl().getNextHopList().end(); ++nhit) |
| 62 | { |
| 63 | (*it).getNhl().addNextHop((*nhit)); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | //debugging purpose |
| 69 | ostream& |
| 70 | operator<<(ostream& os, Npte& npte) |
| 71 | { |
| 72 | os << "Name: " << npte.getNamePrefix() << endl; |
| 73 | std::list<RoutingTableEntry> rteList = npte.getRteList(); |
| 74 | for (std::list<RoutingTableEntry>::iterator it = rteList.begin(); |
| 75 | it != rteList.end(); ++it) |
| 76 | { |
| 77 | cout << (*it); |
| 78 | } |
| 79 | os << npte.getNhl(); |
| 80 | return os; |
| 81 | } |
| 82 | |
| 83 | }//namespace nlsr |