akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <list> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 2 | #include <cmath> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 3 | |
| 4 | #include "nlsr.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 5 | #include "nexthop-list.hpp" |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 6 | #include "fib.hpp" |
| 7 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 8 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 9 | |
| 10 | |
| 11 | namespace nlsr { |
| 12 | |
| 13 | using namespace std; |
| 14 | using namespace ndn; |
| 15 | |
| 16 | static bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 17 | fibEntryNameCompare(const FibEntry& fibEntry, const string& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 18 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 19 | return fibEntry.getName() == name ; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void |
| 23 | Fib::cancelScheduledExpiringEvent(Nlsr& pnlsr, EventId eid) |
| 24 | { |
| 25 | pnlsr.getScheduler().cancelEvent(eid); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | ndn::EventId |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 30 | Fib::scheduleEntryRefreshing(Nlsr& pnlsr, const string& name, int32_t feSeqNum, |
| 31 | int32_t refreshTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 33 | std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl; |
| 34 | std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 36 | ndn::bind(&Fib::refreshEntry, this, |
| 37 | boost::ref(pnlsr), |
| 38 | name, feSeqNum)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 42 | Fib::refreshEntry(Nlsr& nlsr, const string& name, int32_t feSeqNum) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 44 | std::cout << "Fib::refreshEntry Called" << std::endl; |
| 45 | std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl; |
| 46 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 47 | m_table.end(), |
| 48 | bind(&fibEntryNameCompare, _1, name)); |
| 49 | if (it != m_table.end()) |
| 50 | { |
| 51 | std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl; |
| 52 | if (it->getSeqNo() == feSeqNum) |
| 53 | { |
| 54 | std::cout << "Refreshing the FIB entry" << std::endl; |
| 55 | for (std::list<NextHop>::iterator nhit = |
| 56 | (*it).getNexthopList().getNextHops().begin(); |
| 57 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) |
| 58 | { |
| 59 | // add entry to NDN-FIB |
| 60 | registerPrefixInNfd(it->getName(), nhit->getConnectingFace(), std::ceil(nhit->getRouteCost())); |
| 61 | } |
| 62 | |
| 63 | // increase sequence number and schedule refresh again |
| 64 | it->setSeqNo(feSeqNum + 1); |
| 65 | it->setExpiringEventId(scheduleEntryRefreshing(nlsr, |
| 66 | it->getName() , |
| 67 | it->getSeqNo(), |
| 68 | m_refreshTime)); |
| 69 | |
| 70 | } |
| 71 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 75 | Fib::remove(Nlsr& pnlsr, const std::string& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | { |
| 77 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 78 | m_table.end(), |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 79 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | if (it != m_table.end()) |
| 81 | { |
| 82 | for (std::list<NextHop>::iterator nhit = |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 83 | (*it).getNexthopList().getNextHops().begin(); |
| 84 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | { |
| 86 | //remove entry from NDN-FIB |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 87 | if (!pnlsr.getAdjacencyList().isNeighbor(it->getName())) |
| 88 | { |
| 89 | unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace()); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | if(pnlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() != |
| 94 | nhit->getConnectingFace()) |
| 95 | { |
| 96 | unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace()); |
| 97 | } |
| 98 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 100 | std::cout << "Cancellling Scheduled event" << std::endl; |
| 101 | std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId()); |
| 103 | m_table.erase(it); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 109 | Fib::update(Nlsr& pnlsr, const string& name, NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 110 | { |
| 111 | std::cout << "Fib::updateFib Called" << std::endl; |
| 112 | int startFace = 0; |
| 113 | int endFace = getNumberOfFacesForName(nextHopList, |
| 114 | pnlsr.getConfParameter().getMaxFacesPerPrefix()); |
| 115 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 116 | m_table.end(), |
| 117 | bind(&fibEntryNameCompare, _1, name)); |
| 118 | if (it == m_table.end()) |
| 119 | { |
| 120 | if (nextHopList.getSize() > 0) |
| 121 | { |
| 122 | nextHopList.sort(); |
| 123 | FibEntry newEntry(name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 124 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | std::list<NextHop>::iterator nhit = nhl.begin(); |
| 126 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) |
| 127 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 128 | newEntry.getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | //Add entry to NDN-FIB |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 130 | registerPrefixInNfd(name, nhit->getConnectingFace(), std::ceil(nhit->getRouteCost())); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 132 | newEntry.getNexthopList().sort(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | newEntry.setTimeToRefresh(m_refreshTime); |
| 134 | newEntry.setSeqNo(1); |
| 135 | newEntry.setExpiringEventId(scheduleEntryRefreshing(pnlsr, |
| 136 | name , 1, m_refreshTime)); |
| 137 | m_table.push_back(newEntry); |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | std::cout << "Old FIB Entry" << std::endl; |
| 143 | if (nextHopList.getSize() > 0) |
| 144 | { |
| 145 | nextHopList.sort(); |
| 146 | if (!it->isEqualNextHops(nextHopList)) |
| 147 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 148 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 149 | std::list<NextHop>::iterator nhit = nhl.begin(); |
| 150 | // Add first Entry to NDN-FIB |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 151 | registerPrefixInNfd(name, nhit->getConnectingFace(), std::ceil(nhit->getRouteCost())); |
| 152 | removeHop(pnlsr, it->getNexthopList(), nhit->getConnectingFace(), name); |
| 153 | it->getNexthopList().reset(); |
| 154 | it->getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 155 | ++startFace; |
| 156 | ++nhit; |
| 157 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) |
| 158 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 159 | it->getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 160 | //Add Entry to NDN_FIB |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 161 | registerPrefixInNfd(name, nhit->getConnectingFace(), std::ceil(nhit->getRouteCost())); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | it->setTimeToRefresh(m_refreshTime); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 165 | std::cout << "Cancellling Scheduled event" << std::endl; |
| 166 | std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | cancelScheduledExpiringEvent(pnlsr, it->getExpiringEventId()); |
| 168 | it->setSeqNo(it->getSeqNo() + 1); |
| 169 | (*it).setExpiringEventId(scheduleEntryRefreshing(pnlsr, |
| 170 | it->getName() , |
| 171 | it->getSeqNo(), m_refreshTime)); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | remove(pnlsr, name); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | void |
| 183 | Fib::clean(Nlsr& pnlsr) |
| 184 | { |
| 185 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
| 186 | ++it) |
| 187 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 188 | std::cout << "Cancellling Scheduled event" << std::endl; |
| 189 | std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() << std::endl; |
| 190 | cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | for (std::list<NextHop>::iterator nhit = |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 192 | (*it).getNexthopList().getNextHops().begin(); |
| 193 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 194 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | //Remove entry from NDN-FIB |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 196 | if (!pnlsr.getAdjacencyList().isNeighbor(it->getName())) |
| 197 | { |
| 198 | unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace()); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | if(pnlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() != |
| 203 | nhit->getConnectingFace()) |
| 204 | { |
| 205 | unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace()); |
| 206 | } |
| 207 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | if (m_table.size() > 0) |
| 211 | { |
| 212 | m_table.clear(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 217 | Fib::getNumberOfFacesForName(NexthopList& nextHopList, uint32_t maxFacesPerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | { |
| 219 | int endFace = 0; |
| 220 | if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) |
| 221 | { |
| 222 | return nextHopList.getSize(); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | return maxFacesPerPrefix; |
| 227 | } |
| 228 | return endFace; |
| 229 | } |
| 230 | |
| 231 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 232 | Fib::removeHop(Nlsr& pnlsr, NexthopList& nl, uint32_t doNotRemoveHopFaceId, |
| 233 | const std::string& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 234 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 235 | for (std::list<NextHop>::iterator it = nl.getNextHops().begin(); |
| 236 | it != nl.getNextHops().end(); ++it) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 237 | { |
| 238 | if (it->getConnectingFace() != doNotRemoveHopFaceId) |
| 239 | { |
| 240 | //Remove FIB Entry from NDN-FIB |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 241 | if (!pnlsr.getAdjacencyList().isNeighbor(name)) |
| 242 | { |
| 243 | unregisterPrefixFromNfd(name, it->getConnectingFace()); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | if(pnlsr.getAdjacencyList().getAdjacent(name).getConnectingFace() != |
| 248 | it->getConnectingFace()) |
| 249 | { |
| 250 | unregisterPrefixFromNfd(name, it->getConnectingFace()); |
| 251 | } |
| 252 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 258 | Fib::registerPrefixInNfd(const std::string& namePrefix, uint64_t faceId, uint64_t faceCost) |
| 259 | { |
| 260 | ndn::nfd::ControlParameters controlParameters; |
| 261 | controlParameters |
| 262 | .setName(namePrefix) |
| 263 | .setCost(faceCost) |
| 264 | .setFaceId(faceId) |
| 265 | .setExpirationPeriod(ndn::time::milliseconds(m_refreshTime*1000)) |
| 266 | .setOrigin(128); |
| 267 | |
| 268 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
| 269 | ndn::bind(&Fib::onSuccess, this, _1, |
| 270 | "Successful in name registration"), |
| 271 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 272 | "Failed in name registration")); |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | Fib::unregisterPrefixFromNfd(const std::string& namePrefix, uint64_t faceId) |
| 277 | { |
| 278 | ndn::nfd::ControlParameters controlParameters; |
| 279 | controlParameters |
| 280 | .setName(namePrefix) |
| 281 | .setFaceId(faceId) |
| 282 | .setOrigin(128); |
| 283 | |
| 284 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
| 285 | ndn::bind(&Fib::onSuccess, this, _1, |
| 286 | "Successful in unregistering name"), |
| 287 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 288 | "Failed in unregistering name")); |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, const std::string& message) |
| 293 | { |
| 294 | std::cout << message << ": " << commandSuccessResult << std::endl; |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | Fib::onFailure(uint32_t code, const std::string& error, const std::string& message) |
| 299 | { |
| 300 | std::cout << message << ": " << error << " (code: " << code << ")"; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 305 | Fib::print() |
| 306 | { |
| 307 | cout << "-------------------FIB-----------------------------" << endl; |
| 308 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
| 309 | ++it) |
| 310 | { |
| 311 | cout << (*it); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | } //namespace nlsr |