blob: ed769c972df1d3225dd91d95d32e4ef10c0d9e6c [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include <iostream>
akmhoque05d5fcf2014-04-15 14:58:45 -05002#include "utility/nlsr_logger.hpp"
akmhoqueba094742014-02-28 11:47:21 -06003#include "nlsr_nhl.hpp"
4#include "nlsr_nexthop.hpp"
5
akmhoque05d5fcf2014-04-15 14:58:45 -05006#define THIS_FILE "nlsr_nhl.cpp"
7
akmhoqueba094742014-02-28 11:47:21 -06008namespace nlsr
9{
10
akmhoque5a44dd42014-03-12 18:11:32 -050011 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060012
akmhoque5a44dd42014-03-12 18:11:32 -050013 static bool
14 nexthopCompare(NextHop& nh1, NextHop& nh2)
15 {
16 return nh1.getConnectingFace()==nh2.getConnectingFace();
17 }
18
19 static bool
20 nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
21 {
22 return (nh1.getConnectingFace()==nh2.getConnectingFace() &&
23 nh1.getRouteCost() == nh2.getRouteCost()) ;
24 }
25
26 static bool
27 nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
28 {
29 return nh1.getRouteCost() < nh2.getRouteCost();
30 }
31
32 /**
33 Add next hop to the Next Hop list
34 If next hop is new it is added
35 If next hop already exists in next
36 hop list then updates the route
37 cost with new next hop's route cost
38 */
39
40 void
41 Nhl::addNextHop(NextHop& nh)
42 {
akmhoque05d5fcf2014-04-15 14:58:45 -050043 std::list<NextHop >::iterator it = std::find_if( m_nexthopList.begin(),
44 m_nexthopList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050045 bind(&nexthopCompare, _1, nh));
akmhoque05d5fcf2014-04-15 14:58:45 -050046 if ( it == m_nexthopList.end() )
akmhoqueba094742014-02-28 11:47:21 -060047 {
akmhoque05d5fcf2014-04-15 14:58:45 -050048 m_nexthopList.push_back(nh);
akmhoqueba094742014-02-28 11:47:21 -060049 }
akmhoque5a44dd42014-03-12 18:11:32 -050050 if ( (*it).getRouteCost() > nh.getRouteCost() )
akmhoqueba094742014-02-28 11:47:21 -060051 {
akmhoque5a44dd42014-03-12 18:11:32 -050052 (*it).setRouteCost(nh.getRouteCost());
akmhoqueba094742014-02-28 11:47:21 -060053 }
akmhoque5a44dd42014-03-12 18:11:32 -050054 }
akmhoqueba094742014-02-28 11:47:21 -060055
akmhoque5a44dd42014-03-12 18:11:32 -050056 /**
57 Remove a next hop only if both next hop face and route cost are same
58
59 */
60
61 void
62 Nhl::removeNextHop(NextHop &nh)
63 {
akmhoque05d5fcf2014-04-15 14:58:45 -050064 std::list<NextHop >::iterator it = std::find_if( m_nexthopList.begin(),
65 m_nexthopList.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050066 bind(&nexthopRemoveCompare, _1, nh));
akmhoque05d5fcf2014-04-15 14:58:45 -050067 if ( it != m_nexthopList.end() )
akmhoqueba094742014-02-28 11:47:21 -060068 {
akmhoque05d5fcf2014-04-15 14:58:45 -050069 m_nexthopList.erase(it);
akmhoqueba094742014-02-28 11:47:21 -060070 }
akmhoque5a44dd42014-03-12 18:11:32 -050071 }
akmhoqueba094742014-02-28 11:47:21 -060072
akmhoque5a44dd42014-03-12 18:11:32 -050073 void
akmhoque05d5fcf2014-04-15 14:58:45 -050074 Nhl::sort()
akmhoque5a44dd42014-03-12 18:11:32 -050075 {
akmhoque05d5fcf2014-04-15 14:58:45 -050076 m_nexthopList.sort(nextHopSortingComparator);
akmhoque5a44dd42014-03-12 18:11:32 -050077 }
akmhoqueba094742014-02-28 11:47:21 -060078
akmhoque5a44dd42014-03-12 18:11:32 -050079 ostream&
80 operator<<(ostream& os, Nhl& nhl)
81 {
82 std::list< NextHop > nexthopList = nhl.getNextHopList();
83 int i=1;
84 for( std::list<NextHop>::iterator it=nexthopList.begin();
85 it!= nexthopList.end() ; it++,i++)
akmhoqueba094742014-02-28 11:47:21 -060086 {
akmhoque5a44dd42014-03-12 18:11:32 -050087 os << "Nexthop "<<i<<": "<<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -060088 }
akmhoque5a44dd42014-03-12 18:11:32 -050089 return os;
90 }
akmhoqueba094742014-02-28 11:47:21 -060091
92}//namespace nlsr