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