blob: c75d8dc081cdf3f568998eb28fd70082135744fa [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>
akmhoquec8a10f72014-04-25 18:42:55 -050024
Vince Lehman942eb7b2014-10-02 10:09:27 -050025#include "adjacency-list.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050026#include "common.hpp"
Vince Lehman942eb7b2014-10-02 10:09:27 -050027#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050028#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050029#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -050030#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("Fib");
36
akmhoque393d4ff2014-07-16 14:27:03 -050037const uint64_t Fib::GRACE_PERIOD = 10;
38
akmhoque53353462014-04-22 08:43:45 -050039using namespace std;
40using namespace ndn;
41
akmhoque53353462014-04-22 08:43:45 -050042void
akmhoque31d1d4b2014-05-05 22:08:14 -050043Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050044{
Vince Lehman7c603292014-09-11 17:48:16 -050045 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050046}
47
akmhoque53353462014-04-22 08:43:45 -050048ndn::EventId
Vince Lehman18841082014-08-19 17:15:24 -050049Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum,
akmhoquec7a79b22014-05-26 08:06:19 -050050 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050051{
Vince Lehman18841082014-08-19 17:15:24 -050052 _LOG_DEBUG("Fib::scheduleEntryExpiration Called");
53 _LOG_INFO("Name: " << name << " Seq Num: " << feSeqNum);
Vince Lehman7c603292014-09-11 17:48:16 -050054
55 return m_scheduler.scheduleEvent(expTime, ndn::bind(&Fib::remove, this, name));
akmhoque53353462014-04-22 08:43:45 -050056}
57
58void
akmhoque31d1d4b2014-05-05 22:08:14 -050059Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050060{
akmhoque674b0b12014-05-20 14:33:28 -050061 _LOG_DEBUG("Fib::remove called");
Nick Gordonb7168472016-09-21 13:57:17 -050062 std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
akmhoque157b0a42014-05-13 00:26:37 -050063 if (it != m_table.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050064 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -050065 (it->second).getNexthopList().getNextHops().begin();
66 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050067 //remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -050068 if (isPrefixUpdatable((it->second).getName())) {
69 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050070 }
akmhoque53353462014-04-22 08:43:45 -050071 }
akmhoque674b0b12014-05-20 14:33:28 -050072 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
Nick Gordonb7168472016-09-21 13:57:17 -050073 cancelScheduledExpiringEvent((it->second).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -050074 m_table.erase(it);
75 }
76}
77
Vince Lehman18841082014-08-19 17:15:24 -050078bool
79compareFaceUri(const NextHop& hop, const std::string& faceUri)
80{
81 return hop.getConnectingFaceUri() == faceUri;
82}
83
84void
Vince Lehman942eb7b2014-10-02 10:09:27 -050085Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050086{
87 const ndn::Name& name = entry.getName();
88
Vince Lehman942eb7b2014-10-02 10:09:27 -050089 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -050090 {
91 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -050092 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -050093
94 if (isPrefixUpdatable(name)) {
95 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -050096 registerPrefix(name, it->getConnectingFaceUri(),
97 it->getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050098 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
99 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
100 }
101 }
Vince Lehman942eb7b2014-10-02 10:09:27 -0500102}
103
104void
105Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
106{
107 _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
108
109 const ndn::Name& name = entry.getName();
110 NexthopList& entryHopList = entry.getNexthopList();
111
112 for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
113
114 const std::string& faceUri = it->getConnectingFaceUri();
115
116 // See if the nexthop is installed in NFD's FIB
117 NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
118 installedHops.cend(),
119 bind(&compareFaceUri, _1, faceUri));
120
121 // The next hop is not installed
122 if (foundIt == installedHops.cend()) {
123
124 if (isPrefixUpdatable(name)) {
125 // Remove the nexthop from NDN's FIB
126 unregisterPrefix(name, it->getConnectingFaceUri());
127 }
128
129 // Remove the next hop from the FIB entry
130 _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
131 // Since the iterator will be invalidated on removal, dereference the original
132 // and increment the copy
133 entryHopList.removeNextHop(*(it++));
134 }
135 else {
136 ++it;
137 }
138 }
Vince Lehman18841082014-08-19 17:15:24 -0500139}
140
141void
Vince Lehman942eb7b2014-10-02 10:09:27 -0500142Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -0500143{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500144 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500145
Vince Lehman942eb7b2014-10-02 10:09:27 -0500146 // Get the max possible faces which is the minumum of the configuration setting and
147 // the length of the list of all next hops.
148 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500149
Vince Lehman942eb7b2014-10-02 10:09:27 -0500150 NexthopList hopsToAdd;
151 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500152
Vince Lehman942eb7b2014-10-02 10:09:27 -0500153 // Create a list of next hops to be installed with length == maxFaces
154 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
155 ++it, ++nFaces)
156 {
157 hopsToAdd.addNextHop(*it);
158 }
Vince Lehman18841082014-08-19 17:15:24 -0500159
Nick Gordonb7168472016-09-21 13:57:17 -0500160 std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500161
Vince Lehman18841082014-08-19 17:15:24 -0500162 // New FIB entry
163 if (entryIt == m_table.end()) {
164 _LOG_DEBUG("New FIB Entry");
165
166 // Don't create an entry for a name with no nexthops
Vince Lehman942eb7b2014-10-02 10:09:27 -0500167 if (hopsToAdd.getSize() == 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500168 return;
akmhoque53353462014-04-22 08:43:45 -0500169 }
Vince Lehman18841082014-08-19 17:15:24 -0500170
171 FibEntry entry(name);
172
Vince Lehman942eb7b2014-10-02 10:09:27 -0500173 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500174
175 // Set entry's expiration time point and sequence number
176 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
177 ndn::time::seconds(m_refreshTime));
178 entry.setSeqNo(1);
179
180 // Schedule entry to be refreshed
181 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
182 ndn::time::seconds(m_refreshTime)));
Nick Gordonb7168472016-09-21 13:57:17 -0500183 m_table.emplace(name, entry);
akmhoque53353462014-04-22 08:43:45 -0500184 }
akmhoque157b0a42014-05-13 00:26:37 -0500185 else {
Vince Lehman18841082014-08-19 17:15:24 -0500186 // Existing FIB entry
187 _LOG_DEBUG("Existing FIB Entry");
Nick Gordonb7168472016-09-21 13:57:17 -0500188 FibEntry& entry = (entryIt->second);
Vince Lehman18841082014-08-19 17:15:24 -0500189
190 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500191 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500192 remove(name);
Vince Lehman18841082014-08-19 17:15:24 -0500193 return;
akmhoque53353462014-04-22 08:43:45 -0500194 }
Vince Lehman18841082014-08-19 17:15:24 -0500195
Vince Lehman942eb7b2014-10-02 10:09:27 -0500196 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500197
Vince Lehman942eb7b2014-10-02 10:09:27 -0500198 removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500199
200 // Set entry's expiration time point
201 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
202 ndn::time::seconds(m_refreshTime));
203 // Increment sequence number
204 entry.setSeqNo(entry.getSeqNo() + 1);
205
Vince Lehman942eb7b2014-10-02 10:09:27 -0500206 // Cancel previously scheduled event
Vince Lehman7c603292014-09-11 17:48:16 -0500207 m_scheduler.cancelEvent(entry.getExpiringEventId());
Vince Lehman18841082014-08-19 17:15:24 -0500208
209 // Schedule entry to be refreshed
210 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
211 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500212 }
213}
214
akmhoque53353462014-04-22 08:43:45 -0500215void
akmhoque31d1d4b2014-05-05 22:08:14 -0500216Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500217{
akmhoque674b0b12014-05-20 14:33:28 -0500218 _LOG_DEBUG("Fib::clean called");
Nick Gordonb7168472016-09-21 13:57:17 -0500219 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
220 it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500221 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500222 _LOG_DEBUG("Cancelling Scheduled event. Name: " << (it->second).getName());
223 cancelScheduledExpiringEvent((it->second).getExpiringEventId());
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500224 for (std::set<NextHop, NextHopComparator>::iterator nhit =
Nick Gordonb7168472016-09-21 13:57:17 -0500225 (it->second).getNexthopList().getNextHops().begin();
226 nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500227 //Remove entry from NDN-FIB
Nick Gordonb7168472016-09-21 13:57:17 -0500228 unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500229 }
230 }
akmhoque157b0a42014-05-13 00:26:37 -0500231 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500232 m_table.clear();
233 }
234}
235
Vince Lehman942eb7b2014-10-02 10:09:27 -0500236unsigned int
237Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500238{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500239 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
240 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
241
242 // Allow all faces
243 if (nMaxFaces == 0) {
244 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500245 }
akmhoque157b0a42014-05-13 00:26:37 -0500246 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500247 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500248 }
akmhoque53353462014-04-22 08:43:45 -0500249}
250
akmhoque393d4ff2014-07-16 14:27:03 -0500251bool
252Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500253 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500254 return true;
255 }
256
257 return false;
258}
259
akmhoque53353462014-04-22 08:43:45 -0500260void
akmhoque157b0a42014-05-13 00:26:37 -0500261Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500262 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500263{
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500264 for (std::set<NextHop, NextHopComparator>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500265 it != nl.getNextHops().end(); ++it) {
266 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500267 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500268 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500269 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500270 }
akmhoque53353462014-04-22 08:43:45 -0500271 }
272 }
273}
274
275void
akmhoquec04e7272014-07-02 11:00:14 -0500276Fib::createFace(const std::string& faceUri,
277 const CommandSucceedCallback& onSuccess,
278 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500279{
Vince Lehman27f1add2014-10-16 17:14:46 -0500280 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500281}
282
283void
284Fib::destroyFace(const std::string& faceUri,
285 const CommandSucceedCallback& onSuccess,
286 const CommandFailCallback& onFailure)
287{
288 createFace(faceUri,
289 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
290 onFailure);
291}
292
293void
294Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
295 const CommandSucceedCallback& onSuccess,
296 const CommandFailCallback& onFailure)
297{
298 ndn::nfd::ControlParameters faceParameters;
299 faceParameters
300 .setFaceId(faceDestroyResult.getFaceId());
301 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
302 onSuccess,
303 onFailure);
304}
305
306void
307Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500308 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500309 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500310{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500311 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
akmhoque102aea42014-08-04 10:22:12 -0500312 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500313 ndn::nfd::ControlParameters faceParameters;
314 faceParameters
315 .setName(namePrefix)
316 .setFaceId(faceId)
317 .setFlags(flags)
318 .setCost(faceCost)
319 .setExpirationPeriod(timeout)
320 .setOrigin(128);
321
akmhoque102aea42014-08-04 10:22:12 -0500322 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
323 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500324 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500325 }
326 else {
327 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
328 }
akmhoquec04e7272014-07-02 11:00:14 -0500329}
330
Vince Lehman0a7da612014-10-29 14:39:29 -0500331typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&,
332 const ndn::nfd::ControlParameters&, uint8_t,
333 const CommandSucceedCallback&,
334 const CommandFailCallback&);
335
akmhoquec04e7272014-07-02 11:00:14 -0500336void
337Fib::registerPrefix(const ndn::Name& namePrefix,
338 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500339 uint64_t faceCost,
340 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500341 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500342 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500343 const CommandSucceedCallback& onSuccess,
344 const CommandFailCallback& onFailure)
345
346{
akmhoque060d3022014-08-12 13:35:06 -0500347 ndn::nfd::ControlParameters parameters;
348 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500349 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500350 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500351 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500352 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500353 .setOrigin(128);
akmhoque060d3022014-08-12 13:35:06 -0500354 createFace(faceUri,
Vince Lehman0a7da612014-10-29 14:39:29 -0500355 ndn::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd),
356 this, _1, parameters, times, onSuccess, onFailure),
akmhoque060d3022014-08-12 13:35:06 -0500357 onFailure);
358}
359
360void
361Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
362 const std::string& faceUri,
363 uint8_t times)
364{
365 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
akmhoque157b0a42014-05-13 00:26:37 -0500366 ndn::bind(&Fib::onRegistration, this, _1,
367 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500368 faceUri),
akmhoque102aea42014-08-04 10:22:12 -0500369 ndn::bind(&Fib::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000370 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500371 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500372 parameters,
373 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500374}
akmhoque31d1d4b2014-05-05 22:08:14 -0500375
akmhoquefdbddb12014-05-02 18:35:19 -0500376void
akmhoquec04e7272014-07-02 11:00:14 -0500377Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500378 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500379 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500380 const CommandSucceedCallback& onSuccess,
381 const CommandFailCallback& onFailure)
382{
383 ndn::nfd::ControlParameters controlParameters;
384 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500385 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500386 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500387 .setCost(parameters.getCost())
388 .setFlags(parameters.getFlags())
389 .setExpirationPeriod(parameters.getExpirationPeriod())
akmhoquec04e7272014-07-02 11:00:14 -0500390 .setOrigin(128);
391 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
392 onSuccess,
393 onFailure);
394}
395
396void
akmhoque157b0a42014-05-13 00:26:37 -0500397Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500398{
akmhoque157b0a42014-05-13 00:26:37 -0500399 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500400 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500401 if (faceId > 0) {
402 ndn::nfd::ControlParameters controlParameters;
403 controlParameters
404 .setName(namePrefix)
405 .setFaceId(faceId)
406 .setOrigin(128);
407 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500408 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500409 "Successful in unregistering name"),
akmhoque102aea42014-08-04 10:22:12 -0500410 ndn::bind(&Fib::onUnregistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000411 this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500412 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500413 }
akmhoquefdbddb12014-05-02 18:35:19 -0500414}
415
416void
akmhoque393d4ff2014-07-16 14:27:03 -0500417Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500418{
419 ndn::nfd::ControlParameters parameters;
420 parameters
421 .setName(name)
422 .setStrategy(strategy);
423
424 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500425 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500426 "Successfully set strategy choice"),
Junxiao Shi63bd0342016-08-17 16:57:14 +0000427 bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500428 parameters,
429 count,
akmhoque157b0a42014-05-13 00:26:37 -0500430 "Failed to set strategy choice"));
431}
432
433void
434Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
435 const std::string& message, const std::string& faceUri)
436{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500437 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
438 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500439 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500440 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500441}
442
akmhoque157b0a42014-05-13 00:26:37 -0500443void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500444Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
445 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500446{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500447 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
448 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500449}
450
451void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000452Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500453 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500454 const ndn::nfd::ControlParameters& parameters,
455 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500456 uint8_t times)
457{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000458 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500459 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500460 if (times < 3) {
461 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500462 registerPrefix(parameters.getName(), faceUri,
463 parameters.getCost(),
464 parameters.getExpirationPeriod(),
465 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500466 }
467 else {
468 _LOG_DEBUG("Registration trial given up");
469 }
470}
471
472void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000473Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
474 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500475{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000476 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500477}
478
akmhoque674b0b12014-05-20 14:33:28 -0500479void
akmhoque393d4ff2014-07-16 14:27:03 -0500480Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
481 const std::string& message)
482{
483 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
484 << "for name: " << commandSuccessResult.getName());
485}
486
487void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000488Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
489 const ndn::nfd::ControlParameters& parameters,
490 uint32_t count,
491 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500492{
493 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
494 << "for name: " << parameters.getName());
495 if (count < 3) {
496 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
497 }
498}
499
500void
akmhoque674b0b12014-05-20 14:33:28 -0500501Fib::writeLog()
502{
503 _LOG_DEBUG("-------------------FIB-----------------------------");
Nick Gordonb7168472016-09-21 13:57:17 -0500504 for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
505 it != m_table.end();
akmhoque674b0b12014-05-20 14:33:28 -0500506 ++it) {
Nick Gordonb7168472016-09-21 13:57:17 -0500507 (it->second).writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500508 }
509}
akmhoquefdbddb12014-05-02 18:35:19 -0500510
Nick Gordonfad8e252016-08-11 14:21:38 -0500511} // namespace nlsr