blob: b99d7a2414a508727c2da5cb52b5f80f727e3f03 [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 _LOG_DEBUG("Refreshing the FIB entry. Name: " << name);
78 for (std::list<NextHop>::iterator nhit =
79 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050080 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque393d4ff2014-07-16 14:27:03 -050081 // add entry to NDN-FIB
82 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -050083 registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -050084 std::ceil(nhit->getRouteCost()),
akmhoque102aea42014-08-04 10:22:12 -050085 ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
akmhoquefdbddb12014-05-02 18:35:19 -050086 }
akmhoquefdbddb12014-05-02 18:35:19 -050087 }
akmhoque393d4ff2014-07-16 14:27:03 -050088 // increase sequence number and schedule refresh again
89 it->setSeqNo(feSeqNum + 1);
90 it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
91 it->getSeqNo(),
92 ndn::time::seconds(m_refreshTime)));
akmhoquefdbddb12014-05-02 18:35:19 -050093 }
akmhoque53353462014-04-22 08:43:45 -050094}
95
96void
akmhoque31d1d4b2014-05-05 22:08:14 -050097Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050098{
akmhoque674b0b12014-05-20 14:33:28 -050099 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -0500100 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -0500101 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -0500102 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500103 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -0500104 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500105 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500106 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500107 //remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500108 if (isPrefixUpdatable(it->getName())) {
akmhoque157b0a42014-05-13 00:26:37 -0500109 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500110 }
akmhoque53353462014-04-22 08:43:45 -0500111 }
akmhoque674b0b12014-05-20 14:33:28 -0500112 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500114 m_table.erase(it);
115 }
116}
117
118
119void
akmhoque31d1d4b2014-05-05 22:08:14 -0500120Fib::update(const ndn::Name& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500121{
akmhoque674b0b12014-05-20 14:33:28 -0500122 _LOG_DEBUG("Fib::updateFib Called");
akmhoque53353462014-04-22 08:43:45 -0500123 int startFace = 0;
124 int endFace = getNumberOfFacesForName(nextHopList,
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 m_nlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque53353462014-04-22 08:43:45 -0500126 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
127 m_table.end(),
128 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500129 if (it == m_table.end()) {
130 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500131 nextHopList.sort();
132 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500133 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500134 std::list<NextHop>::iterator nhit = nhl.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500135 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500136 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500137 //Add entry to NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500138 if (isPrefixUpdatable(name)) {
139 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500140 std::ceil(nhit->getRouteCost()),
akmhoque102aea42014-08-04 10:22:12 -0500141 ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500142 }
akmhoque53353462014-04-22 08:43:45 -0500143 }
akmhoquefdbddb12014-05-02 18:35:19 -0500144 newEntry.getNexthopList().sort();
akmhoquec7a79b22014-05-26 08:06:19 -0500145 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
146 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
147 newEntry.setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500148 newEntry.setSeqNo(1);
akmhoquec7a79b22014-05-26 08:06:19 -0500149 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1,
150 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500151 m_table.push_back(newEntry);
152 }
153 }
akmhoque157b0a42014-05-13 00:26:37 -0500154 else {
akmhoque2f423352014-06-03 11:49:35 -0500155 _LOG_DEBUG("Old FIB Entry");
akmhoque157b0a42014-05-13 00:26:37 -0500156 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500157 nextHopList.sort();
akmhoque157b0a42014-05-13 00:26:37 -0500158 if (!it->isEqualNextHops(nextHopList)) {
akmhoquefdbddb12014-05-02 18:35:19 -0500159 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500160 std::list<NextHop>::iterator nhit = nhl.begin();
161 // Add first Entry to NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500162 if (isPrefixUpdatable(name)) {
163 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500164 std::ceil(nhit->getRouteCost()),
akmhoque102aea42014-08-04 10:22:12 -0500165 ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500166 }
akmhoque157b0a42014-05-13 00:26:37 -0500167 removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500168 it->getNexthopList().reset();
169 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500170 ++startFace;
171 ++nhit;
akmhoque157b0a42014-05-13 00:26:37 -0500172 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500173 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500174 //Add Entry to NDN_FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500175 if (isPrefixUpdatable(name)) {
176 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500177 std::ceil(nhit->getRouteCost()),
akmhoque102aea42014-08-04 10:22:12 -0500178 ndn::time::seconds(m_refreshTime + GRACE_PERIOD), 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500179 }
akmhoque53353462014-04-22 08:43:45 -0500180 }
181 }
akmhoquec7a79b22014-05-26 08:06:19 -0500182 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
183 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
184 it->setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500185 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500186 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoquec7a79b22014-05-26 08:06:19 -0500187 it->getSeqNo(),
188 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500189 }
akmhoque157b0a42014-05-13 00:26:37 -0500190 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500192 }
193 }
194}
195
196
197
198void
akmhoque31d1d4b2014-05-05 22:08:14 -0500199Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500200{
akmhoque674b0b12014-05-20 14:33:28 -0500201 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500202 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500203 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500204 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500205 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500206 for (std::list<NextHop>::iterator nhit =
akmhoque393d4ff2014-07-16 14:27:03 -0500207 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500208 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500209 //Remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500210 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500211 }
212 }
akmhoque157b0a42014-05-13 00:26:37 -0500213 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500214 m_table.clear();
215 }
216}
217
218int
akmhoque31d1d4b2014-05-05 22:08:14 -0500219Fib::getNumberOfFacesForName(NexthopList& nextHopList,
220 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500221{
222 int endFace = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500223 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
akmhoque53353462014-04-22 08:43:45 -0500224 return nextHopList.getSize();
225 }
akmhoque157b0a42014-05-13 00:26:37 -0500226 else {
akmhoque53353462014-04-22 08:43:45 -0500227 return maxFacesPerPrefix;
228 }
229 return endFace;
230}
231
akmhoque393d4ff2014-07-16 14:27:03 -0500232bool
233Fib::isPrefixUpdatable(const ndn::Name& name) {
234 if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
235 return true;
236 }
237
238 return false;
239}
240
akmhoque53353462014-04-22 08:43:45 -0500241void
akmhoque157b0a42014-05-13 00:26:37 -0500242Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500243 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500244{
akmhoquefdbddb12014-05-02 18:35:19 -0500245 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500246 it != nl.getNextHops().end(); ++it) {
247 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500248 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500249 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500250 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500251 }
akmhoque53353462014-04-22 08:43:45 -0500252 }
253 }
254}
255
256void
akmhoquec04e7272014-07-02 11:00:14 -0500257Fib::createFace(const std::string& faceUri,
258 const CommandSucceedCallback& onSuccess,
259 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500260{
261 ndn::nfd::ControlParameters faceParameters;
262 faceParameters
akmhoquec04e7272014-07-02 11:00:14 -0500263 .setUri(faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500264 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
akmhoquec04e7272014-07-02 11:00:14 -0500265 onSuccess,
266 onFailure);
267}
268
269void
270Fib::destroyFace(const std::string& faceUri,
271 const CommandSucceedCallback& onSuccess,
272 const CommandFailCallback& onFailure)
273{
274 createFace(faceUri,
275 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
276 onFailure);
277}
278
279void
280Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
281 const CommandSucceedCallback& onSuccess,
282 const CommandFailCallback& onFailure)
283{
284 ndn::nfd::ControlParameters faceParameters;
285 faceParameters
286 .setFaceId(faceDestroyResult.getFaceId());
287 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
288 onSuccess,
289 onFailure);
290}
291
292void
293Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500294 uint64_t faceCost, const ndn::time::milliseconds& timeout,
295 uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500296{
akmhoque102aea42014-08-04 10:22:12 -0500297 uint64_t faceId = m_nlsr.getAdjacencyList().getFaceId(faceUri);
298 if (faceId != 0) {
299 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
300 << " Face Id: " << faceId);
301 registerPrefixInNfd(namePrefix, faceId,
302 faceCost, timeout, faceUri, times);
303 }
304 else {
305 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
306 }
akmhoquec04e7272014-07-02 11:00:14 -0500307}
308
309void
310Fib::registerPrefix(const ndn::Name& namePrefix,
311 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500312 uint64_t faceCost,
313 const ndn::time::milliseconds& timeout,
akmhoque102aea42014-08-04 10:22:12 -0500314 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500315 const CommandSucceedCallback& onSuccess,
316 const CommandFailCallback& onFailure)
317
318{
319 createFace(faceUri,
320 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
akmhoque102aea42014-08-04 10:22:12 -0500321 namePrefix, faceCost,
322 timeout, times, onSuccess, onFailure),
akmhoquec04e7272014-07-02 11:00:14 -0500323 onFailure);
akmhoque157b0a42014-05-13 00:26:37 -0500324}
325
326void
akmhoque102aea42014-08-04 10:22:12 -0500327Fib::registerPrefixInNfd(const ndn::Name& namePrefix,
328 uint64_t faceId,
329 uint64_t faceCost,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500330 const ndn::time::milliseconds& timeout,
akmhoque102aea42014-08-04 10:22:12 -0500331 const std::string& faceUri,
332 uint8_t times)
akmhoquefdbddb12014-05-02 18:35:19 -0500333{
334 ndn::nfd::ControlParameters controlParameters;
335 controlParameters
akmhoque157b0a42014-05-13 00:26:37 -0500336 .setName(namePrefix)
akmhoque102aea42014-08-04 10:22:12 -0500337 .setFaceId(faceId)
akmhoque157b0a42014-05-13 00:26:37 -0500338 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500339 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500340 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500341 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
akmhoque157b0a42014-05-13 00:26:37 -0500342 ndn::bind(&Fib::onRegistration, this, _1,
343 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500344 faceUri),
akmhoque102aea42014-08-04 10:22:12 -0500345 ndn::bind(&Fib::onRegistrationFailure,
346 this, _1, _2,
347 "Failed in name registration",
348 namePrefix, faceUri, faceCost,
349 timeout, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500350}
akmhoque31d1d4b2014-05-05 22:08:14 -0500351
akmhoquefdbddb12014-05-02 18:35:19 -0500352void
akmhoquec04e7272014-07-02 11:00:14 -0500353Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoquebf11c5f2014-07-21 14:49:47 -0500354 const ndn::Name& namePrefix, uint64_t faceCost,
355 const ndn::time::milliseconds& timeout,
akmhoque102aea42014-08-04 10:22:12 -0500356 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500357 const CommandSucceedCallback& onSuccess,
358 const CommandFailCallback& onFailure)
359{
360 ndn::nfd::ControlParameters controlParameters;
361 controlParameters
362 .setName(namePrefix)
363 .setFaceId(faceCreateResult.getFaceId())
364 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500365 .setExpirationPeriod(timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500366 .setOrigin(128);
367 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
368 onSuccess,
369 onFailure);
370}
371
372void
akmhoque157b0a42014-05-13 00:26:37 -0500373Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500374{
akmhoque157b0a42014-05-13 00:26:37 -0500375 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500376 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500377 if (faceId > 0) {
378 ndn::nfd::ControlParameters controlParameters;
379 controlParameters
380 .setName(namePrefix)
381 .setFaceId(faceId)
382 .setOrigin(128);
383 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500384 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500385 "Successful in unregistering name"),
akmhoque102aea42014-08-04 10:22:12 -0500386 ndn::bind(&Fib::onUnregistrationFailure,
387 this, _1, _2,
akmhoquefdbddb12014-05-02 18:35:19 -0500388 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500389 }
akmhoquefdbddb12014-05-02 18:35:19 -0500390}
391
392void
akmhoque393d4ff2014-07-16 14:27:03 -0500393Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500394{
395 ndn::nfd::ControlParameters parameters;
396 parameters
397 .setName(name)
398 .setStrategy(strategy);
399
400 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500401 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500402 "Successfully set strategy choice"),
akmhoque393d4ff2014-07-16 14:27:03 -0500403 bind(&Fib::onSetStrategyFailure, this, _1, _2,
404 parameters,
405 count,
akmhoque157b0a42014-05-13 00:26:37 -0500406 "Failed to set strategy choice"));
407}
408
409void
410Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
411 const std::string& message, const std::string& faceUri)
412{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500413 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
414 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500415 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500416 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500417}
418
419
420void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500421Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
422 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500423{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500424 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
425 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500426}
427
428void
akmhoque102aea42014-08-04 10:22:12 -0500429Fib::onRegistrationFailure(uint32_t code, const std::string& error,
430 const std::string& message,
431 const ndn::Name& namePrefix, const std::string& faceUri,
432 uint64_t faceCost, const ndn::time::milliseconds& timeout,
433 uint8_t times)
434{
435 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
436 _LOG_DEBUG("Prefix: " << namePrefix << " failed for: " << times);
437 if (times < 3) {
438 _LOG_DEBUG("Trying to register again...");
439 registerPrefix(namePrefix, faceUri, faceCost, timeout, times+1);
440 }
441 else {
442 _LOG_DEBUG("Registration trial given up");
443 }
444}
445
446void
447Fib::onUnregistrationFailure(uint32_t code, const std::string& error,
448 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500449{
akmhoque2f423352014-06-03 11:49:35 -0500450 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500451}
452
akmhoque674b0b12014-05-20 14:33:28 -0500453void
akmhoque393d4ff2014-07-16 14:27:03 -0500454Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
455 const std::string& message)
456{
457 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
458 << "for name: " << commandSuccessResult.getName());
459}
460
461void
462Fib::onSetStrategyFailure(uint32_t code, const std::string& error,
463 const ndn::nfd::ControlParameters& parameters,
464 uint32_t count,
465 const std::string& message)
466{
467 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
468 << "for name: " << parameters.getName());
469 if (count < 3) {
470 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
471 }
472}
473
474void
akmhoque674b0b12014-05-20 14:33:28 -0500475Fib::writeLog()
476{
477 _LOG_DEBUG("-------------------FIB-----------------------------");
478 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
479 ++it) {
480 (*it).writeLog();
481 }
482}
akmhoquefdbddb12014-05-02 18:35:19 -0500483
akmhoque53353462014-04-22 08:43:45 -0500484} //namespace nlsr