blob: 49ce647a08c92756365ecea936e5e1a1c10f0b80 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("Fib");
35
akmhoque393d4ff2014-07-16 14:27:03 -050036const uint64_t Fib::GRACE_PERIOD = 10;
37
akmhoque53353462014-04-22 08:43:45 -050038using namespace std;
39using namespace ndn;
40
akmhoque53353462014-04-22 08:43:45 -050041void
akmhoque31d1d4b2014-05-05 22:08:14 -050042Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050043{
akmhoque674b0b12014-05-20 14:33:28 -050044 _LOG_DEBUG("Fib::remove called");
Nick Gordonb7168472016-09-21 13:57:17 -050045 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
akmhoque157b0a42014-05-13 00:26:37 -050046 if (it != m_table.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050047 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -050048 (it->second).getNexthopList().getNextHops().begin();
49 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050050 //remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -050051 if (isPrefixUpdatable((it->second).getName())) {
52 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050053 }
akmhoque53353462014-04-22 08:43:45 -050054 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050055 cancelEntryRefresh(it->second);
akmhoque53353462014-04-22 08:43:45 -050056 m_table.erase(it);
57 }
58}
59
Vince Lehman18841082014-08-19 17:15:24 -050060bool
61compareFaceUri(const NextHop& hop, const std::string& faceUri)
62{
63 return hop.getConnectingFaceUri() == faceUri;
64}
65
66void
Vince Lehman942eb7b2014-10-02 10:09:27 -050067Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050068{
69 const ndn::Name& name = entry.getName();
70
Vince Lehman942eb7b2014-10-02 10:09:27 -050071 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -050072 {
73 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -050074 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -050075
76 if (isPrefixUpdatable(name)) {
77 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -050078 registerPrefix(name, it->getConnectingFaceUri(),
79 it->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
Vince Lehman942eb7b2014-10-02 10:09:27 -050087Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050088{
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050089 FibEntry* entry = processUpdate(name, allHops);
90 if (entry != nullptr && !entry->getRefreshEventId()) {
91 scheduleEntryRefresh(*entry,
92 [this] (FibEntry& fibEntry) {
93 this->scheduleLoop(fibEntry);
94 });
95 }
96}
97
98FibEntry*
99Fib::processUpdate(const ndn::Name& name, NexthopList& allHops)
100{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500101 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500102
Vince Lehman942eb7b2014-10-02 10:09:27 -0500103 // Get the max possible faces which is the minumum of the configuration setting and
104 // the length of the list of all next hops.
105 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500106
Vince Lehman942eb7b2014-10-02 10:09:27 -0500107 NexthopList hopsToAdd;
108 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500109
Vince Lehman942eb7b2014-10-02 10:09:27 -0500110 // Create a list of next hops to be installed with length == maxFaces
111 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500112 ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500113 hopsToAdd.addNextHop(*it);
114 }
Vince Lehman18841082014-08-19 17:15:24 -0500115
Nick Gordonb7168472016-09-21 13:57:17 -0500116 std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500117
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500118 // New FIB entry that has nextHops
119 if (entryIt == m_table.end() && hopsToAdd.getSize() != 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500120 _LOG_DEBUG("New FIB Entry");
121
Vince Lehman18841082014-08-19 17:15:24 -0500122 FibEntry entry(name);
123
Vince Lehman942eb7b2014-10-02 10:09:27 -0500124 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500125
Nick Gordonb7168472016-09-21 13:57:17 -0500126 m_table.emplace(name, entry);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500127
128 return &m_table.find(name)->second;
akmhoque53353462014-04-22 08:43:45 -0500129 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500130 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500131 else {
Vince Lehman18841082014-08-19 17:15:24 -0500132 // Existing FIB entry
133 _LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500134
135 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500136 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500137 remove(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500138 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500139 }
Vince Lehman18841082014-08-19 17:15:24 -0500140
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500141 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500142 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500143
laqinfancd7ca052017-06-02 04:57:18 -0500144 std::set<NextHop, NextHopComparator> hopsToRemove;
145 std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
146 hopsToAdd.begin(), hopsToAdd.end(),
147 std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
148
149 bool isUpdatable = isPrefixUpdatable(entry.getName());
150 // Remove the uninstalled next hops from NFD and FIB entry
151 for (const auto& hop: hopsToRemove){
152 if (isUpdatable) {
153 unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
154 }
155 _LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
156 entry.getNexthopList().removeNextHop(hop);
157 }
Vince Lehman18841082014-08-19 17:15:24 -0500158
Vince Lehman18841082014-08-19 17:15:24 -0500159 // Increment sequence number
160 entry.setSeqNo(entry.getSeqNo() + 1);
161
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500162 return &(m_table.find(name)->second);
akmhoque53353462014-04-22 08:43:45 -0500163 }
164}
165
akmhoque53353462014-04-22 08:43:45 -0500166void
akmhoque31d1d4b2014-05-05 22:08:14 -0500167Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500168{
akmhoque674b0b12014-05-20 14:33:28 -0500169 _LOG_DEBUG("Fib::clean called");
Nick Gordonb7168472016-09-21 13:57:17 -0500170 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
171 it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500172 ++it) {
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500173 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->second.getName());
174 cancelEntryRefresh(it->second);
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500175 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -0500176 (it->second).getNexthopList().getNextHops().begin();
177 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500178 //Remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -0500179 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500180 }
181 }
akmhoque157b0a42014-05-13 00:26:37 -0500182 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500183 m_table.clear();
184 }
185}
186
Vince Lehman942eb7b2014-10-02 10:09:27 -0500187unsigned int
188Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500189{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500190 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
191 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
192
193 // Allow all faces
194 if (nMaxFaces == 0) {
195 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500196 }
akmhoque157b0a42014-05-13 00:26:37 -0500197 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500198 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500199 }
akmhoque53353462014-04-22 08:43:45 -0500200}
201
akmhoque393d4ff2014-07-16 14:27:03 -0500202bool
203Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500204 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500205 return true;
206 }
207
208 return false;
209}
210
akmhoque53353462014-04-22 08:43:45 -0500211void
akmhoquec04e7272014-07-02 11:00:14 -0500212Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500213 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500214 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500215{
Nick Gordone9733ed2017-04-26 10:48:39 -0500216 uint64_t faceId = m_adjacencyList.getFaceId(ndn::util::FaceUri(faceUri));
akmhoque102aea42014-08-04 10:22:12 -0500217 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500218 ndn::nfd::ControlParameters faceParameters;
219 faceParameters
220 .setName(namePrefix)
221 .setFaceId(faceId)
222 .setFlags(flags)
223 .setCost(faceCost)
224 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500225 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500226
akmhoque102aea42014-08-04 10:22:12 -0500227 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
228 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500229 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500230 }
231 else {
232 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
233 }
akmhoquec04e7272014-07-02 11:00:14 -0500234}
235
akmhoque060d3022014-08-12 13:35:06 -0500236void
237Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
238 const std::string& faceUri,
239 uint8_t times)
240{
dmcoomes9eaf3f42017-02-21 11:39:01 -0600241 _LOG_DEBUG("Registering prefix: " << parameters.getName() << " faceUri: " << faceUri);
akmhoque060d3022014-08-12 13:35:06 -0500242 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500243 std::bind(&Fib::onRegistrationSuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500244 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500245 faceUri),
dmcoomes9f936662017-03-02 10:33:09 -0600246 std::bind(&Fib::onRegistrationFailure,
Nick Gordond5c1a372016-10-31 13:56:23 -0500247 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500248 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500249 parameters,
250 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500251}
akmhoque31d1d4b2014-05-05 22:08:14 -0500252
akmhoquefdbddb12014-05-02 18:35:19 -0500253void
akmhoque157b0a42014-05-13 00:26:37 -0500254Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500255{
akmhoque157b0a42014-05-13 00:26:37 -0500256 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500257 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500258 if (faceId > 0) {
259 ndn::nfd::ControlParameters controlParameters;
260 controlParameters
261 .setName(namePrefix)
262 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500263 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500264 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500265 std::bind(&Fib::onUnregistrationSuccess, this, _1,
266 "Successful in unregistering name"),
267 std::bind(&Fib::onUnregistrationFailure,
268 this, _1,
269 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500270 }
akmhoquefdbddb12014-05-02 18:35:19 -0500271}
272
273void
akmhoque393d4ff2014-07-16 14:27:03 -0500274Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500275{
276 ndn::nfd::ControlParameters parameters;
277 parameters
278 .setName(name)
279 .setStrategy(strategy);
280
281 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600282 std::bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500283 "Successfully set strategy choice"),
dmcoomes9f936662017-03-02 10:33:09 -0600284 std::bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500285 parameters,
286 count,
akmhoque157b0a42014-05-13 00:26:37 -0500287 "Failed to set strategy choice"));
288}
289
290void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500291Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
292 const std::string& message, const std::string& faceUri)
akmhoque157b0a42014-05-13 00:26:37 -0500293{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500294 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
295 " Face Uri: " << faceUri);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500296
Nick Gordond5c1a372016-10-31 13:56:23 -0500297 // Update the adjacency list with the new Face ID
298 m_adjacencyList.findAdjacent(faceUri)->setFaceId(commandSuccessResult.getFaceId());
299 // Update the fast-access FaceMap with the new Face ID, too
akmhoque157b0a42014-05-13 00:26:37 -0500300 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500301 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500302}
303
akmhoque157b0a42014-05-13 00:26:37 -0500304void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000305Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500306 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500307 const ndn::nfd::ControlParameters& parameters,
308 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500309 uint8_t times)
310{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000311 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500312 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500313 if (times < 3) {
314 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500315 registerPrefix(parameters.getName(), faceUri,
316 parameters.getCost(),
317 parameters.getExpirationPeriod(),
318 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500319 }
320 else {
321 _LOG_DEBUG("Registration trial given up");
322 }
323}
324
325void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500326Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
327 const std::string& message)
328{
329 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
330 " Face Id: " << commandSuccessResult.getFaceId());
331}
332
333void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000334Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
335 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500336{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000337 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500338}
339
akmhoque674b0b12014-05-20 14:33:28 -0500340void
akmhoque393d4ff2014-07-16 14:27:03 -0500341Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
342 const std::string& message)
343{
344 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
345 << "for name: " << commandSuccessResult.getName());
346}
347
348void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000349Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
350 const ndn::nfd::ControlParameters& parameters,
351 uint32_t count,
352 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500353{
354 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
355 << "for name: " << parameters.getName());
356 if (count < 3) {
357 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
358 }
359}
360
361void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500362Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
363{
364 _LOG_DEBUG("Scheduling refresh for " << entry.getName()
365 << " Seq Num: " << entry.getSeqNo()
366 << " in " << m_refreshTime << " seconds");
367
368 entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime),
369 std::bind(&Fib::refreshEntry, this,
370 entry.getName(), refreshCallback)));
371}
372
373void
374Fib::scheduleLoop(FibEntry& entry)
375{
376 scheduleEntryRefresh(entry,
377 std::bind(&Fib::scheduleLoop, this, _1));
378}
379
380void
381Fib::cancelEntryRefresh(const FibEntry& entry)
382{
383 _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
384
385 m_scheduler.cancelEvent(entry.getRefreshEventId());
386 entry.getRefreshEventId().reset();
387}
388
389void
390Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
391{
392 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
393
394 if (it == m_table.end()) {
395 return;
396 }
397
398 FibEntry& entry = it->second;
399 _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
400
401 // Increment sequence number
402 entry.setSeqNo(entry.getSeqNo() + 1);
403
404 for (const NextHop& hop : entry) {
405 registerPrefix(entry.getName(),
406 hop.getConnectingFaceUri(),
407 hop.getRouteCostAsAdjustedInteger(),
408 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
409 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
410 }
411
412 refreshCb(entry);
413}
414
415void
akmhoque674b0b12014-05-20 14:33:28 -0500416Fib::writeLog()
417{
418 _LOG_DEBUG("-------------------FIB-----------------------------");
Nick Gordonb7168472016-09-21 13:57:17 -0500419 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
420 it != m_table.end();
akmhoque674b0b12014-05-20 14:33:28 -0500421 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500422 (it->second).writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500423 }
424}
akmhoquefdbddb12014-05-02 18:35:19 -0500425
Nick Gordonfad8e252016-08-11 14:21:38 -0500426} // namespace nlsr