blob: dad73ca5e3215da61d15698dd68a991971018739 [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/>.
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"
Vince Lehman0a7da612014-10-29 14:39:29 -050028#include "common.hpp"
Vince Lehman942eb7b2014-10-02 10:09:27 -050029#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050030#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050031#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -050032#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050033#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
akmhoque674b0b12014-05-20 14:33:28 -050037INIT_LOGGER("Fib");
38
akmhoque393d4ff2014-07-16 14:27:03 -050039const uint64_t Fib::GRACE_PERIOD = 10;
40
akmhoque53353462014-04-22 08:43:45 -050041using namespace std;
42using namespace ndn;
43
44static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050045fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050046{
Vince Lehman942eb7b2014-10-02 10:09:27 -050047 return fibEntry.getName() == name;
akmhoque53353462014-04-22 08:43:45 -050048}
49
50void
akmhoque31d1d4b2014-05-05 22:08:14 -050051Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050052{
Vince Lehman7c603292014-09-11 17:48:16 -050053 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050054}
55
56
57ndn::EventId
Vince Lehman18841082014-08-19 17:15:24 -050058Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum,
akmhoquec7a79b22014-05-26 08:06:19 -050059 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050060{
Vince Lehman18841082014-08-19 17:15:24 -050061 _LOG_DEBUG("Fib::scheduleEntryExpiration Called");
62 _LOG_INFO("Name: " << name << " Seq Num: " << feSeqNum);
Vince Lehman7c603292014-09-11 17:48:16 -050063
64 return m_scheduler.scheduleEvent(expTime, ndn::bind(&Fib::remove, this, name));
akmhoque53353462014-04-22 08:43:45 -050065}
66
67void
akmhoque31d1d4b2014-05-05 22:08:14 -050068Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050069{
akmhoque674b0b12014-05-20 14:33:28 -050070 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -050071 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050072 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050073 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050074 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050075 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050076 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050077 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050078 //remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -050079 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -050080 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050081 }
akmhoque53353462014-04-22 08:43:45 -050082 }
akmhoque674b0b12014-05-20 14:33:28 -050083 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050084 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -050085 m_table.erase(it);
86 }
87}
88
Vince Lehman18841082014-08-19 17:15:24 -050089bool
90compareFaceUri(const NextHop& hop, const std::string& faceUri)
91{
92 return hop.getConnectingFaceUri() == faceUri;
93}
94
95void
Vince Lehman942eb7b2014-10-02 10:09:27 -050096Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050097{
98 const ndn::Name& name = entry.getName();
99
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100 for (NexthopList::iterator it = hopsToAdd.begin(); it != hopsToAdd.end(); ++it)
Vince Lehman18841082014-08-19 17:15:24 -0500101 {
102 // Add nexthop to FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500103 entry.getNexthopList().addNextHop(*it);
Vince Lehman18841082014-08-19 17:15:24 -0500104
105 if (isPrefixUpdatable(name)) {
106 // Add nexthop to NDN-FIB
Vince Lehman942eb7b2014-10-02 10:09:27 -0500107 registerPrefix(name, it->getConnectingFaceUri(),
108 it->getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -0500109 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
110 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
111 }
112 }
Vince Lehman942eb7b2014-10-02 10:09:27 -0500113
114 entry.getNexthopList().sort();
115}
116
117void
118Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
119{
120 _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
121
122 const ndn::Name& name = entry.getName();
123 NexthopList& entryHopList = entry.getNexthopList();
124
125 for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
126
127 const std::string& faceUri = it->getConnectingFaceUri();
128
129 // See if the nexthop is installed in NFD's FIB
130 NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
131 installedHops.cend(),
132 bind(&compareFaceUri, _1, faceUri));
133
134 // The next hop is not installed
135 if (foundIt == installedHops.cend()) {
136
137 if (isPrefixUpdatable(name)) {
138 // Remove the nexthop from NDN's FIB
139 unregisterPrefix(name, it->getConnectingFaceUri());
140 }
141
142 // Remove the next hop from the FIB entry
143 _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
144 // Since the iterator will be invalidated on removal, dereference the original
145 // and increment the copy
146 entryHopList.removeNextHop(*(it++));
147 }
148 else {
149 ++it;
150 }
151 }
Vince Lehman18841082014-08-19 17:15:24 -0500152}
153
154void
Vince Lehman942eb7b2014-10-02 10:09:27 -0500155Fib::update(const ndn::Name& name, NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -0500156{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500157 _LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -0500158
Vince Lehman942eb7b2014-10-02 10:09:27 -0500159 // Sort all of the next hops so lower cost hops are prioritized
160 allHops.sort();
Vince Lehman18841082014-08-19 17:15:24 -0500161
Vince Lehman942eb7b2014-10-02 10:09:27 -0500162 // Get the max possible faces which is the minumum of the configuration setting and
163 // the length of the list of all next hops.
164 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -0500165
Vince Lehman942eb7b2014-10-02 10:09:27 -0500166 NexthopList hopsToAdd;
167 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -0500168
Vince Lehman942eb7b2014-10-02 10:09:27 -0500169 // Create a list of next hops to be installed with length == maxFaces
170 for (NexthopList::iterator it = allHops.begin(); it != allHops.end() && nFaces < maxFaces;
171 ++it, ++nFaces)
172 {
173 hopsToAdd.addNextHop(*it);
174 }
Vince Lehman18841082014-08-19 17:15:24 -0500175
176 std::list<FibEntry>::iterator entryIt = std::find_if(m_table.begin(),
177 m_table.end(),
178 bind(&fibEntryNameCompare, _1, name));
Vince Lehman942eb7b2014-10-02 10:09:27 -0500179
Vince Lehman18841082014-08-19 17:15:24 -0500180 // New FIB entry
181 if (entryIt == m_table.end()) {
182 _LOG_DEBUG("New FIB Entry");
183
184 // Don't create an entry for a name with no nexthops
Vince Lehman942eb7b2014-10-02 10:09:27 -0500185 if (hopsToAdd.getSize() == 0) {
Vince Lehman18841082014-08-19 17:15:24 -0500186 return;
akmhoque53353462014-04-22 08:43:45 -0500187 }
Vince Lehman18841082014-08-19 17:15:24 -0500188
189 FibEntry entry(name);
190
Vince Lehman942eb7b2014-10-02 10:09:27 -0500191 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500192
193 // Set entry's expiration time point and sequence number
194 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
195 ndn::time::seconds(m_refreshTime));
196 entry.setSeqNo(1);
197
198 // Schedule entry to be refreshed
199 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
200 ndn::time::seconds(m_refreshTime)));
201 m_table.push_back(entry);
akmhoque53353462014-04-22 08:43:45 -0500202 }
akmhoque157b0a42014-05-13 00:26:37 -0500203 else {
Vince Lehman18841082014-08-19 17:15:24 -0500204 // Existing FIB entry
205 _LOG_DEBUG("Existing FIB Entry");
206
207 FibEntry& entry = *entryIt;
208
209 // Remove empty FIB entry
Vince Lehman942eb7b2014-10-02 10:09:27 -0500210 if (hopsToAdd.getSize() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500211 remove(name);
Vince Lehman18841082014-08-19 17:15:24 -0500212 return;
akmhoque53353462014-04-22 08:43:45 -0500213 }
Vince Lehman18841082014-08-19 17:15:24 -0500214
Vince Lehman942eb7b2014-10-02 10:09:27 -0500215 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500216
Vince Lehman942eb7b2014-10-02 10:09:27 -0500217 removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500218
219 // Set entry's expiration time point
220 entry.setExpirationTimePoint(ndn::time::system_clock::now() +
221 ndn::time::seconds(m_refreshTime));
222 // Increment sequence number
223 entry.setSeqNo(entry.getSeqNo() + 1);
224
Vince Lehman942eb7b2014-10-02 10:09:27 -0500225 // Cancel previously scheduled event
Vince Lehman7c603292014-09-11 17:48:16 -0500226 m_scheduler.cancelEvent(entry.getExpiringEventId());
Vince Lehman18841082014-08-19 17:15:24 -0500227
228 // Schedule entry to be refreshed
229 entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
230 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500231 }
232}
233
akmhoque53353462014-04-22 08:43:45 -0500234void
akmhoque31d1d4b2014-05-05 22:08:14 -0500235Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500236{
akmhoque674b0b12014-05-20 14:33:28 -0500237 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500238 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500239 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500240 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500242 for (std::list<NextHop>::iterator nhit =
akmhoque393d4ff2014-07-16 14:27:03 -0500243 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500244 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500245 //Remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500246 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500247 }
248 }
akmhoque157b0a42014-05-13 00:26:37 -0500249 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500250 m_table.clear();
251 }
252}
253
Vince Lehman942eb7b2014-10-02 10:09:27 -0500254unsigned int
255Fib::getNumberOfFacesForName(NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500256{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500257 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
258 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
259
260 // Allow all faces
261 if (nMaxFaces == 0) {
262 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500263 }
akmhoque157b0a42014-05-13 00:26:37 -0500264 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500265 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500266 }
akmhoque53353462014-04-22 08:43:45 -0500267}
268
akmhoque393d4ff2014-07-16 14:27:03 -0500269bool
270Fib::isPrefixUpdatable(const ndn::Name& name) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500271 if (!m_adjacencyList.isNeighbor(name)) {
akmhoque393d4ff2014-07-16 14:27:03 -0500272 return true;
273 }
274
275 return false;
276}
277
akmhoque53353462014-04-22 08:43:45 -0500278void
akmhoque157b0a42014-05-13 00:26:37 -0500279Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500280 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500281{
akmhoquefdbddb12014-05-02 18:35:19 -0500282 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500283 it != nl.getNextHops().end(); ++it) {
284 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500285 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500286 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500287 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500288 }
akmhoque53353462014-04-22 08:43:45 -0500289 }
290 }
291}
292
293void
akmhoquec04e7272014-07-02 11:00:14 -0500294Fib::createFace(const std::string& faceUri,
295 const CommandSucceedCallback& onSuccess,
296 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500297{
Vince Lehman27f1add2014-10-16 17:14:46 -0500298 m_faceController.createFace(faceUri, onSuccess, onFailure);
akmhoquec04e7272014-07-02 11:00:14 -0500299}
300
301void
302Fib::destroyFace(const std::string& faceUri,
303 const CommandSucceedCallback& onSuccess,
304 const CommandFailCallback& onFailure)
305{
306 createFace(faceUri,
307 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
308 onFailure);
309}
310
311void
312Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
313 const CommandSucceedCallback& onSuccess,
314 const CommandFailCallback& onFailure)
315{
316 ndn::nfd::ControlParameters faceParameters;
317 faceParameters
318 .setFaceId(faceDestroyResult.getFaceId());
319 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
320 onSuccess,
321 onFailure);
322}
323
324void
325Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500326 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500327 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500328{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500329 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
akmhoque102aea42014-08-04 10:22:12 -0500330 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500331 ndn::nfd::ControlParameters faceParameters;
332 faceParameters
333 .setName(namePrefix)
334 .setFaceId(faceId)
335 .setFlags(flags)
336 .setCost(faceCost)
337 .setExpirationPeriod(timeout)
338 .setOrigin(128);
339
akmhoque102aea42014-08-04 10:22:12 -0500340 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
341 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500342 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500343 }
344 else {
345 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
346 }
akmhoquec04e7272014-07-02 11:00:14 -0500347}
348
Vince Lehman0a7da612014-10-29 14:39:29 -0500349typedef void(Fib::*RegisterPrefixCallback)(const ndn::nfd::ControlParameters&,
350 const ndn::nfd::ControlParameters&, uint8_t,
351 const CommandSucceedCallback&,
352 const CommandFailCallback&);
353
akmhoquec04e7272014-07-02 11:00:14 -0500354void
355Fib::registerPrefix(const ndn::Name& namePrefix,
356 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500357 uint64_t faceCost,
358 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500359 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500360 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500361 const CommandSucceedCallback& onSuccess,
362 const CommandFailCallback& onFailure)
363
364{
akmhoque060d3022014-08-12 13:35:06 -0500365 ndn::nfd::ControlParameters parameters;
366 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500367 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500368 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500369 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500370 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500371 .setOrigin(128);
akmhoque060d3022014-08-12 13:35:06 -0500372 createFace(faceUri,
Vince Lehman0a7da612014-10-29 14:39:29 -0500373 ndn::bind(static_cast<RegisterPrefixCallback>(&Fib::registerPrefixInNfd),
374 this, _1, parameters, times, onSuccess, onFailure),
akmhoque060d3022014-08-12 13:35:06 -0500375 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