blob: 8dca590b01c0c180430c380cd76a1c6c8a262f3f [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
akmhoque31d1d4b2014-05-05 22:08:14 -050015mapEntryCompareByRouter(MapEntry& mpe1, const ndn::Name& 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
akmhoque31d1d4b2014-05-05 22:08:14 -050027Map::addEntry(const ndn::Name& rtrName)
akmhoque53353462014-04-22 08:43:45 -050028{
29 MapEntry me(rtrName, m_mappingIndex);
akmhoque157b0a42014-05-13 00:26:37 -050030 if (addEntry(me)) {
akmhoque53353462014-04-22 08:43:45 -050031 m_mappingIndex++;
32 }
33}
34
35bool
akmhoquefdbddb12014-05-02 18:35:19 -050036Map::addEntry(MapEntry& mpe)
akmhoque53353462014-04-22 08:43:45 -050037{
38 //cout << mpe;
39 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
40 m_table.end(),
akmhoque157b0a42014-05-13 00:26:37 -050041 ndn::bind(&mapEntryCompareByRouter,
42 _1, mpe.getRouter()));
43 if (it == m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050044 m_table.push_back(mpe);
45 return true;
46 }
47 return false;
48}
49
akmhoque31d1d4b2014-05-05 22:08:14 -050050const ndn::Name
akmhoquefdbddb12014-05-02 18:35:19 -050051Map::getRouterNameByMappingNo(int32_t mn)
akmhoque53353462014-04-22 08:43:45 -050052{
53 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
54 m_table.end(),
akmhoque157b0a42014-05-13 00:26:37 -050055 ndn::bind(&mapEntryCompareByMappingNo,
56 _1, mn));
57 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050058 return (*it).getRouter();
59 }
akmhoque31d1d4b2014-05-05 22:08:14 -050060 return ndn::Name();
akmhoque53353462014-04-22 08:43:45 -050061}
62
akmhoquefdbddb12014-05-02 18:35:19 -050063int32_t
akmhoque31d1d4b2014-05-05 22:08:14 -050064Map::getMappingNoByRouterName(const ndn::Name& rName)
akmhoque53353462014-04-22 08:43:45 -050065{
66 std::list<MapEntry>::iterator it = std::find_if(m_table.begin(),
67 m_table.end(),
akmhoque157b0a42014-05-13 00:26:37 -050068 ndn::bind(&mapEntryCompareByRouter,
69 _1, rName));
70 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050071 return (*it).getMappingNumber();
72 }
73 return -1;
74}
75
76void
77Map::createFromAdjLsdb(Nlsr& pnlsr)
78{
79 std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb();
80 for (std::list<AdjLsa>::iterator it = adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -050081 it != adjLsdb.end() ; it++) {
akmhoque31d1d4b2014-05-05 22:08:14 -050082 addEntry((*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -050083 std::list<Adjacent> adl = (*it).getAdl().getAdjList();
84 for (std::list<Adjacent>::iterator itAdl = adl.begin();
akmhoque157b0a42014-05-13 00:26:37 -050085 itAdl != adl.end() ; itAdl++) {
akmhoque31d1d4b2014-05-05 22:08:14 -050086 addEntry((*itAdl).getName());
akmhoque53353462014-04-22 08:43:45 -050087 }
88 }
89}
90
91void
92Map::reset()
93{
94 m_table.clear();
95 m_mappingIndex = 0;
96}
97
98ostream&
99operator<<(ostream& os, Map& map)
100{
101 os << "---------------Map----------------------" << endl;
102 std::list<MapEntry> ml = map.getMapList();
akmhoque157b0a42014-05-13 00:26:37 -0500103 for (std::list<MapEntry>::iterator it = ml.begin(); it != ml.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500104 os << (*it);
105 }
106 return os;
107}
108
109} //namespace nlsr