blob: 141bf3a200fdf49ccba18e4b7606d0109cc13443 [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"
akmhoque05d5fcf2014-04-15 14:58:45 -05006#include "utility/nlsr_logger.hpp"
7
8#define THIS_FILE "nlsr_fib.cpp"
akmhoqueba094742014-02-28 11:47:21 -06009
10namespace nlsr
11{
12
akmhoque5a44dd42014-03-12 18:11:32 -050013 using namespace std;
14 using namespace ndn;
akmhoqueba094742014-02-28 11:47:21 -060015
akmhoque5a44dd42014-03-12 18:11:32 -050016 static bool
17 fibEntryNameCompare(FibEntry& fe, string name)
18 {
19 return fe.getName() == name ;
20 }
21
22 void
akmhoque05d5fcf2014-04-15 14:58:45 -050023 Fib::cancelScheduledExpiringEvent(Nlsr& pnlsr, EventId eid)
akmhoque5a44dd42014-03-12 18:11:32 -050024 {
25 pnlsr.getScheduler().cancelEvent(eid);
26 }
27
28
29 ndn::EventId
akmhoque05d5fcf2014-04-15 14:58:45 -050030 Fib::scheduleEntryRefreshing(Nlsr& pnlsr, string name, int feSeqNum,
akmhoque5a44dd42014-03-12 18:11:32 -050031 int refreshTime)
32 {
33 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
akmhoque05d5fcf2014-04-15 14:58:45 -050034 ndn::bind(&Fib::refreshEntry,this,name,feSeqNum));
akmhoque5a44dd42014-03-12 18:11:32 -050035 }
36
37 void
akmhoque05d5fcf2014-04-15 14:58:45 -050038 Fib::refreshEntry(string name, int feSeqNum)
akmhoque5a44dd42014-03-12 18:11:32 -050039 {
40 }
41
42 void
akmhoque05d5fcf2014-04-15 14:58:45 -050043 Fib::remove(Nlsr& pnlsr, string name)
akmhoque5a44dd42014-03-12 18:11:32 -050044 {
akmhoque05d5fcf2014-04-15 14:58:45 -050045 std::list<FibEntry >::iterator it = std::find_if( m_table.begin(),
46 m_table.end(), bind(&fibEntryNameCompare, _1, name));
47 if( it != m_table.end() )
akmhoqueba094742014-02-28 11:47:21 -060048 {
akmhoque5a44dd42014-03-12 18:11:32 -050049 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
50 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
51 {
52 //remove entry from NDN-FIB
53 }
akmhoque05d5fcf2014-04-15 14:58:45 -050054 cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId());
55 m_table.erase(it);
akmhoqueba094742014-02-28 11:47:21 -060056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 }
akmhoqueba094742014-02-28 11:47:21 -060058
akmhoque5a44dd42014-03-12 18:11:32 -050059
60 void
akmhoque05d5fcf2014-04-15 14:58:45 -050061 Fib::update(Nlsr& pnlsr,string name, Nhl& nextHopList)
akmhoque5a44dd42014-03-12 18:11:32 -050062 {
63 std::cout<<"Fib::updateFib Called"<<std::endl;
64 int startFace=0;
65 int endFace=getNumberOfFacesForName(nextHopList,
66 pnlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque05d5fcf2014-04-15 14:58:45 -050067 std::list<FibEntry >::iterator it = std::find_if( m_table.begin(),
68 m_table.end(),
akmhoque5a44dd42014-03-12 18:11:32 -050069 bind(&fibEntryNameCompare, _1, name));
akmhoque05d5fcf2014-04-15 14:58:45 -050070 if( it == m_table.end() )
akmhoqueba094742014-02-28 11:47:21 -060071 {
akmhoque05d5fcf2014-04-15 14:58:45 -050072 if( nextHopList.getSize() > 0 )
akmhoque5a44dd42014-03-12 18:11:32 -050073 {
akmhoque05d5fcf2014-04-15 14:58:45 -050074 nextHopList.sort();
akmhoque5a44dd42014-03-12 18:11:32 -050075 FibEntry newEntry(name);
76 std::list<NextHop> nhl=nextHopList.getNextHopList();
77 std::list<NextHop>::iterator nhit=nhl.begin();
78 for(int i=startFace; i< endFace && nhit!=nhl.end(); ++nhit, i++)
akmhoqueba094742014-02-28 11:47:21 -060079 {
akmhoque5a44dd42014-03-12 18:11:32 -050080 newEntry.getNhl().addNextHop((*nhit));
81 //Add entry to NDN-FIB
akmhoqueba094742014-02-28 11:47:21 -060082 }
akmhoque05d5fcf2014-04-15 14:58:45 -050083 newEntry.getNhl().sort();
84 newEntry.setTimeToRefresh(m_refreshTime);
85 newEntry.setSeqNo(1);
86 newEntry.setExpiringEventId(scheduleEntryRefreshing(pnlsr,
87 name ,1,m_refreshTime));
88 m_table.push_back(newEntry);
akmhoque5a44dd42014-03-12 18:11:32 -050089 }
akmhoqueba094742014-02-28 11:47:21 -060090 }
akmhoque5a44dd42014-03-12 18:11:32 -050091 else
akmhoqueba094742014-02-28 11:47:21 -060092 {
akmhoque5a44dd42014-03-12 18:11:32 -050093 std::cout<<"Old FIB Entry"<<std::endl;
akmhoque05d5fcf2014-04-15 14:58:45 -050094 if( nextHopList.getSize() > 0 )
akmhoque5a44dd42014-03-12 18:11:32 -050095 {
akmhoque05d5fcf2014-04-15 14:58:45 -050096 nextHopList.sort();
akmhoque5a44dd42014-03-12 18:11:32 -050097 if ( !it->isEqualNextHops(nextHopList) )
akmhoqueba094742014-02-28 11:47:21 -060098 {
akmhoque5a44dd42014-03-12 18:11:32 -050099 std::list<NextHop> nhl=nextHopList.getNextHopList();
100 std::list<NextHop>::iterator nhit=nhl.begin();
101 // Add first Entry to NDN-FIB
akmhoque05d5fcf2014-04-15 14:58:45 -0500102 removeHop(pnlsr, it->getNhl(),nhit->getConnectingFace());
103 it->getNhl().reset();
akmhoque5a44dd42014-03-12 18:11:32 -0500104 it->getNhl().addNextHop((*nhit));
105 ++startFace;
106 ++nhit;
107 for(int i=startFace; i< endFace && nhit!=nhl.end(); ++nhit, i++)
108 {
109 it->getNhl().addNextHop((*nhit));
110 //Add Entry to NDN_FIB
111 }
akmhoqueba094742014-02-28 11:47:21 -0600112 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500113 it->setTimeToRefresh(m_refreshTime);
114 cancelScheduledExpiringEvent(pnlsr, it->getExpiringEventId());
115 it->setSeqNo(it->getSeqNo()+1);
116 (*it).setExpiringEventId(scheduleEntryRefreshing(pnlsr,
akmhoque5a44dd42014-03-12 18:11:32 -0500117 it->getName() ,
akmhoque05d5fcf2014-04-15 14:58:45 -0500118 it->getSeqNo(),m_refreshTime));
akmhoque5a44dd42014-03-12 18:11:32 -0500119 }
120 else
121 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500122 remove(pnlsr,name);
akmhoque5a44dd42014-03-12 18:11:32 -0500123 }
akmhoqueba094742014-02-28 11:47:21 -0600124 }
akmhoque5a44dd42014-03-12 18:11:32 -0500125 }
akmhoqueba094742014-02-28 11:47:21 -0600126
127
128
akmhoque05d5fcf2014-04-15 14:58:45 -0500129 void Fib::clean(Nlsr& pnlsr)
akmhoque5a44dd42014-03-12 18:11:32 -0500130 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500131 for( std::list<FibEntry >::iterator it=m_table.begin(); it != m_table.end();
akmhoque5a44dd42014-03-12 18:11:32 -0500132 ++it)
akmhoqueba094742014-02-28 11:47:21 -0600133 {
akmhoque5a44dd42014-03-12 18:11:32 -0500134 for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin();
135 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
136 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500137 cancelScheduledExpiringEvent(pnlsr,(*it).getExpiringEventId());
akmhoque5a44dd42014-03-12 18:11:32 -0500138 //Remove entry from NDN-FIB
139 }
akmhoqueba094742014-02-28 11:47:21 -0600140 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500141 if ( m_table.size() > 0 )
akmhoqueba094742014-02-28 11:47:21 -0600142 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500143 m_table.clear();
akmhoqueba094742014-02-28 11:47:21 -0600144 }
akmhoque5a44dd42014-03-12 18:11:32 -0500145 }
akmhoqueba094742014-02-28 11:47:21 -0600146
akmhoque5a44dd42014-03-12 18:11:32 -0500147 int
148 Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix)
149 {
150 int endFace=0;
akmhoque05d5fcf2014-04-15 14:58:45 -0500151 if((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix))
akmhoqueba094742014-02-28 11:47:21 -0600152 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500153 return nextHopList.getSize();
akmhoqueba094742014-02-28 11:47:21 -0600154 }
akmhoque5a44dd42014-03-12 18:11:32 -0500155 else
akmhoqueba094742014-02-28 11:47:21 -0600156 {
akmhoque5a44dd42014-03-12 18:11:32 -0500157 return maxFacesPerPrefix;
akmhoqueba094742014-02-28 11:47:21 -0600158 }
akmhoque5a44dd42014-03-12 18:11:32 -0500159 return endFace;
160 }
161
162 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500163 Fib::removeHop(Nlsr& pnlsr, Nhl& nl, int doNotRemoveHopFaceId)
akmhoque5a44dd42014-03-12 18:11:32 -0500164 {
165 for( std::list<NextHop >::iterator it=nl.getNextHopList().begin();
166 it != nl.getNextHopList().end(); ++it)
167 {
168 if ( it->getConnectingFace() != doNotRemoveHopFaceId )
169 {
170 //Remove FIB Entry from NDN-FIB
171 }
172 }
173 }
174
175 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500176 Fib::print()
akmhoque5a44dd42014-03-12 18:11:32 -0500177 {
178 cout<<"-------------------FIB-----------------------------"<<endl;
akmhoque05d5fcf2014-04-15 14:58:45 -0500179 for(std::list<FibEntry>::iterator it = m_table.begin(); it!=m_table.end();
akmhoque5a44dd42014-03-12 18:11:32 -0500180 ++it)
181 {
182 cout<<(*it);
183 }
184 }
akmhoqueba094742014-02-28 11:47:21 -0600185
186} //namespace nlsr