akmhoque | ba09474 | 2014-02-28 11:47:21 -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 | namespace nlsr |
| 8 | { |
| 9 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 10 | using namespace std; |
| 11 | using namespace ndn; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 12 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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 | } |
| 24 | |
| 25 | |
| 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 | } |
| 33 | |
| 34 | void |
| 35 | Fib::refreshFibEntry(string name, int feSeqNum) |
| 36 | { |
| 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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 45 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 53 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 54 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 55 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 56 | |
| 57 | void |
| 58 | Fib::updateFib(Nlsr& pnlsr,string name, Nhl& nextHopList) |
| 59 | { |
| 60 | std::cout<<"Fib::updateFib Called"<<std::endl; |
| 61 | int startFace=0; |
| 62 | int endFace=getNumberOfFacesForName(nextHopList, |
| 63 | pnlsr.getConfParameter().getMaxFacesPerPrefix()); |
| 64 | std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(), |
| 65 | fibTable.end(), |
| 66 | bind(&fibEntryNameCompare, _1, name)); |
| 67 | if( it == fibTable.end() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 68 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 69 | if( nextHopList.getNhlSize() > 0 ) |
| 70 | { |
| 71 | nextHopList.sortNhl(); |
| 72 | FibEntry newEntry(name); |
| 73 | std::list<NextHop> nhl=nextHopList.getNextHopList(); |
| 74 | std::list<NextHop>::iterator nhit=nhl.begin(); |
| 75 | for(int i=startFace; i< endFace && nhit!=nhl.end(); ++nhit, i++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 76 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 77 | newEntry.getNhl().addNextHop((*nhit)); |
| 78 | //Add entry to NDN-FIB |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 79 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 80 | newEntry.getNhl().sortNhl(); |
| 81 | newEntry.setTimeToRefresh(fibEntryRefreshTime); |
| 82 | newEntry.setFeSeqNo(1); |
| 83 | newEntry.setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr, |
| 84 | name ,1,fibEntryRefreshTime)); |
| 85 | fibTable.push_back(newEntry); |
| 86 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 87 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 88 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 89 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 90 | std::cout<<"Old FIB Entry"<<std::endl; |
| 91 | if( nextHopList.getNhlSize() > 0 ) |
| 92 | { |
| 93 | nextHopList.sortNhl(); |
| 94 | if ( !it->isEqualNextHops(nextHopList) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 95 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 96 | std::list<NextHop> nhl=nextHopList.getNextHopList(); |
| 97 | std::list<NextHop>::iterator nhit=nhl.begin(); |
| 98 | // Add first Entry to NDN-FIB |
| 99 | removeFibEntryHop(pnlsr, it->getNhl(),nhit->getConnectingFace()); |
| 100 | it->getNhl().resetNhl(); |
| 101 | it->getNhl().addNextHop((*nhit)); |
| 102 | ++startFace; |
| 103 | ++nhit; |
| 104 | for(int i=startFace; i< endFace && nhit!=nhl.end(); ++nhit, i++) |
| 105 | { |
| 106 | it->getNhl().addNextHop((*nhit)); |
| 107 | //Add Entry to NDN_FIB |
| 108 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 109 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 110 | it->setTimeToRefresh(fibEntryRefreshTime); |
| 111 | cancelScheduledFeExpiringEvent(pnlsr, it->getFeExpiringEventId()); |
| 112 | it->setFeSeqNo(it->getFeSeqNo()+1); |
| 113 | (*it).setFeExpiringEventId(scheduleFibEntryRefreshing(pnlsr, |
| 114 | it->getName() , |
| 115 | it->getFeSeqNo(),fibEntryRefreshTime)); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | removeFromFib(pnlsr,name); |
| 120 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 121 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 122 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 123 | |
| 124 | |
| 125 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 126 | void Fib::cleanFib(Nlsr& pnlsr) |
| 127 | { |
| 128 | for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end(); |
| 129 | ++it) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 130 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 131 | for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin(); |
| 132 | nhit != (*it).getNhl().getNextHopList().begin(); nhit++) |
| 133 | { |
| 134 | cancelScheduledFeExpiringEvent(pnlsr,(*it).getFeExpiringEventId()); |
| 135 | //Remove entry from NDN-FIB |
| 136 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 137 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 138 | if ( fibTable.size() > 0 ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 139 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 140 | fibTable.clear(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 141 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 142 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 143 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 144 | int |
| 145 | Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix) |
| 146 | { |
| 147 | int endFace=0; |
| 148 | if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix)) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 149 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 150 | return nextHopList.getNhlSize(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 151 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 152 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 153 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 154 | return maxFacesPerPrefix; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 155 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 156 | return endFace; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | Fib::removeFibEntryHop(Nlsr& pnlsr, Nhl& nl, int doNotRemoveHopFaceId) |
| 161 | { |
| 162 | for( std::list<NextHop >::iterator it=nl.getNextHopList().begin(); |
| 163 | it != nl.getNextHopList().end(); ++it) |
| 164 | { |
| 165 | if ( it->getConnectingFace() != doNotRemoveHopFaceId ) |
| 166 | { |
| 167 | //Remove FIB Entry from NDN-FIB |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | Fib::printFib() |
| 174 | { |
| 175 | cout<<"-------------------FIB-----------------------------"<<endl; |
| 176 | for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end(); |
| 177 | ++it) |
| 178 | { |
| 179 | cout<<(*it); |
| 180 | } |
| 181 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 182 | |
| 183 | } //namespace nlsr |