blob: fd4b20988cb342bbbea1214dd26e0592a5506b35 [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;
akmhoque85d88332014-02-17 21:11:21 -06008using namespace ndn;
akmhoque298385a2014-02-13 14:13:09 -06009
10static bool
11fibEntryNameCompare(FibEntry& fe, string name)
12{
13 return fe.getName() == name ;
14}
15
akmhoque85d88332014-02-17 21:11:21 -060016void
17Fib::cancelScheduledFeExpiringEvent(nlsr& pnlsr, EventId eid)
18{
19 pnlsr.getScheduler().cancelEvent(eid);
20}
akmhoque298385a2014-02-13 14:13:09 -060021
22
akmhoque85d88332014-02-17 21:11:21 -060023ndn::EventId
24Fib::scheduleFibEntryRefreshing(nlsr& pnlsr, string name, int feSeqNum, int refreshTime)
25{
26 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
27 ndn::bind(&Fib::refreshFibEntry,this,name,feSeqNum));
28}
29
30void
31Fib::refreshFibEntry(string name, int feSeqNum)
32{
33
34}
35
akmhoque298385a2014-02-13 14:13:09 -060036void
akmhoque85d88332014-02-17 21:11:21 -060037Fib::removeFromFib(nlsr& pnlsr, string name)
akmhoque298385a2014-02-13 14:13:09 -060038{
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 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
44 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
45 {
46 //remove entry from NDN-FIB
47 }
akmhoque85d88332014-02-17 21:11:21 -060048 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
akmhoque298385a2014-02-13 14:13:09 -060049 fibTable.erase(it);
50 }
51}
52
53
54void
akmhoque85d88332014-02-17 21:11:21 -060055Fib::updateFib(nlsr& pnlsr,string name, Nhl& nextHopList, int maxFacesPerPrefix)
akmhoque298385a2014-02-13 14:13:09 -060056{
57 int startFace=0;
58 int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
59 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
60 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
61 if( it != fibTable.end() )
62 {
63 nextHopList.sortNhl();
64 if ( !(*it).isEqualNextHops(nextHopList) )
65 {
66 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
67 (*it).getNhl().addNextHop((*nhit));
68 removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace());
69 startFace++;
70 nhit++;
71 for( int i=startFace;i< endFace;nhit++,i++)
72 {
73 (*it).getNhl().addNextHop((*nhit));
74 }
75
76 (*it).setTimeToRefresh(fibEntryRefreshTime);
77 }
78 (*it).getNhl().sortNhl();
akmhoque85d88332014-02-17 21:11:21 -060079 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
80 (*it).setFeSeqNo((*it).getFeSeqNo()+1);
81 (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
82 (*it).getName() ,
83 (*it).getFeSeqNo(),fibEntryRefreshTime));
akmhoque298385a2014-02-13 14:13:09 -060084 //update NDN-FIB
85 }
86 else
87 {
88 nextHopList.sortNhl();
89 FibEntry newEntry(name);
90 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
91 for(int i=startFace; i< endFace ; i++)
92 {
93 newEntry.getNhl().addNextHop((*nhit));
94 ++nhit;
95 }
96 newEntry.getNhl().sortNhl();
97 newEntry.setTimeToRefresh(fibEntryRefreshTime);
akmhoque85d88332014-02-17 21:11:21 -060098 newEntry.setFeSeqNo(1);
akmhoque298385a2014-02-13 14:13:09 -060099 fibTable.push_back(newEntry);
akmhoque85d88332014-02-17 21:11:21 -0600100
101 //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId());
102
akmhoque298385a2014-02-13 14:13:09 -0600103 //Update NDN-FIB
104 }
105}
106
akmhoque298385a2014-02-13 14:13:09 -0600107
akmhoque298385a2014-02-13 14:13:09 -0600108
akmhoque85d88332014-02-17 21:11:21 -0600109void Fib::cleanFib(nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600110{
111 for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
112 ++it)
113 {
114 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
115 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
116 {
akmhoque85d88332014-02-17 21:11:21 -0600117 cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId());
akmhoque298385a2014-02-13 14:13:09 -0600118 //remove entry from NDN-FIB
119 }
120 }
121
122 if ( fibTable.size() > 0 )
123 {
124 fibTable.clear();
125 }
126}
127
128
129void
130Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId)
131{
132 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
133 it != nl.getNextHopList().end(); ++it)
134 {
135 if ( (*it).getConnectingFace() != doNotRemoveHopFaceId )
136 {
137 nl.getNextHopList().erase(it);
138 }
139 }
140}
141
142
143int
144Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
145{
146 int endFace=0;
147 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
148 {
149 return nextHopList.getNhlSize();
150 }
151 else
152 {
153 return maxFacesPerPrefix;
154 }
155
156 return endFace;
157}
158
159void
160Fib::printFib()
161{
162 cout<<"-------------------FIB-----------------------------"<<endl;
163 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
164 ++it)
165 {
166 cout<<(*it);
167 }
168}