blob: 7c14d78369113fd5152f8c394b37fc808a7d9c5d [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{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500269 uint64_t faceId = m_adjacencyList.getFaceId(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{
323 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500324 std::bind(&Fib::onRegistrationSuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500325 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500326 faceUri),
dmcoomes9f936662017-03-02 10:33:09 -0600327 std::bind(&Fib::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000328 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500329 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500330 parameters,
331 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500332}
akmhoque31d1d4b2014-05-05 22:08:14 -0500333
akmhoquefdbddb12014-05-02 18:35:19 -0500334void
akmhoquec04e7272014-07-02 11:00:14 -0500335Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500336 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500337 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500338 const CommandSucceedCallback& onSuccess,
339 const CommandFailCallback& onFailure)
340{
341 ndn::nfd::ControlParameters controlParameters;
342 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500343 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500344 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500345 .setCost(parameters.getCost())
346 .setFlags(parameters.getFlags())
347 .setExpirationPeriod(parameters.getExpirationPeriod())
Davide Pesavento87911da2017-02-21 22:10:55 -0500348 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoquec04e7272014-07-02 11:00:14 -0500349 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
350 onSuccess,
351 onFailure);
352}
353
354void
akmhoque157b0a42014-05-13 00:26:37 -0500355Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500356{
akmhoque157b0a42014-05-13 00:26:37 -0500357 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500358 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500359 if (faceId > 0) {
360 ndn::nfd::ControlParameters controlParameters;
361 controlParameters
362 .setName(namePrefix)
363 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500364 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500365 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500366 std::bind(&Fib::onUnregistrationSuccess, this, _1,
367 "Successful in unregistering name"),
368 std::bind(&Fib::onUnregistrationFailure,
369 this, _1,
370 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500371 }
akmhoquefdbddb12014-05-02 18:35:19 -0500372}
373
374void
akmhoque393d4ff2014-07-16 14:27:03 -0500375Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500376{
377 ndn::nfd::ControlParameters parameters;
378 parameters
379 .setName(name)
380 .setStrategy(strategy);
381
382 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600383 std::bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500384 "Successfully set strategy choice"),
dmcoomes9f936662017-03-02 10:33:09 -0600385 std::bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500386 parameters,
387 count,
akmhoque157b0a42014-05-13 00:26:37 -0500388 "Failed to set strategy choice"));
389}
390
391void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500392Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
393 const std::string& message, const std::string& faceUri)
akmhoque157b0a42014-05-13 00:26:37 -0500394{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500395 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
396 " Face Uri: " << faceUri);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500397
akmhoque157b0a42014-05-13 00:26:37 -0500398 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500399 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500400}
401
akmhoque157b0a42014-05-13 00:26:37 -0500402void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000403Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500404 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500405 const ndn::nfd::ControlParameters& parameters,
406 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500407 uint8_t times)
408{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000409 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500410 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500411 if (times < 3) {
412 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500413 registerPrefix(parameters.getName(), faceUri,
414 parameters.getCost(),
415 parameters.getExpirationPeriod(),
416 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500417 }
418 else {
419 _LOG_DEBUG("Registration trial given up");
420 }
421}
422
423void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500424Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
425 const std::string& message)
426{
427 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
428 " Face Id: " << commandSuccessResult.getFaceId());
429}
430
431void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000432Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
433 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500434{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000435 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500436}
437
akmhoque674b0b12014-05-20 14:33:28 -0500438void
akmhoque393d4ff2014-07-16 14:27:03 -0500439Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
440 const std::string& message)
441{
442 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
443 << "for name: " << commandSuccessResult.getName());
444}
445
446void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000447Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
448 const ndn::nfd::ControlParameters& parameters,
449 uint32_t count,
450 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500451{
452 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
453 << "for name: " << parameters.getName());
454 if (count < 3) {
455 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
456 }
457}
458
459void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500460Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
461{
462 _LOG_DEBUG("Scheduling refresh for " << entry.getName()
463 << " Seq Num: " << entry.getSeqNo()
464 << " in " << m_refreshTime << " seconds");
465
466 entry.setRefreshEventId(m_scheduler.scheduleEvent(ndn::time::seconds(m_refreshTime),
467 std::bind(&Fib::refreshEntry, this,
468 entry.getName(), refreshCallback)));
469}
470
471void
472Fib::scheduleLoop(FibEntry& entry)
473{
474 scheduleEntryRefresh(entry,
475 std::bind(&Fib::scheduleLoop, this, _1));
476}
477
478void
479Fib::cancelEntryRefresh(const FibEntry& entry)
480{
481 _LOG_DEBUG("Cancelling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
482
483 m_scheduler.cancelEvent(entry.getRefreshEventId());
484 entry.getRefreshEventId().reset();
485}
486
487void
488Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
489{
490 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
491
492 if (it == m_table.end()) {
493 return;
494 }
495
496 FibEntry& entry = it->second;
497 _LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
498
499 // Increment sequence number
500 entry.setSeqNo(entry.getSeqNo() + 1);
501
502 for (const NextHop& hop : entry) {
503 registerPrefix(entry.getName(),
504 hop.getConnectingFaceUri(),
505 hop.getRouteCostAsAdjustedInteger(),
506 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
507 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
508 }
509
510 refreshCb(entry);
511}
512
513void
akmhoque674b0b12014-05-20 14:33:28 -0500514Fib::writeLog()
515{
516 _LOG_DEBUG("-------------------FIB-----------------------------");
Nick Gordonb7168472016-09-21 13:57:17 -0500517 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
518 it != m_table.end();
akmhoque674b0b12014-05-20 14:33:28 -0500519 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500520 (it->second).writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500521 }
522}
akmhoquefdbddb12014-05-02 18:35:19 -0500523
Nick Gordonfad8e252016-08-11 14:21:38 -0500524} // namespace nlsr