blob: e7ab979335a17a372cc496f53d39ae91ef6cf2f5 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <list>
3
4#include "nlsr.hpp"
5#include "adjacent.hpp"
6#include "lsa.hpp"
7#include "lsdb.hpp"
8#include "map.hpp"
9
10namespace nlsr {
11
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 return os;
20}
21
22static bool
23mapEntryCompareByRouter(MapEntry& mpe1, string& rtrName)
24{
25 return mpe1.getRouter() == rtrName;
26}
27
28static bool
29mapEntryCompareByMappingNo(MapEntry& mpe1, int mappingNo)
30{
31 return mpe1.getMappingNumber() == mappingNo;
32}
33
34void
35Map::addElement(string& rtrName)
36{
37 MapEntry me(rtrName, m_mappingIndex);
38 if (addElement(me))
39 {
40 m_mappingIndex++;
41 }
42}
43
44bool
45Map::addElement(MapEntry& mpe)
46{
47 //cout << mpe;
48 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
49 m_table.end(),
50 bind(&mapEntryCompareByRouter, _1, mpe.getRouter()));
51 if (it == m_table.end())
52 {
53 m_table.push_back(mpe);
54 return true;
55 }
56 return false;
57}
58
59string
60Map::getRouterNameByMappingNo(int mn)
61{
62 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
63 m_table.end(),
64 bind(&mapEntryCompareByMappingNo,
65 _1, mn));
66 if (it != m_table.end())
67 {
68 return (*it).getRouter();
69 }
70 return "";
71}
72
73int
74Map::getMappingNoByRouterName(string& rName)
75{
76 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
77 m_table.end(),
78 bind(&mapEntryCompareByRouter,
79 _1, rName));
80 if (it != m_table.end())
81 {
82 return (*it).getMappingNumber();
83 }
84 return -1;
85}
86
87void
88Map::createFromAdjLsdb(Nlsr& pnlsr)
89{
90 std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb();
91 for (std::list<AdjLsa>::iterator it = adjLsdb.begin();
92 it != adjLsdb.end() ; it++)
93 {
94 string linkStartRouter = (*it).getOrigRouter();
95 addElement(linkStartRouter);
96 std::list<Adjacent> adl = (*it).getAdl().getAdjList();
97 for (std::list<Adjacent>::iterator itAdl = adl.begin();
98 itAdl != adl.end() ; itAdl++)
99 {
100 string linkEndRouter = (*itAdl).getName();
101 addElement(linkEndRouter);
102 }
103 }
104}
105
106void
107Map::reset()
108{
109 m_table.clear();
110 m_mappingIndex = 0;
111}
112
113ostream&
114operator<<(ostream& os, Map& map)
115{
116 os << "---------------Map----------------------" << endl;
117 std::list<MapEntry> ml = map.getMapList();
118 for (std::list<MapEntry>::iterator it = ml.begin(); it != ml.end() ; it++)
119 {
120 os << (*it);
121 }
122 return os;
123}
124
125} //namespace nlsr