blob: 12536f13d8549da7c31f7a1cad23c87921fdeb1d [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"
4
5namespace nlsr {
6
7using namespace std;
8
9static bool
10nexthopCompare(NextHop& nh1, NextHop& nh2)
11{
akmhoque157b0a42014-05-13 00:26:37 -050012 return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -050013}
14
15static bool
16nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
17{
akmhoque157b0a42014-05-13 00:26:37 -050018 return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
akmhoque53353462014-04-22 08:43:45 -050019 nh1.getRouteCost() == nh2.getRouteCost()) ;
20}
21
22static bool
23nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
24{
25 return nh1.getRouteCost() < nh2.getRouteCost();
26}
27
28/**
29Add next hop to the Next Hop list
30If next hop is new it is added
31If next hop already exists in next
32hop list then updates the route
33cost with new next hop's route cost
34*/
35
36void
akmhoquec8a10f72014-04-25 18:42:55 -050037NexthopList::addNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050038{
39 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
40 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050041 ndn::bind(&nexthopCompare, _1, nh));
42 if (it == m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050043 m_nexthopList.push_back(nh);
44 }
akmhoque157b0a42014-05-13 00:26:37 -050045 if ((*it).getRouteCost() > nh.getRouteCost()) {
akmhoque53353462014-04-22 08:43:45 -050046 (*it).setRouteCost(nh.getRouteCost());
47 }
48}
49
50/**
51Remove a next hop only if both next hop face and route cost are same
52
53*/
54
55void
akmhoquec8a10f72014-04-25 18:42:55 -050056NexthopList::removeNextHop(NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -050057{
58 std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
59 m_nexthopList.end(),
akmhoque157b0a42014-05-13 00:26:37 -050060 ndn::bind(&nexthopRemoveCompare, _1, nh));
61 if (it != m_nexthopList.end()) {
akmhoque53353462014-04-22 08:43:45 -050062 m_nexthopList.erase(it);
63 }
64}
65
66void
akmhoquec8a10f72014-04-25 18:42:55 -050067NexthopList::sort()
akmhoque53353462014-04-22 08:43:45 -050068{
69 m_nexthopList.sort(nextHopSortingComparator);
70}
71
72ostream&
akmhoquec8a10f72014-04-25 18:42:55 -050073operator<<(ostream& os, NexthopList& nhl)
akmhoque53353462014-04-22 08:43:45 -050074{
akmhoquefdbddb12014-05-02 18:35:19 -050075 std::list<NextHop> nexthopList = nhl.getNextHops();
akmhoque53353462014-04-22 08:43:45 -050076 int i = 1;
77 for (std::list<NextHop>::iterator it = nexthopList.begin();
akmhoque157b0a42014-05-13 00:26:37 -050078 it != nexthopList.end() ; it++, i++) {
akmhoque53353462014-04-22 08:43:45 -050079 os << "Nexthop " << i << ": " << (*it) << endl;
80 }
81 return os;
82}
83
84}//namespace nlsr