blob: 73cde75e1ffbe090147f27850ea11e829b541cc7 [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::createFace(const std::string& faceUri,
213 const CommandSucceedCallback& onSuccess,
214 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500215{
Vince Lehman27f1add2014-10-16 17:14:46 -0500216 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500217}
218
219void
220Fib::destroyFace(const std::string& faceUri,
221 const CommandSucceedCallback& onSuccess,
222 const CommandFailCallback& onFailure)
223{
224 createFace(faceUri,
dmcoomes9f936662017-03-02 10:33:09 -0600225 std::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
akmhoquec04e7272014-07-02 11:00:14 -0500226 onFailure);
227}
228
229void
230Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
231 const CommandSucceedCallback& onSuccess,
232 const CommandFailCallback& onFailure)
233{
234 ndn::nfd::ControlParameters faceParameters;
235 faceParameters
236 .setFaceId(faceDestroyResult.getFaceId());
237 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
238 onSuccess,
239 onFailure);
240}
241
242void
243Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500244 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500245 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500246{
Nick Gordone9733ed2017-04-26 10:48:39 -0500247 uint64_t faceId = m_adjacencyList.getFaceId(ndn::util::FaceUri(faceUri));
akmhoque102aea42014-08-04 10:22:12 -0500248 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500249 ndn::nfd::ControlParameters faceParameters;
250 faceParameters
251 .setName(namePrefix)
252 .setFaceId(faceId)
253 .setFlags(flags)
254 .setCost(faceCost)
255 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500256 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500257
akmhoque102aea42014-08-04 10:22:12 -0500258 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
259 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500260 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500261 }
262 else {
263 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
264 }
akmhoquec04e7272014-07-02 11:00:14 -0500265}
266
Vince Lehman0a7da612014-10-29 14:39:29 -0500267typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&,
268 const ndn::nfd::ControlParameters&, uint8_t,
269 const CommandSucceedCallback&,
270 const CommandFailCallback&);
271
akmhoquec04e7272014-07-02 11:00:14 -0500272void
273Fib::registerPrefix(const ndn::Name& namePrefix,
274 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500275 uint64_t faceCost,
276 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500277 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500278 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500279 const CommandSucceedCallback& onSuccess,
280 const CommandFailCallback& onFailure)
281
282{
akmhoque060d3022014-08-12 13:35:06 -0500283 ndn::nfd::ControlParameters parameters;
284 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500285 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500286 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500287 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500288 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500289 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500290 createFace(faceUri,
dmcoomes9f936662017-03-02 10:33:09 -0600291 std::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd),
Vince Lehman0a7da612014-10-29 14:39:29 -0500292 this, _1, parameters, times, onSuccess, onFailure),
akmhoque060d3022014-08-12 13:35:06 -0500293 onFailure);
294}
295
296void
297Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
298 const std::string& faceUri,
299 uint8_t times)
300{
dmcoomes9eaf3f42017-02-21 11:39:01 -0600301 _LOG_DEBUG("Registering prefix: " << parameters.getName() << " faceUri: " << faceUri);
akmhoque060d3022014-08-12 13:35:06 -0500302 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500303 std::bind(&Fib::onRegistrationSuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500304 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500305 faceUri),
dmcoomes9f936662017-03-02 10:33:09 -0600306 std::bind(&Fib::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000307 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500308 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500309 parameters,
310 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500311}
akmhoque31d1d4b2014-05-05 22:08:14 -0500312
akmhoquefdbddb12014-05-02 18:35:19 -0500313void
akmhoquec04e7272014-07-02 11:00:14 -0500314Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500315 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500316 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500317 const CommandSucceedCallback& onSuccess,
318 const CommandFailCallback& onFailure)
319{
320 ndn::nfd::ControlParameters controlParameters;
321 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500322 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500323 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500324 .setCost(parameters.getCost())
325 .setFlags(parameters.getFlags())
326 .setExpirationPeriod(parameters.getExpirationPeriod())
Davide Pesavento87911da2017-02-21 22:10:55 -0500327 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoquec04e7272014-07-02 11:00:14 -0500328 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
329 onSuccess,
330 onFailure);
331}
332
333void
akmhoque157b0a42014-05-13 00:26:37 -0500334Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500335{
akmhoque157b0a42014-05-13 00:26:37 -0500336 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500337 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500338 if (faceId > 0) {
339 ndn::nfd::ControlParameters controlParameters;
340 controlParameters
341 .setName(namePrefix)
342 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500343 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500344 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500345 std::bind(&Fib::onUnregistrationSuccess, this, _1,
346 "Successful in unregistering name"),
347 std::bind(&Fib::onUnregistrationFailure,
348 this, _1,
349 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500350 }
akmhoquefdbddb12014-05-02 18:35:19 -0500351}
352
353void
akmhoque393d4ff2014-07-16 14:27:03 -0500354Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500355{
356 ndn::nfd::ControlParameters parameters;
357 parameters
358 .setName(name)
359 .setStrategy(strategy);
360
361 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600362 std::bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500363 "Successfully set strategy choice"),
dmcoomes9f936662017-03-02 10:33:09 -0600364 std::bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500365 parameters,
366 count,
akmhoque157b0a42014-05-13 00:26:37 -0500367 "Failed to set strategy choice"));
368}
369
370void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500371Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
372 const std::string& message, const std::string& faceUri)
akmhoque157b0a42014-05-13 00:26:37 -0500373{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500374 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
375 " Face Uri: " << faceUri);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500376
akmhoque157b0a42014-05-13 00:26:37 -0500377 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500378 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500379}
380
akmhoque157b0a42014-05-13 00:26:37 -0500381void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000382Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500383 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500384 const ndn::nfd::ControlParameters& parameters,
385 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500386 uint8_t times)
387{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000388 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500389 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500390 if (times < 3) {
391 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500392 registerPrefix(parameters.getName(), faceUri,
393 parameters.getCost(),
394 parameters.getExpirationPeriod(),
395 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500396 }
397 else {
398 _LOG_DEBUG("Registration trial given up");
399 }
400}
401
402void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500403Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
404 const std::string& message)
405{
406 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
407 " Face Id: " << commandSuccessResult.getFaceId());
408}
409
410void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000411Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
412 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500413{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000414 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500415}
416
akmhoque674b0b12014-05-20 14:33:28 -0500417void
akmhoque393d4ff2014-07-16 14:27:03 -0500418Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
419 const std::string& message)
420{
421 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
422 << "for name: " << commandSuccessResult.getName());
423}
424
425void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000426Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
427 const ndn::nfd::ControlParameters& parameters,
428 uint32_t count,
429 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500430{
431 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
432 << "for name: " << parameters.getName());
433 if (count < 3) {
434 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
435 }
436}
437
438void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500439Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
440{
441 _LOG_DEBUG("Scheduling refresh for " << entry.getName()
442 << " Seq Num: " << entry.getSeqNo()
443 << " in " << m_refreshTime << " seconds");
444
445 entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime),
446 std::bind(&Fib::refreshEntry, this,
447 entry.getName(), refreshCallback)));
448}
449
450void
451Fib::scheduleLoop(FibEntry& entry)
452{
453 scheduleEntryRefresh(entry,
454 std::bind(&Fib::scheduleLoop, this, _1));
455}
456
457void
458Fib::cancelEntryRefresh(const FibEntry& entry)
459{
460 _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
461
462 m_scheduler.cancelEvent(entry.getRefreshEventId());
463 entry.getRefreshEventId().reset();
464}
465
466void
467Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
468{
469 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
470
471 if (it == m_table.end()) {
472 return;
473 }
474
475 FibEntry& entry = it->second;
476 _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
477
478 // Increment sequence number
479 entry.setSeqNo(entry.getSeqNo() + 1);
480
481 for (const NextHop& hop : entry) {
482 registerPrefix(entry.getName(),
483 hop.getConnectingFaceUri(),
484 hop.getRouteCostAsAdjustedInteger(),
485 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
486 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
487 }
488
489 refreshCb(entry);
490}
491
492void
akmhoque674b0b12014-05-20 14:33:28 -0500493Fib::writeLog()
494{
495 _LOG_DEBUG("-------------------FIB-----------------------------");
Nick Gordonb7168472016-09-21 13:57:17 -0500496 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
497 it != m_table.end();
akmhoque674b0b12014-05-20 14:33:28 -0500498 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500499 (it->second).writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500500 }
501}
akmhoquefdbddb12014-05-02 18:35:19 -0500502
Nick Gordonfad8e252016-08-11 14:21:38 -0500503} // namespace nlsr