blob: 7a8e98b050913cc7fcf921c10a40ca9c62825906 [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()),
akmhoque060d3022014-08-12 13:35:06 -050085 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
86 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
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()),
akmhoque060d3022014-08-12 13:35:06 -0500142 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
143 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500144 }
akmhoque53353462014-04-22 08:43:45 -0500145 }
akmhoquefdbddb12014-05-02 18:35:19 -0500146 newEntry.getNexthopList().sort();
akmhoquec7a79b22014-05-26 08:06:19 -0500147 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
148 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
149 newEntry.setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500150 newEntry.setSeqNo(1);
akmhoquec7a79b22014-05-26 08:06:19 -0500151 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1,
152 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500153 m_table.push_back(newEntry);
154 }
155 }
akmhoque157b0a42014-05-13 00:26:37 -0500156 else {
akmhoque2f423352014-06-03 11:49:35 -0500157 _LOG_DEBUG("Old FIB Entry");
akmhoque157b0a42014-05-13 00:26:37 -0500158 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500159 nextHopList.sort();
akmhoque157b0a42014-05-13 00:26:37 -0500160 if (!it->isEqualNextHops(nextHopList)) {
akmhoquefdbddb12014-05-02 18:35:19 -0500161 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500162 std::list<NextHop>::iterator nhit = nhl.begin();
163 // Add first Entry to NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500164 if (isPrefixUpdatable(name)) {
165 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500166 std::ceil(nhit->getRouteCost()),
akmhoque060d3022014-08-12 13:35:06 -0500167 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
168 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500169 }
akmhoque157b0a42014-05-13 00:26:37 -0500170 removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500171 it->getNexthopList().reset();
172 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500173 ++startFace;
174 ++nhit;
akmhoque157b0a42014-05-13 00:26:37 -0500175 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500176 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500177 //Add Entry to NDN_FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500178 if (isPrefixUpdatable(name)) {
179 registerPrefix(name, nhit->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500180 std::ceil(nhit->getRouteCost()),
akmhoque060d3022014-08-12 13:35:06 -0500181 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
182 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500183 }
akmhoque53353462014-04-22 08:43:45 -0500184 }
185 }
akmhoquec7a79b22014-05-26 08:06:19 -0500186 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
187 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
188 it->setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500189 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500190 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoquec7a79b22014-05-26 08:06:19 -0500191 it->getSeqNo(),
192 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500193 }
akmhoque157b0a42014-05-13 00:26:37 -0500194 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500196 }
197 }
198}
199
200
201
202void
akmhoque31d1d4b2014-05-05 22:08:14 -0500203Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500204{
akmhoque674b0b12014-05-20 14:33:28 -0500205 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500206 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500207 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500208 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500209 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500210 for (std::list<NextHop>::iterator nhit =
akmhoque393d4ff2014-07-16 14:27:03 -0500211 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500212 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500213 //Remove entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500214 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500215 }
216 }
akmhoque157b0a42014-05-13 00:26:37 -0500217 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500218 m_table.clear();
219 }
220}
221
222int
akmhoque31d1d4b2014-05-05 22:08:14 -0500223Fib::getNumberOfFacesForName(NexthopList& nextHopList,
224 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500225{
226 int endFace = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500227 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
akmhoque53353462014-04-22 08:43:45 -0500228 return nextHopList.getSize();
229 }
akmhoque157b0a42014-05-13 00:26:37 -0500230 else {
akmhoque53353462014-04-22 08:43:45 -0500231 return maxFacesPerPrefix;
232 }
233 return endFace;
234}
235
akmhoque393d4ff2014-07-16 14:27:03 -0500236bool
237Fib::isPrefixUpdatable(const ndn::Name& name) {
238 if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
239 return true;
240 }
241
242 return false;
243}
244
akmhoque53353462014-04-22 08:43:45 -0500245void
akmhoque157b0a42014-05-13 00:26:37 -0500246Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500247 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500248{
akmhoquefdbddb12014-05-02 18:35:19 -0500249 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500250 it != nl.getNextHops().end(); ++it) {
251 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500252 //Remove FIB Entry from NDN-FIB
akmhoque393d4ff2014-07-16 14:27:03 -0500253 if (isPrefixUpdatable(name)) {
akmhoque157b0a42014-05-13 00:26:37 -0500254 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500255 }
akmhoque53353462014-04-22 08:43:45 -0500256 }
257 }
258}
259
260void
akmhoquec04e7272014-07-02 11:00:14 -0500261Fib::createFace(const std::string& faceUri,
262 const CommandSucceedCallback& onSuccess,
263 const CommandFailCallback& onFailure)
akmhoque157b0a42014-05-13 00:26:37 -0500264{
265 ndn::nfd::ControlParameters faceParameters;
266 faceParameters
akmhoquec04e7272014-07-02 11:00:14 -0500267 .setUri(faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500268 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
akmhoquec04e7272014-07-02 11:00:14 -0500269 onSuccess,
270 onFailure);
271}
272
273void
274Fib::destroyFace(const std::string& faceUri,
275 const CommandSucceedCallback& onSuccess,
276 const CommandFailCallback& onFailure)
277{
278 createFace(faceUri,
279 ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure),
280 onFailure);
281}
282
283void
284Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult,
285 const CommandSucceedCallback& onSuccess,
286 const CommandFailCallback& onFailure)
287{
288 ndn::nfd::ControlParameters faceParameters;
289 faceParameters
290 .setFaceId(faceDestroyResult.getFaceId());
291 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
292 onSuccess,
293 onFailure);
294}
295
296void
297Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500298 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500299 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500300{
akmhoque102aea42014-08-04 10:22:12 -0500301 uint64_t faceId = m_nlsr.getAdjacencyList().getFaceId(faceUri);
302 if (faceId != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500303 ndn::nfd::ControlParameters faceParameters;
304 faceParameters
305 .setName(namePrefix)
306 .setFaceId(faceId)
307 .setFlags(flags)
308 .setCost(faceCost)
309 .setExpirationPeriod(timeout)
310 .setOrigin(128);
311
akmhoque102aea42014-08-04 10:22:12 -0500312 _LOG_DEBUG("Registering prefix: " << namePrefix << " Face Uri: " << faceUri
313 << " Face Id: " << faceId);
akmhoque060d3022014-08-12 13:35:06 -0500314 registerPrefixInNfd(faceParameters, faceUri, times);
akmhoque102aea42014-08-04 10:22:12 -0500315 }
316 else {
317 _LOG_DEBUG("Error: No Face Id for face uri: " << faceUri);
318 }
akmhoquec04e7272014-07-02 11:00:14 -0500319}
320
321void
322Fib::registerPrefix(const ndn::Name& namePrefix,
323 const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500324 uint64_t faceCost,
325 const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500326 uint64_t flags,
akmhoque102aea42014-08-04 10:22:12 -0500327 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500328 const CommandSucceedCallback& onSuccess,
329 const CommandFailCallback& onFailure)
330
331{
akmhoque060d3022014-08-12 13:35:06 -0500332 ndn::nfd::ControlParameters parameters;
333 parameters
akmhoque157b0a42014-05-13 00:26:37 -0500334 .setName(namePrefix)
akmhoque060d3022014-08-12 13:35:06 -0500335 .setFlags(flags)
akmhoque157b0a42014-05-13 00:26:37 -0500336 .setCost(faceCost)
akmhoquebf11c5f2014-07-21 14:49:47 -0500337 .setExpirationPeriod(timeout)
akmhoque157b0a42014-05-13 00:26:37 -0500338 .setOrigin(128);
akmhoque060d3022014-08-12 13:35:06 -0500339 createFace(faceUri,
340 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
341 parameters,
342 times, onSuccess, onFailure),
343 onFailure);
344}
345
346void
347Fib::registerPrefixInNfd(ndn::nfd::ControlParameters& parameters,
348 const std::string& faceUri,
349 uint8_t times)
350{
351 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
akmhoque157b0a42014-05-13 00:26:37 -0500352 ndn::bind(&Fib::onRegistration, this, _1,
353 "Successful in name registration",
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500354 faceUri),
akmhoque102aea42014-08-04 10:22:12 -0500355 ndn::bind(&Fib::onRegistrationFailure,
356 this, _1, _2,
357 "Failed in name registration",
akmhoque060d3022014-08-12 13:35:06 -0500358 parameters,
359 faceUri, times));
akmhoquefdbddb12014-05-02 18:35:19 -0500360}
akmhoque31d1d4b2014-05-05 22:08:14 -0500361
akmhoquefdbddb12014-05-02 18:35:19 -0500362void
akmhoquec04e7272014-07-02 11:00:14 -0500363Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
akmhoque060d3022014-08-12 13:35:06 -0500364 const ndn::nfd::ControlParameters& parameters,
akmhoque102aea42014-08-04 10:22:12 -0500365 uint8_t times,
akmhoquec04e7272014-07-02 11:00:14 -0500366 const CommandSucceedCallback& onSuccess,
367 const CommandFailCallback& onFailure)
368{
369 ndn::nfd::ControlParameters controlParameters;
370 controlParameters
akmhoque060d3022014-08-12 13:35:06 -0500371 .setName(parameters.getName())
akmhoquec04e7272014-07-02 11:00:14 -0500372 .setFaceId(faceCreateResult.getFaceId())
akmhoque060d3022014-08-12 13:35:06 -0500373 .setCost(parameters.getCost())
374 .setFlags(parameters.getFlags())
375 .setExpirationPeriod(parameters.getExpirationPeriod())
akmhoquec04e7272014-07-02 11:00:14 -0500376 .setOrigin(128);
377 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
378 onSuccess,
379 onFailure);
380}
381
382void
akmhoque157b0a42014-05-13 00:26:37 -0500383Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500384{
akmhoque157b0a42014-05-13 00:26:37 -0500385 uint32_t faceId = m_faceMap.getFaceId(faceUri);
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500386 _LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500387 if (faceId > 0) {
388 ndn::nfd::ControlParameters controlParameters;
389 controlParameters
390 .setName(namePrefix)
391 .setFaceId(faceId)
392 .setOrigin(128);
393 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500394 ndn::bind(&Fib::onUnregistration, this, _1,
akmhoquefdbddb12014-05-02 18:35:19 -0500395 "Successful in unregistering name"),
akmhoque102aea42014-08-04 10:22:12 -0500396 ndn::bind(&Fib::onUnregistrationFailure,
397 this, _1, _2,
akmhoquefdbddb12014-05-02 18:35:19 -0500398 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500399 }
akmhoquefdbddb12014-05-02 18:35:19 -0500400}
401
402void
akmhoque393d4ff2014-07-16 14:27:03 -0500403Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500404{
405 ndn::nfd::ControlParameters parameters;
406 parameters
407 .setName(name)
408 .setStrategy(strategy);
409
410 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
akmhoque393d4ff2014-07-16 14:27:03 -0500411 bind(&Fib::onSetStrategySuccess, this, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500412 "Successfully set strategy choice"),
akmhoque393d4ff2014-07-16 14:27:03 -0500413 bind(&Fib::onSetStrategyFailure, this, _1, _2,
414 parameters,
415 count,
akmhoque157b0a42014-05-13 00:26:37 -0500416 "Failed to set strategy choice"));
417}
418
419void
420Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
421 const std::string& message, const std::string& faceUri)
422{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500423 _LOG_DEBUG("Register successful Prefix: " << commandSuccessResult.getName() <<
424 " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500425 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500426 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500427}
428
akmhoque157b0a42014-05-13 00:26:37 -0500429void
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500430Fib::onUnregistration(const ndn::nfd::ControlParameters& commandSuccessResult,
431 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500432{
akmhoqueb5b3b4f2014-07-23 16:36:51 -0500433 _LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
434 " Face Id: " << commandSuccessResult.getFaceId());
akmhoquefdbddb12014-05-02 18:35:19 -0500435}
436
437void
akmhoque102aea42014-08-04 10:22:12 -0500438Fib::onRegistrationFailure(uint32_t code, const std::string& error,
439 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500440 const ndn::nfd::ControlParameters& parameters,
441 const std::string& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500442 uint8_t times)
443{
444 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoque060d3022014-08-12 13:35:06 -0500445 _LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
akmhoque102aea42014-08-04 10:22:12 -0500446 if (times < 3) {
447 _LOG_DEBUG("Trying to register again...");
akmhoque060d3022014-08-12 13:35:06 -0500448 registerPrefix(parameters.getName(), faceUri,
449 parameters.getCost(),
450 parameters.getExpirationPeriod(),
451 parameters.getFlags(), times+1);
akmhoque102aea42014-08-04 10:22:12 -0500452 }
453 else {
454 _LOG_DEBUG("Registration trial given up");
455 }
456}
457
458void
459Fib::onUnregistrationFailure(uint32_t code, const std::string& error,
460 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500461{
akmhoque2f423352014-06-03 11:49:35 -0500462 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500463}
464
akmhoque674b0b12014-05-20 14:33:28 -0500465void
akmhoque393d4ff2014-07-16 14:27:03 -0500466Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
467 const std::string& message)
468{
469 _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " "
470 << "for name: " << commandSuccessResult.getName());
471}
472
473void
474Fib::onSetStrategyFailure(uint32_t code, const std::string& error,
475 const ndn::nfd::ControlParameters& parameters,
476 uint32_t count,
477 const std::string& message)
478{
479 _LOG_DEBUG(message << ": " << parameters.getStrategy() << " "
480 << "for name: " << parameters.getName());
481 if (count < 3) {
482 setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1);
483 }
484}
485
486void
akmhoque674b0b12014-05-20 14:33:28 -0500487Fib::writeLog()
488{
489 _LOG_DEBUG("-------------------FIB-----------------------------");
490 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
491 ++it) {
492 (*it).writeLog();
493 }
494}
akmhoquefdbddb12014-05-02 18:35:19 -0500495
akmhoque53353462014-04-22 08:43:45 -0500496} //namespace nlsr