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 | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 15 | mapEntryCompareByRouter(MapEntry& mpe1, const string& 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 | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 27 | Map::addEntry(const string& rtrName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | { |
| 29 | MapEntry me(rtrName, m_mappingIndex); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 30 | if (addEntry(me)) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | { |
| 32 | m_mappingIndex++; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 37 | Map::addEntry(MapEntry& mpe) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | { |
| 39 | //cout << mpe; |
| 40 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 41 | m_table.end(), |
| 42 | bind(&mapEntryCompareByRouter, _1, mpe.getRouter())); |
| 43 | if (it == m_table.end()) |
| 44 | { |
| 45 | m_table.push_back(mpe); |
| 46 | return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | string |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 52 | Map::getRouterNameByMappingNo(int32_t mn) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 53 | { |
| 54 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 55 | m_table.end(), |
| 56 | bind(&mapEntryCompareByMappingNo, |
| 57 | _1, mn)); |
| 58 | if (it != m_table.end()) |
| 59 | { |
| 60 | return (*it).getRouter(); |
| 61 | } |
| 62 | return ""; |
| 63 | } |
| 64 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 65 | int32_t |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | Map::getMappingNoByRouterName(string& rName) |
| 67 | { |
| 68 | std::list<MapEntry>::iterator it = std::find_if(m_table.begin(), |
| 69 | m_table.end(), |
| 70 | bind(&mapEntryCompareByRouter, |
| 71 | _1, rName)); |
| 72 | if (it != m_table.end()) |
| 73 | { |
| 74 | return (*it).getMappingNumber(); |
| 75 | } |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Map::createFromAdjLsdb(Nlsr& pnlsr) |
| 81 | { |
| 82 | std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb(); |
| 83 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); |
| 84 | it != adjLsdb.end() ; it++) |
| 85 | { |
| 86 | string linkStartRouter = (*it).getOrigRouter(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 87 | addEntry(linkStartRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
| 89 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); |
| 90 | itAdl != adl.end() ; itAdl++) |
| 91 | { |
| 92 | string linkEndRouter = (*itAdl).getName(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 93 | addEntry(linkEndRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | Map::reset() |
| 100 | { |
| 101 | m_table.clear(); |
| 102 | m_mappingIndex = 0; |
| 103 | } |
| 104 | |
| 105 | ostream& |
| 106 | operator<<(ostream& os, Map& map) |
| 107 | { |
| 108 | os << "---------------Map----------------------" << endl; |
| 109 | std::list<MapEntry> ml = map.getMapList(); |
| 110 | for (std::list<MapEntry>::iterator it = ml.begin(); it != ml.end() ; it++) |
| 111 | { |
| 112 | os << (*it); |
| 113 | } |
| 114 | return os; |
| 115 | } |
| 116 | |
| 117 | } //namespace nlsr |