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 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 7 | namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 8 | { |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 9 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 10 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 24 | |
| 25 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 26 | 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 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 33 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 34 | void |
| 35 | Fib::refreshFibEntry(string name, int feSeqNum) |
| 36 | { |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 37 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 38 | } |
| 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 56 | |
| 57 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 58 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 79 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 80 | (*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); |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 104 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 105 | //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId()); |
| 106 | |
| 107 | //Update NDN-FIB |
| 108 | } |
| 109 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 110 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 111 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 112 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 113 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 125 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 126 | if ( fibTable.size() > 0 ) |
| 127 | { |
| 128 | fibTable.clear(); |
| 129 | } |
| 130 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 131 | |
| 132 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 133 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 145 | |
| 146 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 147 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 159 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 160 | return endFace; |
| 161 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 162 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame^] | 163 | 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 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 173 | |
| 174 | } //namespace nlsr |