blob: 399584f767fc7839c7efd2e782be00e541435628 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include<list>
2#include "nlsr_fe.hpp"
3#include "nlsr_fib.hpp"
4#include "nlsr_nhl.hpp"
5#include "nlsr.hpp"
6
7using namespace std;
8
9static bool
10fibEntryNameCompare(FibEntry& fe, string name)
11{
12 return fe.getName() == name ;
13}
14
15
16
17void
18Fib::removeFromFib(string name)
19{
20 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
21 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
22 if( it != fibTable.end() )
23 {
24 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
25 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
26 {
27 //remove entry from NDN-FIB
28 }
29 fibTable.erase(it);
30 }
31}
32
33
34void
35Fib::updateFib(string name, Nhl& nextHopList, int maxFacesPerPrefix)
36{
37 int startFace=0;
38 int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
39 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
40 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
41 if( it != fibTable.end() )
42 {
43 nextHopList.sortNhl();
44 if ( !(*it).isEqualNextHops(nextHopList) )
45 {
46 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
47 (*it).getNhl().addNextHop((*nhit));
48 removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace());
49 startFace++;
50 nhit++;
51 for( int i=startFace;i< endFace;nhit++,i++)
52 {
53 (*it).getNhl().addNextHop((*nhit));
54 }
55
56 (*it).setTimeToRefresh(fibEntryRefreshTime);
57 }
58 (*it).getNhl().sortNhl();
59 //update NDN-FIB
60 }
61 else
62 {
63 nextHopList.sortNhl();
64 FibEntry newEntry(name);
65 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
66 for(int i=startFace; i< endFace ; i++)
67 {
68 newEntry.getNhl().addNextHop((*nhit));
69 ++nhit;
70 }
71 newEntry.getNhl().sortNhl();
72 newEntry.setTimeToRefresh(fibEntryRefreshTime);
73 fibTable.push_back(newEntry);
74 //Update NDN-FIB
75 }
76}
77
78void
79Fib::refreshFib(nlsr& pnlsr)
80{
81 for ( std::list<FibEntry >::iterator it = fibTable.begin() ;
82 it != fibTable.end() ; ++it)
83 {
84 (*it).setTimeToRefresh((*it).getTimeToRefresh()-60);
85 if( (*it).getTimeToRefresh() < 0 )
86 {
87 cout<<"Refreshing FIB entry : "<<endl;
88 cout<<(*it)<<endl;
89 (*it).setTimeToRefresh(fibEntryRefreshTime);
90 //update NDN-FIB
91 }
92 }
93
94 printFib();
95 scheduleFibRefreshing(pnlsr,60);
96}
97
98void
99Fib::scheduleFibRefreshing(nlsr& pnlsr, int refreshTime)
100{
101 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
102 ndn::bind(&Fib::refreshFib,this,boost::ref(pnlsr)));
103}
104
105void Fib::cleanFib()
106{
107 for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
108 ++it)
109 {
110 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
111 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
112 {
113 //remove entry from NDN-FIB
114 }
115 }
116
117 if ( fibTable.size() > 0 )
118 {
119 fibTable.clear();
120 }
121}
122
123
124void
125Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId)
126{
127 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
128 it != nl.getNextHopList().end(); ++it)
129 {
130 if ( (*it).getConnectingFace() != doNotRemoveHopFaceId )
131 {
132 nl.getNextHopList().erase(it);
133 }
134 }
135}
136
137
138int
139Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
140{
141 int endFace=0;
142 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
143 {
144 return nextHopList.getNhlSize();
145 }
146 else
147 {
148 return maxFacesPerPrefix;
149 }
150
151 return endFace;
152}
153
154void
155Fib::printFib()
156{
157 cout<<"-------------------FIB-----------------------------"<<endl;
158 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
159 ++it)
160 {
161 cout<<(*it);
162 }
163}