blob: 4701a050630cbc50913f8c315d7e20262750d909 [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
akmhoque53353462014-04-22 08:43:45 -050014static bool
akmhoquefdbddb12014-05-02 18:35:19 -050015mapEntryCompareByRouter(MapEntry& mpe1, const string& rtrName)
akmhoque53353462014-04-22 08:43:45 -050016{
17 return mpe1.getRouter() == rtrName;
18}
19
20static bool
akmhoquefdbddb12014-05-02 18:35:19 -050021mapEntryCompareByMappingNo(MapEntry& mpe1, int32_t mappingNo)
akmhoque53353462014-04-22 08:43:45 -050022{
23 return mpe1.getMappingNumber() == mappingNo;
24}
25
26void
akmhoquefdbddb12014-05-02 18:35:19 -050027Map::addEntry(const string& rtrName)
akmhoque53353462014-04-22 08:43:45 -050028{
29 MapEntry me(rtrName, m_mappingIndex);
akmhoquefdbddb12014-05-02 18:35:19 -050030 if (addEntry(me))
akmhoque53353462014-04-22 08:43:45 -050031 {
32 m_mappingIndex++;
33 }
34}
35
36bool
akmhoquefdbddb12014-05-02 18:35:19 -050037Map::addEntry(MapEntry& mpe)
akmhoque53353462014-04-22 08:43:45 -050038{
39 //cout << mpe;
40 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
41 m_table.end(),
42 bind(&mapEntryCompareByRouter, _1, mpe.getRouter()));
43 if (it == m_table.end())
44 {
45 m_table.push_back(mpe);
46 return true;
47 }
48 return false;
49}
50
51string
akmhoquefdbddb12014-05-02 18:35:19 -050052Map::getRouterNameByMappingNo(int32_t mn)
akmhoque53353462014-04-22 08:43:45 -050053{
54 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
55 m_table.end(),
56 bind(&mapEntryCompareByMappingNo,
57 _1, mn));
58 if (it != m_table.end())
59 {
60 return (*it).getRouter();
61 }
62 return "";
63}
64
akmhoquefdbddb12014-05-02 18:35:19 -050065int32_t
akmhoque53353462014-04-22 08:43:45 -050066Map::getMappingNoByRouterName(string& rName)
67{
68 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
69 m_table.end(),
70 bind(&mapEntryCompareByRouter,
71 _1, rName));
72 if (it != m_table.end())
73 {
74 return (*it).getMappingNumber();
75 }
76 return -1;
77}
78
79void
80Map::createFromAdjLsdb(Nlsr& pnlsr)
81{
82 std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb();
83 for (std::list<AdjLsa>::iterator it = adjLsdb.begin();
84 it != adjLsdb.end() ; it++)
85 {
86 string linkStartRouter = (*it).getOrigRouter();
akmhoquefdbddb12014-05-02 18:35:19 -050087 addEntry(linkStartRouter);
akmhoque53353462014-04-22 08:43:45 -050088 std::list<Adjacent> adl = (*it).getAdl().getAdjList();
89 for (std::list<Adjacent>::iterator itAdl = adl.begin();
90 itAdl != adl.end() ; itAdl++)
91 {
92 string linkEndRouter = (*itAdl).getName();
akmhoquefdbddb12014-05-02 18:35:19 -050093 addEntry(linkEndRouter);
akmhoque53353462014-04-22 08:43:45 -050094 }
95 }
96}
97
98void
99Map::reset()
100{
101 m_table.clear();
102 m_mappingIndex = 0;
103}
104
105ostream&
106operator<<(ostream& os, Map& map)
107{
108 os << "---------------Map----------------------" << endl;
109 std::list<MapEntry> ml = map.getMapList();
110 for (std::list<MapEntry>::iterator it = ml.begin(); it != ml.end() ; it++)
111 {
112 os << (*it);
113 }
114 return os;
115}
116
117} //namespace nlsr