blob: a1e49684691bea966c6bf6acf57fd1b3e9c438a3 [file] [log] [blame]
akmhoque62a8e402014-02-08 12:00:27 -06001#include<list>
2#include "nlsr_fe.hpp"
3#include "nlsr_fib.hpp"
4#include "nlsr_nhl.hpp"
5
6using namespace std;
7
8static bool
9fibEntryNameCompare(FibEntry& fe, string name)
10{
11 return fe.getName() == name ;
12}
13
14
15
16void
17Fib::removeFromFib(string name)
18{
19 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
20 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
21 if( it != fibTable.end() )
22 {
23 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
24 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
25 {
26 //remove entry from NDN-FIB
27 }
28 fibTable.erase(it);
29 }
30}
31
32/**
33If NHL is equal for current FIB and NPT then to change
34Otherwise
35 Add the first Nexthop to FIB
36 remove all old nexthop from FIB
37 And add all other Nexthop to FIB
38*/
39
40void
41Fib::updateFib(string name, Nhl& nextHopList, int maxFacesPerPrefix)
42{
43 int startFace=0;
44 int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
45 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
46 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
47 if( it != fibTable.end() )
48 {
49 if ( !(*it).isEqualNextHops(nextHopList) )
50 {
51 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
52 (*it).getNhl().addNextHop((*nhit));
53 removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace());
54 startFace++;
55 nhit++;
56 for( int i=startFace;i< endFace;nhit++,i++)
57 {
58 (*it).getNhl().addNextHop((*nhit));
59 }
60 }
61 }
62 else
63 {
64 FibEntry newEntry(name);
65 for(std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
66 nhit!=nextHopList.getNextHopList().end();++nhit)
67 {
68 newEntry.getNhl().addNextHop((*nhit));
69 }
70 fibTable.push_back(newEntry);
71 }
72}
73
74void Fib::cleanFib()
75{
76 for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
77 ++it)
78 {
79 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
80 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
81 {
82 //remove entry from NDN-FIB
83 }
84 }
85
86 if ( fibTable.size() > 0 )
87 {
88 fibTable.clear();
89 }
90}
91
92
93void
94Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHop)
95{
96 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
97 it != nl.getNextHopList().end(); ++it)
98 {
99 if ( (*it).getConnectingFace() != doNotRemoveHop )
100 {
101 nl.getNextHopList().erase(it);
102 }
103 }
104}
105
106
107int
108Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
109{
110 int endFace=0;
111 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
112 {
113 return nextHopList.getNhlSize();
114 }
115 else
116 {
117 return maxFacesPerPrefix;
118 }
119
120 return endFace;
121}
122
123void
124Fib::printFib()
125{
126 cout<<"-------------------FIB-----------------------------"<<endl;
127 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
128 ++it)
129 {
130 cout<<(*it);
131 }
132}