blob: 319463525e16f244d2fad57279b2e7161b9c2027 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
2#include <utility>
3#include "npte.hpp"
4#include "routing-table-entry.hpp"
5#include "nexthop.hpp"
6
7namespace nlsr {
8
9using namespace std;
10
11void
12Npte::generateNhlfromRteList()
13{
14 m_nhl.reset();
15 for (std::list<RoutingTableEntry>::iterator it = m_rteList.begin();
16 it != m_rteList.end(); ++it)
17 {
18 for (std::list<NextHop>::iterator nhit =
19 (*it).getNhl().getNextHopList().begin();
20 nhit != (*it).getNhl().getNextHopList().end(); ++nhit)
21 {
22 m_nhl.addNextHop((*nhit));
23 }
24 }
25}
26
27
28
29static bool
30rteCompare(RoutingTableEntry& rte, string& destRouter)
31{
32 return rte.getDestination() == destRouter;
33}
34
35void
36Npte::removeRoutingTableEntry(RoutingTableEntry& rte)
37{
38 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rteList.begin(),
39 m_rteList.end(),
40 bind(&rteCompare, _1, rte.getDestination()));
41 if (it != m_rteList.end())
42 {
43 m_rteList.erase(it);
44 }
45}
46
47void
48Npte::addRoutingTableEntry(RoutingTableEntry& rte)
49{
50 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rteList.begin(),
51 m_rteList.end(),
52 bind(&rteCompare, _1, rte.getDestination()));
53 if (it == m_rteList.end())
54 {
55 m_rteList.push_back(rte);
56 }
57 else
58 {
59 (*it).getNhl().reset(); // 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 }
65 }
66}
67
68//debugging purpose
69ostream&
70operator<<(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)
76 {
77 cout << (*it);
78 }
79 os << npte.getNhl();
80 return os;
81}
82
83}//namespace nlsr