blob: 5de9929850e5b02391955b639e90c1d64e5b1b99 [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#include <map>
21
akmhoquefdbddb12014-05-02 18:35:19 -050022#include <cmath>
akmhoque157b0a42014-05-13 00:26:37 -050023#include <ndn-cxx/common.hpp>
Davide Pesavento87911da2017-02-21 22:10:55 -050024#include <ndn-cxx/encoding/nfd-constants.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050025
Vince Lehman942eb7b2014-10-02 10:09:27 -050026#include "adjacency-list.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050027#include "common.hpp"
Vince Lehman942eb7b2014-10-02 10:09:27 -050028#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050029#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050030#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -050031#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050032#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050033
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
akmhoque674b0b12014-05-20 14:33:28 -050036INIT_LOGGER("Fib");
37
akmhoque393d4ff2014-07-16 14:27:03 -050038const uint64_t Fib::GRACE_PERIOD = 10;
39
akmhoque53353462014-04-22 08:43:45 -050040using namespace std;
41using namespace ndn;
42
akmhoque53353462014-04-22 08:43:45 -050043void
akmhoque31d1d4b2014-05-05 22:08:14 -050044Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050045{
Vince Lehman7c603292014-09-11 17:48:16 -050046 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050047}
48
akmhoque53353462014-04-22 08:43:45 -050049ndn::EventId
Vince Lehman18841082014-08-19 17:15:24 -050050Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum,
akmhoquec7a79b22014-05-26 08:06:19 -050051 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050052{
Vince Lehman18841082014-08-19 17:15:24 -050053 _LOG_DEBUG("Fib::scheduleEntryExpiration Called");
54 _LOG_INFO("Name: " << name << " Seq Num: " << feSeqNum);
Vince Lehman7c603292014-09-11 17:48:16 -050055
dmcoomes9f936662017-03-02 10:33:09 -060056 return m_scheduler.scheduleEvent(expTime, std::bind(&Fib::remove, this, name));
akmhoque53353462014-04-22 08:43:45 -050057}
58
59void
akmhoque31d1d4b2014-05-05 22:08:14 -050060Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050061{
akmhoque674b0b12014-05-20 14:33:28 -050062 _LOG_DEBUG("Fib::remove called");
Nick Gordonb7168472016-09-21 13:57:17 -050063 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
akmhoque157b0a42014-05-13 00:26:37 -050064 if (it != m_table.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050065 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -050066 (it->second).getNexthopList().getNextHops().begin();
67 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050068 //remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -050069 if (isPrefixUpdatable((it->second).getName())) {
70 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050071 }
akmhoque53353462014-04-22 08:43:45 -050072 }
akmhoque674b0b12014-05-20 14:33:28 -050073 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
Nick Gordonb7168472016-09-21 13:57:17 -050074 cancelScheduledExpiringEvent((it->second).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -050075 m_table.erase(it);
76 }
77}
78
Vince Lehman18841082014-08-19 17:15:24 -050079bool
80compareFaceUri(const NextHop& hop, const std::string& faceUri)
81{
82 return hop.getConnectingFaceUri() == faceUri;
83}
84
85void
Vince Lehman942eb7b2014-10-02 10:09:27 -050086Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050087{
88 const ndn::Name& name = entry.getName();
89
Vince Lehman942eb7b2014-10-02 10:09:27 -050090 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -050091 {
92 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -050093 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -050094
95 if (isPrefixUpdatable(name)) {
96 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 registerPrefix(name, it->getConnectingFaceUri(),
98 it->getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050099 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
100 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
101 }
102 }
Vince Lehman942eb7b2014-10-02 10:09:27 -0500103}
104
105void
106Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
107{
108 _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
109
110 const ndn::Name& name = entry.getName();
111 NexthopList& entryHopList = entry.getNexthopList();
112
113 for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
114
115 const std::string& faceUri = it->getConnectingFaceUri();
116
117 // See if the nexthop is installed in NFD's FIB
118 NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
119 installedHops.cend(),
dmcoomes9f936662017-03-02 10:33:09 -0600120 std::bind(&compareFaceUri, _1, faceUri));
Vince Lehman942eb7b2014-10-02 10:09:27 -0500121
122 // The next hop is not installed
123 if (foundIt == installedHops.cend()) {
124
125 if (isPrefixUpdatable(name)) {
126 // Remove the nexthop from NDN's FIB
127 unregisterPrefix(name, it->getConnectingFaceUri());
128 }
129
130 // Remove the next hop from the FIB entry
131 _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
132 // Since the iterator will be invalidated on removal, dereference the original
133 // and increment the copy
134 entryHopList.removeNextHop(*(it++));
135 }
136 else {
137 ++it;
138 }
139 }
Vince Lehman18841082014-08-19 17:15:24 -0500140}
141
142void
Vince Lehman942eb7b2014-10-02 10:09:27 -0500143Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -0500144{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500145 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500146
Vince Lehman942eb7b2014-10-02 10:09:27 -0500147 // Get the max possible faces which is the minumum of the configuration setting and
148 // the length of the list of all next hops.
149 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500150
Vince Lehman942eb7b2014-10-02 10:09:27 -0500151 NexthopList hopsToAdd;
152 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500153
Vince Lehman942eb7b2014-10-02 10:09:27 -0500154 // Create a list of next hops to be installed with length == maxFaces
155 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
156 ++it, ++nFaces)
157 {
158 hopsToAdd.addNextHop(*it);
159 }
Vince Lehman18841082014-08-19 17:15:24 -0500160
Nick Gordonb7168472016-09-21 13:57:17 -0500161 std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500162
Vince Lehman18841082014-08-19 17:15:24 -0500163 // New FIB entry
164 if (entryIt == m_table.end()) {
165 _LOG_DEBUG("New FIB Entry");
166
167 // Don't create an entry for a name with no nexthops
Vince Lehman942eb7b2014-10-02 10:09:27 -0500168 if (hopsToAdd.getSize() == 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500169 return;
akmhoque53353462014-04-22 08:43:45 -0500170 }
Vince Lehman18841082014-08-19 17:15:24 -0500171
172 FibEntry entry(name);
173
Vince Lehman942eb7b2014-10-02 10:09:27 -0500174 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500175
176 // Set entry's expiration time point and sequence number
177 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
178 ndn::time::seconds(m_refreshTime));
179 entry.setSeqNo(1);
180
181 // Schedule entry to be refreshed
182 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
183 ndn::time::seconds(m_refreshTime)));
Nick Gordonb7168472016-09-21 13:57:17 -0500184 m_table.emplace(name, entry);
akmhoque53353462014-04-22 08:43:45 -0500185 }
akmhoque157b0a42014-05-13 00:26:37 -0500186 else {
Vince Lehman18841082014-08-19 17:15:24 -0500187 // Existing FIB entry
188 _LOG_DEBUG("Existing FIB Entry");
Nick Gordonb7168472016-09-21 13:57:17 -0500189 FibEntry& entry = (entryIt->second);
Vince Lehman18841082014-08-19 17:15:24 -0500190
191 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500192 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500193 remove(name);
Vince Lehman18841082014-08-19 17:15:24 -0500194 return;
akmhoque53353462014-04-22 08:43:45 -0500195 }
Vince Lehman18841082014-08-19 17:15:24 -0500196
Vince Lehman942eb7b2014-10-02 10:09:27 -0500197 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500198
Vince Lehman942eb7b2014-10-02 10:09:27 -0500199 removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500200
201 // Set entry's expiration time point
202 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
203 ndn::time::seconds(m_refreshTime));
204 // Increment sequence number
205 entry.setSeqNo(entry.getSeqNo() + 1);
206
Vince Lehman942eb7b2014-10-02 10:09:27 -0500207 // Cancel previously scheduled event
Vince Lehman7c603292014-09-11 17:48:16 -0500208 m_scheduler.cancelEvent(entry.getExpiringEventId());
Vince Lehman18841082014-08-19 17:15:24 -0500209
210 // Schedule entry to be refreshed
211 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
212 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500213 }
214}
215
akmhoque53353462014-04-22 08:43:45 -0500216void
akmhoque31d1d4b2014-05-05 22:08:14 -0500217Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500218{
akmhoque674b0b12014-05-20 14:33:28 -0500219 _LOG_DEBUG("Fib::clean called");
Nick Gordonb7168472016-09-21 13:57:17 -0500220 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
221 it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500222 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500223 _LOG_DEBUG("Cancelling Scheduled event. Name: " << (it->second).getName());
224 cancelScheduledExpiringEvent((it->second).getExpiringEventId());
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500225 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -0500226 (it->second).getNexthopList().getNextHops().begin();
227 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500228 //Remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -0500229 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500230 }
231 }
akmhoque157b0a42014-05-13 00:26:37 -0500232 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500233 m_table.clear();
234 }
235}
236
Vince Lehman942eb7b2014-10-02 10:09:27 -0500237unsigned int
238Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500239{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500240 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
241 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
242
243 // Allow all faces
244 if (nMaxFaces == 0) {
245 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500246 }
akmhoque157b0a42014-05-13 00:26:37 -0500247 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500248 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500249 }
akmhoque53353462014-04-22 08:43:45 -0500250}
251
akmhoque393d4ff2014-07-16 14:27:03 -0500252bool
253Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500254 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500255 return true;
256 }
257
258 return false;
259}
260
akmhoque53353462014-04-22 08:43:45 -0500261void
akmhoque157b0a42014-05-13 00:26:37 -0500262Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500264{
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500265 for (std::set<NextHop, NextHopComparator>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500266 it != nl.getNextHops().end(); ++it) {
267 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500268 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500269 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500270 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500271 }
akmhoque53353462014-04-22 08:43:45 -0500272 }
273 }
274}
275
276void
akmhoquec04e7272014-07-02 11:00:14 -0500277Fib::createFace(const std::string& faceUri,
278 const CommandSucceedCallback& onSuccess,
279 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500280{
Vince Lehman27f1add2014-10-16 17:14:46 -0500281 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500282}
283
284void
285Fib::destroyFace(const std::string& faceUri,
286 const CommandSucceedCallback& onSuccess,
287 const CommandFailCallback& onFailure)
288{
289 createFace(faceUri,
dmcoomes9f936662017-03-02 10:33:09 -0600290 std::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
akmhoquec04e7272014-07-02 11:00:14 -0500291 onFailure);
292}
293
294void
295Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
296 const CommandSucceedCallback& onSuccess,
297 const CommandFailCallback& onFailure)
298{
299 ndn::nfd::ControlParameters faceParameters;
300 faceParameters
301 .setFaceId(faceDestroyResult.getFaceId());
302 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
303 onSuccess,
304 onFailure);
305}
306
307void
308Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500309 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500310 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500311{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500312 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
akmhoque102aea42014-08-04 10:22:12 -0500313 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500314 ndn::nfd::ControlParameters faceParameters;
315 faceParameters
316 .setName(namePrefix)
317 .setFaceId(faceId)
318 .setFlags(flags)
319 .setCost(faceCost)
320 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500321 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500322
akmhoque102aea42014-08-04 10:22:12 -0500323 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
324 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500325 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500326 }
327 else {
328 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
329 }
akmhoquec04e7272014-07-02 11:00:14 -0500330}
331
Vince Lehman0a7da612014-10-29 14:39:29 -0500332typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&,
333 const ndn::nfd::ControlParameters&, uint8_t,
334 const CommandSucceedCallback&,
335 const CommandFailCallback&);
336
akmhoquec04e7272014-07-02 11:00:14 -0500337void
338Fib::registerPrefix(const ndn::Name& namePrefix,
339 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500340 uint64_t faceCost,
341 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500342 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500343 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500344 const CommandSucceedCallback& onSuccess,
345 const CommandFailCallback& onFailure)
346
347{
akmhoque060d3022014-08-12 13:35:06 -0500348 ndn::nfd::ControlParameters parameters;
349 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500350 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500351 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500352 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500353 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500354 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500355 createFace(faceUri,
dmcoomes9f936662017-03-02 10:33:09 -0600356 std::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd),
Vince Lehman0a7da612014-10-29 14:39:29 -0500357 this, _1, parameters, times, onSuccess, onFailure),
akmhoque060d3022014-08-12 13:35:06 -0500358 onFailure);
359}
360
361void
362Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
363 const std::string& faceUri,
364 uint8_t times)
365{
366 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600367 std::bind(&Fib::onRegistration, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500368 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500369 faceUri),
dmcoomes9f936662017-03-02 10:33:09 -0600370 std::bind(&Fib::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000371 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500372 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500373 parameters,
374 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500375}
akmhoque31d1d4b2014-05-05 22:08:14 -0500376
akmhoquefdbddb12014-05-02 18:35:19 -0500377void
akmhoquec04e7272014-07-02 11:00:14 -0500378Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500379 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500380 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500381 const CommandSucceedCallback& onSuccess,
382 const CommandFailCallback& onFailure)
383{
384 ndn::nfd::ControlParameters controlParameters;
385 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500386 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500387 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500388 .setCost(parameters.getCost())
389 .setFlags(parameters.getFlags())
390 .setExpirationPeriod(parameters.getExpirationPeriod())
Davide Pesavento87911da2017-02-21 22:10:55 -0500391 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoquec04e7272014-07-02 11:00:14 -0500392 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
393 onSuccess,
394 onFailure);
395}
396
397void
akmhoque157b0a42014-05-13 00:26:37 -0500398Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500399{
akmhoque157b0a42014-05-13 00:26:37 -0500400 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500401 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500402 if (faceId > 0) {
403 ndn::nfd::ControlParameters controlParameters;
404 controlParameters
405 .setName(namePrefix)
406 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500407 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque157b0a42014-05-13 00:26:37 -0500408 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
dmcoomes9f936662017-03-02 10:33:09 -0600409 std::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500410 "Successful in unregistering name"),
dmcoomes9f936662017-03-02 10:33:09 -0600411 std::bind(&Fib::onUnregistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000412 this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500413 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500414 }
akmhoquefdbddb12014-05-02 18:35:19 -0500415}
416
417void
akmhoque393d4ff2014-07-16 14:27:03 -0500418Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500419{
420 ndn::nfd::ControlParameters parameters;
421 parameters
422 .setName(name)
423 .setStrategy(strategy);
424
425 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
dmcoomes9f936662017-03-02 10:33:09 -0600426 std::bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500427 "Successfully set strategy choice"),
dmcoomes9f936662017-03-02 10:33:09 -0600428 std::bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500429 parameters,
430 count,
akmhoque157b0a42014-05-13 00:26:37 -0500431 "Failed to set strategy choice"));
432}
433
434void
435Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
436 const std::string& message, const std::string& faceUri)
437{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500438 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
439 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500440 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500441 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500442}
443
akmhoque157b0a42014-05-13 00:26:37 -0500444void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500445Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
446 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500447{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500448 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
449 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500450}
451
452void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000453Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500454 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500455 const ndn::nfd::ControlParameters& parameters,
456 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500457 uint8_t times)
458{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000459 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500460 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500461 if (times < 3) {
462 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500463 registerPrefix(parameters.getName(), faceUri,
464 parameters.getCost(),
465 parameters.getExpirationPeriod(),
466 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500467 }
468 else {
469 _LOG_DEBUG("Registration trial given up");
470 }
471}
472
473void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000474Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
475 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500476{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000477 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500478}
479
akmhoque674b0b12014-05-20 14:33:28 -0500480void
akmhoque393d4ff2014-07-16 14:27:03 -0500481Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
482 const std::string& message)
483{
484 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
485 << "for name: " << commandSuccessResult.getName());
486}
487
488void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000489Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
490 const ndn::nfd::ControlParameters& parameters,
491 uint32_t count,
492 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500493{
494 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
495 << "for name: " << parameters.getName());
496 if (count < 3) {
497 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
498 }
499}
500
501void
akmhoque674b0b12014-05-20 14:33:28 -0500502Fib::writeLog()
503{
504 _LOG_DEBUG("-------------------FIB-----------------------------");
Nick Gordonb7168472016-09-21 13:57:17 -0500505 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
506 it != m_table.end();
akmhoque674b0b12014-05-20 14:33:28 -0500507 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500508 (it->second).writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500509 }
510}
akmhoquefdbddb12014-05-02 18:35:19 -0500511
Nick Gordonfad8e252016-08-11 14:21:38 -0500512} // namespace nlsr