blob: 19c57c1db93fd6da55e88883ca6742e3a7894225 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#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
10namespace nlsr
11{
12
akmhoque5a44dd42014-03-12 18:11:32 -050013 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060014
akmhoque5a44dd42014-03-12 18:11:32 -050015 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 }
akmhoqueba094742014-02-28 11:47:21 -060022
akmhoque5a44dd42014-03-12 18:11:32 -050023 static bool
24 mapEntryCompareByRouter(MapEntry& mpe1, string& rtrName)
25 {
26 return mpe1.getRouter()==rtrName;
27 }
akmhoqueba094742014-02-28 11:47:21 -060028
akmhoque5a44dd42014-03-12 18:11:32 -050029 static bool
30 mapEntryCompareByMappingNo(MapEntry& mpe1, int mappingNo)
31 {
32 return mpe1.getMappingNumber()==mappingNo;
33 }
akmhoqueba094742014-02-28 11:47:21 -060034
akmhoque5a44dd42014-03-12 18:11:32 -050035 void
36 Map::addMapElement(string& rtrName)
37 {
38 MapEntry me(rtrName,mappingIndex);
39 if ( addMapElement(me) )
akmhoqueba094742014-02-28 11:47:21 -060040 {
akmhoque5a44dd42014-03-12 18:11:32 -050041 mappingIndex++;
akmhoqueba094742014-02-28 11:47:21 -060042 }
akmhoque5a44dd42014-03-12 18:11:32 -050043 }
akmhoqueba094742014-02-28 11:47:21 -060044
akmhoque5a44dd42014-03-12 18:11:32 -050045 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() )
akmhoqueba094742014-02-28 11:47:21 -060053 {
akmhoque5a44dd42014-03-12 18:11:32 -050054 rMap.push_back(mpe);
55 return true;
akmhoqueba094742014-02-28 11:47:21 -060056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 return false;
58 }
akmhoqueba094742014-02-28 11:47:21 -060059
akmhoque5a44dd42014-03-12 18:11:32 -050060 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() )
akmhoqueba094742014-02-28 11:47:21 -060067 {
akmhoque5a44dd42014-03-12 18:11:32 -050068 return (*it).getRouter();
akmhoqueba094742014-02-28 11:47:21 -060069 }
akmhoque5a44dd42014-03-12 18:11:32 -050070 return "";
71 }
akmhoqueba094742014-02-28 11:47:21 -060072
akmhoque5a44dd42014-03-12 18:11:32 -050073 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() )
akmhoqueba094742014-02-28 11:47:21 -060080 {
akmhoque5a44dd42014-03-12 18:11:32 -050081 return (*it).getMappingNumber();
akmhoqueba094742014-02-28 11:47:21 -060082 }
akmhoque5a44dd42014-03-12 18:11:32 -050083 return -1;
84 }
akmhoqueba094742014-02-28 11:47:21 -060085
akmhoque5a44dd42014-03-12 18:11:32 -050086 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++)
akmhoqueba094742014-02-28 11:47:21 -060092 {
akmhoque5a44dd42014-03-12 18:11:32 -050093 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 }
akmhoqueba094742014-02-28 11:47:21 -0600102 }
akmhoque5a44dd42014-03-12 18:11:32 -0500103 }
akmhoqueba094742014-02-28 11:47:21 -0600104
akmhoque5a44dd42014-03-12 18:11:32 -0500105 void
106 Map::resetMap()
107 {
108 rMap.clear();
109 mappingIndex=0;
110 }
akmhoqueba094742014-02-28 11:47:21 -0600111
akmhoque5a44dd42014-03-12 18:11:32 -0500112 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++)
akmhoqueba094742014-02-28 11:47:21 -0600118 {
akmhoque5a44dd42014-03-12 18:11:32 -0500119 os<< (*it);
akmhoqueba094742014-02-28 11:47:21 -0600120 }
akmhoque5a44dd42014-03-12 18:11:32 -0500121 return os;
122 }
akmhoqueba094742014-02-28 11:47:21 -0600123
124} //namespace nlsr