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