blob: 54b6834ad044675b2655a50f7536b3dfaae11fc0 [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
akmhoqueb1710aa2014-02-19 17:13:36 -06007namespace nlsr {
8
akmhoque298385a2014-02-13 14:13:09 -06009using namespace std;
akmhoque85d88332014-02-17 21:11:21 -060010using namespace ndn;
akmhoque298385a2014-02-13 14:13:09 -060011
12static bool
13fibEntryNameCompare(FibEntry& fe, string name)
14{
15 return fe.getName() == name ;
16}
17
akmhoque85d88332014-02-17 21:11:21 -060018void
akmhoque1a481092014-02-19 16:34:22 -060019Fib::cancelScheduledFeExpiringEvent(Nlsr& pnlsr, EventId eid)
akmhoque85d88332014-02-17 21:11:21 -060020{
21 pnlsr.getScheduler().cancelEvent(eid);
22}
akmhoque298385a2014-02-13 14:13:09 -060023
24
akmhoque85d88332014-02-17 21:11:21 -060025ndn::EventId
akmhoque1a481092014-02-19 16:34:22 -060026Fib::scheduleFibEntryRefreshing(Nlsr& pnlsr, string name, int feSeqNum, int refreshTime)
akmhoque85d88332014-02-17 21:11:21 -060027{
28 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
29 ndn::bind(&Fib::refreshFibEntry,this,name,feSeqNum));
30}
31
32void
33Fib::refreshFibEntry(string name, int feSeqNum)
34{
35
36}
37
akmhoque298385a2014-02-13 14:13:09 -060038void
akmhoque1a481092014-02-19 16:34:22 -060039Fib::removeFromFib(Nlsr& pnlsr, string name)
akmhoque298385a2014-02-13 14:13:09 -060040{
41 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
42 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
43 if( it != fibTable.end() )
44 {
45 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
46 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
47 {
48 //remove entry from NDN-FIB
49 }
akmhoque85d88332014-02-17 21:11:21 -060050 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
akmhoque298385a2014-02-13 14:13:09 -060051 fibTable.erase(it);
52 }
53}
54
55
56void
akmhoque1a481092014-02-19 16:34:22 -060057Fib::updateFib(Nlsr& pnlsr,string name, Nhl& nextHopList, int maxFacesPerPrefix)
akmhoque298385a2014-02-13 14:13:09 -060058{
59 int startFace=0;
60 int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix);
61 std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(),
62 fibTable.end(), bind(&fibEntryNameCompare, _1, name));
63 if( it != fibTable.end() )
64 {
65 nextHopList.sortNhl();
66 if ( !(*it).isEqualNextHops(nextHopList) )
67 {
68 std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin();
69 (*it).getNhl().addNextHop((*nhit));
70 removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace());
71 startFace++;
72 nhit++;
73 for( int i=startFace;i< endFace;nhit++,i++)
74 {
75 (*it).getNhl().addNextHop((*nhit));
76 }
77
78 (*it).setTimeToRefresh(fibEntryRefreshTime);
79 }
80 (*it).getNhl().sortNhl();
akmhoque85d88332014-02-17 21:11:21 -060081 cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId());
82 (*it).setFeSeqNo((*it).getFeSeqNo()+1);
83 (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr,
84 (*it).getName() ,
85 (*it).getFeSeqNo(),fibEntryRefreshTime));
akmhoque298385a2014-02-13 14:13:09 -060086 //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);
akmhoque85d88332014-02-17 21:11:21 -0600100 newEntry.setFeSeqNo(1);
akmhoque298385a2014-02-13 14:13:09 -0600101 fibTable.push_back(newEntry);
akmhoque85d88332014-02-17 21:11:21 -0600102
103 //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId());
104
akmhoque298385a2014-02-13 14:13:09 -0600105 //Update NDN-FIB
106 }
107}
108
akmhoque298385a2014-02-13 14:13:09 -0600109
akmhoque298385a2014-02-13 14:13:09 -0600110
akmhoque1a481092014-02-19 16:34:22 -0600111void Fib::cleanFib(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600112{
113 for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end();
114 ++it)
115 {
116 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
117 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
118 {
akmhoque85d88332014-02-17 21:11:21 -0600119 cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId());
akmhoque298385a2014-02-13 14:13:09 -0600120 //remove entry from NDN-FIB
121 }
122 }
123
124 if ( fibTable.size() > 0 )
125 {
126 fibTable.clear();
127 }
128}
129
130
131void
132Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId)
133{
134 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
135 it != nl.getNextHopList().end(); ++it)
136 {
137 if ( (*it).getConnectingFace() != doNotRemoveHopFaceId )
138 {
139 nl.getNextHopList().erase(it);
140 }
141 }
142}
143
144
145int
146Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
147{
148 int endFace=0;
149 if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix))
150 {
151 return nextHopList.getNhlSize();
152 }
153 else
154 {
155 return maxFacesPerPrefix;
156 }
157
158 return endFace;
159}
160
161void
162Fib::printFib()
163{
164 cout<<"-------------------FIB-----------------------------"<<endl;
165 for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end();
166 ++it)
167 {
168 cout<<(*it);
169 }
170}
akmhoqueb1710aa2014-02-19 17:13:36 -0600171
172} //namespace nlsr