blob: c4ddad844e2145e9142b75d891aca6a7d556792a [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>
akmhoquec8a10f72014-04-25 18:42:55 -050029
akmhoque53353462014-04-22 08:43:45 -050030namespace nlsr {
31
akmhoque674b0b12014-05-20 14:33:28 -050032INIT_LOGGER("Fib");
33
akmhoque393d4ff2014-07-16 14:27:03 -050034const uint64_t Fib::GRACE_PERIOD = 10;
35
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37using namespace ndn;
38
akmhoque53353462014-04-22 08:43:45 -050039void
akmhoque31d1d4b2014-05-05 22:08:14 -050040Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050041{
akmhoque674b0b12014-05-20 14:33:28 -050042 _LOG_DEBUG("Fib::remove called");
Nick Gordonb7168472016-09-21 13:57:17 -050043 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
akmhoque157b0a42014-05-13 00:26:37 -050044 if (it != m_table.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050045 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -050046 (it->second).getNexthopList().getNextHops().begin();
47 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050048 //remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -050049 if (isPrefixUpdatable((it->second).getName())) {
50 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050051 }
akmhoque53353462014-04-22 08:43:45 -050052 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050053 cancelEntryRefresh(it->second);
akmhoque53353462014-04-22 08:43:45 -050054 m_table.erase(it);
55 }
56}
57
Vince Lehman18841082014-08-19 17:15:24 -050058bool
59compareFaceUri(const NextHop& hop, const std::string& faceUri)
60{
61 return hop.getConnectingFaceUri() == faceUri;
62}
63
64void
Vince Lehman942eb7b2014-10-02 10:09:27 -050065Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050066{
67 const ndn::Name& name = entry.getName();
68
Vince Lehman942eb7b2014-10-02 10:09:27 -050069 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -050070 {
71 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -050072 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -050073
74 if (isPrefixUpdatable(name)) {
75 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -050076 registerPrefix(name, it->getConnectingFaceUri(),
77 it->getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050078 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
79 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
80 }
81 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050082}
83
84void
85Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
86{
87 _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
88
89 const ndn::Name& name = entry.getName();
90 NexthopList& entryHopList = entry.getNexthopList();
91
92 for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
93
94 const std::string& faceUri = it->getConnectingFaceUri();
95
96 // See if the nexthop is installed in NFD's FIB
97 NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
98 installedHops.cend(),
dmcoomes9f936662017-03-02 10:33:09 -060099 std::bind(&compareFaceUri, _1, faceUri));
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100
101 // The next hop is not installed
102 if (foundIt == installedHops.cend()) {
103
104 if (isPrefixUpdatable(name)) {
105 // Remove the nexthop from NDN's FIB
106 unregisterPrefix(name, it->getConnectingFaceUri());
107 }
108
109 // Remove the next hop from the FIB entry
110 _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
111 // Since the iterator will be invalidated on removal, dereference the original
112 // and increment the copy
113 entryHopList.removeNextHop(*(it++));
114 }
115 else {
116 ++it;
117 }
118 }
Vince Lehman18841082014-08-19 17:15:24 -0500119}
120
121void
Vince Lehman942eb7b2014-10-02 10:09:27 -0500122Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -0500123{
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500124 FibEntry* entry = processUpdate(name, allHops);
125 if (entry != nullptr && !entry->getRefreshEventId()) {
126 scheduleEntryRefresh(*entry,
127 [this] (FibEntry& fibEntry) {
128 this->scheduleLoop(fibEntry);
129 });
130 }
131}
132
133FibEntry*
134Fib::processUpdate(const ndn::Name& name, NexthopList& allHops)
135{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500136 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500137
Vince Lehman942eb7b2014-10-02 10:09:27 -0500138 // Get the max possible faces which is the minumum of the configuration setting and
139 // the length of the list of all next hops.
140 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500141
Vince Lehman942eb7b2014-10-02 10:09:27 -0500142 NexthopList hopsToAdd;
143 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500144
Vince Lehman942eb7b2014-10-02 10:09:27 -0500145 // Create a list of next hops to be installed with length == maxFaces
146 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500147 ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500148 hopsToAdd.addNextHop(*it);
149 }
Vince Lehman18841082014-08-19 17:15:24 -0500150
Nick Gordonb7168472016-09-21 13:57:17 -0500151 std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500152
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500153 // New FIB entry that has nextHops
154 if (entryIt == m_table.end() && hopsToAdd.getSize() != 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500155 _LOG_DEBUG("New FIB Entry");
156
Vince Lehman18841082014-08-19 17:15:24 -0500157 FibEntry entry(name);
158
Vince Lehman942eb7b2014-10-02 10:09:27 -0500159 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500160
Nick Gordonb7168472016-09-21 13:57:17 -0500161 m_table.emplace(name, entry);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500162
163 return &m_table.find(name)->second;
akmhoque53353462014-04-22 08:43:45 -0500164 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500165 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500166 else {
Vince Lehman18841082014-08-19 17:15:24 -0500167 // Existing FIB entry
168 _LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500169
170 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500171 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 remove(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500173 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500174 }
Vince Lehman18841082014-08-19 17:15:24 -0500175
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500176 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500177 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500178
Vince Lehman942eb7b2014-10-02 10:09:27 -0500179 removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500180
Vince Lehman18841082014-08-19 17:15:24 -0500181 // Increment sequence number
182 entry.setSeqNo(entry.getSeqNo() + 1);
183
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500184 return &(m_table.find(name)->second);
akmhoque53353462014-04-22 08:43:45 -0500185 }
186}
187
akmhoque53353462014-04-22 08:43:45 -0500188void
akmhoque31d1d4b2014-05-05 22:08:14 -0500189Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500190{
akmhoque674b0b12014-05-20 14:33:28 -0500191 _LOG_DEBUG("Fib::clean called");
Nick Gordonb7168472016-09-21 13:57:17 -0500192 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
193 it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500194 ++it) {
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500195 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->second.getName());
196 cancelEntryRefresh(it->second);
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500197 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -0500198 (it->second).getNexthopList().getNextHops().begin();
199 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500200 //Remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -0500201 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500202 }
203 }
akmhoque157b0a42014-05-13 00:26:37 -0500204 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500205 m_table.clear();
206 }
207}
208
Vince Lehman942eb7b2014-10-02 10:09:27 -0500209unsigned int
210Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500211{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500212 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
213 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
214
215 // Allow all faces
216 if (nMaxFaces == 0) {
217 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500218 }
akmhoque157b0a42014-05-13 00:26:37 -0500219 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500220 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500221 }
akmhoque53353462014-04-22 08:43:45 -0500222}
223
akmhoque393d4ff2014-07-16 14:27:03 -0500224bool
225Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500226 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500227 return true;
228 }
229
230 return false;
231}
232
akmhoque53353462014-04-22 08:43:45 -0500233void
akmhoquec04e7272014-07-02 11:00:14 -0500234Fib::createFace(const std::string& faceUri,
235 const CommandSucceedCallback& onSuccess,
236 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500237{
Vince Lehman27f1add2014-10-16 17:14:46 -0500238 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500239}
240
241void
242Fib::destroyFace(const std::string& faceUri,
243 const CommandSucceedCallback& onSuccess,
244 const CommandFailCallback& onFailure)
245{
246 createFace(faceUri,
dmcoomes9f936662017-03-02 10:33:09 -0600247 std::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
akmhoquec04e7272014-07-02 11:00:14 -0500248 onFailure);
249}
250
251void
252Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
253 const CommandSucceedCallback& onSuccess,
254 const CommandFailCallback& onFailure)
255{
256 ndn::nfd::ControlParameters faceParameters;
257 faceParameters
258 .setFaceId(faceDestroyResult.getFaceId());
259 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
260 onSuccess,
261 onFailure);
262}
263
264void
265Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500266 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500267 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500268{
Nick Gordone9733ed2017-04-26 10:48:39 -0500269 uint64_t faceId = m_adjacencyList.getFaceId(ndn::util::FaceUri(faceUri));
akmhoque102aea42014-08-04 10:22:12 -0500270 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500271 ndn::nfd::ControlParameters faceParameters;
272 faceParameters
273 .setName(namePrefix)
274 .setFaceId(faceId)
275 .setFlags(flags)
276 .setCost(faceCost)
277 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500278 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500279
akmhoque102aea42014-08-04 10:22:12 -0500280 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
281 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500282 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500283 }
284 else {
285 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
286 }
akmhoquec04e7272014-07-02 11:00:14 -0500287}
288
Vince Lehman0a7da612014-10-29 14:39:29 -0500289typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&,
290 const ndn::nfd::ControlParameters&, uint8_t,
291 const CommandSucceedCallback&,
292 const CommandFailCallback&);
293
akmhoquec04e7272014-07-02 11:00:14 -0500294void
295Fib::registerPrefix(const ndn::Name& namePrefix,
296 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500297 uint64_t faceCost,
298 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500299 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500300 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500301 const CommandSucceedCallback& onSuccess,
302 const CommandFailCallback& onFailure)
303
304{
akmhoque060d3022014-08-12 13:35:06 -0500305 ndn::nfd::ControlParameters parameters;
306 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500307 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500308 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500309 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500310 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500311 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500312 createFace(faceUri,
dmcoomes9f936662017-03-02 10:33:09 -0600313 std::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd),
Vince Lehman0a7da612014-10-29 14:39:29 -0500314 this, _1, parameters, times, onSuccess, onFailure),
akmhoque060d3022014-08-12 13:35:06 -0500315 onFailure);
316}
317
318void
319Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
320 const std::string& faceUri,
321 uint8_t times)
322{
dmcoomes9eaf3f42017-02-21 11:39:01 -0600323 _LOG_DEBUG("Registering prefix: " << parameters.getName() << " faceUri: " << faceUri);
akmhoque060d3022014-08-12 13:35:06 -0500324 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500325 std::bind(&Fib::onRegistrationSuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500326 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500327 faceUri),
dmcoomes9f936662017-03-02 10:33:09 -0600328 std::bind(&Fib::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000329 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500330 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500331 parameters,
332 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500333}
akmhoque31d1d4b2014-05-05 22:08:14 -0500334
akmhoquefdbddb12014-05-02 18:35:19 -0500335void
akmhoquec04e7272014-07-02 11:00:14 -0500336Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500337 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500338 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500339 const CommandSucceedCallback& onSuccess,
340 const CommandFailCallback& onFailure)
341{
342 ndn::nfd::ControlParameters controlParameters;
343 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500344 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500345 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500346 .setCost(parameters.getCost())
347 .setFlags(parameters.getFlags())
348 .setExpirationPeriod(parameters.getExpirationPeriod())
Davide Pesavento87911da2017-02-21 22:10:55 -0500349 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoquec04e7272014-07-02 11:00:14 -0500350 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
351 onSuccess,
352 onFailure);
353}
354
355void
akmhoque157b0a42014-05-13 00:26:37 -0500356Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500357{
akmhoque157b0a42014-05-13 00:26:37 -0500358 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500359 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500360 if (faceId > 0) {
361 ndn::nfd::ControlParameters controlParameters;
362 controlParameters
363 .setName(namePrefix)
364 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500365 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500366 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500367 std::bind(&Fib::onUnregistrationSuccess, this, _1,
368 "Successful in unregistering name"),
369 std::bind(&Fib::onUnregistrationFailure,
370 this, _1,
371 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500372 }
akmhoquefdbddb12014-05-02 18:35:19 -0500373}
374
375void
akmhoque393d4ff2014-07-16 14:27:03 -0500376Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500377{
378 ndn::nfd::ControlParameters parameters;
379 parameters
380 .setName(name)
381 .setStrategy(strategy);
382
383 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600384 std::bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500385 "Successfully set strategy choice"),
dmcoomes9f936662017-03-02 10:33:09 -0600386 std::bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500387 parameters,
388 count,
akmhoque157b0a42014-05-13 00:26:37 -0500389 "Failed to set strategy choice"));
390}
391
392void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500393Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
394 const std::string& message, const std::string& faceUri)
akmhoque157b0a42014-05-13 00:26:37 -0500395{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500396 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
397 " Face Uri: " << faceUri);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500398
akmhoque157b0a42014-05-13 00:26:37 -0500399 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500400 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500401}
402
akmhoque157b0a42014-05-13 00:26:37 -0500403void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000404Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500405 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500406 const ndn::nfd::ControlParameters& parameters,
407 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500408 uint8_t times)
409{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000410 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500411 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500412 if (times < 3) {
413 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500414 registerPrefix(parameters.getName(), faceUri,
415 parameters.getCost(),
416 parameters.getExpirationPeriod(),
417 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500418 }
419 else {
420 _LOG_DEBUG("Registration trial given up");
421 }
422}
423
424void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500425Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
426 const std::string& message)
427{
428 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
429 " Face Id: " << commandSuccessResult.getFaceId());
430}
431
432void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000433Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
434 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500435{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000436 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500437}
438
akmhoque674b0b12014-05-20 14:33:28 -0500439void
akmhoque393d4ff2014-07-16 14:27:03 -0500440Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
441 const std::string& message)
442{
443 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
444 << "for name: " << commandSuccessResult.getName());
445}
446
447void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000448Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
449 const ndn::nfd::ControlParameters& parameters,
450 uint32_t count,
451 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500452{
453 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
454 << "for name: " << parameters.getName());
455 if (count < 3) {
456 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
457 }
458}
459
460void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500461Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
462{
463 _LOG_DEBUG("Scheduling refresh for " << entry.getName()
464 << " Seq Num: " << entry.getSeqNo()
465 << " in " << m_refreshTime << " seconds");
466
467 entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime),
468 std::bind(&Fib::refreshEntry, this,
469 entry.getName(), refreshCallback)));
470}
471
472void
473Fib::scheduleLoop(FibEntry& entry)
474{
475 scheduleEntryRefresh(entry,
476 std::bind(&Fib::scheduleLoop, this, _1));
477}
478
479void
480Fib::cancelEntryRefresh(const FibEntry& entry)
481{
482 _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
483
484 m_scheduler.cancelEvent(entry.getRefreshEventId());
485 entry.getRefreshEventId().reset();
486}
487
488void
489Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
490{
491 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
492
493 if (it == m_table.end()) {
494 return;
495 }
496
497 FibEntry& entry = it->second;
498 _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
499
500 // Increment sequence number
501 entry.setSeqNo(entry.getSeqNo() + 1);
502
503 for (const NextHop& hop : entry) {
504 registerPrefix(entry.getName(),
505 hop.getConnectingFaceUri(),
506 hop.getRouteCostAsAdjustedInteger(),
507 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
508 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
509 }
510
511 refreshCb(entry);
512}
513
514void
akmhoque674b0b12014-05-20 14:33:28 -0500515Fib::writeLog()
516{
517 _LOG_DEBUG("-------------------FIB-----------------------------");
Nick Gordonb7168472016-09-21 13:57:17 -0500518 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
519 it != m_table.end();
akmhoque674b0b12014-05-20 14:33:28 -0500520 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500521 (it->second).writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500522 }
523}
akmhoquefdbddb12014-05-02 18:35:19 -0500524
Nick Gordonfad8e252016-08-11 14:21:38 -0500525} // namespace nlsr