blob: 4a82661154f9227895e1f08b6269cb46547a9efb [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05003 * Copyright (c) 2014-2016, The University of Memphis,
4 * 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 **/
akmhoque53353462014-04-22 08:43:45 -050020#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -050021#include <cmath>
akmhoque157b0a42014-05-13 00:26:37 -050022#include <ndn-cxx/common.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050023
Vince Lehman942eb7b2014-10-02 10:09:27 -050024#include "adjacency-list.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050025#include "common.hpp"
Vince Lehman942eb7b2014-10-02 10:09:27 -050026#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050027#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050028#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -050029#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050031
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("Fib");
35
akmhoque393d4ff2014-07-16 14:27:03 -050036const uint64_t Fib::GRACE_PERIOD = 10;
37
akmhoque53353462014-04-22 08:43:45 -050038using namespace std;
39using namespace ndn;
40
41static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050042fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050043{
Vince Lehman942eb7b2014-10-02 10:09:27 -050044 return fibEntry.getName() == name;
akmhoque53353462014-04-22 08:43:45 -050045}
46
47void
akmhoque31d1d4b2014-05-05 22:08:14 -050048Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050049{
Vince Lehman7c603292014-09-11 17:48:16 -050050 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050051}
52
53
54ndn::EventId
Vince Lehman18841082014-08-19 17:15:24 -050055Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum,
akmhoquec7a79b22014-05-26 08:06:19 -050056 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050057{
Vince Lehman18841082014-08-19 17:15:24 -050058 _LOG_DEBUG("Fib::scheduleEntryExpiration Called");
59 _LOG_INFO("Name: " << name << " Seq Num: " << feSeqNum);
Vince Lehman7c603292014-09-11 17:48:16 -050060
61 return m_scheduler.scheduleEvent(expTime, ndn::bind(&Fib::remove, this, name));
akmhoque53353462014-04-22 08:43:45 -050062}
63
64void
akmhoque31d1d4b2014-05-05 22:08:14 -050065Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050066{
akmhoque674b0b12014-05-20 14:33:28 -050067 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -050068 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050069 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050070 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050071 if (it != m_table.end()) {
Vince Lehmanef21d8e2015-04-01 15:59:39 -050072 for (std::set<NextHop, NextHopComparator>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050073 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050074 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050075 //remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -050076 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -050077 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050078 }
akmhoque53353462014-04-22 08:43:45 -050079 }
akmhoque674b0b12014-05-20 14:33:28 -050080 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050081 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -050082 m_table.erase(it);
83 }
84}
85
Vince Lehman18841082014-08-19 17:15:24 -050086bool
87compareFaceUri(const NextHop& hop, const std::string& faceUri)
88{
89 return hop.getConnectingFaceUri() == faceUri;
90}
91
92void
Vince Lehman942eb7b2014-10-02 10:09:27 -050093Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050094{
95 const ndn::Name& name = entry.getName();
96
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -050098 {
99 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -0500101
102 if (isPrefixUpdatable(name)) {
103 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -0500104 registerPrefix(name, it->getConnectingFaceUri(),
105 it->getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -0500106 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
107 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
108 }
109 }
Vince Lehman942eb7b2014-10-02 10:09:27 -0500110}
111
112void
113Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
114{
115 _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
116
117 const ndn::Name& name = entry.getName();
118 NexthopList& entryHopList = entry.getNexthopList();
119
120 for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
121
122 const std::string& faceUri = it->getConnectingFaceUri();
123
124 // See if the nexthop is installed in NFD's FIB
125 NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
126 installedHops.cend(),
127 bind(&compareFaceUri, _1, faceUri));
128
129 // The next hop is not installed
130 if (foundIt == installedHops.cend()) {
131
132 if (isPrefixUpdatable(name)) {
133 // Remove the nexthop from NDN's FIB
134 unregisterPrefix(name, it->getConnectingFaceUri());
135 }
136
137 // Remove the next hop from the FIB entry
138 _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
139 // Since the iterator will be invalidated on removal, dereference the original
140 // and increment the copy
141 entryHopList.removeNextHop(*(it++));
142 }
143 else {
144 ++it;
145 }
146 }
Vince Lehman18841082014-08-19 17:15:24 -0500147}
148
149void
Vince Lehman942eb7b2014-10-02 10:09:27 -0500150Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -0500151{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500152 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500153
Vince Lehman942eb7b2014-10-02 10:09:27 -0500154 // Get the max possible faces which is the minumum of the configuration setting and
155 // the length of the list of all next hops.
156 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500157
Vince Lehman942eb7b2014-10-02 10:09:27 -0500158 NexthopList hopsToAdd;
159 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500160
Vince Lehman942eb7b2014-10-02 10:09:27 -0500161 // Create a list of next hops to be installed with length == maxFaces
162 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
163 ++it, ++nFaces)
164 {
165 hopsToAdd.addNextHop(*it);
166 }
Vince Lehman18841082014-08-19 17:15:24 -0500167
168 std::list<FibEntry>::iterator entryIt = std::find_if(m_table.begin(),
169 m_table.end(),
170 bind(&fibEntryNameCompare, _1, name));
Vince Lehman942eb7b2014-10-02 10:09:27 -0500171
Vince Lehman18841082014-08-19 17:15:24 -0500172 // New FIB entry
173 if (entryIt == m_table.end()) {
174 _LOG_DEBUG("New FIB Entry");
175
176 // Don't create an entry for a name with no nexthops
Vince Lehman942eb7b2014-10-02 10:09:27 -0500177 if (hopsToAdd.getSize() == 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500178 return;
akmhoque53353462014-04-22 08:43:45 -0500179 }
Vince Lehman18841082014-08-19 17:15:24 -0500180
181 FibEntry entry(name);
182
Vince Lehman942eb7b2014-10-02 10:09:27 -0500183 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500184
185 // Set entry's expiration time point and sequence number
186 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
187 ndn::time::seconds(m_refreshTime));
188 entry.setSeqNo(1);
189
190 // Schedule entry to be refreshed
191 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
192 ndn::time::seconds(m_refreshTime)));
193 m_table.push_back(entry);
akmhoque53353462014-04-22 08:43:45 -0500194 }
akmhoque157b0a42014-05-13 00:26:37 -0500195 else {
Vince Lehman18841082014-08-19 17:15:24 -0500196 // Existing FIB entry
197 _LOG_DEBUG("Existing FIB Entry");
198
199 FibEntry& entry = *entryIt;
200
201 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500202 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500203 remove(name);
Vince Lehman18841082014-08-19 17:15:24 -0500204 return;
akmhoque53353462014-04-22 08:43:45 -0500205 }
Vince Lehman18841082014-08-19 17:15:24 -0500206
Vince Lehman942eb7b2014-10-02 10:09:27 -0500207 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500208
Vince Lehman942eb7b2014-10-02 10:09:27 -0500209 removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500210
211 // Set entry's expiration time point
212 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
213 ndn::time::seconds(m_refreshTime));
214 // Increment sequence number
215 entry.setSeqNo(entry.getSeqNo() + 1);
216
Vince Lehman942eb7b2014-10-02 10:09:27 -0500217 // Cancel previously scheduled event
Vince Lehman7c603292014-09-11 17:48:16 -0500218 m_scheduler.cancelEvent(entry.getExpiringEventId());
Vince Lehman18841082014-08-19 17:15:24 -0500219
220 // Schedule entry to be refreshed
221 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
222 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500223 }
224}
225
akmhoque53353462014-04-22 08:43:45 -0500226void
akmhoque31d1d4b2014-05-05 22:08:14 -0500227Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500228{
akmhoque674b0b12014-05-20 14:33:28 -0500229 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500230 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500231 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500232 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500233 cancelScheduledExpiringEvent((*it).getExpiringEventId());
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500234 for (std::set<NextHop, NextHopComparator>::iterator nhit =
akmhoque393d4ff2014-07-16 14:27:03 -0500235 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500236 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500237 //Remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500238 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500239 }
240 }
akmhoque157b0a42014-05-13 00:26:37 -0500241 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500242 m_table.clear();
243 }
244}
245
Vince Lehman942eb7b2014-10-02 10:09:27 -0500246unsigned int
247Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500248{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500249 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
250 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
251
252 // Allow all faces
253 if (nMaxFaces == 0) {
254 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500255 }
akmhoque157b0a42014-05-13 00:26:37 -0500256 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500257 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500258 }
akmhoque53353462014-04-22 08:43:45 -0500259}
260
akmhoque393d4ff2014-07-16 14:27:03 -0500261bool
262Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500263 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500264 return true;
265 }
266
267 return false;
268}
269
akmhoque53353462014-04-22 08:43:45 -0500270void
akmhoque157b0a42014-05-13 00:26:37 -0500271Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500272 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500273{
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500274 for (std::set<NextHop, NextHopComparator>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500275 it != nl.getNextHops().end(); ++it) {
276 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500277 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500278 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500279 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500280 }
akmhoque53353462014-04-22 08:43:45 -0500281 }
282 }
283}
284
285void
akmhoquec04e7272014-07-02 11:00:14 -0500286Fib::createFace(const std::string& faceUri,
287 const CommandSucceedCallback& onSuccess,
288 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500289{
Vince Lehman27f1add2014-10-16 17:14:46 -0500290 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500291}
292
293void
294Fib::destroyFace(const std::string& faceUri,
295 const CommandSucceedCallback& onSuccess,
296 const CommandFailCallback& onFailure)
297{
298 createFace(faceUri,
299 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
300 onFailure);
301}
302
303void
304Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
305 const CommandSucceedCallback& onSuccess,
306 const CommandFailCallback& onFailure)
307{
308 ndn::nfd::ControlParameters faceParameters;
309 faceParameters
310 .setFaceId(faceDestroyResult.getFaceId());
311 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
312 onSuccess,
313 onFailure);
314}
315
316void
317Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500318 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500319 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500320{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500321 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
akmhoque102aea42014-08-04 10:22:12 -0500322 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500323 ndn::nfd::ControlParameters faceParameters;
324 faceParameters
325 .setName(namePrefix)
326 .setFaceId(faceId)
327 .setFlags(flags)
328 .setCost(faceCost)
329 .setExpirationPeriod(timeout)
330 .setOrigin(128);
331
akmhoque102aea42014-08-04 10:22:12 -0500332 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
333 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500334 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500335 }
336 else {
337 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
338 }
akmhoquec04e7272014-07-02 11:00:14 -0500339}
340
Vince Lehman0a7da612014-10-29 14:39:29 -0500341typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&,
342 const ndn::nfd::ControlParameters&, uint8_t,
343 const CommandSucceedCallback&,
344 const CommandFailCallback&);
345
akmhoquec04e7272014-07-02 11:00:14 -0500346void
347Fib::registerPrefix(const ndn::Name& namePrefix,
348 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500349 uint64_t faceCost,
350 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500351 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500352 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500353 const CommandSucceedCallback& onSuccess,
354 const CommandFailCallback& onFailure)
355
356{
akmhoque060d3022014-08-12 13:35:06 -0500357 ndn::nfd::ControlParameters parameters;
358 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500359 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500360 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500361 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500362 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500363 .setOrigin(128);
akmhoque060d3022014-08-12 13:35:06 -0500364 createFace(faceUri,
Vince Lehman0a7da612014-10-29 14:39:29 -0500365 ndn::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd),
366 this, _1, parameters, times, onSuccess, onFailure),
akmhoque060d3022014-08-12 13:35:06 -0500367 onFailure);
368}
369
370void
371Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
372 const std::string& faceUri,
373 uint8_t times)
374{
375 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
akmhoque157b0a42014-05-13 00:26:37 -0500376 ndn::bind(&Fib::onRegistration, this, _1,
377 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500378 faceUri),
akmhoque102aea42014-08-04 10:22:12 -0500379 ndn::bind(&Fib::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000380 this, _1,
akmhoque102aea42014-08-04 10:22:12 -0500381 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500382 parameters,
383 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500384}
akmhoque31d1d4b2014-05-05 22:08:14 -0500385
akmhoquefdbddb12014-05-02 18:35:19 -0500386void
akmhoquec04e7272014-07-02 11:00:14 -0500387Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500388 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500389 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500390 const CommandSucceedCallback& onSuccess,
391 const CommandFailCallback& onFailure)
392{
393 ndn::nfd::ControlParameters controlParameters;
394 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500395 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500396 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500397 .setCost(parameters.getCost())
398 .setFlags(parameters.getFlags())
399 .setExpirationPeriod(parameters.getExpirationPeriod())
akmhoquec04e7272014-07-02 11:00:14 -0500400 .setOrigin(128);
401 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
402 onSuccess,
403 onFailure);
404}
405
406void
akmhoque157b0a42014-05-13 00:26:37 -0500407Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500408{
akmhoque157b0a42014-05-13 00:26:37 -0500409 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500410 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500411 if (faceId > 0) {
412 ndn::nfd::ControlParameters controlParameters;
413 controlParameters
414 .setName(namePrefix)
415 .setFaceId(faceId)
416 .setOrigin(128);
417 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500418 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500419 "Successful in unregistering name"),
akmhoque102aea42014-08-04 10:22:12 -0500420 ndn::bind(&Fib::onUnregistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000421 this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500422 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500423 }
akmhoquefdbddb12014-05-02 18:35:19 -0500424}
425
426void
akmhoque393d4ff2014-07-16 14:27:03 -0500427Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500428{
429 ndn::nfd::ControlParameters parameters;
430 parameters
431 .setName(name)
432 .setStrategy(strategy);
433
434 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500435 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500436 "Successfully set strategy choice"),
Junxiao Shi63bd0342016-08-17 16:57:14 +0000437 bind(&Fib::onSetStrategyFailure, this, _1,
akmhoque393d4ff2014-07-16 14:27:03 -0500438 parameters,
439 count,
akmhoque157b0a42014-05-13 00:26:37 -0500440 "Failed to set strategy choice"));
441}
442
443void
444Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
445 const std::string& message, const std::string& faceUri)
446{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500447 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
448 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500449 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500450 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500451}
452
akmhoque157b0a42014-05-13 00:26:37 -0500453void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500454Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
455 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500456{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500457 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
458 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500459}
460
461void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000462Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500463 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500464 const ndn::nfd::ControlParameters& parameters,
465 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500466 uint8_t times)
467{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000468 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoque060d3022014-08-12 13:35:06 -0500469 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500470 if (times < 3) {
471 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500472 registerPrefix(parameters.getName(), faceUri,
473 parameters.getCost(),
474 parameters.getExpirationPeriod(),
475 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500476 }
477 else {
478 _LOG_DEBUG("Registration trial given up");
479 }
480}
481
482void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000483Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
484 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500485{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000486 _LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500487}
488
akmhoque674b0b12014-05-20 14:33:28 -0500489void
akmhoque393d4ff2014-07-16 14:27:03 -0500490Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
491 const std::string& message)
492{
493 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
494 << "for name: " << commandSuccessResult.getName());
495}
496
497void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000498Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
499 const ndn::nfd::ControlParameters& parameters,
500 uint32_t count,
501 const std::string& message)
akmhoque393d4ff2014-07-16 14:27:03 -0500502{
503 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
504 << "for name: " << parameters.getName());
505 if (count < 3) {
506 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
507 }
508}
509
510void
akmhoque674b0b12014-05-20 14:33:28 -0500511Fib::writeLog()
512{
513 _LOG_DEBUG("-------------------FIB-----------------------------");
514 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
515 ++it) {
516 (*it).writeLog();
517 }
518}
akmhoquefdbddb12014-05-02 18:35:19 -0500519
Nick Gordonfad8e252016-08-11 14:21:38 -0500520} // namespace nlsr