akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame^] | 1 | #include<iostream> |
| 2 | #include<list> |
| 3 | |
| 4 | #include "nlsr.hpp" |
| 5 | #include "nlsr_adjacent.hpp" |
| 6 | #include "nlsr_lsa.hpp" |
| 7 | #include "nlsr_lsdb.hpp" |
| 8 | #include "nlsr_map.hpp" |
| 9 | |
| 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | ostream& |
| 14 | operator<<(ostream& os, MapEntry& mpe) |
| 15 | { |
| 16 | os<<"MapEntry: ( Router: "<<mpe.getRouter()<<" Mapping No: "; |
| 17 | os<<mpe.getMappingNumber()<<" )"<<endl; |
| 18 | |
| 19 | return os; |
| 20 | } |
| 21 | |
| 22 | static bool |
| 23 | mapEntryCompareByRouter(MapEntry& mpe1, string& rtrName){ |
| 24 | return mpe1.getRouter()==rtrName; |
| 25 | } |
| 26 | |
| 27 | static bool |
| 28 | mapEntryCompareByMappingNo(MapEntry& mpe1, int mappingNo){ |
| 29 | return mpe1.getMappingNumber()==mappingNo; |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | Map::addMapElement(string& rtrName) |
| 34 | { |
| 35 | MapEntry me(rtrName,mappingIndex); |
| 36 | if ( addMapElement(me) ) |
| 37 | { |
| 38 | mappingIndex++; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool |
| 43 | Map::addMapElement(MapEntry& mpe) |
| 44 | { |
| 45 | //cout << mpe; |
| 46 | std::list<MapEntry >::iterator it = std::find_if( rMap.begin(), |
| 47 | rMap.end(), |
| 48 | bind(&mapEntryCompareByRouter, _1, mpe.getRouter())); |
| 49 | if ( it == rMap.end() ){ |
| 50 | rMap.push_back(mpe); |
| 51 | return true; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | string |
| 57 | Map::getRouterNameByMappingNo(int mn) |
| 58 | { |
| 59 | std::list<MapEntry >::iterator it = std::find_if( rMap.begin(), |
| 60 | rMap.end(), |
| 61 | bind(&mapEntryCompareByMappingNo, _1, mn)); |
| 62 | if ( it != rMap.end() ){ |
| 63 | return (*it).getRouter(); |
| 64 | } |
| 65 | return ""; |
| 66 | } |
| 67 | |
| 68 | int |
| 69 | Map::getMappingNoByRouterName(string& rName) |
| 70 | { |
| 71 | std::list<MapEntry >::iterator it = std::find_if( rMap.begin(), |
| 72 | rMap.end(), |
| 73 | bind(&mapEntryCompareByRouter, _1, rName)); |
| 74 | if ( it != rMap.end() ){ |
| 75 | return (*it).getMappingNumber(); |
| 76 | } |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | Map::createMapFromAdjLsdb(nlsr& pnlsr) |
| 82 | { |
| 83 | std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb(); |
| 84 | for( std::list<AdjLsa>::iterator it=adjLsdb.begin(); |
| 85 | it!= adjLsdb.end() ; it++) |
| 86 | { |
| 87 | string linkStartRouter=(*it).getOrigRouter(); |
| 88 | addMapElement(linkStartRouter); |
| 89 | std::list<Adjacent> adl=(*it).getAdl().getAdjList(); |
| 90 | for( std::list<Adjacent>::iterator itAdl=adl.begin(); |
| 91 | itAdl!= adl.end() ; itAdl++) |
| 92 | { |
| 93 | string linkEndRouter=(*itAdl).getAdjacentName(); |
| 94 | addMapElement(linkEndRouter); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | Map::resetMap() |
| 102 | { |
| 103 | rMap.clear(); |
| 104 | mappingIndex=0; |
| 105 | } |
| 106 | |
| 107 | ostream& |
| 108 | operator<<(ostream& os, Map& rMap) |
| 109 | { |
| 110 | os<<"---------------Map----------------------"<<endl; |
| 111 | std::list< MapEntry > ml=rMap.getMapList(); |
| 112 | for( std::list<MapEntry>::iterator it=ml.begin(); it!= ml.end() ; it++){ |
| 113 | os<< (*it); |
| 114 | } |
| 115 | |
| 116 | return os; |
| 117 | } |