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