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