blob: 4ff49b23a8b806b3f4c5aaf5df2f4067f89cbaf4 [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
akmhoque1fd8c1e2014-02-19 19:41:49 -06007namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -06008{
akmhoque298385a2014-02-13 14:13:09 -06009
akmhoque1fd8c1e2014-02-19 19:41:49 -060010 using namespace std;
11 using namespace ndn;
12
13 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 }
akmhoque298385a2014-02-13 14:13:09 -060024
25
akmhoque1fd8c1e2014-02-19 19:41:49 -060026 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 }
akmhoque85d88332014-02-17 21:11:21 -060033
akmhoque1fd8c1e2014-02-19 19:41:49 -060034 void
35 Fib::refreshFibEntry(string name, int feSeqNum)
36 {
akmhoque85d88332014-02-17 21:11:21 -060037
akmhoque1fd8c1e2014-02-19 19:41:49 -060038 }
39
40 void
41 Fib::removeFromFib(Nlsr& pnlsr, string name)
42 {
43 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
44 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
45 if( it != fibTable.end() )
46 {
47 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
48 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
49 {
50 //remove entry from NDN-FIB
51 }
52 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
53 fibTable.erase(it);
54 }
55 }
akmhoque298385a2014-02-13 14:13:09 -060056
57
akmhoque1fd8c1e2014-02-19 19:41:49 -060058 void
59 Fib::updateFib(Nlsr& pnlsr,string name, Nhl& nextHopList, int maxFacesPerPrefix)
60 {
61 int startFace=0;
62 int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
63 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
64 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
65 if( it != fibTable.end() )
66 {
67 nextHopList.sortNhl();
68 if ( !(*it).isEqualNextHops(nextHopList) )
69 {
70 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
71 (*it).getNhl().addNextHop((*nhit));
72 removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace());
73 startFace++;
74 nhit++;
75 for( int i=startFace; i< endFace; nhit++,i++)
76 {
77 (*it).getNhl().addNextHop((*nhit));
78 }
akmhoque298385a2014-02-13 14:13:09 -060079
akmhoque1fd8c1e2014-02-19 19:41:49 -060080 (*it).setTimeToRefresh(fibEntryRefreshTime);
81 }
82 (*it).getNhl().sortNhl();
83 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
84 (*it).setFeSeqNo((*it).getFeSeqNo()+1);
85 (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
86 (*it).getName() ,
87 (*it).getFeSeqNo(),fibEntryRefreshTime));
88 //update NDN-FIB
89 }
90 else
91 {
92 nextHopList.sortNhl();
93 FibEntry newEntry(name);
94 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
95 for(int i=startFace; i< endFace ; i++)
96 {
97 newEntry.getNhl().addNextHop((*nhit));
98 ++nhit;
99 }
100 newEntry.getNhl().sortNhl();
101 newEntry.setTimeToRefresh(fibEntryRefreshTime);
102 newEntry.setFeSeqNo(1);
103 fibTable.push_back(newEntry);
akmhoque85d88332014-02-17 21:11:21 -0600104
akmhoque1fd8c1e2014-02-19 19:41:49 -0600105 //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId());
106
107 //Update NDN-FIB
108 }
109 }
akmhoque298385a2014-02-13 14:13:09 -0600110
akmhoque298385a2014-02-13 14:13:09 -0600111
akmhoque298385a2014-02-13 14:13:09 -0600112
akmhoque1fd8c1e2014-02-19 19:41:49 -0600113 void Fib::cleanFib(Nlsr& pnlsr)
114 {
115 for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
116 ++it)
117 {
118 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
119 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
120 {
121 cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId());
122 //remove entry from NDN-FIB
123 }
124 }
akmhoque298385a2014-02-13 14:13:09 -0600125
akmhoque1fd8c1e2014-02-19 19:41:49 -0600126 if ( fibTable.size() > 0 )
127 {
128 fibTable.clear();
129 }
130 }
akmhoque298385a2014-02-13 14:13:09 -0600131
132
akmhoque1fd8c1e2014-02-19 19:41:49 -0600133 void
134 Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId)
135 {
136 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
137 it != nl.getNextHopList().end(); ++it)
138 {
139 if ( (*it).getConnectingFace() != doNotRemoveHopFaceId )
140 {
141 nl.getNextHopList().erase(it);
142 }
143 }
144 }
akmhoque298385a2014-02-13 14:13:09 -0600145
146
akmhoque1fd8c1e2014-02-19 19:41:49 -0600147 int
148 Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
149 {
150 int endFace=0;
151 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
152 {
153 return nextHopList.getNhlSize();
154 }
155 else
156 {
157 return maxFacesPerPrefix;
158 }
akmhoque298385a2014-02-13 14:13:09 -0600159
akmhoque1fd8c1e2014-02-19 19:41:49 -0600160 return endFace;
161 }
akmhoque298385a2014-02-13 14:13:09 -0600162
akmhoque1fd8c1e2014-02-19 19:41:49 -0600163 void
164 Fib::printFib()
165 {
166 cout<<"-------------------FIB-----------------------------"<<endl;
167 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
168 ++it)
169 {
170 cout<<(*it);
171 }
172 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600173
174} //namespace nlsr