akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame^] | 1 | #include <list> |
| 2 | #include <utility> |
| 3 | #include <algorithm> |
| 4 | |
| 5 | #include "npt.hpp" |
| 6 | #include "npte.hpp" |
| 7 | #include "nlsr.hpp" |
| 8 | |
| 9 | |
| 10 | namespace nlsr { |
| 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | static bool |
| 15 | npteCompare(Npte& npte, string& name) |
| 16 | { |
| 17 | return npte.getNamePrefix() == name; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | void |
| 23 | Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr) |
| 24 | { |
| 25 | std::list<Npte>::iterator it = std::find_if(m_npteList.begin(), |
| 26 | m_npteList.end(), bind(&npteCompare, _1, name)); |
| 27 | if (it == m_npteList.end()) |
| 28 | { |
| 29 | Npte newEntry(name); |
| 30 | newEntry.addRoutingTableEntry(rte); |
| 31 | newEntry.generateNhlfromRteList(); |
| 32 | newEntry.getNhl().sort(); |
| 33 | m_npteList.push_back(newEntry); |
| 34 | if (rte.getNhl().getSize() > 0) |
| 35 | { |
| 36 | pnlsr.getFib().update(pnlsr, name, newEntry.getNhl()); |
| 37 | } |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | if (rte.getNhl().getSize() > 0) |
| 42 | { |
| 43 | (*it).addRoutingTableEntry(rte); |
| 44 | (*it).generateNhlfromRteList(); |
| 45 | (*it).getNhl().sort(); |
| 46 | pnlsr.getFib().update(pnlsr, name, (*it).getNhl()); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | (*it).resetRteListNextHop(); |
| 51 | (*it).getNhl().reset(); |
| 52 | pnlsr.getFib().remove(pnlsr, name); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr) |
| 59 | { |
| 60 | std::list<Npte>::iterator it = std::find_if(m_npteList.begin(), |
| 61 | m_npteList.end(), bind(&npteCompare, _1, name)); |
| 62 | if (it != m_npteList.end()) |
| 63 | { |
| 64 | string destRouter = rte.getDestination(); |
| 65 | (*it).removeRoutingTableEntry(rte); |
| 66 | if (((*it).getRteListSize() == 0) && |
| 67 | (!pnlsr.getLsdb().doesLsaExist(destRouter + "/1", 1)) && |
| 68 | (!pnlsr.getLsdb().doesLsaExist(destRouter + "/2", 2)) && |
| 69 | (!pnlsr.getLsdb().doesLsaExist(destRouter + "/3", 3))) |
| 70 | { |
| 71 | m_npteList.erase(it); |
| 72 | pnlsr.getFib().remove(pnlsr, name); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | (*it).generateNhlfromRteList(); |
| 77 | pnlsr.getFib().update(pnlsr, name, (*it).getNhl()); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void |
| 84 | Npt::addNpteByDestName(string name, string destRouter, Nlsr& pnlsr) |
| 85 | { |
| 86 | std::pair<RoutingTableEntry&, bool> rteCheck = |
| 87 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 88 | if (rteCheck.second) |
| 89 | { |
| 90 | addNpte(name, rteCheck.first, pnlsr); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | RoutingTableEntry rte(destRouter); |
| 95 | addNpte(name, rte, pnlsr); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr) |
| 101 | { |
| 102 | std::pair<RoutingTableEntry&, bool> rteCheck = |
| 103 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
| 104 | if (rteCheck.second) |
| 105 | { |
| 106 | removeNpte(name, rteCheck.first, pnlsr); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | RoutingTableEntry rte(destRouter); |
| 111 | removeNpte(name, rte, pnlsr); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | Npt::updateWithNewRoute(Nlsr& pnlsr) |
| 117 | { |
| 118 | for (std::list<Npte>::iterator it = m_npteList.begin(); it != m_npteList.end(); |
| 119 | ++it) |
| 120 | { |
| 121 | std::list<RoutingTableEntry> rteList = (*it).getRteList(); |
| 122 | for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin(); |
| 123 | rteit != rteList.end(); ++rteit) |
| 124 | { |
| 125 | std::pair<RoutingTableEntry&, bool> rteCheck = |
| 126 | pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination()); |
| 127 | if (rteCheck.second) |
| 128 | { |
| 129 | addNpte((*it).getNamePrefix(), rteCheck.first, pnlsr); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | RoutingTableEntry rte((*rteit).getDestination()); |
| 134 | addNpte((*it).getNamePrefix(), rte, pnlsr); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | Npt::print() |
| 142 | { |
| 143 | std::cout << "----------------NPT----------------------" << std::endl; |
| 144 | for (std::list<Npte>::iterator it = m_npteList.begin(); it != m_npteList.end(); |
| 145 | ++it) |
| 146 | { |
| 147 | cout << (*it) << endl; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } //namespace nlsr |