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