blob: 5966108bc347fdacb862d965d0ce9376918283af [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;
37
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060038Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
39 ConfParameter& conf, ndn::security::v2::KeyChain& keyChain)
40 : m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041 , m_refreshTime(2 * conf.getLsaRefreshTime())
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060042 , m_controller(face, keyChain)
43 , m_adjacencyList(adjacencyList)
44 , m_confParameter(conf)
45{
46}
47
akmhoque53353462014-04-22 08:43:45 -050048void
akmhoque31d1d4b2014-05-05 22:08:14 -050049Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050050{
dmcoomes5bcb39e2017-10-31 15:07:55 -050051 NLSR_LOG_DEBUG("Fib::remove called");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040052 auto it = m_table.find(name);
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060053
54 // Only unregister the prefix if it ISN'T a neighbor.
55 if (it != m_table.end() && isNotNeighbor((it->second).getName())) {
56 for (const auto& nexthop : (it->second).getNexthopList().getNextHops()) {
57 unregisterPrefix((it->second).getName(), nexthop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -050058 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050059 cancelEntryRefresh(it->second);
akmhoque53353462014-04-22 08:43:45 -050060 m_table.erase(it);
61 }
62}
63
Vince Lehman18841082014-08-19 17:15:24 -050064void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050065Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050066{
67 const ndn::Name& name = entry.getName();
68
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060069 bool shouldRegister = isNotNeighbor(name);
70
71 for (const auto& hop : hopsToAdd.getNextHops())
Vince Lehman18841082014-08-19 17:15:24 -050072 {
73 // Add nexthop to FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060074 entry.getNexthopList().addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050075
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060076 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050077 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060078 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
79 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050080 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
81 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
82 }
83 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050084}
85
86void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050087Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050088{
dmcoomes5bcb39e2017-10-31 15:07:55 -050089 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050090
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060091 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050092 // the length of the list of all next hops.
93 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050094
Vince Lehman942eb7b2014-10-02 10:09:27 -050095 NexthopList hopsToAdd;
96 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050097
Vince Lehman942eb7b2014-10-02 10:09:27 -050098 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040099 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100 hopsToAdd.addNextHop(*it);
101 }
Vince Lehman18841082014-08-19 17:15:24 -0500102
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400103 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500104
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500105 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500106 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500107 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500108
Vince Lehman18841082014-08-19 17:15:24 -0500109 FibEntry entry(name);
110
Vince Lehman942eb7b2014-10-02 10:09:27 -0500111 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500112
Nick Gordonb7168472016-09-21 13:57:17 -0500113 m_table.emplace(name, entry);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500114
Nick Gordon5867b522017-09-06 17:41:37 -0500115 entryIt = m_table.find(name);
akmhoque53353462014-04-22 08:43:45 -0500116 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500117 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500118 else {
Vince Lehman18841082014-08-19 17:15:24 -0500119 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500120 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500121
122 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500123 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500125 return;
akmhoque53353462014-04-22 08:43:45 -0500126 }
Vince Lehman18841082014-08-19 17:15:24 -0500127
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500128 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500129 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500130
laqinfancd7ca052017-06-02 04:57:18 -0500131 std::set<NextHop, NextHopComparator> hopsToRemove;
132 std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
133 hopsToAdd.begin(), hopsToAdd.end(),
134 std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
135
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600136 bool isUpdatable = isNotNeighbor(entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500137 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600138 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500139 if (isUpdatable) {
140 unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
141 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500142 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500143 entry.getNexthopList().removeNextHop(hop);
144 }
Vince Lehman18841082014-08-19 17:15:24 -0500145
Vince Lehman18841082014-08-19 17:15:24 -0500146 // Increment sequence number
147 entry.setSeqNo(entry.getSeqNo() + 1);
148
Nick Gordon5867b522017-09-06 17:41:37 -0500149 entryIt = m_table.find(name);
150
151 }
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600152 if (entryIt != m_table.end() &&
153 !entryIt->second.getRefreshEventId() &&
154 isNotNeighbor(entryIt->second.getName())) {
Nick Gordon5867b522017-09-06 17:41:37 -0500155 scheduleEntryRefresh(entryIt->second,
156 [this] (FibEntry& entry) {
157 scheduleLoop(entry);
158 });
akmhoque53353462014-04-22 08:43:45 -0500159 }
160}
161
akmhoque53353462014-04-22 08:43:45 -0500162void
akmhoque31d1d4b2014-05-05 22:08:14 -0500163Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500164{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500165 NLSR_LOG_DEBUG("Fib::clean called");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600166 // can't use const ref here as getNexthopList can't be marked const
167 for (auto&& it : m_table) {
168 NLSR_LOG_DEBUG("Canceling Scheduled event. Name: " << it.second.getName());
169 cancelEntryRefresh(it.second);
170
171 for (const auto& hop : it.second.getNexthopList().getNextHops()) {
172 unregisterPrefix(it.second.getName(), hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500173 }
174 }
akmhoque53353462014-04-22 08:43:45 -0500175}
176
Vince Lehman942eb7b2014-10-02 10:09:27 -0500177unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500178Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500179{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500180 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
181 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
182
183 // Allow all faces
184 if (nMaxFaces == 0) {
185 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500186 }
akmhoque157b0a42014-05-13 00:26:37 -0500187 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500188 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500189 }
akmhoque53353462014-04-22 08:43:45 -0500190}
191
akmhoque393d4ff2014-07-16 14:27:03 -0500192bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600193Fib::isNotNeighbor(const ndn::Name& name)
194{
195 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500196}
197
akmhoque53353462014-04-22 08:43:45 -0500198void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500199Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500200 uint64_t faceCost,
201 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500202 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500203{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500204 uint64_t faceId = m_adjacencyList.getFaceId(ndn::FaceUri(faceUri));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500205
akmhoque102aea42014-08-04 10:22:12 -0500206 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500207 ndn::nfd::ControlParameters faceParameters;
208 faceParameters
209 .setName(namePrefix)
210 .setFaceId(faceId)
211 .setFlags(flags)
212 .setCost(faceCost)
213 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500214 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500215
dmcoomes5bcb39e2017-10-31 15:07:55 -0500216 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500217 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600218 std::bind(&Fib::onRegistrationSuccess, this, _1,
219 "Successful in name registration",
220 faceUri),
221 std::bind(&Fib::onRegistrationFailure, this, _1,
222 "Failed in name registration",
223 faceParameters,
224 faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500225 }
226 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500227 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500228 }
229}
230
231void
232Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500233 const std::string& message, const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500234{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500235 NLSR_LOG_DEBUG(message << ": " << commandSuccessResult.getName() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600236 " Face Uri: " << faceUri << " faceId: " << commandSuccessResult.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500237
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400238 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500239 if (adjacent != m_adjacencyList.end()) {
240 adjacent->setFaceId(commandSuccessResult.getFaceId());
241 }
242
243 // Update the fast-access FaceMap with the new Face ID, too
244 m_faceMap.update(faceUri.toString(), commandSuccessResult.getFaceId());
245 m_faceMap.writeLog();
246}
247
248void
249Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
250 const std::string& message,
251 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500252 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500253 uint8_t times)
254{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500255 NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
256 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500257 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500258 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500259 registerPrefix(parameters.getName(), faceUri,
260 parameters.getCost(),
261 parameters.getExpirationPeriod(),
262 parameters.getFlags(), times+1);
263 }
264 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500265 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500266 }
akmhoquefdbddb12014-05-02 18:35:19 -0500267}
akmhoque31d1d4b2014-05-05 22:08:14 -0500268
akmhoquefdbddb12014-05-02 18:35:19 -0500269void
akmhoque157b0a42014-05-13 00:26:37 -0500270Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500271{
akmhoque157b0a42014-05-13 00:26:37 -0500272 uint32_t faceId = m_faceMap.getFaceId(faceUri);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500273 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500274 if (faceId > 0) {
275 ndn::nfd::ControlParameters controlParameters;
276 controlParameters
277 .setName(namePrefix)
278 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500279 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500280 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500281 std::bind(&Fib::onUnregistrationSuccess, this, _1,
282 "Successful in unregistering name"),
283 std::bind(&Fib::onUnregistrationFailure,
284 this, _1,
285 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500286 }
akmhoquefdbddb12014-05-02 18:35:19 -0500287}
288
289void
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500290Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
291 const std::string& message)
292{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500293 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600294 " Face Id: " << commandSuccessResult.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500295}
296
297void
298Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
299 const std::string& message)
300{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500301 NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500302}
303
304void
akmhoque393d4ff2014-07-16 14:27:03 -0500305Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500306{
307 ndn::nfd::ControlParameters parameters;
308 parameters
309 .setName(name)
310 .setStrategy(strategy);
311
312 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400313 std::bind(&Fib::onSetStrategySuccess, this, _1),
dmcoomes9f936662017-03-02 10:33:09 -0600314 std::bind(&Fib::onSetStrategyFailure, this, _1,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400315 parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500316}
317
318void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400319Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500320{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400321 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600322 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500323}
324
325void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000326Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
327 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400328 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500329{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400330 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600331 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500332 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600333 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500334 }
335}
336
337void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500338Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
339{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600340 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.getName() <<
341 " Seq Num: " << entry.getSeqNo() <<
342 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500343
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400344 entry.setRefreshEventId(m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
345 std::bind(&Fib::refreshEntry, this,
346 entry.getName(), refreshCallback)));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500347}
348
349void
350Fib::scheduleLoop(FibEntry& entry)
351{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600352 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500353}
354
355void
356Fib::cancelEntryRefresh(const FibEntry& entry)
357{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600358 NLSR_LOG_DEBUG("Canceling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400359 entry.getRefreshEventId().cancel();
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500360}
361
362void
363Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
364{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400365 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500366 if (it == m_table.end()) {
367 return;
368 }
369
370 FibEntry& entry = it->second;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500371 NLSR_LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500372
373 // Increment sequence number
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600374 entry.setSeqNo(entry.getSeqNo() + 1);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500375
376 for (const NextHop& hop : entry) {
377 registerPrefix(entry.getName(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500378 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500379 hop.getRouteCostAsAdjustedInteger(),
380 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
381 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500382 }
383
384 refreshCb(entry);
385}
386
387void
akmhoque674b0b12014-05-20 14:33:28 -0500388Fib::writeLog()
389{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500390 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600391 for (const auto& entry : m_table) {
392 entry.second.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500393 }
394}
akmhoquefdbddb12014-05-02 18:35:19 -0500395
Nick Gordonfad8e252016-08-11 14:21:38 -0500396} // namespace nlsr