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 | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 37 | } |
| 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 55 | |
| 56 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 57 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 78 | (*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); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 102 | //cancelScheduledFeExpiringEvent(pnlsr, newEntry().getFeExpiringEventId()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 103 | //Update NDN-FIB |
| 104 | } |
| 105 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 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 | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 109 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 121 | if ( fibTable.size() > 0 ) |
| 122 | { |
| 123 | fibTable.clear(); |
| 124 | } |
| 125 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 126 | |
| 127 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 128 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 140 | |
| 141 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 142 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 154 | return endFace; |
| 155 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 156 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 157 | 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 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 167 | |
| 168 | } //namespace nlsr |