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