blob: 1815b274e07bc44663d33210dd9361957da7ad3d [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{
297 ndn::nfd::ControlParameters faceParameters;
298 faceParameters
akmhoquec04e7272014-07-02 11:00:14 -0500299 .setUri(faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500300 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
akmhoquec04e7272014-07-02 11:00:14 -0500301 onSuccess,
302 onFailure);
303}
304
305void
306Fib::destroyFace(const std::string& faceUri,
307 const CommandSucceedCallback& onSuccess,
308 const CommandFailCallback& onFailure)
309{
310 createFace(faceUri,
311 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
312 onFailure);
313}
314
315void
316Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
317 const CommandSucceedCallback& onSuccess,
318 const CommandFailCallback& onFailure)
319{
320 ndn::nfd::ControlParameters faceParameters;
321 faceParameters
322 .setFaceId(faceDestroyResult.getFaceId());
323 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
324 onSuccess,
325 onFailure);
326}
327
328void
329Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500330 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500331 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500332{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500333 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
akmhoque102aea42014-08-04 10:22:12 -0500334 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500335 ndn::nfd::ControlParameters faceParameters;
336 faceParameters
337 .setName(namePrefix)
338 .setFaceId(faceId)
339 .setFlags(flags)
340 .setCost(faceCost)
341 .setExpirationPeriod(timeout)
342 .setOrigin(128);
343
akmhoque102aea42014-08-04 10:22:12 -0500344 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
345 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500346 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500347 }
348 else {
349 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
350 }
akmhoquec04e7272014-07-02 11:00:14 -0500351}
352
353void
354Fib::registerPrefix(const ndn::Name& namePrefix,
355 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500356 uint64_t faceCost,
357 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500358 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500359 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500360 const CommandSucceedCallback& onSuccess,
361 const CommandFailCallback& onFailure)
362
363{
akmhoque060d3022014-08-12 13:35:06 -0500364 ndn::nfd::ControlParameters parameters;
365 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500366 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500367 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500368 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500369 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500370 .setOrigin(128);
akmhoque060d3022014-08-12 13:35:06 -0500371 createFace(faceUri,
372 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
373 parameters,
374 times, onSuccess, onFailure),
375 onFailure);
376}
377
378void
379Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
380 const std::string& faceUri,
381 uint8_t times)
382{
383 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
akmhoque157b0a42014-05-13 00:26:37 -0500384 ndn::bind(&Fib::onRegistration, this, _1,
385 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500386 faceUri),
akmhoque102aea42014-08-04 10:22:12 -0500387 ndn::bind(&Fib::onRegistrationFailure,
388 this, _1, _2,
389 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500390 parameters,
391 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500392}
akmhoque31d1d4b2014-05-05 22:08:14 -0500393
akmhoquefdbddb12014-05-02 18:35:19 -0500394void
akmhoquec04e7272014-07-02 11:00:14 -0500395Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500396 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500397 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500398 const CommandSucceedCallback& onSuccess,
399 const CommandFailCallback& onFailure)
400{
401 ndn::nfd::ControlParameters controlParameters;
402 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500403 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500404 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500405 .setCost(parameters.getCost())
406 .setFlags(parameters.getFlags())
407 .setExpirationPeriod(parameters.getExpirationPeriod())
akmhoquec04e7272014-07-02 11:00:14 -0500408 .setOrigin(128);
409 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
410 onSuccess,
411 onFailure);
412}
413
414void
akmhoque157b0a42014-05-13 00:26:37 -0500415Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500416{
akmhoque157b0a42014-05-13 00:26:37 -0500417 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500418 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500419 if (faceId > 0) {
420 ndn::nfd::ControlParameters controlParameters;
421 controlParameters
422 .setName(namePrefix)
423 .setFaceId(faceId)
424 .setOrigin(128);
425 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500426 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500427 "Successful in unregistering name"),
akmhoque102aea42014-08-04 10:22:12 -0500428 ndn::bind(&Fib::onUnregistrationFailure,
429 this, _1, _2,
akmhoquefdbddb12014-05-02 18:35:19 -0500430 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500431 }
akmhoquefdbddb12014-05-02 18:35:19 -0500432}
433
434void
akmhoque393d4ff2014-07-16 14:27:03 -0500435Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500436{
437 ndn::nfd::ControlParameters parameters;
438 parameters
439 .setName(name)
440 .setStrategy(strategy);
441
442 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500443 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500444 "Successfully set strategy choice"),
akmhoque393d4ff2014-07-16 14:27:03 -0500445 bind(&Fib::onSetStrategyFailure, this, _1, _2,
446 parameters,
447 count,
akmhoque157b0a42014-05-13 00:26:37 -0500448 "Failed to set strategy choice"));
449}
450
451void
452Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
453 const std::string& message, const std::string& faceUri)
454{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500455 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
456 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500457 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500458 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500459}
460
akmhoque157b0a42014-05-13 00:26:37 -0500461void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500462Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
463 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500464{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500465 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
466 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500467}
468
469void
akmhoque102aea42014-08-04 10:22:12 -0500470Fib::onRegistrationFailure(uint32_t code, const std::string& error,
471 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500472 const ndn::nfd::ControlParameters& parameters,
473 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500474 uint8_t times)
475{
476 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoque060d3022014-08-12 13:35:06 -0500477 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500478 if (times < 3) {
479 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500480 registerPrefix(parameters.getName(), faceUri,
481 parameters.getCost(),
482 parameters.getExpirationPeriod(),
483 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500484 }
485 else {
486 _LOG_DEBUG("Registration trial given up");
487 }
488}
489
490void
491Fib::onUnregistrationFailure(uint32_t code, const std::string& error,
492 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500493{
akmhoque2f423352014-06-03 11:49:35 -0500494 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500495}
496
akmhoque674b0b12014-05-20 14:33:28 -0500497void
akmhoque393d4ff2014-07-16 14:27:03 -0500498Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
499 const std::string& message)
500{
501 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
502 << "for name: " << commandSuccessResult.getName());
503}
504
505void
506Fib::onSetStrategyFailure(uint32_t code, const std::string& error,
507 const ndn::nfd::ControlParameters& parameters,
508 uint32_t count,
509 const std::string& message)
510{
511 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
512 << "for name: " << parameters.getName());
513 if (count < 3) {
514 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
515 }
516}
517
518void
akmhoque674b0b12014-05-20 14:33:28 -0500519Fib::writeLog()
520{
521 _LOG_DEBUG("-------------------FIB-----------------------------");
522 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
523 ++it) {
524 (*it).writeLog();
525 }
526}
akmhoquefdbddb12014-05-02 18:35:19 -0500527
akmhoque53353462014-04-22 08:43:45 -0500528} //namespace nlsr