blob: d8e83c39f6c8b11427a89d996cce16b210c009f8 [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"
akmhoque05d5fcf2014-04-15 14:58:45 -05006#include "utility/nlsr_logger.hpp"
7
8#define THIS_FILE "nlsr_npte.cpp"
akmhoqueba094742014-02-28 11:47:21 -06009
10namespace nlsr
11{
12
akmhoque5a44dd42014-03-12 18:11:32 -050013 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060014
akmhoque5a44dd42014-03-12 18:11:32 -050015 void
16 Npte::generateNhlfromRteList()
17 {
akmhoque05d5fcf2014-04-15 14:58:45 -050018 m_nhl.reset();
19 for( std::list<RoutingTableEntry>::iterator it=m_rteList.begin();
20 it != m_rteList.end(); ++it )
akmhoqueba094742014-02-28 11:47:21 -060021 {
akmhoque5a44dd42014-03-12 18:11:32 -050022 for(std::list< NextHop >::iterator nhit=(*it).getNhl().getNextHopList().begin();
23 nhit != (*it).getNhl().getNextHopList().end(); ++nhit)
24 {
akmhoque05d5fcf2014-04-15 14:58:45 -050025 m_nhl.addNextHop((*nhit));
akmhoque5a44dd42014-03-12 18:11:32 -050026 }
akmhoqueba094742014-02-28 11:47:21 -060027 }
akmhoque5a44dd42014-03-12 18:11:32 -050028 }
akmhoqueba094742014-02-28 11:47:21 -060029
30
31
akmhoque5a44dd42014-03-12 18:11:32 -050032 static bool
33 rteCompare(RoutingTableEntry& rte, string& destRouter)
34 {
35 return rte.getDestination()==destRouter;
36 }
37
38 void
39 Npte::removeRoutingTableEntry(RoutingTableEntry& rte)
40 {
akmhoque05d5fcf2014-04-15 14:58:45 -050041 std::list<RoutingTableEntry >::iterator it = std::find_if( m_rteList.begin(),
42 m_rteList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050043 bind(&rteCompare, _1, rte.getDestination()));
akmhoque05d5fcf2014-04-15 14:58:45 -050044 if ( it != m_rteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060045 {
akmhoque05d5fcf2014-04-15 14:58:45 -050046 m_rteList.erase(it);
akmhoqueba094742014-02-28 11:47:21 -060047 }
akmhoque5a44dd42014-03-12 18:11:32 -050048 }
akmhoqueba094742014-02-28 11:47:21 -060049
akmhoque5a44dd42014-03-12 18:11:32 -050050 void
51 Npte::addRoutingTableEntry(RoutingTableEntry &rte)
52 {
akmhoque05d5fcf2014-04-15 14:58:45 -050053 std::list<RoutingTableEntry >::iterator it = std::find_if( m_rteList.begin(),
54 m_rteList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050055 bind(&rteCompare, _1, rte.getDestination()));
akmhoque05d5fcf2014-04-15 14:58:45 -050056 if ( it == m_rteList.end() )
akmhoqueba094742014-02-28 11:47:21 -060057 {
akmhoque05d5fcf2014-04-15 14:58:45 -050058 m_rteList.push_back(rte);
akmhoqueba094742014-02-28 11:47:21 -060059 }
akmhoque5a44dd42014-03-12 18:11:32 -050060 else
akmhoqueba094742014-02-28 11:47:21 -060061 {
akmhoque05d5fcf2014-04-15 14:58:45 -050062 (*it).getNhl().reset(); // reseting existing routing table's next hop
akmhoque5a44dd42014-03-12 18:11:32 -050063 for(std::list< NextHop >::iterator nhit=rte.getNhl().getNextHopList().begin();
64 nhit != rte.getNhl().getNextHopList().end(); ++nhit)
65 {
66 (*it).getNhl().addNextHop((*nhit));
67 }
akmhoqueba094742014-02-28 11:47:21 -060068 }
akmhoque5a44dd42014-03-12 18:11:32 -050069 }
akmhoqueba094742014-02-28 11:47:21 -060070
71//debugging purpose
akmhoque5a44dd42014-03-12 18:11:32 -050072 ostream&
73 operator<<(ostream& os, Npte& npte)
74 {
75 os<<"Name: "<<npte.getNamePrefix()<<endl;
76 std::list<RoutingTableEntry> rteList=npte.getRteList();
77 for(std::list<RoutingTableEntry >::iterator it=rteList.begin();
78 it !=rteList.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -060079 {
akmhoque5a44dd42014-03-12 18:11:32 -050080 cout<<(*it);
akmhoqueba094742014-02-28 11:47:21 -060081 }
akmhoque5a44dd42014-03-12 18:11:32 -050082 os<<npte.getNhl();
83 return os;
84 }
akmhoqueba094742014-02-28 11:47:21 -060085
86}//namespace nlsr