blob: 3c4ee867eaf75a30059ebe394db2030ac9265ffe [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -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
10
11using namespace std;
12
13ostream&
14operator<<(ostream& os, MapEntry& mpe)
15{
16 os<<"MapEntry: ( Router: "<<mpe.getRouter()<<" Mapping No: ";
17 os<<mpe.getMappingNumber()<<" )"<<endl;
18
19 return os;
20}
21
22static bool
23mapEntryCompareByRouter(MapEntry& mpe1, string& rtrName){
24 return mpe1.getRouter()==rtrName;
25}
26
27static bool
28mapEntryCompareByMappingNo(MapEntry& mpe1, int mappingNo){
29 return mpe1.getMappingNumber()==mappingNo;
30}
31
32void
33Map::addMapElement(string& rtrName)
34{
35 MapEntry me(rtrName,mappingIndex);
36 if ( addMapElement(me) )
37 {
38 mappingIndex++;
39 }
40}
41
42bool
43Map::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
56string
57Map::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
68int
69Map::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
80void
81Map::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
100void
101Map::resetMap()
102{
103 rMap.clear();
104 mappingIndex=0;
105}
106
107ostream&
108operator<<(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}