akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <list> |
| 3 | |
| 4 | #include "nlsr.hpp" |
| 5 | #include "adjacent.hpp" |
| 6 | #include "lsa.hpp" |
| 7 | #include "lsdb.hpp" |
| 8 | #include "map.hpp" |
| 9 | |
| 10 | namespace nlsr { |
| 11 | |
| 12 | using namespace std; |
| 13 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 14 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 15 | mapEntryCompareByRouter(MapEntry& mpe1, const ndn::Name& rtrName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 16 | { |
| 17 | return mpe1.getRouter() == rtrName; |
| 18 | } |
| 19 | |
| 20 | static bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 21 | mapEntryCompareByMappingNo(MapEntry& mpe1, int32_t mappingNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | { |
| 23 | return mpe1.getMappingNumber() == mappingNo; |
| 24 | } |
| 25 | |
| 26 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 27 | Map::addEntry(const ndn::Name& rtrName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | { |
| 29 | MapEntry me(rtrName, m_mappingIndex); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 30 | if (addEntry(me)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | m_mappingIndex++; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 36 | Map::addEntry(MapEntry& mpe) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | { |
| 38 | //cout << mpe; |
| 39 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 40 | m_table.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 41 | ndn::bind(&mapEntryCompareByRouter, |
| 42 | _1, mpe.getRouter())); |
| 43 | if (it == m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | m_table.push_back(mpe); |
| 45 | return true; |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 50 | const ndn::Name |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 51 | Map::getRouterNameByMappingNo(int32_t mn) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | { |
| 53 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 54 | m_table.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 55 | ndn::bind(&mapEntryCompareByMappingNo, |
| 56 | _1, mn)); |
| 57 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | return (*it).getRouter(); |
| 59 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 60 | return ndn::Name(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | } |
| 62 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 63 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 64 | Map::getMappingNoByRouterName(const ndn::Name& rName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | { |
| 66 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 67 | m_table.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 68 | ndn::bind(&mapEntryCompareByRouter, |
| 69 | _1, rName)); |
| 70 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | return (*it).getMappingNumber(); |
| 72 | } |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | Map::createFromAdjLsdb(Nlsr& pnlsr) |
| 78 | { |
| 79 | std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb(); |
| 80 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 81 | it != adjLsdb.end() ; it++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 82 | addEntry((*it).getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 83 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
| 84 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 85 | itAdl != adl.end() ; itAdl++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 86 | addEntry((*itAdl).getName()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Map::reset() |
| 93 | { |
| 94 | m_table.clear(); |
| 95 | m_mappingIndex = 0; |
| 96 | } |
| 97 | |
| 98 | ostream& |
| 99 | operator<<(ostream& os, Map& map) |
| 100 | { |
| 101 | os << "---------------Map----------------------" << endl; |
| 102 | std::list<MapEntry> ml = map.getMapList(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame^] | 103 | for (std::list<MapEntry>::iterator it = ml.begin(); it != ml.end() ; it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | os << (*it); |
| 105 | } |
| 106 | return os; |
| 107 | } |
| 108 | |
| 109 | } //namespace nlsr |