blob: ade99d5d953aad21ecad263bb17d63e48e4209a6 [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
27#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050028#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050029#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -050030#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050032
akmhoque53353462014-04-22 08:43:45 -050033
34
35namespace 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{
akmhoquefdbddb12014-05-02 18:35:19 -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{
akmhoque31d1d4b2014-05-05 22:08:14 -050053 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050054}
55
56
57ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050058Fib::scheduleEntryRefreshing(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{
akmhoque674b0b12014-05-20 14:33:28 -050061 _LOG_DEBUG("Fib::scheduleEntryRefreshing Called");
62 _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum);
akmhoquec7a79b22014-05-26 08:06:19 -050063 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -050064 ndn::bind(&Fib::refreshEntry, this,
65 name, feSeqNum));
akmhoque53353462014-04-22 08:43:45 -050066}
67
68void
akmhoque31d1d4b2014-05-05 22:08:14 -050069Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum)
akmhoque53353462014-04-22 08:43:45 -050070{
akmhoque2f423352014-06-03 11:49:35 -050071 _LOG_DEBUG("Fib::refreshEntry Called");
72 _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum);
akmhoquefdbddb12014-05-02 18:35:19 -050073 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
74 m_table.end(),
75 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050076 if (it != m_table.end()) {
akmhoque393d4ff2014-07-16 14:27:03 -050077 cancelScheduledExpiringEvent((*it).getExpiringEventId());
78 _LOG_DEBUG("Refreshing the FIB entry. Name: " << name);
79 for (std::list<NextHop>::iterator nhit =
80 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050081 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque393d4ff2014-07-16 14:27:03 -050082 // add entry to NDN-FIB
83 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -050084 registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -050085 std::ceil(nhit->getRouteCost()),
86 ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
akmhoquefdbddb12014-05-02 18:35:19 -050087 }
akmhoquefdbddb12014-05-02 18:35:19 -050088 }
akmhoque393d4ff2014-07-16 14:27:03 -050089 // increase sequence number and schedule refresh again
90 it->setSeqNo(feSeqNum + 1);
91 it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
92 it->getSeqNo(),
93 ndn::time::seconds(m_refreshTime)));
akmhoquefdbddb12014-05-02 18:35:19 -050094 }
akmhoque53353462014-04-22 08:43:45 -050095}
96
97void
akmhoque31d1d4b2014-05-05 22:08:14 -050098Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050099{
akmhoque674b0b12014-05-20 14:33:28 -0500100 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -0500101 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -0500102 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -0500103 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -0500105 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500106 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500107 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500108 //remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500109 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -0500110 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500111 }
akmhoque53353462014-04-22 08:43:45 -0500112 }
akmhoque674b0b12014-05-20 14:33:28 -0500113 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500115 m_table.erase(it);
116 }
117}
118
119
120void
akmhoque31d1d4b2014-05-05 22:08:14 -0500121Fib::update(const ndn::Name& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500122{
akmhoque674b0b12014-05-20 14:33:28 -0500123 _LOG_DEBUG("Fib::updateFib Called");
akmhoque53353462014-04-22 08:43:45 -0500124 int startFace = 0;
125 int endFace = getNumberOfFacesForName(nextHopList,
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 m_nlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque53353462014-04-22 08:43:45 -0500127 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
128 m_table.end(),
129 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500130 if (it == m_table.end()) {
131 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500132 nextHopList.sort();
133 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500134 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500135 std::list<NextHop>::iterator nhit = nhl.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500136 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500137 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500138 //Add entry to NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500139 if (isPrefixUpdatable(name)) {
140 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500141 std::ceil(nhit->getRouteCost()),
142 ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
akmhoque393d4ff2014-07-16 14:27:03 -0500143 }
akmhoque53353462014-04-22 08:43:45 -0500144 }
akmhoquefdbddb12014-05-02 18:35:19 -0500145 newEntry.getNexthopList().sort();
akmhoquec7a79b22014-05-26 08:06:19 -0500146 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
147 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
148 newEntry.setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500149 newEntry.setSeqNo(1);
akmhoquec7a79b22014-05-26 08:06:19 -0500150 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1,
151 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500152 m_table.push_back(newEntry);
153 }
154 }
akmhoque157b0a42014-05-13 00:26:37 -0500155 else {
akmhoque2f423352014-06-03 11:49:35 -0500156 _LOG_DEBUG("Old FIB Entry");
akmhoque157b0a42014-05-13 00:26:37 -0500157 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500158 nextHopList.sort();
akmhoque157b0a42014-05-13 00:26:37 -0500159 if (!it->isEqualNextHops(nextHopList)) {
akmhoquefdbddb12014-05-02 18:35:19 -0500160 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500161 std::list<NextHop>::iterator nhit = nhl.begin();
162 // Add first Entry to NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500163 if (isPrefixUpdatable(name)) {
164 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500165 std::ceil(nhit->getRouteCost()),
166 ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
akmhoque393d4ff2014-07-16 14:27:03 -0500167 }
akmhoque157b0a42014-05-13 00:26:37 -0500168 removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500169 it->getNexthopList().reset();
170 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500171 ++startFace;
172 ++nhit;
akmhoque157b0a42014-05-13 00:26:37 -0500173 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500174 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500175 //Add Entry to NDN_FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500176 if (isPrefixUpdatable(name)) {
177 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500178 std::ceil(nhit->getRouteCost()),
179 ndn::time::seconds(m_refreshTime + GRACE_PERIOD));
akmhoque393d4ff2014-07-16 14:27:03 -0500180 }
akmhoque53353462014-04-22 08:43:45 -0500181 }
182 }
akmhoquec7a79b22014-05-26 08:06:19 -0500183 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
184 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
185 it->setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500186 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500187 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoquec7a79b22014-05-26 08:06:19 -0500188 it->getSeqNo(),
189 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500190 }
akmhoque157b0a42014-05-13 00:26:37 -0500191 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500192 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500193 }
194 }
195}
196
197
198
199void
akmhoque31d1d4b2014-05-05 22:08:14 -0500200Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500201{
akmhoque674b0b12014-05-20 14:33:28 -0500202 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500203 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500204 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500205 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500206 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500207 for (std::list<NextHop>::iterator nhit =
akmhoque393d4ff2014-07-16 14:27:03 -0500208 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500209 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500210 //Remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500211 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500212 }
213 }
akmhoque157b0a42014-05-13 00:26:37 -0500214 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500215 m_table.clear();
216 }
217}
218
219int
akmhoque31d1d4b2014-05-05 22:08:14 -0500220Fib::getNumberOfFacesForName(NexthopList& nextHopList,
221 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500222{
223 int endFace = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500224 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
akmhoque53353462014-04-22 08:43:45 -0500225 return nextHopList.getSize();
226 }
akmhoque157b0a42014-05-13 00:26:37 -0500227 else {
akmhoque53353462014-04-22 08:43:45 -0500228 return maxFacesPerPrefix;
229 }
230 return endFace;
231}
232
akmhoque393d4ff2014-07-16 14:27:03 -0500233bool
234Fib::isPrefixUpdatable(const ndn::Name& name) {
235 if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
236 return true;
237 }
238
239 return false;
240}
241
akmhoque53353462014-04-22 08:43:45 -0500242void
akmhoque157b0a42014-05-13 00:26:37 -0500243Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500244 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500245{
akmhoquefdbddb12014-05-02 18:35:19 -0500246 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500247 it != nl.getNextHops().end(); ++it) {
248 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500249 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500250 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500251 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500252 }
akmhoque53353462014-04-22 08:43:45 -0500253 }
254 }
255}
256
257void
akmhoquec04e7272014-07-02 11:00:14 -0500258Fib::createFace(const std::string& faceUri,
259 const CommandSucceedCallback& onSuccess,
260 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500261{
262 ndn::nfd::ControlParameters faceParameters;
263 faceParameters
akmhoquec04e7272014-07-02 11:00:14 -0500264 .setUri(faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500265 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
akmhoquec04e7272014-07-02 11:00:14 -0500266 onSuccess,
267 onFailure);
268}
269
270void
271Fib::destroyFace(const std::string& faceUri,
272 const CommandSucceedCallback& onSuccess,
273 const CommandFailCallback& onFailure)
274{
275 createFace(faceUri,
276 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
277 onFailure);
278}
279
280void
281Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
282 const CommandSucceedCallback& onSuccess,
283 const CommandFailCallback& onFailure)
284{
285 ndn::nfd::ControlParameters faceParameters;
286 faceParameters
287 .setFaceId(faceDestroyResult.getFaceId());
288 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
289 onSuccess,
290 onFailure);
291}
292
293void
294Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500295 uint64_t faceCost, const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500296{
297 createFace(faceUri,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500298 ndn::bind(&Fib::registerPrefixInNfd, this,_1, namePrefix, faceCost, timeout,
299 faceUri),
akmhoquec04e7272014-07-02 11:00:14 -0500300 ndn::bind(&Fib::onFailure, this, _1, _2,"Failed in name registration"));
301}
302
303void
304Fib::registerPrefix(const ndn::Name& namePrefix,
305 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500306 uint64_t faceCost,
307 const ndn::time::milliseconds& timeout,
akmhoquec04e7272014-07-02 11:00:14 -0500308 const CommandSucceedCallback& onSuccess,
309 const CommandFailCallback& onFailure)
310
311{
312 createFace(faceUri,
313 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
314 namePrefix, faceCost, timeout, onSuccess, onFailure),
315 onFailure);
akmhoque157b0a42014-05-13 00:26:37 -0500316}
317
318void
319Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoquebf11c5f2014-07-21 14:49:47 -0500320 const ndn::Name& namePrefix, uint64_t faceCost,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500321 const ndn::time::milliseconds& timeout,
322 const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500323{
324 ndn::nfd::ControlParameters controlParameters;
325 controlParameters
akmhoque157b0a42014-05-13 00:26:37 -0500326 .setName(namePrefix)
327 .setFaceId(faceCreateResult.getFaceId())
328 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500329 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500330 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500331 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
akmhoque157b0a42014-05-13 00:26:37 -0500332 ndn::bind(&Fib::onRegistration, this, _1,
333 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500334 faceUri),
akmhoquefdbddb12014-05-02 18:35:19 -0500335 ndn::bind(&Fib::onFailure, this, _1, _2,
336 "Failed in name registration"));
337}
akmhoque31d1d4b2014-05-05 22:08:14 -0500338
akmhoquefdbddb12014-05-02 18:35:19 -0500339void
akmhoquec04e7272014-07-02 11:00:14 -0500340Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoquebf11c5f2014-07-21 14:49:47 -0500341 const ndn::Name& namePrefix, uint64_t faceCost,
342 const ndn::time::milliseconds& timeout,
akmhoquec04e7272014-07-02 11:00:14 -0500343 const CommandSucceedCallback& onSuccess,
344 const CommandFailCallback& onFailure)
345{
346 ndn::nfd::ControlParameters controlParameters;
347 controlParameters
348 .setName(namePrefix)
349 .setFaceId(faceCreateResult.getFaceId())
350 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500351 .setExpirationPeriod(timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500352 .setOrigin(128);
353 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
354 onSuccess,
355 onFailure);
356}
357
358void
akmhoque157b0a42014-05-13 00:26:37 -0500359Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500360{
akmhoque157b0a42014-05-13 00:26:37 -0500361 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500362 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500363 if (faceId > 0) {
364 ndn::nfd::ControlParameters controlParameters;
365 controlParameters
366 .setName(namePrefix)
367 .setFaceId(faceId)
368 .setOrigin(128);
369 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500370 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500371 "Successful in unregistering name"),
372 ndn::bind(&Fib::onFailure, this, _1, _2,
373 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500374 }
akmhoquefdbddb12014-05-02 18:35:19 -0500375}
376
377void
akmhoque393d4ff2014-07-16 14:27:03 -0500378Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500379{
380 ndn::nfd::ControlParameters parameters;
381 parameters
382 .setName(name)
383 .setStrategy(strategy);
384
385 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500386 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500387 "Successfully set strategy choice"),
akmhoque393d4ff2014-07-16 14:27:03 -0500388 bind(&Fib::onSetStrategyFailure, this, _1, _2,
389 parameters,
390 count,
akmhoque157b0a42014-05-13 00:26:37 -0500391 "Failed to set strategy choice"));
392}
393
394void
395Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
396 const std::string& message, const std::string& faceUri)
397{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500398 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
399 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500400 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500401 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500402}
403
404
405void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500406Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
407 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500408{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500409 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
410 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500411}
412
413void
akmhoque31d1d4b2014-05-05 22:08:14 -0500414Fib::onFailure(uint32_t code, const std::string& error,
415 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500416{
akmhoque2f423352014-06-03 11:49:35 -0500417 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500418}
419
akmhoque674b0b12014-05-20 14:33:28 -0500420void
akmhoque393d4ff2014-07-16 14:27:03 -0500421Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
422 const std::string& message)
423{
424 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
425 << "for name: " << commandSuccessResult.getName());
426}
427
428void
429Fib::onSetStrategyFailure(uint32_t code, const std::string& error,
430 const ndn::nfd::ControlParameters& parameters,
431 uint32_t count,
432 const std::string& message)
433{
434 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
435 << "for name: " << parameters.getName());
436 if (count < 3) {
437 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
438 }
439}
440
441void
akmhoque674b0b12014-05-20 14:33:28 -0500442Fib::writeLog()
443{
444 _LOG_DEBUG("-------------------FIB-----------------------------");
445 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
446 ++it) {
447 (*it).writeLog();
448 }
449}
akmhoquefdbddb12014-05-02 18:35:19 -0500450
akmhoque53353462014-04-22 08:43:45 -0500451} //namespace nlsr