blob: 37f3480234c78fb77bd23c3cd3593c5da00ca9ae [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
akmhoquec8a10f72014-04-25 18:42:55 -05002#include "nexthop-list.hpp"
akmhoque53353462014-04-22 08:43:45 -05003#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05004#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -05005
6namespace nlsr {
7
akmhoque674b0b12014-05-20 14:33:28 -05008INIT_LOGGER("NexthopList");
9
akmhoque53353462014-04-22 08:43:45 -050010using namespace std;
11
12static bool
13nexthopCompare(NextHop& nh1, NextHop& nh2)
14{
akmhoque157b0a42014-05-13 00:26:37 -050015 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050016}
17
18static bool
19nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
20{
akmhoque157b0a42014-05-13 00:26:37 -050021 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
akmhoque53353462014-04-22 08:43:45 -050022 nh1.getRouteCost() == nh2.getRouteCost()) ;
23}
24
25static bool
26nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
27{
28 return nh1.getRouteCost() < nh2.getRouteCost();
29}
30
31/**
32Add next hop to the Next Hop list
33If next hop is new it is added
34If next hop already exists in next
35hop list then updates the route
36cost with new next hop's route cost
37*/
38
39void
akmhoquec8a10f72014-04-25 18:42:55 -050040NexthopList::addNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050041{
42 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
43 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050044 ndn::bind(&nexthopCompare, _1, nh));
45 if (it == m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050046 m_nexthopList.push_back(nh);
47 }
akmhoque157b0a42014-05-13 00:26:37 -050048 if ((*it).getRouteCost() > nh.getRouteCost()) {
akmhoque53353462014-04-22 08:43:45 -050049 (*it).setRouteCost(nh.getRouteCost());
50 }
51}
52
53/**
54Remove a next hop only if both next hop face and route cost are same
55
56*/
57
58void
akmhoquec8a10f72014-04-25 18:42:55 -050059NexthopList::removeNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050060{
61 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
62 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050063 ndn::bind(&nexthopRemoveCompare, _1, nh));
64 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050065 m_nexthopList.erase(it);
66 }
67}
68
69void
akmhoquec8a10f72014-04-25 18:42:55 -050070NexthopList::sort()
akmhoque53353462014-04-22 08:43:45 -050071{
72 m_nexthopList.sort(nextHopSortingComparator);
73}
74
akmhoque674b0b12014-05-20 14:33:28 -050075void
76NexthopList::writeLog()
77{
78 int i = 1;
79 for (std::list<NextHop>::iterator it = m_nexthopList.begin();
80 it != m_nexthopList.end() ; it++, i++) {
81 _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
82 << " Route Cost: " << (*it).getRouteCost());
83 }
84}
85
akmhoque53353462014-04-22 08:43:45 -050086ostream&
akmhoquec8a10f72014-04-25 18:42:55 -050087operator<<(ostream& os, NexthopList& nhl)
akmhoque53353462014-04-22 08:43:45 -050088{
akmhoquefdbddb12014-05-02 18:35:19 -050089 std::list<NextHop> nexthopList = nhl.getNextHops();
akmhoque53353462014-04-22 08:43:45 -050090 int i = 1;
91 for (std::list<NextHop>::iterator it = nexthopList.begin();
akmhoque157b0a42014-05-13 00:26:37 -050092 it != nexthopList.end() ; it++, i++) {
akmhoque53353462014-04-22 08:43:45 -050093 os << "Nexthop " << i << ": " << (*it) << endl;
94 }
95 return os;
96}
97
98}//namespace nlsr