blob: a38d33ba4545611e54a93040c224a271ed0417d8 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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)
41 , m_refreshTime(0)
42 , 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");
Nick Gordonb7168472016-09-21 13:57:17 -050052 std::map<ndn::Name, FibEntry>::iterator 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
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050099 for (NexthopList::iterator it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500100 ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500101 hopsToAdd.addNextHop(*it);
102 }
Vince Lehman18841082014-08-19 17:15:24 -0500103
Nick Gordonb7168472016-09-21 13:57:17 -0500104 std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500105
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500106 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500107 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500108 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500109
Vince Lehman18841082014-08-19 17:15:24 -0500110 FibEntry entry(name);
111
Vince Lehman942eb7b2014-10-02 10:09:27 -0500112 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500113
Nick Gordonb7168472016-09-21 13:57:17 -0500114 m_table.emplace(name, entry);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500115
Nick Gordon5867b522017-09-06 17:41:37 -0500116 entryIt = m_table.find(name);
akmhoque53353462014-04-22 08:43:45 -0500117 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500118 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500119 else {
Vince Lehman18841082014-08-19 17:15:24 -0500120 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500121 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500122
123 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500124 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500126 return;
akmhoque53353462014-04-22 08:43:45 -0500127 }
Vince Lehman18841082014-08-19 17:15:24 -0500128
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500129 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500130 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500131
laqinfancd7ca052017-06-02 04:57:18 -0500132 std::set<NextHop, NextHopComparator> hopsToRemove;
133 std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
134 hopsToAdd.begin(), hopsToAdd.end(),
135 std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
136
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600137 bool isUpdatable = isNotNeighbor(entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500138 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600139 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500140 if (isUpdatable) {
141 unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
142 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500143 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500144 entry.getNexthopList().removeNextHop(hop);
145 }
Vince Lehman18841082014-08-19 17:15:24 -0500146
Vince Lehman18841082014-08-19 17:15:24 -0500147 // Increment sequence number
148 entry.setSeqNo(entry.getSeqNo() + 1);
149
Nick Gordon5867b522017-09-06 17:41:37 -0500150 entryIt = m_table.find(name);
151
152 }
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600153 if (entryIt != m_table.end() &&
154 !entryIt->second.getRefreshEventId() &&
155 isNotNeighbor(entryIt->second.getName())) {
Nick Gordon5867b522017-09-06 17:41:37 -0500156 scheduleEntryRefresh(entryIt->second,
157 [this] (FibEntry& entry) {
158 scheduleLoop(entry);
159 });
akmhoque53353462014-04-22 08:43:45 -0500160 }
161}
162
akmhoque53353462014-04-22 08:43:45 -0500163void
akmhoque31d1d4b2014-05-05 22:08:14 -0500164Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500165{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500166 NLSR_LOG_DEBUG("Fib::clean called");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600167 // can't use const ref here as getNexthopList can't be marked const
168 for (auto&& it : m_table) {
169 NLSR_LOG_DEBUG("Canceling Scheduled event. Name: " << it.second.getName());
170 cancelEntryRefresh(it.second);
171
172 for (const auto& hop : it.second.getNexthopList().getNextHops()) {
173 unregisterPrefix(it.second.getName(), hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500174 }
175 }
akmhoque53353462014-04-22 08:43:45 -0500176}
177
Vince Lehman942eb7b2014-10-02 10:09:27 -0500178unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500179Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500180{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500181 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
182 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
183
184 // Allow all faces
185 if (nMaxFaces == 0) {
186 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500187 }
akmhoque157b0a42014-05-13 00:26:37 -0500188 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500189 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500190 }
akmhoque53353462014-04-22 08:43:45 -0500191}
192
akmhoque393d4ff2014-07-16 14:27:03 -0500193bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600194Fib::isNotNeighbor(const ndn::Name& name)
195{
196 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500197}
198
akmhoque53353462014-04-22 08:43:45 -0500199void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500200Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500201 uint64_t faceCost,
202 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500203 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500204{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500205 uint64_t faceId = m_adjacencyList.getFaceId(ndn::FaceUri(faceUri));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500206
akmhoque102aea42014-08-04 10:22:12 -0500207 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500208 ndn::nfd::ControlParameters faceParameters;
209 faceParameters
210 .setName(namePrefix)
211 .setFaceId(faceId)
212 .setFlags(flags)
213 .setCost(faceCost)
214 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500215 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500216
dmcoomes5bcb39e2017-10-31 15:07:55 -0500217 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500218 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600219 std::bind(&Fib::onRegistrationSuccess, this, _1,
220 "Successful in name registration",
221 faceUri),
222 std::bind(&Fib::onRegistrationFailure, this, _1,
223 "Failed in name registration",
224 faceParameters,
225 faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500226 }
227 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500228 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500229 }
230}
231
232void
233Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500234 const std::string& message, const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500235{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500236 NLSR_LOG_DEBUG(message << ": " << commandSuccessResult.getName() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600237 " Face Uri: " << faceUri << " faceId: " << commandSuccessResult.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500238
239 AdjacencyList::iterator adjacent = m_adjacencyList.findAdjacent(faceUri);
240 if (adjacent != m_adjacencyList.end()) {
241 adjacent->setFaceId(commandSuccessResult.getFaceId());
242 }
243
244 // Update the fast-access FaceMap with the new Face ID, too
245 m_faceMap.update(faceUri.toString(), commandSuccessResult.getFaceId());
246 m_faceMap.writeLog();
247}
248
249void
250Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
251 const std::string& message,
252 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500253 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500254 uint8_t times)
255{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500256 NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
257 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500258 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500259 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500260 registerPrefix(parameters.getName(), faceUri,
261 parameters.getCost(),
262 parameters.getExpirationPeriod(),
263 parameters.getFlags(), times+1);
264 }
265 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500266 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500267 }
akmhoquefdbddb12014-05-02 18:35:19 -0500268}
akmhoque31d1d4b2014-05-05 22:08:14 -0500269
akmhoquefdbddb12014-05-02 18:35:19 -0500270void
akmhoque157b0a42014-05-13 00:26:37 -0500271Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500272{
akmhoque157b0a42014-05-13 00:26:37 -0500273 uint32_t faceId = m_faceMap.getFaceId(faceUri);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500274 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500275 if (faceId > 0) {
276 ndn::nfd::ControlParameters controlParameters;
277 controlParameters
278 .setName(namePrefix)
279 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500280 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500281 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500282 std::bind(&Fib::onUnregistrationSuccess, this, _1,
283 "Successful in unregistering name"),
284 std::bind(&Fib::onUnregistrationFailure,
285 this, _1,
286 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500287 }
akmhoquefdbddb12014-05-02 18:35:19 -0500288}
289
290void
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500291Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
292 const std::string& message)
293{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500294 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600295 " Face Id: " << commandSuccessResult.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500296}
297
298void
299Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
300 const std::string& message)
301{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500302 NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500303}
304
305void
akmhoque393d4ff2014-07-16 14:27:03 -0500306Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500307{
308 ndn::nfd::ControlParameters parameters;
309 parameters
310 .setName(name)
311 .setStrategy(strategy);
312
313 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600314 std::bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500315 "Successfully set strategy choice"),
dmcoomes9f936662017-03-02 10:33:09 -0600316 std::bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500317 parameters,
318 count,
akmhoque157b0a42014-05-13 00:26:37 -0500319 "Failed to set strategy choice"));
320}
321
322void
akmhoque393d4ff2014-07-16 14:27:03 -0500323Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
324 const std::string& message)
325{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600326 NLSR_LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() <<
327 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500328}
329
330void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000331Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
332 const ndn::nfd::ControlParameters& parameters,
333 uint32_t count,
334 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500335{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600336 NLSR_LOG_DEBUG(message << ": " << parameters.getStrategy() <<
337 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500338 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600339 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500340 }
341}
342
343void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500344Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
345{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600346 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.getName() <<
347 " Seq Num: " << entry.getSeqNo() <<
348 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500349
350 entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime),
351 std::bind(&Fib::refreshEntry, this,
352 entry.getName(), refreshCallback)));
353}
354
355void
356Fib::scheduleLoop(FibEntry& entry)
357{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600358 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500359}
360
361void
362Fib::cancelEntryRefresh(const FibEntry& entry)
363{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600364 NLSR_LOG_DEBUG("Canceling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500365
366 m_scheduler.cancelEvent(entry.getRefreshEventId());
367 entry.getRefreshEventId().reset();
368}
369
370void
371Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
372{
373 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
374
375 if (it == m_table.end()) {
376 return;
377 }
378
379 FibEntry& entry = it->second;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500380 NLSR_LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500381
382 // Increment sequence number
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600383 entry.setSeqNo(entry.getSeqNo() + 1);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500384
385 for (const NextHop& hop : entry) {
386 registerPrefix(entry.getName(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500387 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500388 hop.getRouteCostAsAdjustedInteger(),
389 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
390 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500391 }
392
393 refreshCb(entry);
394}
395
396void
akmhoque674b0b12014-05-20 14:33:28 -0500397Fib::writeLog()
398{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500399 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600400 for (const auto& entry : m_table) {
401 entry.second.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500402 }
403}
akmhoquefdbddb12014-05-02 18:35:19 -0500404
Nick Gordonfad8e252016-08-11 14:21:38 -0500405} // namespace nlsr