akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * |
| 22 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <list> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 24 | #include <cmath> |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/common.hpp> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 26 | |
| 27 | #include "nlsr.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 28 | #include "nexthop-list.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 29 | #include "face-map.hpp" |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 30 | #include "fib.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | #include "logger.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | |
| 34 | |
| 35 | namespace nlsr { |
| 36 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 37 | INIT_LOGGER("Fib"); |
| 38 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 39 | const uint64_t Fib::GRACE_PERIOD = 10; |
| 40 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | using namespace std; |
| 42 | using namespace ndn; |
| 43 | |
| 44 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 45 | fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 47 | return fibEntry.getName() == name ; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 51 | Fib::cancelScheduledExpiringEvent(EventId eid) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 53 | m_nlsr.getScheduler().cancelEvent(eid); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | ndn::EventId |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 58 | Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 59 | const ndn::time::seconds& expTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | { |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 61 | _LOG_DEBUG("Fib::scheduleEntryExpiration Called"); |
| 62 | _LOG_INFO("Name: " << name << " Seq Num: " << feSeqNum); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 63 | return m_nlsr.getScheduler().scheduleEvent(expTime, |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 64 | ndn::bind(&Fib::remove, this, name)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 68 | Fib::remove(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 70 | _LOG_DEBUG("Fib::remove called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 72 | m_table.end(), |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 73 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 74 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 75 | for (std::list<NextHop>::iterator nhit = |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 76 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 77 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | //remove entry from NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 79 | if (isPrefixUpdatable(it->getName())) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 80 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 81 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 82 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 83 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << name); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 84 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | m_table.erase(it); |
| 86 | } |
| 87 | } |
| 88 | |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 89 | bool |
| 90 | compareFaceUri(const NextHop& hop, const std::string& faceUri) |
| 91 | { |
| 92 | return hop.getConnectingFaceUri() == faceUri; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& nextHopList) |
| 97 | { |
| 98 | const ndn::Name& name = entry.getName(); |
| 99 | |
| 100 | nextHopList.sort(); |
| 101 | |
| 102 | int numFaces = 0; |
| 103 | int maxFaces = getNumberOfFacesForName(nextHopList, |
| 104 | m_nlsr.getConfParameter().getMaxFacesPerPrefix()); |
| 105 | |
| 106 | for (NexthopList::iterator hopIt = nextHopList.begin(); |
| 107 | hopIt != nextHopList.end() && numFaces < maxFaces; ++hopIt, ++numFaces) |
| 108 | { |
| 109 | // Add nexthop to FIB entry |
| 110 | entry.getNexthopList().addNextHop(*hopIt); |
| 111 | |
| 112 | if (isPrefixUpdatable(name)) { |
| 113 | // Add nexthop to NDN-FIB |
| 114 | registerPrefix(name, hopIt->getConnectingFaceUri(), |
| 115 | std::ceil(hopIt->getRouteCost()), |
| 116 | ndn::time::seconds(m_refreshTime + GRACE_PERIOD), |
| 117 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, NexthopList& newHopList) |
| 124 | { |
| 125 | _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called"); |
| 126 | const ndn::Name& name = entry.getName(); |
| 127 | NexthopList& entryHopList = entry.getNexthopList(); |
| 128 | NexthopList itHopList = entryHopList; |
| 129 | |
| 130 | for (NexthopList::iterator it = itHopList.begin(); it != itHopList.end(); ++it) { |
| 131 | |
| 132 | // See if the nexthop is in the new nexthop list |
| 133 | const std::string& faceUri = it->getConnectingFaceUri(); |
| 134 | NexthopList::iterator foundIt = std::find_if(newHopList.begin(), |
| 135 | newHopList.end(), |
| 136 | bind(&compareFaceUri, _1, faceUri)); |
| 137 | |
| 138 | // The next hop is not in the new nexthop list |
| 139 | if (foundIt == newHopList.end()) { |
| 140 | // Remove the next hop from the FIB entry |
| 141 | _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name); |
| 142 | entryHopList.removeNextHop(*it); |
| 143 | |
| 144 | if (isPrefixUpdatable(name)) { |
| 145 | // Remove the nexthop from NDN-FIB |
| 146 | unregisterPrefix(name, it->getConnectingFaceUri()); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | |
| 152 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 153 | Fib::update(const ndn::Name& name, NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 155 | _LOG_DEBUG("Fib::updateFib Called"); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 156 | |
| 157 | std::list<FibEntry>::iterator entryIt = std::find_if(m_table.begin(), |
| 158 | m_table.end(), |
| 159 | bind(&fibEntryNameCompare, _1, name)); |
| 160 | // New FIB entry |
| 161 | if (entryIt == m_table.end()) { |
| 162 | _LOG_DEBUG("New FIB Entry"); |
| 163 | |
| 164 | // Don't create an entry for a name with no nexthops |
| 165 | if (nextHopList.getSize() == 0) { |
| 166 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 168 | |
| 169 | FibEntry entry(name); |
| 170 | |
| 171 | addNextHopsToFibEntryAndNfd(entry, nextHopList); |
| 172 | |
| 173 | entry.getNexthopList().sort(); |
| 174 | |
| 175 | // Set entry's expiration time point and sequence number |
| 176 | entry.setExpirationTimePoint(ndn::time::system_clock::now() + |
| 177 | ndn::time::seconds(m_refreshTime)); |
| 178 | entry.setSeqNo(1); |
| 179 | |
| 180 | // Schedule entry to be refreshed |
| 181 | entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(), |
| 182 | ndn::time::seconds(m_refreshTime))); |
| 183 | m_table.push_back(entry); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 185 | else { |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 186 | // Existing FIB entry |
| 187 | _LOG_DEBUG("Existing FIB Entry"); |
| 188 | |
| 189 | FibEntry& entry = *entryIt; |
| 190 | |
| 191 | // Remove empty FIB entry |
| 192 | if (nextHopList.getSize() == 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 193 | remove(name); |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 194 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | } |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame^] | 196 | |
| 197 | addNextHopsToFibEntryAndNfd(entry, nextHopList); |
| 198 | removeOldNextHopsFromFibEntryAndNfd(entry, nextHopList); |
| 199 | |
| 200 | entry.getNexthopList().sort(); |
| 201 | |
| 202 | // Set entry's expiration time point |
| 203 | entry.setExpirationTimePoint(ndn::time::system_clock::now() + |
| 204 | ndn::time::seconds(m_refreshTime)); |
| 205 | // Increment sequence number |
| 206 | entry.setSeqNo(entry.getSeqNo() + 1); |
| 207 | |
| 208 | // Cancel previosuly scheduled event |
| 209 | m_nlsr.getScheduler().cancelEvent(entry.getExpiringEventId()); |
| 210 | |
| 211 | // Schedule entry to be refreshed |
| 212 | entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(), |
| 213 | ndn::time::seconds(m_refreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 217 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 218 | Fib::clean() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 219 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 220 | _LOG_DEBUG("Fib::clean called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 221 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 222 | ++it) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 223 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 224 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 225 | for (std::list<NextHop>::iterator nhit = |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 226 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 227 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 228 | //Remove entry from NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 229 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | } |
| 231 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 232 | if (m_table.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 233 | m_table.clear(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | int |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 238 | Fib::getNumberOfFacesForName(NexthopList& nextHopList, |
| 239 | uint32_t maxFacesPerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | { |
| 241 | int endFace = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 242 | if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 243 | return nextHopList.getSize(); |
| 244 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 245 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | return maxFacesPerPrefix; |
| 247 | } |
| 248 | return endFace; |
| 249 | } |
| 250 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 251 | bool |
| 252 | Fib::isPrefixUpdatable(const ndn::Name& name) { |
| 253 | if (!m_nlsr.getAdjacencyList().isNeighbor(name)) { |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 260 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 261 | Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 262 | const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 263 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 264 | for (std::list<NextHop>::iterator it = nl.getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 265 | it != nl.getNextHops().end(); ++it) { |
| 266 | if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 267 | //Remove FIB Entry from NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 268 | if (isPrefixUpdatable(name)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 269 | unregisterPrefix(name, it->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 270 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 276 | Fib::createFace(const std::string& faceUri, |
| 277 | const CommandSucceedCallback& onSuccess, |
| 278 | const CommandFailCallback& onFailure) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 279 | { |
| 280 | ndn::nfd::ControlParameters faceParameters; |
| 281 | faceParameters |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 282 | .setUri(faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 283 | m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 284 | onSuccess, |
| 285 | onFailure); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | Fib::destroyFace(const std::string& faceUri, |
| 290 | const CommandSucceedCallback& onSuccess, |
| 291 | const CommandFailCallback& onFailure) |
| 292 | { |
| 293 | createFace(faceUri, |
| 294 | ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure), |
| 295 | onFailure); |
| 296 | } |
| 297 | |
| 298 | void |
| 299 | Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult, |
| 300 | const CommandSucceedCallback& onSuccess, |
| 301 | const CommandFailCallback& onFailure) |
| 302 | { |
| 303 | ndn::nfd::ControlParameters faceParameters; |
| 304 | faceParameters |
| 305 | .setFaceId(faceDestroyResult.getFaceId()); |
| 306 | m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters, |
| 307 | onSuccess, |
| 308 | onFailure); |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 313 | uint64_t faceCost, const ndn::time::milliseconds& timeout, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 314 | uint64_t flags, uint8_t times) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 315 | { |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 316 | uint64_t faceId = m_nlsr.getAdjacencyList().getFaceId(faceUri); |
| 317 | if (faceId != 0) { |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 318 | ndn::nfd::ControlParameters faceParameters; |
| 319 | faceParameters |
| 320 | .setName(namePrefix) |
| 321 | .setFaceId(faceId) |
| 322 | .setFlags(flags) |
| 323 | .setCost(faceCost) |
| 324 | .setExpirationPeriod(timeout) |
| 325 | .setOrigin(128); |
| 326 | |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 327 | _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri |
| 328 | << " Face Id: " << faceId); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 329 | registerPrefixInNfd(faceParameters, faceUri, times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 330 | } |
| 331 | else { |
| 332 | _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri); |
| 333 | } |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void |
| 337 | Fib::registerPrefix(const ndn::Name& namePrefix, |
| 338 | const std::string& faceUri, |
akmhoque | bf11c5f | 2014-07-21 14:49:47 -0500 | [diff] [blame] | 339 | uint64_t faceCost, |
| 340 | const ndn::time::milliseconds& timeout, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 341 | uint64_t flags, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 342 | uint8_t times, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 343 | const CommandSucceedCallback& onSuccess, |
| 344 | const CommandFailCallback& onFailure) |
| 345 | |
| 346 | { |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 347 | ndn::nfd::ControlParameters parameters; |
| 348 | parameters |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 349 | .setName(namePrefix) |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 350 | .setFlags(flags) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 351 | .setCost(faceCost) |
akmhoque | bf11c5f | 2014-07-21 14:49:47 -0500 | [diff] [blame] | 352 | .setExpirationPeriod(timeout) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 353 | .setOrigin(128); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 354 | createFace(faceUri, |
| 355 | ndn::bind(&Fib::registerPrefixInNfd, this,_1, |
| 356 | parameters, |
| 357 | times, onSuccess, onFailure), |
| 358 | onFailure); |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters, |
| 363 | const std::string& faceUri, |
| 364 | uint8_t times) |
| 365 | { |
| 366 | m_controller.start<ndn::nfd::RibRegisterCommand>(parameters, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 367 | ndn::bind(&Fib::onRegistration, this, _1, |
| 368 | "Successful in name registration", |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 369 | faceUri), |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 370 | ndn::bind(&Fib::onRegistrationFailure, |
| 371 | this, _1, _2, |
| 372 | "Failed in name registration", |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 373 | parameters, |
| 374 | faceUri, times)); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 375 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 376 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 377 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 378 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 379 | const ndn::nfd::ControlParameters& parameters, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 380 | uint8_t times, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 381 | const CommandSucceedCallback& onSuccess, |
| 382 | const CommandFailCallback& onFailure) |
| 383 | { |
| 384 | ndn::nfd::ControlParameters controlParameters; |
| 385 | controlParameters |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 386 | .setName(parameters.getName()) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 387 | .setFaceId(faceCreateResult.getFaceId()) |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 388 | .setCost(parameters.getCost()) |
| 389 | .setFlags(parameters.getFlags()) |
| 390 | .setExpirationPeriod(parameters.getExpirationPeriod()) |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 391 | .setOrigin(128); |
| 392 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
| 393 | onSuccess, |
| 394 | onFailure); |
| 395 | } |
| 396 | |
| 397 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 398 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 399 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 400 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 401 | _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 402 | if (faceId > 0) { |
| 403 | ndn::nfd::ControlParameters controlParameters; |
| 404 | controlParameters |
| 405 | .setName(namePrefix) |
| 406 | .setFaceId(faceId) |
| 407 | .setOrigin(128); |
| 408 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 409 | ndn::bind(&Fib::onUnregistration, this, _1, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 410 | "Successful in unregistering name"), |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 411 | ndn::bind(&Fib::onUnregistrationFailure, |
| 412 | this, _1, _2, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 413 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 414 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 418 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 419 | { |
| 420 | ndn::nfd::ControlParameters parameters; |
| 421 | parameters |
| 422 | .setName(name) |
| 423 | .setStrategy(strategy); |
| 424 | |
| 425 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 426 | bind(&Fib::onSetStrategySuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 427 | "Successfully set strategy choice"), |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 428 | bind(&Fib::onSetStrategyFailure, this, _1, _2, |
| 429 | parameters, |
| 430 | count, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 431 | "Failed to set strategy choice")); |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 436 | const std::string& message, const std::string& faceUri) |
| 437 | { |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 438 | _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() << |
| 439 | " Face Uri: " << faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 440 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 441 | m_faceMap.writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 442 | } |
| 443 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 444 | void |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 445 | Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 446 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 447 | { |
akmhoque | b5b3b4f | 2014-07-23 16:36:51 -0500 | [diff] [blame] | 448 | _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() << |
| 449 | " Face Id: " << commandSuccessResult.getFaceId()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 453 | Fib::onRegistrationFailure(uint32_t code, const std::string& error, |
| 454 | const std::string& message, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 455 | const ndn::nfd::ControlParameters& parameters, |
| 456 | const std::string& faceUri, |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 457 | uint8_t times) |
| 458 | { |
| 459 | _LOG_DEBUG(message << ": " << error << " (code: " << code << ")"); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 460 | _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 461 | if (times < 3) { |
| 462 | _LOG_DEBUG("Trying to register again..."); |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 463 | registerPrefix(parameters.getName(), faceUri, |
| 464 | parameters.getCost(), |
| 465 | parameters.getExpirationPeriod(), |
| 466 | parameters.getFlags(), times+1); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 467 | } |
| 468 | else { |
| 469 | _LOG_DEBUG("Registration trial given up"); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void |
| 474 | Fib::onUnregistrationFailure(uint32_t code, const std::string& error, |
| 475 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 476 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 477 | _LOG_DEBUG(message << ": " << error << " (code: " << code << ")"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 478 | } |
| 479 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 480 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 481 | Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 482 | const std::string& message) |
| 483 | { |
| 484 | _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " " |
| 485 | << "for name: " << commandSuccessResult.getName()); |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | Fib::onSetStrategyFailure(uint32_t code, const std::string& error, |
| 490 | const ndn::nfd::ControlParameters& parameters, |
| 491 | uint32_t count, |
| 492 | const std::string& message) |
| 493 | { |
| 494 | _LOG_DEBUG(message << ": " << parameters.getStrategy() << " " |
| 495 | << "for name: " << parameters.getName()); |
| 496 | if (count < 3) { |
| 497 | setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 502 | Fib::writeLog() |
| 503 | { |
| 504 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
| 505 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
| 506 | ++it) { |
| 507 | (*it).writeLog(); |
| 508 | } |
| 509 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 510 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 511 | } //namespace nlsr |