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