blob: c342a21301d12ebb4c3c2dc1b07ecbe6b20271be [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include <list>
2#include <utility>
3#include "nlsr_npte.hpp"
4#include "nlsr_rte.hpp"
5#include "nlsr_nexthop.hpp"
6
7namespace nlsr
8{
9
akmhoque5a44dd42014-03-12 18:11:32 -050010 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060011
akmhoque5a44dd42014-03-12 18:11:32 -050012 void
13 Npte::generateNhlfromRteList()
14 {
15 nhl.resetNhl();
16 for( std::list<RoutingTableEntry>::iterator it=rteList.begin();
17 it != rteList.end(); ++it )
akmhoqueba094742014-02-28 11:47:21 -060018 {
akmhoque5a44dd42014-03-12 18:11:32 -050019 for(std::list< NextHop >::iterator nhit=(*it).getNhl().getNextHopList().begin();
20 nhit != (*it).getNhl().getNextHopList().end(); ++nhit)
21 {
22 nhl.addNextHop((*nhit));
23 }
akmhoqueba094742014-02-28 11:47:21 -060024 }
akmhoque5a44dd42014-03-12 18:11:32 -050025 }
akmhoqueba094742014-02-28 11:47:21 -060026
27
28
akmhoque5a44dd42014-03-12 18:11:32 -050029 static bool
30 rteCompare(RoutingTableEntry& rte, string& destRouter)
31 {
32 return rte.getDestination()==destRouter;
33 }
34
35 void
36 Npte::removeRoutingTableEntry(RoutingTableEntry& rte)
37 {
38 std::list<RoutingTableEntry >::iterator it = std::find_if( rteList.begin(),
39 rteList.end(),
40 bind(&rteCompare, _1, rte.getDestination()));
41 if ( it != rteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060042 {
akmhoque5a44dd42014-03-12 18:11:32 -050043 rteList.erase(it);
akmhoqueba094742014-02-28 11:47:21 -060044 }
akmhoque5a44dd42014-03-12 18:11:32 -050045 }
akmhoqueba094742014-02-28 11:47:21 -060046
akmhoque5a44dd42014-03-12 18:11:32 -050047 void
48 Npte::addRoutingTableEntry(RoutingTableEntry &rte)
49 {
50 std::list<RoutingTableEntry >::iterator it = std::find_if( rteList.begin(),
51 rteList.end(),
52 bind(&rteCompare, _1, rte.getDestination()));
53 if ( it == rteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060054 {
akmhoque5a44dd42014-03-12 18:11:32 -050055 rteList.push_back(rte);
akmhoqueba094742014-02-28 11:47:21 -060056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 else
akmhoqueba094742014-02-28 11:47:21 -060058 {
akmhoque5a44dd42014-03-12 18:11:32 -050059 (*it).getNhl().resetNhl(); // reseting existing routing table's next hop
60 for(std::list< NextHop >::iterator nhit=rte.getNhl().getNextHopList().begin();
61 nhit != rte.getNhl().getNextHopList().end(); ++nhit)
62 {
63 (*it).getNhl().addNextHop((*nhit));
64 }
akmhoqueba094742014-02-28 11:47:21 -060065 }
akmhoque5a44dd42014-03-12 18:11:32 -050066 }
akmhoqueba094742014-02-28 11:47:21 -060067
68//debugging purpose
akmhoque5a44dd42014-03-12 18:11:32 -050069 ostream&
70 operator<<(ostream& os, Npte& npte)
71 {
72 os<<"Name: "<<npte.getNamePrefix()<<endl;
73 std::list<RoutingTableEntry> rteList=npte.getRteList();
74 for(std::list<RoutingTableEntry >::iterator it=rteList.begin();
75 it !=rteList.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -060076 {
akmhoque5a44dd42014-03-12 18:11:32 -050077 cout<<(*it);
akmhoqueba094742014-02-28 11:47:21 -060078 }
akmhoque5a44dd42014-03-12 18:11:32 -050079 os<<npte.getNhl();
80 return os;
81 }
akmhoqueba094742014-02-28 11:47:21 -060082
83}//namespace nlsr