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 | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | using namespace std; |
| 40 | using namespace ndn; |
| 41 | |
| 42 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 43 | fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 44 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 45 | return fibEntry.getName() == name ; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 49 | Fib::cancelScheduledExpiringEvent(EventId eid) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 51 | m_nlsr.getScheduler().cancelEvent(eid); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | ndn::EventId |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 56 | Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 57 | const ndn::time::seconds& expTime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 59 | _LOG_DEBUG("Fib::scheduleEntryRefreshing Called"); |
| 60 | _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 61 | return m_nlsr.getScheduler().scheduleEvent(expTime, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 62 | ndn::bind(&Fib::refreshEntry, this, |
| 63 | name, feSeqNum)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 67 | Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame^] | 69 | _LOG_DEBUG("Fib::refreshEntry Called"); |
| 70 | _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 71 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 72 | m_table.end(), |
| 73 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 74 | if (it != m_table.end()) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 75 | if (it->getSeqNo() == feSeqNum) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 76 | _LOG_DEBUG("Refreshing the FIB entry. Name: " << name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 77 | for (std::list<NextHop>::iterator nhit = |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 78 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 80 | // add entry to NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | registerPrefix(it->getName(), nhit->getConnectingFaceUri(), |
| 82 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 83 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 84 | // increase sequence number and schedule refresh again |
| 85 | it->setSeqNo(feSeqNum + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 86 | it->setExpiringEventId(scheduleEntryRefreshing(it->getName() , |
| 87 | it->getSeqNo(), |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 88 | ndn::time::seconds(m_refreshTime))); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 94 | Fib::remove(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 96 | _LOG_DEBUG("Fib::remove called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 98 | m_table.end(), |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 99 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | if (it != m_table.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | for (std::list<NextHop>::iterator nhit = |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 102 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 103 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | //remove entry from NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 105 | if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) { |
| 106 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 110 | if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() != |
| 111 | nhit->getConnectingFaceUri()) { |
| 112 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 113 | } |
| 114 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 115 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 116 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << name); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 117 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | m_table.erase(it); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 124 | Fib::update(const ndn::Name& name, NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 126 | _LOG_DEBUG("Fib::updateFib Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | int startFace = 0; |
| 128 | int endFace = getNumberOfFacesForName(nextHopList, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 129 | m_nlsr.getConfParameter().getMaxFacesPerPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 131 | m_table.end(), |
| 132 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 133 | if (it == m_table.end()) { |
| 134 | if (nextHopList.getSize() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | nextHopList.sort(); |
| 136 | FibEntry newEntry(name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 137 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | std::list<NextHop>::iterator nhit = nhl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 139 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 140 | newEntry.getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | //Add entry to NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 142 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 143 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
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 | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 163 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 164 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
| 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 | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 173 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 174 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | } |
| 176 | } |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 177 | ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now(); |
| 178 | expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime); |
| 179 | it->setExpirationTimePoint(expirationTimePoint); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 180 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << name); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 181 | cancelScheduledExpiringEvent(it->getExpiringEventId()); |
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 | fdbddb1 | 2014-05-02 18:35:19 -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 | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 207 | if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) { |
| 208 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 209 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 210 | else { |
| 211 | if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() != |
| 212 | nhit->getConnectingFaceUri()) { |
| 213 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 214 | } |
| 215 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | } |
| 217 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 218 | if (m_table.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 219 | m_table.clear(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | int |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 224 | Fib::getNumberOfFacesForName(NexthopList& nextHopList, |
| 225 | uint32_t maxFacesPerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 226 | { |
| 227 | int endFace = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 228 | if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | return nextHopList.getSize(); |
| 230 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 231 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 232 | return maxFacesPerPrefix; |
| 233 | } |
| 234 | return endFace; |
| 235 | } |
| 236 | |
| 237 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 238 | Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 239 | const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 241 | for (std::list<NextHop>::iterator it = nl.getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 242 | it != nl.getNextHops().end(); ++it) { |
| 243 | if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 244 | //Remove FIB Entry from NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 245 | if (!m_nlsr.getAdjacencyList().isNeighbor(name)) { |
| 246 | unregisterPrefix(name, it->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 247 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 248 | else { |
| 249 | if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() != |
| 250 | it->getConnectingFaceUri()) { |
| 251 | unregisterPrefix(name, it->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 252 | } |
| 253 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 259 | Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri, |
| 260 | uint64_t faceCost, uint64_t timeout) |
| 261 | { |
| 262 | ndn::nfd::ControlParameters faceParameters; |
| 263 | faceParameters |
| 264 | .setUri(faceUri); |
| 265 | |
| 266 | m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, |
| 267 | ndn::bind(&Fib::registerPrefixInNfd, this,_1, |
| 268 | namePrefix, faceCost, timeout), |
| 269 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 270 | "Failed in name registration")); |
| 271 | |
| 272 | } |
| 273 | |
| 274 | void |
| 275 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
| 276 | const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 277 | { |
| 278 | ndn::nfd::ControlParameters controlParameters; |
| 279 | controlParameters |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 280 | .setName(namePrefix) |
| 281 | .setFaceId(faceCreateResult.getFaceId()) |
| 282 | .setCost(faceCost) |
| 283 | .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000)) |
| 284 | .setOrigin(128); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 285 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 286 | ndn::bind(&Fib::onRegistration, this, _1, |
| 287 | "Successful in name registration", |
| 288 | faceCreateResult.getUri()), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 289 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 290 | "Failed in name registration")); |
| 291 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 292 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 293 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 294 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 295 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 296 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
| 297 | if (faceId > 0) { |
| 298 | ndn::nfd::ControlParameters controlParameters; |
| 299 | controlParameters |
| 300 | .setName(namePrefix) |
| 301 | .setFaceId(faceId) |
| 302 | .setOrigin(128); |
| 303 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 304 | ndn::bind(&Fib::onSuccess, this, _1, |
| 305 | "Successful in unregistering name"), |
| 306 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 307 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 308 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 312 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy) |
| 313 | { |
| 314 | ndn::nfd::ControlParameters parameters; |
| 315 | parameters |
| 316 | .setName(name) |
| 317 | .setStrategy(strategy); |
| 318 | |
| 319 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
| 320 | bind(&Fib::onSuccess, this, _1, |
| 321 | "Successfully set strategy choice"), |
| 322 | bind(&Fib::onFailure, this, _1, _2, |
| 323 | "Failed to set strategy choice")); |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 328 | const std::string& message, const std::string& faceUri) |
| 329 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 330 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame^] | 331 | m_faceMap.writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 336 | Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 337 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 338 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 342 | Fib::onFailure(uint32_t code, const std::string& error, |
| 343 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 344 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame^] | 345 | _LOG_DEBUG(message << ": " << error << " (code: " << code << ")"); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 346 | } |
| 347 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 348 | void |
| 349 | Fib::writeLog() |
| 350 | { |
| 351 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
| 352 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
| 353 | ++it) { |
| 354 | (*it).writeLog(); |
| 355 | } |
| 356 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 357 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 358 | } //namespace nlsr |