blob: 8b0340918c7a4c1b86965ad153cd250cb49432b6 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include<list>
2#include "nlsr_fe.hpp"
3#include "nlsr_fib.hpp"
4#include "nlsr_nhl.hpp"
5#include "nlsr.hpp"
6
7namespace nlsr
8{
9
akmhoque5a44dd42014-03-12 18:11:32 -050010 using namespace std;
11 using namespace ndn;
akmhoqueba094742014-02-28 11:47:21 -060012
akmhoque5a44dd42014-03-12 18:11:32 -050013 static bool
14 fibEntryNameCompare(FibEntry& fe, string name)
15 {
16 return fe.getName() == name ;
17 }
18
19 void
20 Fib::cancelScheduledFeExpiringEvent(Nlsr& pnlsr, EventId eid)
21 {
22 pnlsr.getScheduler().cancelEvent(eid);
23 }
24
25
26 ndn::EventId
27 Fib::scheduleFibEntryRefreshing(Nlsr& pnlsr, string name, int feSeqNum,
28 int refreshTime)
29 {
30 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
31 ndn::bind(&Fib::refreshFibEntry,this,name,feSeqNum));
32 }
33
34 void
35 Fib::refreshFibEntry(string name, int feSeqNum)
36 {
37 }
38
39 void
40 Fib::removeFromFib(Nlsr& pnlsr, string name)
41 {
42 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
43 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
44 if( it != fibTable.end() )
akmhoqueba094742014-02-28 11:47:21 -060045 {
akmhoque5a44dd42014-03-12 18:11:32 -050046 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
47 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
48 {
49 //remove entry from NDN-FIB
50 }
51 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
52 fibTable.erase(it);
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 void
58 Fib::updateFib(Nlsr& pnlsr,string name, Nhl& nextHopList)
59 {
60 std::cout<<"Fib::updateFib Called"<<std::endl;
61 int startFace=0;
62 int endFace=getNumberOfFacesForName(nextHopList,
63 pnlsr.getConfParameter().getMaxFacesPerPrefix());
64 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
65 fibTable.end(),
66 bind(&fibEntryNameCompare, _1, name));
67 if( it == fibTable.end() )
akmhoqueba094742014-02-28 11:47:21 -060068 {
akmhoque5a44dd42014-03-12 18:11:32 -050069 if( nextHopList.getNhlSize() > 0 )
70 {
71 nextHopList.sortNhl();
72 FibEntry newEntry(name);
73 std::list<NextHop> nhl=nextHopList.getNextHopList();
74 std::list<NextHop>::iterator nhit=nhl.begin();
75 for(int i=startFace; i< endFace && nhit!=nhl.end(); ++nhit, i++)
akmhoqueba094742014-02-28 11:47:21 -060076 {
akmhoque5a44dd42014-03-12 18:11:32 -050077 newEntry.getNhl().addNextHop((*nhit));
78 //Add entry to NDN-FIB
akmhoqueba094742014-02-28 11:47:21 -060079 }
akmhoque5a44dd42014-03-12 18:11:32 -050080 newEntry.getNhl().sortNhl();
81 newEntry.setTimeToRefresh(fibEntryRefreshTime);
82 newEntry.setFeSeqNo(1);
83 newEntry.setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
84 name ,1,fibEntryRefreshTime));
85 fibTable.push_back(newEntry);
86 }
akmhoqueba094742014-02-28 11:47:21 -060087 }
akmhoque5a44dd42014-03-12 18:11:32 -050088 else
akmhoqueba094742014-02-28 11:47:21 -060089 {
akmhoque5a44dd42014-03-12 18:11:32 -050090 std::cout<<"Old FIB Entry"<<std::endl;
91 if( nextHopList.getNhlSize() > 0 )
92 {
93 nextHopList.sortNhl();
94 if ( !it->isEqualNextHops(nextHopList) )
akmhoqueba094742014-02-28 11:47:21 -060095 {
akmhoque5a44dd42014-03-12 18:11:32 -050096 std::list<NextHop> nhl=nextHopList.getNextHopList();
97 std::list<NextHop>::iterator nhit=nhl.begin();
98 // Add first Entry to NDN-FIB
99 removeFibEntryHop(pnlsr, it->getNhl(),nhit->getConnectingFace());
100 it->getNhl().resetNhl();
101 it->getNhl().addNextHop((*nhit));
102 ++startFace;
103 ++nhit;
104 for(int i=startFace; i< endFace && nhit!=nhl.end(); ++nhit, i++)
105 {
106 it->getNhl().addNextHop((*nhit));
107 //Add Entry to NDN_FIB
108 }
akmhoqueba094742014-02-28 11:47:21 -0600109 }
akmhoque5a44dd42014-03-12 18:11:32 -0500110 it->setTimeToRefresh(fibEntryRefreshTime);
111 cancelScheduledFeExpiringEvent(pnlsr, it->getFeExpiringEventId());
112 it->setFeSeqNo(it->getFeSeqNo()+1);
113 (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
114 it->getName() ,
115 it->getFeSeqNo(),fibEntryRefreshTime));
116 }
117 else
118 {
119 removeFromFib(pnlsr,name);
120 }
akmhoqueba094742014-02-28 11:47:21 -0600121 }
akmhoque5a44dd42014-03-12 18:11:32 -0500122 }
akmhoqueba094742014-02-28 11:47:21 -0600123
124
125
akmhoque5a44dd42014-03-12 18:11:32 -0500126 void Fib::cleanFib(Nlsr& pnlsr)
127 {
128 for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
129 ++it)
akmhoqueba094742014-02-28 11:47:21 -0600130 {
akmhoque5a44dd42014-03-12 18:11:32 -0500131 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
132 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
133 {
134 cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId());
135 //Remove entry from NDN-FIB
136 }
akmhoqueba094742014-02-28 11:47:21 -0600137 }
akmhoque5a44dd42014-03-12 18:11:32 -0500138 if ( fibTable.size() > 0 )
akmhoqueba094742014-02-28 11:47:21 -0600139 {
akmhoque5a44dd42014-03-12 18:11:32 -0500140 fibTable.clear();
akmhoqueba094742014-02-28 11:47:21 -0600141 }
akmhoque5a44dd42014-03-12 18:11:32 -0500142 }
akmhoqueba094742014-02-28 11:47:21 -0600143
akmhoque5a44dd42014-03-12 18:11:32 -0500144 int
145 Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
146 {
147 int endFace=0;
148 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
akmhoqueba094742014-02-28 11:47:21 -0600149 {
akmhoque5a44dd42014-03-12 18:11:32 -0500150 return nextHopList.getNhlSize();
akmhoqueba094742014-02-28 11:47:21 -0600151 }
akmhoque5a44dd42014-03-12 18:11:32 -0500152 else
akmhoqueba094742014-02-28 11:47:21 -0600153 {
akmhoque5a44dd42014-03-12 18:11:32 -0500154 return maxFacesPerPrefix;
akmhoqueba094742014-02-28 11:47:21 -0600155 }
akmhoque5a44dd42014-03-12 18:11:32 -0500156 return endFace;
157 }
158
159 void
160 Fib::removeFibEntryHop(Nlsr& pnlsr, Nhl& nl, int doNotRemoveHopFaceId)
161 {
162 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
163 it != nl.getNextHopList().end(); ++it)
164 {
165 if ( it->getConnectingFace() != doNotRemoveHopFaceId )
166 {
167 //Remove FIB Entry from NDN-FIB
168 }
169 }
170 }
171
172 void
173 Fib::printFib()
174 {
175 cout<<"-------------------FIB-----------------------------"<<endl;
176 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
177 ++it)
178 {
179 cout<<(*it);
180 }
181 }
akmhoqueba094742014-02-28 11:47:21 -0600182
183} //namespace nlsr