blob: 8fb7928a7d636746234f4643d6d8d729063e9ce0 [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 {
akmhoque1fd8c1e2014-02-19 19:41:49 -060037 }
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() )
45 {
46 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);
53 }
54 }
akmhoque298385a2014-02-13 14:13:09 -060055
56
akmhoque1fd8c1e2014-02-19 19:41:49 -060057 void
58 Fib::updateFib(Nlsr& pnlsr,string name, Nhl& nextHopList, int maxFacesPerPrefix)
59 {
60 int startFace=0;
61 int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
62 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
63 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
64 if( it != fibTable.end() )
65 {
66 nextHopList.sortNhl();
67 if ( !(*it).isEqualNextHops(nextHopList) )
68 {
69 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
70 (*it).getNhl().addNextHop((*nhit));
71 removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace());
72 startFace++;
73 nhit++;
74 for( int i=startFace; i< endFace; nhit++,i++)
75 {
76 (*it).getNhl().addNextHop((*nhit));
77 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060078 (*it).setTimeToRefresh(fibEntryRefreshTime);
79 }
80 (*it).getNhl().sortNhl();
81 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
82 (*it).setFeSeqNo((*it).getFeSeqNo()+1);
83 (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
84 (*it).getName() ,
85 (*it).getFeSeqNo(),fibEntryRefreshTime));
86 //update NDN-FIB
87 }
88 else
89 {
90 nextHopList.sortNhl();
91 FibEntry newEntry(name);
92 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
93 for(int i=startFace; i< endFace ; i++)
94 {
95 newEntry.getNhl().addNextHop((*nhit));
96 ++nhit;
97 }
98 newEntry.getNhl().sortNhl();
99 newEntry.setTimeToRefresh(fibEntryRefreshTime);
100 newEntry.setFeSeqNo(1);
101 fibTable.push_back(newEntry);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600102 //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600103 //Update NDN-FIB
104 }
105 }
akmhoque298385a2014-02-13 14:13:09 -0600106
akmhoque298385a2014-02-13 14:13:09 -0600107
akmhoque298385a2014-02-13 14:13:09 -0600108
akmhoque1fd8c1e2014-02-19 19:41:49 -0600109 void Fib::cleanFib(Nlsr& pnlsr)
110 {
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 {
117 cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId());
118 //remove entry from NDN-FIB
119 }
120 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600121 if ( fibTable.size() > 0 )
122 {
123 fibTable.clear();
124 }
125 }
akmhoque298385a2014-02-13 14:13:09 -0600126
127
akmhoque1fd8c1e2014-02-19 19:41:49 -0600128 void
129 Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId)
130 {
131 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
132 it != nl.getNextHopList().end(); ++it)
133 {
134 if ( (*it).getConnectingFace() != doNotRemoveHopFaceId )
135 {
136 nl.getNextHopList().erase(it);
137 }
138 }
139 }
akmhoque298385a2014-02-13 14:13:09 -0600140
141
akmhoque1fd8c1e2014-02-19 19:41:49 -0600142 int
143 Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
144 {
145 int endFace=0;
146 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
147 {
148 return nextHopList.getNhlSize();
149 }
150 else
151 {
152 return maxFacesPerPrefix;
153 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600154 return endFace;
155 }
akmhoque298385a2014-02-13 14:13:09 -0600156
akmhoque1fd8c1e2014-02-19 19:41:49 -0600157 void
158 Fib::printFib()
159 {
160 cout<<"-------------------FIB-----------------------------"<<endl;
161 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
162 ++it)
163 {
164 cout<<(*it);
165 }
166 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600167
168} //namespace nlsr