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" |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 6 | #include "utility/nlsr_logger.hpp" |
| 7 | |
| 8 | #define THIS_FILE "nlsr_fib.cpp" |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 9 | |
| 10 | namespace nlsr |
| 11 | { |
| 12 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 13 | using namespace std; |
| 14 | using namespace ndn; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 15 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 16 | static bool |
| 17 | fibEntryNameCompare(FibEntry& fe, string name) |
| 18 | { |
| 19 | return fe.getName() == name ; |
| 20 | } |
| 21 | |
| 22 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 23 | Fib::cancelScheduledExpiringEvent(Nlsr& pnlsr, EventId eid) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 24 | { |
| 25 | pnlsr.getScheduler().cancelEvent(eid); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | ndn::EventId |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 30 | Fib::scheduleEntryRefreshing(Nlsr& pnlsr, string name, int feSeqNum, |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 31 | int refreshTime) |
| 32 | { |
| 33 | return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime), |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 34 | ndn::bind(&Fib::refreshEntry,this,name,feSeqNum)); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 38 | Fib::refreshEntry(string name, int feSeqNum) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 43 | Fib::remove(Nlsr& pnlsr, string name) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 44 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 45 | 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() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 48 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 49 | 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 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 54 | cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId()); |
| 55 | m_table.erase(it); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 56 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 57 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 58 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 59 | |
| 60 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 61 | Fib::update(Nlsr& pnlsr,string name, Nhl& nextHopList) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 62 | { |
| 63 | std::cout<<"Fib::updateFib Called"<<std::endl; |
| 64 | int startFace=0; |
| 65 | int endFace=getNumberOfFacesForName(nextHopList, |
| 66 | pnlsr.getConfParameter().getMaxFacesPerPrefix()); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 67 | std::list<FibEntry >::iterator it = std::find_if( m_table.begin(), |
| 68 | m_table.end(), |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 69 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 70 | if( it == m_table.end() ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 71 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 72 | if( nextHopList.getSize() > 0 ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 73 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 74 | nextHopList.sort(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 75 | 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++) |
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().addNextHop((*nhit)); |
| 81 | //Add entry to NDN-FIB |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 82 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 83 | 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); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 89 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 90 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 91 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 92 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 93 | std::cout<<"Old FIB Entry"<<std::endl; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 94 | if( nextHopList.getSize() > 0 ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 95 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 96 | nextHopList.sort(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 97 | if ( !it->isEqualNextHops(nextHopList) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 98 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 99 | std::list<NextHop> nhl=nextHopList.getNextHopList(); |
| 100 | std::list<NextHop>::iterator nhit=nhl.begin(); |
| 101 | // Add first Entry to NDN-FIB |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 102 | removeHop(pnlsr, it->getNhl(),nhit->getConnectingFace()); |
| 103 | it->getNhl().reset(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 104 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 112 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 113 | it->setTimeToRefresh(m_refreshTime); |
| 114 | cancelScheduledExpiringEvent(pnlsr, it->getExpiringEventId()); |
| 115 | it->setSeqNo(it->getSeqNo()+1); |
| 116 | (*it).setExpiringEventId(scheduleEntryRefreshing(pnlsr, |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 117 | it->getName() , |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 118 | it->getSeqNo(),m_refreshTime)); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 122 | remove(pnlsr,name); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 123 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 124 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 125 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 126 | |
| 127 | |
| 128 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 129 | void Fib::clean(Nlsr& pnlsr) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 130 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 131 | for( std::list<FibEntry >::iterator it=m_table.begin(); it != m_table.end(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 132 | ++it) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 133 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 134 | for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin(); |
| 135 | nhit != (*it).getNhl().getNextHopList().begin(); nhit++) |
| 136 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 137 | cancelScheduledExpiringEvent(pnlsr,(*it).getExpiringEventId()); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 138 | //Remove entry from NDN-FIB |
| 139 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 140 | } |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 141 | if ( m_table.size() > 0 ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 142 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 143 | m_table.clear(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 144 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 145 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 146 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 147 | int |
| 148 | Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix) |
| 149 | { |
| 150 | int endFace=0; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 151 | if((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 152 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 153 | return nextHopList.getSize(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 154 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 155 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 156 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 157 | return maxFacesPerPrefix; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 158 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 159 | return endFace; |
| 160 | } |
| 161 | |
| 162 | void |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 163 | Fib::removeHop(Nlsr& pnlsr, Nhl& nl, int doNotRemoveHopFaceId) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 164 | { |
| 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 |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 176 | Fib::print() |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 177 | { |
| 178 | cout<<"-------------------FIB-----------------------------"<<endl; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 179 | for(std::list<FibEntry>::iterator it = m_table.begin(); it!=m_table.end(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 180 | ++it) |
| 181 | { |
| 182 | cout<<(*it); |
| 183 | } |
| 184 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 185 | |
| 186 | } //namespace nlsr |