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