blob: 47b02ecbc64ba3a3c2f8bd99db3b9a1acdfba54c [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
13 using namespace std;
14
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 }
22
23 static bool
24 mapEntryCompareByRouter(MapEntry& mpe1, string& rtrName)
25 {
26 return mpe1.getRouter()==rtrName;
27 }
28
29 static bool
30 mapEntryCompareByMappingNo(MapEntry& mpe1, int mappingNo)
31 {
32 return mpe1.getMappingNumber()==mappingNo;
33 }
34
35 void
36 Map::addMapElement(string& rtrName)
37 {
38 MapEntry me(rtrName,mappingIndex);
39 if ( addMapElement(me) )
40 {
41 mappingIndex++;
42 }
43 }
44
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() )
53 {
54 rMap.push_back(mpe);
55 return true;
56 }
57 return false;
58 }
59
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() )
67 {
68 return (*it).getRouter();
69 }
70 return "";
71 }
72
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() )
80 {
81 return (*it).getMappingNumber();
82 }
83 return -1;
84 }
85
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++)
92 {
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 }
102 }
103 }
104
105 void
106 Map::resetMap()
107 {
108 rMap.clear();
109 mappingIndex=0;
110 }
111
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++)
118 {
119 os<< (*it);
120 }
121 return os;
122 }
123
124} //namespace nlsr