blob: f08f1ae1afd3a5fd4fb7dcf575e6319d682950f0 [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"
akmhoque05d5fcf2014-04-15 14:58:45 -05009#include "utility/nlsr_logger.hpp"
10
11#define THIS_FILE "nlsr_map.cpp"
akmhoqueba094742014-02-28 11:47:21 -060012
13namespace nlsr
14{
15
akmhoque5a44dd42014-03-12 18:11:32 -050016 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060017
akmhoque5a44dd42014-03-12 18:11:32 -050018 ostream&
19 operator<<(ostream& os, MapEntry& mpe)
20 {
21 os<<"MapEntry: ( Router: "<<mpe.getRouter()<<" Mapping No: ";
22 os<<mpe.getMappingNumber()<<" )"<<endl;
23 return os;
24 }
akmhoqueba094742014-02-28 11:47:21 -060025
akmhoque5a44dd42014-03-12 18:11:32 -050026 static bool
27 mapEntryCompareByRouter(MapEntry& mpe1, string& rtrName)
28 {
29 return mpe1.getRouter()==rtrName;
30 }
akmhoqueba094742014-02-28 11:47:21 -060031
akmhoque5a44dd42014-03-12 18:11:32 -050032 static bool
33 mapEntryCompareByMappingNo(MapEntry& mpe1, int mappingNo)
34 {
35 return mpe1.getMappingNumber()==mappingNo;
36 }
akmhoqueba094742014-02-28 11:47:21 -060037
akmhoque5a44dd42014-03-12 18:11:32 -050038 void
akmhoque05d5fcf2014-04-15 14:58:45 -050039 Map::addElement(string& rtrName)
akmhoque5a44dd42014-03-12 18:11:32 -050040 {
akmhoque05d5fcf2014-04-15 14:58:45 -050041 MapEntry me(rtrName,m_mappingIndex);
42 if ( addElement(me) )
akmhoqueba094742014-02-28 11:47:21 -060043 {
akmhoque05d5fcf2014-04-15 14:58:45 -050044 m_mappingIndex++;
akmhoqueba094742014-02-28 11:47:21 -060045 }
akmhoque5a44dd42014-03-12 18:11:32 -050046 }
akmhoqueba094742014-02-28 11:47:21 -060047
akmhoque5a44dd42014-03-12 18:11:32 -050048 bool
akmhoque05d5fcf2014-04-15 14:58:45 -050049 Map::addElement(MapEntry& mpe)
akmhoque5a44dd42014-03-12 18:11:32 -050050 {
51 //cout << mpe;
akmhoque05d5fcf2014-04-15 14:58:45 -050052 std::list<MapEntry >::iterator it = std::find_if( m_table.begin(),
53 m_table.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050054 bind(&mapEntryCompareByRouter, _1, mpe.getRouter()));
akmhoque05d5fcf2014-04-15 14:58:45 -050055 if ( it == m_table.end() )
akmhoqueba094742014-02-28 11:47:21 -060056 {
akmhoque05d5fcf2014-04-15 14:58:45 -050057 m_table.push_back(mpe);
akmhoque5a44dd42014-03-12 18:11:32 -050058 return true;
akmhoqueba094742014-02-28 11:47:21 -060059 }
akmhoque5a44dd42014-03-12 18:11:32 -050060 return false;
61 }
akmhoqueba094742014-02-28 11:47:21 -060062
akmhoque5a44dd42014-03-12 18:11:32 -050063 string
64 Map::getRouterNameByMappingNo(int mn)
65 {
akmhoque05d5fcf2014-04-15 14:58:45 -050066 std::list<MapEntry >::iterator it = std::find_if( m_table.begin(),
67 m_table.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050068 bind(&mapEntryCompareByMappingNo, _1, mn));
akmhoque05d5fcf2014-04-15 14:58:45 -050069 if ( it != m_table.end() )
akmhoqueba094742014-02-28 11:47:21 -060070 {
akmhoque5a44dd42014-03-12 18:11:32 -050071 return (*it).getRouter();
akmhoqueba094742014-02-28 11:47:21 -060072 }
akmhoque5a44dd42014-03-12 18:11:32 -050073 return "";
74 }
akmhoqueba094742014-02-28 11:47:21 -060075
akmhoque5a44dd42014-03-12 18:11:32 -050076 int
77 Map::getMappingNoByRouterName(string& rName)
78 {
akmhoque05d5fcf2014-04-15 14:58:45 -050079 std::list<MapEntry >::iterator it = std::find_if( m_table.begin(),
80 m_table.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050081 bind(&mapEntryCompareByRouter, _1, rName));
akmhoque05d5fcf2014-04-15 14:58:45 -050082 if ( it != m_table.end() )
akmhoqueba094742014-02-28 11:47:21 -060083 {
akmhoque5a44dd42014-03-12 18:11:32 -050084 return (*it).getMappingNumber();
akmhoqueba094742014-02-28 11:47:21 -060085 }
akmhoque5a44dd42014-03-12 18:11:32 -050086 return -1;
87 }
akmhoqueba094742014-02-28 11:47:21 -060088
akmhoque5a44dd42014-03-12 18:11:32 -050089 void
akmhoque05d5fcf2014-04-15 14:58:45 -050090 Map::createFromAdjLsdb(Nlsr& pnlsr)
akmhoque5a44dd42014-03-12 18:11:32 -050091 {
92 std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb();
93 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
94 it!= adjLsdb.end() ; it++)
akmhoqueba094742014-02-28 11:47:21 -060095 {
akmhoque5a44dd42014-03-12 18:11:32 -050096 string linkStartRouter=(*it).getOrigRouter();
akmhoque05d5fcf2014-04-15 14:58:45 -050097 addElement(linkStartRouter);
akmhoque5a44dd42014-03-12 18:11:32 -050098 std::list<Adjacent> adl=(*it).getAdl().getAdjList();
99 for( std::list<Adjacent>::iterator itAdl=adl.begin();
100 itAdl!= adl.end() ; itAdl++)
101 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500102 string linkEndRouter=(*itAdl).getName();
103 addElement(linkEndRouter);
akmhoque5a44dd42014-03-12 18:11:32 -0500104 }
akmhoqueba094742014-02-28 11:47:21 -0600105 }
akmhoque5a44dd42014-03-12 18:11:32 -0500106 }
akmhoqueba094742014-02-28 11:47:21 -0600107
akmhoque5a44dd42014-03-12 18:11:32 -0500108 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500109 Map::reset()
akmhoque5a44dd42014-03-12 18:11:32 -0500110 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500111 m_table.clear();
112 m_mappingIndex=0;
akmhoque5a44dd42014-03-12 18:11:32 -0500113 }
akmhoqueba094742014-02-28 11:47:21 -0600114
akmhoque5a44dd42014-03-12 18:11:32 -0500115 ostream&
akmhoque05d5fcf2014-04-15 14:58:45 -0500116 operator<<(ostream& os, Map& map)
akmhoque5a44dd42014-03-12 18:11:32 -0500117 {
118 os<<"---------------Map----------------------"<<endl;
akmhoque05d5fcf2014-04-15 14:58:45 -0500119 std::list< MapEntry > ml=map.getMapList();
akmhoque5a44dd42014-03-12 18:11:32 -0500120 for( std::list<MapEntry>::iterator it=ml.begin(); it!= ml.end() ; it++)
akmhoqueba094742014-02-28 11:47:21 -0600121 {
akmhoque5a44dd42014-03-12 18:11:32 -0500122 os<< (*it);
akmhoqueba094742014-02-28 11:47:21 -0600123 }
akmhoque5a44dd42014-03-12 18:11:32 -0500124 return os;
125 }
akmhoqueba094742014-02-28 11:47:21 -0600126
127} //namespace nlsr