blob: 9a734f799e3c02871f3fb55a846b04743e669be8 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
2#include <utility>
akmhoquec8a10f72014-04-25 18:42:55 -05003#include "name-prefix-table-entry.hpp"
akmhoque53353462014-04-22 08:43:45 -05004#include "routing-table-entry.hpp"
5#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05006#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -05007
8namespace nlsr {
9
akmhoque674b0b12014-05-20 14:33:28 -050010INIT_LOGGER("NamePrefixTableEntry");
11
akmhoque53353462014-04-22 08:43:45 -050012using namespace std;
13
14void
akmhoquec8a10f72014-04-25 18:42:55 -050015NamePrefixTableEntry::generateNhlfromRteList()
akmhoque53353462014-04-22 08:43:45 -050016{
akmhoquefdbddb12014-05-02 18:35:19 -050017 m_nexthopList.reset();
akmhoque53353462014-04-22 08:43:45 -050018 for (std::list<RoutingTableEntry>::iterator it = m_rteList.begin();
19 it != m_rteList.end(); ++it)
20 {
21 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050022 (*it).getNexthopList().getNextHops().begin();
23 nhit != (*it).getNexthopList().getNextHops().end(); ++nhit)
akmhoque53353462014-04-22 08:43:45 -050024 {
akmhoquefdbddb12014-05-02 18:35:19 -050025 m_nexthopList.addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -050026 }
27 }
28}
29
30
31
32static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050033rteCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -050034{
35 return rte.getDestination() == destRouter;
36}
37
38void
akmhoquec8a10f72014-04-25 18:42:55 -050039NamePrefixTableEntry::removeRoutingTableEntry(RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050040{
41 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rteList.begin(),
42 m_rteList.end(),
43 bind(&rteCompare, _1, rte.getDestination()));
44 if (it != m_rteList.end())
45 {
46 m_rteList.erase(it);
47 }
48}
49
50void
akmhoquec8a10f72014-04-25 18:42:55 -050051NamePrefixTableEntry::addRoutingTableEntry(RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050052{
53 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rteList.begin(),
54 m_rteList.end(),
55 bind(&rteCompare, _1, rte.getDestination()));
56 if (it == m_rteList.end())
57 {
58 m_rteList.push_back(rte);
59 }
60 else
61 {
akmhoquefdbddb12014-05-02 18:35:19 -050062 (*it).getNexthopList().reset(); // reseting existing routing table's next hop
akmhoque31d1d4b2014-05-05 22:08:14 -050063 for (std::list<NextHop>::iterator nhit =
64 rte.getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050065 nhit != rte.getNexthopList().getNextHops().end(); ++nhit) {
akmhoquefdbddb12014-05-02 18:35:19 -050066 (*it).getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -050067 }
68 }
69}
70
akmhoque674b0b12014-05-20 14:33:28 -050071void
72NamePrefixTableEntry::writeLog()
73{
74 _LOG_DEBUG("Name: " << m_namePrefix);
75 for (std::list<RoutingTableEntry>::iterator it = m_rteList.begin();
76 it != m_rteList.end(); ++it) {
77 _LOG_DEBUG("Destination: " << (*it).getDestination());
78 _LOG_DEBUG("Nexthops: ");
79 (*it).getNexthopList().writeLog();
80 }
81 m_nexthopList.writeLog();
82}
83
akmhoque53353462014-04-22 08:43:45 -050084//debugging purpose
85ostream&
akmhoquec8a10f72014-04-25 18:42:55 -050086operator<<(ostream& os, NamePrefixTableEntry& npte)
akmhoque53353462014-04-22 08:43:45 -050087{
88 os << "Name: " << npte.getNamePrefix() << endl;
89 std::list<RoutingTableEntry> rteList = npte.getRteList();
90 for (std::list<RoutingTableEntry>::iterator it = rteList.begin();
akmhoque157b0a42014-05-13 00:26:37 -050091 it != rteList.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -050092 cout << (*it);
93 }
akmhoquefdbddb12014-05-02 18:35:19 -050094 os << npte.getNexthopList();
akmhoque53353462014-04-22 08:43:45 -050095 return os;
96}
97
98}//namespace nlsr