akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- 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 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <list> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 24 | #include <cmath> |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/common.hpp> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 26 | |
| 27 | #include "nlsr.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 28 | #include "nexthop-list.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 29 | #include "face-map.hpp" |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 30 | #include "fib.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | #include "logger.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | |
| 34 | |
| 35 | namespace nlsr { |
| 36 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 37 | INIT_LOGGER("Fib"); |
| 38 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 39 | const uint64_t Fib::GRACE_PERIOD = 10; |
| 40 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | using namespace std; |
| 42 | using namespace ndn; |
| 43 | |
| 44 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 45 | fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 47 | return fibEntry.getName() == name ; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 51 | Fib::cancelScheduledExpiringEvent(EventId eid) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 53 | m_nlsr.getScheduler().cancelEvent(eid); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | ndn::EventId |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 58 | Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 59 | const ndn::time::seconds& expTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 61 | _LOG_DEBUG("Fib::scheduleEntryRefreshing Called"); |
| 62 | _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 63 | return m_nlsr.getScheduler().scheduleEvent(expTime, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 64 | ndn::bind(&Fib::refreshEntry, this, |
| 65 | name, feSeqNum)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 69 | Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 71 | _LOG_DEBUG("Fib::refreshEntry Called"); |
| 72 | _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 73 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 74 | m_table.end(), |
| 75 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 76 | if (it != m_table.end()) { |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 77 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
| 78 | _LOG_DEBUG("Refreshing the FIB entry. Name: " << name); |
| 79 | for (std::list<NextHop>::iterator nhit = |
| 80 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 82 | // add entry to NDN-FIB |
| 83 | if (isPrefixUpdatable(it->getName())) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 84 | registerPrefix(it->getName(), nhit->getConnectingFaceUri(), |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 85 | std::ceil(nhit->getRouteCost()), (m_refreshTime + GRACE_PERIOD)); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 86 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 87 | } |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 88 | // 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))); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 93 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 97 | Fib::remove(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 98 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 99 | _LOG_DEBUG("Fib::remove called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 100 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 101 | m_table.end(), |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 102 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 103 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | for (std::list<NextHop>::iterator nhit = |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 105 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 106 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | //remove entry from NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 108 | if (isPrefixUpdatable(it->getName())) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 109 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 110 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 112 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << name); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 113 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | m_table.erase(it); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 120 | Fib::update(const ndn::Name& name, NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 122 | _LOG_DEBUG("Fib::updateFib Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | int startFace = 0; |
| 124 | int endFace = getNumberOfFacesForName(nextHopList, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 125 | m_nlsr.getConfParameter().getMaxFacesPerPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 126 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 127 | m_table.end(), |
| 128 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 129 | if (it == m_table.end()) { |
| 130 | if (nextHopList.getSize() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | nextHopList.sort(); |
| 132 | FibEntry newEntry(name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 133 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | std::list<NextHop>::iterator nhit = nhl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 135 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 136 | newEntry.getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | //Add entry to NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 138 | if (isPrefixUpdatable(name)) { |
| 139 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 140 | std::ceil(nhit->getRouteCost()), (m_refreshTime + GRACE_PERIOD)); |
| 141 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 143 | newEntry.getNexthopList().sort(); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 144 | ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now(); |
| 145 | expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime); |
| 146 | newEntry.setExpirationTimePoint(expirationTimePoint); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | newEntry.setSeqNo(1); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 148 | newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1, |
| 149 | ndn::time::seconds(m_refreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 150 | m_table.push_back(newEntry); |
| 151 | } |
| 152 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 153 | else { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 154 | _LOG_DEBUG("Old FIB Entry"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 155 | if (nextHopList.getSize() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 156 | nextHopList.sort(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 157 | if (!it->isEqualNextHops(nextHopList)) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 158 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | std::list<NextHop>::iterator nhit = nhl.begin(); |
| 160 | // Add first Entry to NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 161 | if (isPrefixUpdatable(name)) { |
| 162 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 163 | std::ceil(nhit->getRouteCost()), (m_refreshTime + GRACE_PERIOD)); |
| 164 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 165 | removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 166 | it->getNexthopList().reset(); |
| 167 | it->getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | ++startFace; |
| 169 | ++nhit; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 170 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 171 | it->getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | //Add Entry to NDN_FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 173 | if (isPrefixUpdatable(name)) { |
| 174 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 175 | std::ceil(nhit->getRouteCost()), (m_refreshTime + GRACE_PERIOD)); |
| 176 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | } |
| 178 | } |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 179 | ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now(); |
| 180 | expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime); |
| 181 | it->setExpirationTimePoint(expirationTimePoint); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | it->setSeqNo(it->getSeqNo() + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 183 | (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() , |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 184 | it->getSeqNo(), |
| 185 | ndn::time::seconds(m_refreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 187 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 188 | remove(name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 196 | Fib::clean() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 197 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 198 | _LOG_DEBUG("Fib::clean called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 199 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 200 | ++it) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 201 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 202 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 203 | for (std::list<NextHop>::iterator nhit = |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 204 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 205 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | //Remove entry from NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 207 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 208 | } |
| 209 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 210 | if (m_table.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | m_table.clear(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 216 | Fib::getNumberOfFacesForName(NexthopList& nextHopList, |
| 217 | uint32_t maxFacesPerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | { |
| 219 | int endFace = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 220 | if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 221 | return nextHopList.getSize(); |
| 222 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 223 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | return maxFacesPerPrefix; |
| 225 | } |
| 226 | return endFace; |
| 227 | } |
| 228 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 229 | bool |
| 230 | Fib::isPrefixUpdatable(const ndn::Name& name) { |
| 231 | if (!m_nlsr.getAdjacencyList().isNeighbor(name)) { |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 239 | Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 240 | const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 241 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 242 | for (std::list<NextHop>::iterator it = nl.getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 243 | it != nl.getNextHops().end(); ++it) { |
| 244 | if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 245 | //Remove FIB Entry from NDN-FIB |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 246 | if (isPrefixUpdatable(name)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 247 | unregisterPrefix(name, it->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 248 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 254 | Fib::createFace(const std::string& faceUri, |
| 255 | const CommandSucceedCallback& onSuccess, |
| 256 | const CommandFailCallback& onFailure) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 257 | { |
| 258 | ndn::nfd::ControlParameters faceParameters; |
| 259 | faceParameters |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 260 | .setUri(faceUri); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 261 | m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 262 | onSuccess, |
| 263 | onFailure); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | Fib::destroyFace(const std::string& faceUri, |
| 268 | const CommandSucceedCallback& onSuccess, |
| 269 | const CommandFailCallback& onFailure) |
| 270 | { |
| 271 | createFace(faceUri, |
| 272 | ndn::bind(&Fib::destroyFaceInNfd, this, _1, onSuccess, onFailure), |
| 273 | onFailure); |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | Fib::destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult, |
| 278 | const CommandSucceedCallback& onSuccess, |
| 279 | const CommandFailCallback& onFailure) |
| 280 | { |
| 281 | ndn::nfd::ControlParameters faceParameters; |
| 282 | faceParameters |
| 283 | .setFaceId(faceDestroyResult.getFaceId()); |
| 284 | m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters, |
| 285 | onSuccess, |
| 286 | onFailure); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri, |
| 291 | uint64_t faceCost, uint64_t timeout) |
| 292 | { |
| 293 | createFace(faceUri, |
| 294 | ndn::bind(&Fib::registerPrefixInNfd, this,_1, namePrefix, faceCost, timeout), |
| 295 | ndn::bind(&Fib::onFailure, this, _1, _2,"Failed in name registration")); |
| 296 | } |
| 297 | |
| 298 | void |
| 299 | Fib::registerPrefix(const ndn::Name& namePrefix, |
| 300 | const std::string& faceUri, |
| 301 | uint64_t faceCost, uint64_t timeout, |
| 302 | const CommandSucceedCallback& onSuccess, |
| 303 | const CommandFailCallback& onFailure) |
| 304 | |
| 305 | { |
| 306 | createFace(faceUri, |
| 307 | ndn::bind(&Fib::registerPrefixInNfd, this,_1, |
| 308 | namePrefix, faceCost, timeout, onSuccess, onFailure), |
| 309 | onFailure); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void |
| 313 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
| 314 | const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 315 | { |
| 316 | ndn::nfd::ControlParameters controlParameters; |
| 317 | controlParameters |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 318 | .setName(namePrefix) |
| 319 | .setFaceId(faceCreateResult.getFaceId()) |
| 320 | .setCost(faceCost) |
| 321 | .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000)) |
| 322 | .setOrigin(128); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 323 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 324 | ndn::bind(&Fib::onRegistration, this, _1, |
| 325 | "Successful in name registration", |
| 326 | faceCreateResult.getUri()), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 327 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 328 | "Failed in name registration")); |
| 329 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 330 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 331 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 332 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
| 333 | const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout, |
| 334 | const CommandSucceedCallback& onSuccess, |
| 335 | const CommandFailCallback& onFailure) |
| 336 | { |
| 337 | ndn::nfd::ControlParameters controlParameters; |
| 338 | controlParameters |
| 339 | .setName(namePrefix) |
| 340 | .setFaceId(faceCreateResult.getFaceId()) |
| 341 | .setCost(faceCost) |
| 342 | .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000)) |
| 343 | .setOrigin(128); |
| 344 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
| 345 | onSuccess, |
| 346 | onFailure); |
| 347 | } |
| 348 | |
| 349 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 350 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 351 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 352 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
| 353 | if (faceId > 0) { |
| 354 | ndn::nfd::ControlParameters controlParameters; |
| 355 | controlParameters |
| 356 | .setName(namePrefix) |
| 357 | .setFaceId(faceId) |
| 358 | .setOrigin(128); |
| 359 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 360 | ndn::bind(&Fib::onSuccess, this, _1, |
| 361 | "Successful in unregistering name"), |
| 362 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 363 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 364 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 368 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 369 | { |
| 370 | ndn::nfd::ControlParameters parameters; |
| 371 | parameters |
| 372 | .setName(name) |
| 373 | .setStrategy(strategy); |
| 374 | |
| 375 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 376 | bind(&Fib::onSetStrategySuccess, this, _1, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 377 | "Successfully set strategy choice"), |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 378 | bind(&Fib::onSetStrategyFailure, this, _1, _2, |
| 379 | parameters, |
| 380 | count, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 381 | "Failed to set strategy choice")); |
| 382 | } |
| 383 | |
| 384 | void |
| 385 | Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 386 | const std::string& message, const std::string& faceUri) |
| 387 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 388 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 389 | m_faceMap.writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
| 393 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 394 | Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 395 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 396 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 400 | Fib::onFailure(uint32_t code, const std::string& error, |
| 401 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 402 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 403 | _LOG_DEBUG(message << ": " << error << " (code: " << code << ")"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 404 | } |
| 405 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 406 | void |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame^] | 407 | Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 408 | const std::string& message) |
| 409 | { |
| 410 | _LOG_DEBUG(message << ": " << commandSuccessResult.getStrategy() << " " |
| 411 | << "for name: " << commandSuccessResult.getName()); |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | Fib::onSetStrategyFailure(uint32_t code, const std::string& error, |
| 416 | const ndn::nfd::ControlParameters& parameters, |
| 417 | uint32_t count, |
| 418 | const std::string& message) |
| 419 | { |
| 420 | _LOG_DEBUG(message << ": " << parameters.getStrategy() << " " |
| 421 | << "for name: " << parameters.getName()); |
| 422 | if (count < 3) { |
| 423 | setStrategy(parameters.getName(), parameters.getStrategy().toUri(),count+1); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 428 | Fib::writeLog() |
| 429 | { |
| 430 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
| 431 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
| 432 | ++it) { |
| 433 | (*it).writeLog(); |
| 434 | } |
| 435 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 436 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 437 | } //namespace nlsr |