akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include<list> |
| 2 | #include "nlsr_fe.hpp" |
| 3 | #include "nlsr_fib.hpp" |
| 4 | #include "nlsr_nhl.hpp" |
| 5 | #include "nlsr.hpp" |
| 6 | |
| 7 | using namespace std; |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 8 | using namespace ndn; |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 9 | |
| 10 | static bool |
| 11 | fibEntryNameCompare(FibEntry& fe, string name) |
| 12 | { |
| 13 | return fe.getName() == name ; |
| 14 | } |
| 15 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 16 | void |
| 17 | Fib::cancelScheduledFeExpiringEvent(nlsr& pnlsr, EventId eid) |
| 18 | { |
| 19 | pnlsr.getScheduler().cancelEvent(eid); |
| 20 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 21 | |
| 22 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 23 | ndn::EventId |
| 24 | Fib::scheduleFibEntryRefreshing(nlsr& pnlsr, string name, int feSeqNum, int refreshTime) |
| 25 | { |
| 26 | return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime), |
| 27 | ndn::bind(&Fib::refreshFibEntry,this,name,feSeqNum)); |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | Fib::refreshFibEntry(string name, int feSeqNum) |
| 32 | { |
| 33 | |
| 34 | } |
| 35 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 36 | void |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 37 | Fib::removeFromFib(nlsr& pnlsr, string name) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 38 | { |
| 39 | std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(), |
| 40 | fibTable.end(), bind(&fibEntryNameCompare, _1, name)); |
| 41 | if( it != fibTable.end() ) |
| 42 | { |
| 43 | for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin(); |
| 44 | nhit != (*it).getNhl().getNextHopList().begin(); nhit++) |
| 45 | { |
| 46 | //remove entry from NDN-FIB |
| 47 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 48 | cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId()); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 49 | fibTable.erase(it); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 55 | Fib::updateFib(nlsr& pnlsr,string name, Nhl& nextHopList, int maxFacesPerPrefix) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 56 | { |
| 57 | int startFace=0; |
| 58 | int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix); |
| 59 | std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(), |
| 60 | fibTable.end(), bind(&fibEntryNameCompare, _1, name)); |
| 61 | if( it != fibTable.end() ) |
| 62 | { |
| 63 | nextHopList.sortNhl(); |
| 64 | if ( !(*it).isEqualNextHops(nextHopList) ) |
| 65 | { |
| 66 | std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin(); |
| 67 | (*it).getNhl().addNextHop((*nhit)); |
| 68 | removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace()); |
| 69 | startFace++; |
| 70 | nhit++; |
| 71 | for( int i=startFace;i< endFace;nhit++,i++) |
| 72 | { |
| 73 | (*it).getNhl().addNextHop((*nhit)); |
| 74 | } |
| 75 | |
| 76 | (*it).setTimeToRefresh(fibEntryRefreshTime); |
| 77 | } |
| 78 | (*it).getNhl().sortNhl(); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 79 | cancelScheduledFeExpiringEvent(pnlsr, (*it).getFeExpiringEventId()); |
| 80 | (*it).setFeSeqNo((*it).getFeSeqNo()+1); |
| 81 | (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr, |
| 82 | (*it).getName() , |
| 83 | (*it).getFeSeqNo(),fibEntryRefreshTime)); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 84 | //update NDN-FIB |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | nextHopList.sortNhl(); |
| 89 | FibEntry newEntry(name); |
| 90 | std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin(); |
| 91 | for(int i=startFace; i< endFace ; i++) |
| 92 | { |
| 93 | newEntry.getNhl().addNextHop((*nhit)); |
| 94 | ++nhit; |
| 95 | } |
| 96 | newEntry.getNhl().sortNhl(); |
| 97 | newEntry.setTimeToRefresh(fibEntryRefreshTime); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 98 | newEntry.setFeSeqNo(1); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 99 | fibTable.push_back(newEntry); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 100 | |
| 101 | //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId()); |
| 102 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 103 | //Update NDN-FIB |
| 104 | } |
| 105 | } |
| 106 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 107 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 108 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 109 | void Fib::cleanFib(nlsr& pnlsr) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 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 | { |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame^] | 117 | cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId()); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 118 | //remove entry from NDN-FIB |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if ( fibTable.size() > 0 ) |
| 123 | { |
| 124 | fibTable.clear(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void |
| 130 | Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHopFaceId) |
| 131 | { |
| 132 | for( std::list<NextHop >::iterator it=nl.getNextHopList().begin(); |
| 133 | it != nl.getNextHopList().end(); ++it) |
| 134 | { |
| 135 | if ( (*it).getConnectingFace() != doNotRemoveHopFaceId ) |
| 136 | { |
| 137 | nl.getNextHopList().erase(it); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | int |
| 144 | Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix) |
| 145 | { |
| 146 | int endFace=0; |
| 147 | if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix)) |
| 148 | { |
| 149 | return nextHopList.getNhlSize(); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | return maxFacesPerPrefix; |
| 154 | } |
| 155 | |
| 156 | return endFace; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | Fib::printFib() |
| 161 | { |
| 162 | cout<<"-------------------FIB-----------------------------"<<endl; |
| 163 | for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end(); |
| 164 | ++it) |
| 165 | { |
| 166 | cout<<(*it); |
| 167 | } |
| 168 | } |