blob: 7d08e121ad496acdc2c0c4c3766598f9ed1e8267 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Nick Gordonb7168472016-09-21 13:57:17 -050020
akmhoquefdbddb12014-05-02 18:35:19 -050021#include "fib.hpp"
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050022#include "adjacency-list.hpp"
23#include "conf-parameter.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050024#include "logger.hpp"
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050025#include "nexthop-list.hpp"
26
27#include <map>
28#include <cmath>
laqinfancd7ca052017-06-02 04:57:18 -050029#include <algorithm>
30#include <iterator>
akmhoquec8a10f72014-04-25 18:42:55 -050031
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
dmcoomescf8d0ed2017-02-21 11:39:01 -060034INIT_LOGGER(route.Fib);
akmhoque674b0b12014-05-20 14:33:28 -050035
akmhoque393d4ff2014-07-16 14:27:03 -050036const uint64_t Fib::GRACE_PERIOD = 10;
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050037const std::string Fib::MULTICAST_STRATEGY("ndn:/localhost/nfd/strategy/multicast");
38const std::string Fib::BEST_ROUTE_V2_STRATEGY("ndn:/localhost/nfd/strategy/best-route");
akmhoque393d4ff2014-07-16 14:27:03 -050039
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060040Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
41 ConfParameter& conf, ndn::security::v2::KeyChain& keyChain)
42 : m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , m_refreshTime(2 * conf.getLsaRefreshTime())
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060044 , m_controller(face, keyChain)
45 , m_adjacencyList(adjacencyList)
46 , m_confParameter(conf)
47{
48}
49
akmhoque53353462014-04-22 08:43:45 -050050void
akmhoque31d1d4b2014-05-05 22:08:14 -050051Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050052{
dmcoomes5bcb39e2017-10-31 15:07:55 -050053 NLSR_LOG_DEBUG("Fib::remove called");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040054 auto it = m_table.find(name);
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060055
56 // Only unregister the prefix if it ISN'T a neighbor.
57 if (it != m_table.end() && isNotNeighbor((it->second).getName())) {
58 for (const auto& nexthop : (it->second).getNexthopList().getNextHops()) {
59 unregisterPrefix((it->second).getName(), nexthop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -050060 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050061 cancelEntryRefresh(it->second);
akmhoque53353462014-04-22 08:43:45 -050062 m_table.erase(it);
63 }
64}
65
Vince Lehman18841082014-08-19 17:15:24 -050066void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050067Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050068{
69 const ndn::Name& name = entry.getName();
70
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060071 bool shouldRegister = isNotNeighbor(name);
72
73 for (const auto& hop : hopsToAdd.getNextHops())
Vince Lehman18841082014-08-19 17:15:24 -050074 {
75 // Add nexthop to FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060076 entry.getNexthopList().addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050077
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060078 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050079 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060080 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
81 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050082 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
83 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
84 }
85 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050086}
87
88void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050089Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050090{
dmcoomes5bcb39e2017-10-31 15:07:55 -050091 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050092
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060093 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050094 // the length of the list of all next hops.
95 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050096
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 NexthopList hopsToAdd;
98 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050099
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400101 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500102 hopsToAdd.addNextHop(*it);
103 }
Vince Lehman18841082014-08-19 17:15:24 -0500104
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400105 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500106
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500107 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500108 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500109 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500110
Vince Lehman18841082014-08-19 17:15:24 -0500111 FibEntry entry(name);
112
Vince Lehman942eb7b2014-10-02 10:09:27 -0500113 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500114
Nick Gordonb7168472016-09-21 13:57:17 -0500115 m_table.emplace(name, entry);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500116
Nick Gordon5867b522017-09-06 17:41:37 -0500117 entryIt = m_table.find(name);
akmhoque53353462014-04-22 08:43:45 -0500118 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500119 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500120 else {
Vince Lehman18841082014-08-19 17:15:24 -0500121 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500122 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500123
124 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500125 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500127 return;
akmhoque53353462014-04-22 08:43:45 -0500128 }
Vince Lehman18841082014-08-19 17:15:24 -0500129
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500130 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500131 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500132
laqinfancd7ca052017-06-02 04:57:18 -0500133 std::set<NextHop, NextHopComparator> hopsToRemove;
134 std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
135 hopsToAdd.begin(), hopsToAdd.end(),
136 std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
137
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600138 bool isUpdatable = isNotNeighbor(entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500139 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600140 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500141 if (isUpdatable) {
142 unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
143 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500144 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500145 entry.getNexthopList().removeNextHop(hop);
146 }
Vince Lehman18841082014-08-19 17:15:24 -0500147
Vince Lehman18841082014-08-19 17:15:24 -0500148 // Increment sequence number
149 entry.setSeqNo(entry.getSeqNo() + 1);
150
Nick Gordon5867b522017-09-06 17:41:37 -0500151 entryIt = m_table.find(name);
152
153 }
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600154 if (entryIt != m_table.end() &&
155 !entryIt->second.getRefreshEventId() &&
156 isNotNeighbor(entryIt->second.getName())) {
Nick Gordon5867b522017-09-06 17:41:37 -0500157 scheduleEntryRefresh(entryIt->second,
158 [this] (FibEntry& entry) {
159 scheduleLoop(entry);
160 });
akmhoque53353462014-04-22 08:43:45 -0500161 }
162}
163
akmhoque53353462014-04-22 08:43:45 -0500164void
akmhoque31d1d4b2014-05-05 22:08:14 -0500165Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500166{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500167 NLSR_LOG_DEBUG("Fib::clean called");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600168 // can't use const ref here as getNexthopList can't be marked const
169 for (auto&& it : m_table) {
170 NLSR_LOG_DEBUG("Canceling Scheduled event. Name: " << it.second.getName());
171 cancelEntryRefresh(it.second);
172
173 for (const auto& hop : it.second.getNexthopList().getNextHops()) {
174 unregisterPrefix(it.second.getName(), hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500175 }
176 }
akmhoque53353462014-04-22 08:43:45 -0500177}
178
Vince Lehman942eb7b2014-10-02 10:09:27 -0500179unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500180Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500181{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500182 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
183 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
184
185 // Allow all faces
186 if (nMaxFaces == 0) {
187 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500188 }
akmhoque157b0a42014-05-13 00:26:37 -0500189 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500190 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500191 }
akmhoque53353462014-04-22 08:43:45 -0500192}
193
akmhoque393d4ff2014-07-16 14:27:03 -0500194bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600195Fib::isNotNeighbor(const ndn::Name& name)
196{
197 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500198}
199
akmhoque53353462014-04-22 08:43:45 -0500200void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500201Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500202 uint64_t faceCost,
203 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500204 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500205{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500206 uint64_t faceId = m_adjacencyList.getFaceId(ndn::FaceUri(faceUri));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500207
akmhoque102aea42014-08-04 10:22:12 -0500208 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500209 ndn::nfd::ControlParameters faceParameters;
210 faceParameters
211 .setName(namePrefix)
212 .setFaceId(faceId)
213 .setFlags(flags)
214 .setCost(faceCost)
215 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500216 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500217
dmcoomes5bcb39e2017-10-31 15:07:55 -0500218 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500219 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600220 std::bind(&Fib::onRegistrationSuccess, this, _1,
221 "Successful in name registration",
222 faceUri),
223 std::bind(&Fib::onRegistrationFailure, this, _1,
224 "Failed in name registration",
225 faceParameters,
226 faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500227 }
228 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500229 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500230 }
231}
232
233void
234Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500235 const std::string& message, const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500236{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500237 NLSR_LOG_DEBUG(message << ": " << commandSuccessResult.getName() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600238 " Face Uri: " << faceUri << " faceId: " << commandSuccessResult.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500239
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400240 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500241 if (adjacent != m_adjacencyList.end()) {
242 adjacent->setFaceId(commandSuccessResult.getFaceId());
243 }
244
245 // Update the fast-access FaceMap with the new Face ID, too
246 m_faceMap.update(faceUri.toString(), commandSuccessResult.getFaceId());
247 m_faceMap.writeLog();
248}
249
250void
251Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
252 const std::string& message,
253 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500254 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500255 uint8_t times)
256{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500257 NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
258 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500259 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500260 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500261 registerPrefix(parameters.getName(), faceUri,
262 parameters.getCost(),
263 parameters.getExpirationPeriod(),
264 parameters.getFlags(), times+1);
265 }
266 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500267 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500268 }
akmhoquefdbddb12014-05-02 18:35:19 -0500269}
akmhoque31d1d4b2014-05-05 22:08:14 -0500270
akmhoquefdbddb12014-05-02 18:35:19 -0500271void
akmhoque157b0a42014-05-13 00:26:37 -0500272Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500273{
akmhoque157b0a42014-05-13 00:26:37 -0500274 uint32_t faceId = m_faceMap.getFaceId(faceUri);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500275 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500276 if (faceId > 0) {
277 ndn::nfd::ControlParameters controlParameters;
278 controlParameters
279 .setName(namePrefix)
280 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500281 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500282 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500283 std::bind(&Fib::onUnregistrationSuccess, this, _1,
284 "Successful in unregistering name"),
285 std::bind(&Fib::onUnregistrationFailure,
286 this, _1,
287 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500288 }
akmhoquefdbddb12014-05-02 18:35:19 -0500289}
290
291void
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500292Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
293 const std::string& message)
294{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500295 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600296 " Face Id: " << commandSuccessResult.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500297}
298
299void
300Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
301 const std::string& message)
302{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500303 NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500304}
305
306void
akmhoque393d4ff2014-07-16 14:27:03 -0500307Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500308{
309 ndn::nfd::ControlParameters parameters;
310 parameters
311 .setName(name)
312 .setStrategy(strategy);
313
314 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400315 std::bind(&Fib::onSetStrategySuccess, this, _1),
dmcoomes9f936662017-03-02 10:33:09 -0600316 std::bind(&Fib::onSetStrategyFailure, this, _1,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400317 parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500318}
319
320void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400321Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500322{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400323 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600324 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500325}
326
327void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000328Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
329 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400330 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500331{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400332 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600333 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500334 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600335 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500336 }
337}
338
339void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500340Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
341{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600342 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.getName() <<
343 " Seq Num: " << entry.getSeqNo() <<
344 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500345
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400346 entry.setRefreshEventId(m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
347 std::bind(&Fib::refreshEntry, this,
348 entry.getName(), refreshCallback)));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500349}
350
351void
352Fib::scheduleLoop(FibEntry& entry)
353{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600354 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500355}
356
357void
358Fib::cancelEntryRefresh(const FibEntry& entry)
359{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600360 NLSR_LOG_DEBUG("Canceling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400361 entry.getRefreshEventId().cancel();
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500362}
363
364void
365Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
366{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400367 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500368 if (it == m_table.end()) {
369 return;
370 }
371
372 FibEntry& entry = it->second;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500373 NLSR_LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500374
375 // Increment sequence number
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600376 entry.setSeqNo(entry.getSeqNo() + 1);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500377
378 for (const NextHop& hop : entry) {
379 registerPrefix(entry.getName(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500380 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500381 hop.getRouteCostAsAdjustedInteger(),
382 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
383 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500384 }
385
386 refreshCb(entry);
387}
388
389void
akmhoque674b0b12014-05-20 14:33:28 -0500390Fib::writeLog()
391{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500392 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600393 for (const auto& entry : m_table) {
394 entry.second.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500395 }
396}
akmhoquefdbddb12014-05-02 18:35:19 -0500397
Nick Gordonfad8e252016-08-11 14:21:38 -0500398} // namespace nlsr