blob: 16ed8a8e6436416b750f6385b1e995e0ea3916bc [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
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/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -050024#include <cmath>
akmhoque157b0a42014-05-13 00:26:37 -050025#include <ndn-cxx/common.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050026
Vince Lehman942eb7b2014-10-02 10:09:27 -050027#include "adjacency-list.hpp"
28#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
43static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050044fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050045{
Vince Lehman942eb7b2014-10-02 10:09:27 -050046 return fibEntry.getName() == name;
akmhoque53353462014-04-22 08:43:45 -050047}
48
49void
akmhoque31d1d4b2014-05-05 22:08:14 -050050Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050051{
Vince Lehman7c603292014-09-11 17:48:16 -050052 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050053}
54
55
56ndn::EventId
Vince Lehman18841082014-08-19 17:15:24 -050057Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum,
akmhoquec7a79b22014-05-26 08:06:19 -050058 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050059{
Vince Lehman18841082014-08-19 17:15:24 -050060 _LOG_DEBUG("Fib::scheduleEntryExpiration Called");
61 _LOG_INFO("Name: " << name << " Seq Num: " << feSeqNum);
Vince Lehman7c603292014-09-11 17:48:16 -050062
63 return m_scheduler.scheduleEvent(expTime, ndn::bind(&Fib::remove, this, name));
akmhoque53353462014-04-22 08:43:45 -050064}
65
66void
akmhoque31d1d4b2014-05-05 22:08:14 -050067Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050068{
akmhoque674b0b12014-05-20 14:33:28 -050069 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -050070 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050071 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050072 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050073 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050074 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050075 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050076 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050077 //remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -050078 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -050079 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050080 }
akmhoque53353462014-04-22 08:43:45 -050081 }
akmhoque674b0b12014-05-20 14:33:28 -050082 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050083 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -050084 m_table.erase(it);
85 }
86}
87
Vince Lehman18841082014-08-19 17:15:24 -050088bool
89compareFaceUri(const NextHop& hop, const std::string& faceUri)
90{
91 return hop.getConnectingFaceUri() == faceUri;
92}
93
94void
Vince Lehman942eb7b2014-10-02 10:09:27 -050095Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050096{
97 const ndn::Name& name = entry.getName();
98
Vince Lehman942eb7b2014-10-02 10:09:27 -050099 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -0500100 {
101 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500102 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -0500103
104 if (isPrefixUpdatable(name)) {
105 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -0500106 registerPrefix(name, it->getConnectingFaceUri(),
107 it->getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -0500108 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
109 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
110 }
111 }
Vince Lehman942eb7b2014-10-02 10:09:27 -0500112
113 entry.getNexthopList().sort();
114}
115
116void
117Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
118{
119 _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
120
121 const ndn::Name& name = entry.getName();
122 NexthopList& entryHopList = entry.getNexthopList();
123
124 for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
125
126 const std::string& faceUri = it->getConnectingFaceUri();
127
128 // See if the nexthop is installed in NFD's FIB
129 NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
130 installedHops.cend(),
131 bind(&compareFaceUri, _1, faceUri));
132
133 // The next hop is not installed
134 if (foundIt == installedHops.cend()) {
135
136 if (isPrefixUpdatable(name)) {
137 // Remove the nexthop from NDN's FIB
138 unregisterPrefix(name, it->getConnectingFaceUri());
139 }
140
141 // Remove the next hop from the FIB entry
142 _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
143 // Since the iterator will be invalidated on removal, dereference the original
144 // and increment the copy
145 entryHopList.removeNextHop(*(it++));
146 }
147 else {
148 ++it;
149 }
150 }
Vince Lehman18841082014-08-19 17:15:24 -0500151}
152
153void
Vince Lehman942eb7b2014-10-02 10:09:27 -0500154Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -0500155{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500156 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500157
Vince Lehman942eb7b2014-10-02 10:09:27 -0500158 // Sort all of the next hops so lower cost hops are prioritized
159 allHops.sort();
Vince Lehman18841082014-08-19 17:15:24 -0500160
Vince Lehman942eb7b2014-10-02 10:09:27 -0500161 // Get the max possible faces which is the minumum of the configuration setting and
162 // the length of the list of all next hops.
163 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500164
Vince Lehman942eb7b2014-10-02 10:09:27 -0500165 NexthopList hopsToAdd;
166 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500167
Vince Lehman942eb7b2014-10-02 10:09:27 -0500168 // Create a list of next hops to be installed with length == maxFaces
169 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
170 ++it, ++nFaces)
171 {
172 hopsToAdd.addNextHop(*it);
173 }
Vince Lehman18841082014-08-19 17:15:24 -0500174
175 std::list<FibEntry>::iterator entryIt = std::find_if(m_table.begin(),
176 m_table.end(),
177 bind(&fibEntryNameCompare, _1, name));
Vince Lehman942eb7b2014-10-02 10:09:27 -0500178
Vince Lehman18841082014-08-19 17:15:24 -0500179 // New FIB entry
180 if (entryIt == m_table.end()) {
181 _LOG_DEBUG("New FIB Entry");
182
183 // Don't create an entry for a name with no nexthops
Vince Lehman942eb7b2014-10-02 10:09:27 -0500184 if (hopsToAdd.getSize() == 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500185 return;
akmhoque53353462014-04-22 08:43:45 -0500186 }
Vince Lehman18841082014-08-19 17:15:24 -0500187
188 FibEntry entry(name);
189
Vince Lehman942eb7b2014-10-02 10:09:27 -0500190 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500191
192 // Set entry's expiration time point and sequence number
193 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
194 ndn::time::seconds(m_refreshTime));
195 entry.setSeqNo(1);
196
197 // Schedule entry to be refreshed
198 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
199 ndn::time::seconds(m_refreshTime)));
200 m_table.push_back(entry);
akmhoque53353462014-04-22 08:43:45 -0500201 }
akmhoque157b0a42014-05-13 00:26:37 -0500202 else {
Vince Lehman18841082014-08-19 17:15:24 -0500203 // Existing FIB entry
204 _LOG_DEBUG("Existing FIB Entry");
205
206 FibEntry& entry = *entryIt;
207
208 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500209 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500210 remove(name);
Vince Lehman18841082014-08-19 17:15:24 -0500211 return;
akmhoque53353462014-04-22 08:43:45 -0500212 }
Vince Lehman18841082014-08-19 17:15:24 -0500213
Vince Lehman942eb7b2014-10-02 10:09:27 -0500214 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500215
Vince Lehman942eb7b2014-10-02 10:09:27 -0500216 removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500217
218 // Set entry's expiration time point
219 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
220 ndn::time::seconds(m_refreshTime));
221 // Increment sequence number
222 entry.setSeqNo(entry.getSeqNo() + 1);
223
Vince Lehman942eb7b2014-10-02 10:09:27 -0500224 // Cancel previously scheduled event
Vince Lehman7c603292014-09-11 17:48:16 -0500225 m_scheduler.cancelEvent(entry.getExpiringEventId());
Vince Lehman18841082014-08-19 17:15:24 -0500226
227 // Schedule entry to be refreshed
228 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
229 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500230 }
231}
232
akmhoque53353462014-04-22 08:43:45 -0500233void
akmhoque31d1d4b2014-05-05 22:08:14 -0500234Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500235{
akmhoque674b0b12014-05-20 14:33:28 -0500236 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500237 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500238 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500239 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500240 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500241 for (std::list<NextHop>::iterator nhit =
akmhoque393d4ff2014-07-16 14:27:03 -0500242 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500243 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500244 //Remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500245 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500246 }
247 }
akmhoque157b0a42014-05-13 00:26:37 -0500248 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500249 m_table.clear();
250 }
251}
252
Vince Lehman942eb7b2014-10-02 10:09:27 -0500253unsigned int
254Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500255{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500256 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
257 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
258
259 // Allow all faces
260 if (nMaxFaces == 0) {
261 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500262 }
akmhoque157b0a42014-05-13 00:26:37 -0500263 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500264 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500265 }
akmhoque53353462014-04-22 08:43:45 -0500266}
267
akmhoque393d4ff2014-07-16 14:27:03 -0500268bool
269Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500270 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500271 return true;
272 }
273
274 return false;
275}
276
akmhoque53353462014-04-22 08:43:45 -0500277void
akmhoque157b0a42014-05-13 00:26:37 -0500278Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500279 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500280{
akmhoquefdbddb12014-05-02 18:35:19 -0500281 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500282 it != nl.getNextHops().end(); ++it) {
283 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500284 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500285 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500286 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500287 }
akmhoque53353462014-04-22 08:43:45 -0500288 }
289 }
290}
291
292void
akmhoquec04e7272014-07-02 11:00:14 -0500293Fib::createFace(const std::string& faceUri,
294 const CommandSucceedCallback& onSuccess,
295 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500296{
Vince Lehman27f1add2014-10-16 17:14:46 -0500297 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500298}
299
300void
301Fib::destroyFace(const std::string& faceUri,
302 const CommandSucceedCallback& onSuccess,
303 const CommandFailCallback& onFailure)
304{
305 createFace(faceUri,
306 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
307 onFailure);
308}
309
310void
311Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
312 const CommandSucceedCallback& onSuccess,
313 const CommandFailCallback& onFailure)
314{
315 ndn::nfd::ControlParameters faceParameters;
316 faceParameters
317 .setFaceId(faceDestroyResult.getFaceId());
318 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
319 onSuccess,
320 onFailure);
321}
322
323void
324Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500325 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500326 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500327{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500328 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
akmhoque102aea42014-08-04 10:22:12 -0500329 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500330 ndn::nfd::ControlParameters faceParameters;
331 faceParameters
332 .setName(namePrefix)
333 .setFaceId(faceId)
334 .setFlags(flags)
335 .setCost(faceCost)
336 .setExpirationPeriod(timeout)
337 .setOrigin(128);
338
akmhoque102aea42014-08-04 10:22:12 -0500339 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
340 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500341 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500342 }
343 else {
344 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
345 }
akmhoquec04e7272014-07-02 11:00:14 -0500346}
347
348void
349Fib::registerPrefix(const ndn::Name& namePrefix,
350 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500351 uint64_t faceCost,
352 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500353 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500354 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500355 const CommandSucceedCallback& onSuccess,
356 const CommandFailCallback& onFailure)
357
358{
akmhoque060d3022014-08-12 13:35:06 -0500359 ndn::nfd::ControlParameters parameters;
360 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500361 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500362 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500363 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500364 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500365 .setOrigin(128);
akmhoque060d3022014-08-12 13:35:06 -0500366 createFace(faceUri,
367 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
368 parameters,
369 times, onSuccess, onFailure),
370 onFailure);
371}
372
373void
374Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
375 const std::string& faceUri,
376 uint8_t times)
377{
378 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
akmhoque157b0a42014-05-13 00:26:37 -0500379 ndn::bind(&Fib::onRegistration, this, _1,
380 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500381 faceUri),
akmhoque102aea42014-08-04 10:22:12 -0500382 ndn::bind(&Fib::onRegistrationFailure,
383 this, _1, _2,
384 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500385 parameters,
386 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500387}
akmhoque31d1d4b2014-05-05 22:08:14 -0500388
akmhoquefdbddb12014-05-02 18:35:19 -0500389void
akmhoquec04e7272014-07-02 11:00:14 -0500390Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500391 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500392 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500393 const CommandSucceedCallback& onSuccess,
394 const CommandFailCallback& onFailure)
395{
396 ndn::nfd::ControlParameters controlParameters;
397 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500398 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500399 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500400 .setCost(parameters.getCost())
401 .setFlags(parameters.getFlags())
402 .setExpirationPeriod(parameters.getExpirationPeriod())
akmhoquec04e7272014-07-02 11:00:14 -0500403 .setOrigin(128);
404 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
405 onSuccess,
406 onFailure);
407}
408
409void
akmhoque157b0a42014-05-13 00:26:37 -0500410Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500411{
akmhoque157b0a42014-05-13 00:26:37 -0500412 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500413 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500414 if (faceId > 0) {
415 ndn::nfd::ControlParameters controlParameters;
416 controlParameters
417 .setName(namePrefix)
418 .setFaceId(faceId)
419 .setOrigin(128);
420 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500421 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500422 "Successful in unregistering name"),
akmhoque102aea42014-08-04 10:22:12 -0500423 ndn::bind(&Fib::onUnregistrationFailure,
424 this, _1, _2,
akmhoquefdbddb12014-05-02 18:35:19 -0500425 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500426 }
akmhoquefdbddb12014-05-02 18:35:19 -0500427}
428
429void
akmhoque393d4ff2014-07-16 14:27:03 -0500430Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500431{
432 ndn::nfd::ControlParameters parameters;
433 parameters
434 .setName(name)
435 .setStrategy(strategy);
436
437 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500438 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500439 "Successfully set strategy choice"),
akmhoque393d4ff2014-07-16 14:27:03 -0500440 bind(&Fib::onSetStrategyFailure, this, _1, _2,
441 parameters,
442 count,
akmhoque157b0a42014-05-13 00:26:37 -0500443 "Failed to set strategy choice"));
444}
445
446void
447Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
448 const std::string& message, const std::string& faceUri)
449{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500450 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
451 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500452 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500453 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500454}
455
akmhoque157b0a42014-05-13 00:26:37 -0500456void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500457Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
458 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500459{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500460 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
461 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500462}
463
464void
akmhoque102aea42014-08-04 10:22:12 -0500465Fib::onRegistrationFailure(uint32_t code, const std::string& error,
466 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500467 const ndn::nfd::ControlParameters& parameters,
468 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500469 uint8_t times)
470{
471 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoque060d3022014-08-12 13:35:06 -0500472 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500473 if (times < 3) {
474 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500475 registerPrefix(parameters.getName(), faceUri,
476 parameters.getCost(),
477 parameters.getExpirationPeriod(),
478 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500479 }
480 else {
481 _LOG_DEBUG("Registration trial given up");
482 }
483}
484
485void
486Fib::onUnregistrationFailure(uint32_t code, const std::string& error,
487 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500488{
akmhoque2f423352014-06-03 11:49:35 -0500489 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500490}
491
akmhoque674b0b12014-05-20 14:33:28 -0500492void
akmhoque393d4ff2014-07-16 14:27:03 -0500493Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
494 const std::string& message)
495{
496 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
497 << "for name: " << commandSuccessResult.getName());
498}
499
500void
501Fib::onSetStrategyFailure(uint32_t code, const std::string& error,
502 const ndn::nfd::ControlParameters& parameters,
503 uint32_t count,
504 const std::string& message)
505{
506 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
507 << "for name: " << parameters.getName());
508 if (count < 3) {
509 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
510 }
511}
512
513void
akmhoque674b0b12014-05-20 14:33:28 -0500514Fib::writeLog()
515{
516 _LOG_DEBUG("-------------------FIB-----------------------------");
517 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
518 ++it) {
519 (*it).writeLog();
520 }
521}
akmhoquefdbddb12014-05-02 18:35:19 -0500522
akmhoque53353462014-04-22 08:43:45 -0500523} //namespace nlsr