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 | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 59 | std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl; |
| 60 | std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl; |
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 | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 71 | std::cout << "Fib::refreshEntry Called" << std::endl; |
| 72 | std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl; |
| 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 | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 77 | std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 78 | if (it->getSeqNo() == feSeqNum) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 79 | std::cout << "Refreshing the FIB entry" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 80 | _LOG_DEBUG("Refreshing the FIB entry. Name: " << name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 81 | for (std::list<NextHop>::iterator nhit = |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 82 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 83 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 84 | // add entry to NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 85 | registerPrefix(it->getName(), nhit->getConnectingFaceUri(), |
| 86 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 87 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 88 | // increase sequence number and schedule refresh again |
| 89 | it->setSeqNo(feSeqNum + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 90 | it->setExpiringEventId(scheduleEntryRefreshing(it->getName() , |
| 91 | it->getSeqNo(), |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 92 | ndn::time::seconds(m_refreshTime))); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 93 | } |
| 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 | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 109 | if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) { |
| 110 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 114 | if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() != |
| 115 | nhit->getConnectingFaceUri()) { |
| 116 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 120 | std::cout << "Cancellling Scheduled event" << std::endl; |
| 121 | std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 122 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << name); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 123 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | m_table.erase(it); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 130 | Fib::update(const ndn::Name& name, NexthopList& nextHopList) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | { |
| 132 | std::cout << "Fib::updateFib Called" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 133 | _LOG_DEBUG("Fib::updateFib Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | int startFace = 0; |
| 135 | int endFace = getNumberOfFacesForName(nextHopList, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 136 | m_nlsr.getConfParameter().getMaxFacesPerPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | std::list<FibEntry>::iterator it = std::find_if(m_table.begin(), |
| 138 | m_table.end(), |
| 139 | bind(&fibEntryNameCompare, _1, name)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 140 | if (it == m_table.end()) { |
| 141 | if (nextHopList.getSize() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | nextHopList.sort(); |
| 143 | FibEntry newEntry(name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 144 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | std::list<NextHop>::iterator nhit = nhl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 146 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 147 | newEntry.getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 148 | //Add entry to NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 149 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 150 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 152 | newEntry.getNexthopList().sort(); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 153 | ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now(); |
| 154 | expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime); |
| 155 | newEntry.setExpirationTimePoint(expirationTimePoint); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 156 | newEntry.setSeqNo(1); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 157 | newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1, |
| 158 | ndn::time::seconds(m_refreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | m_table.push_back(newEntry); |
| 160 | } |
| 161 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 162 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 163 | std::cout << "Old FIB Entry" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 164 | if (nextHopList.getSize() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 165 | nextHopList.sort(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 166 | if (!it->isEqualNextHops(nextHopList)) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 167 | std::list<NextHop> nhl = nextHopList.getNextHops(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | std::list<NextHop>::iterator nhit = nhl.begin(); |
| 169 | // Add first Entry to NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 170 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 171 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
| 172 | removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 173 | it->getNexthopList().reset(); |
| 174 | it->getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | ++startFace; |
| 176 | ++nhit; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 177 | for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 178 | it->getNexthopList().addNextHop((*nhit)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 179 | //Add Entry to NDN_FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 180 | registerPrefix(name, nhit->getConnectingFaceUri(), |
| 181 | std::ceil(nhit->getRouteCost()), m_refreshTime); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 184 | ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now(); |
| 185 | expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime); |
| 186 | it->setExpirationTimePoint(expirationTimePoint); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 187 | std::cout << "Cancellling Scheduled event" << std::endl; |
| 188 | std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 189 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << name); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 190 | cancelScheduledExpiringEvent(it->getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | it->setSeqNo(it->getSeqNo() + 1); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 192 | (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() , |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 193 | it->getSeqNo(), |
| 194 | ndn::time::seconds(m_refreshTime))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 196 | else { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 197 | remove(name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 205 | Fib::clean() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 207 | _LOG_DEBUG("Fib::clean called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 208 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 209 | ++it) { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 210 | std::cout << "Cancellling Scheduled event" << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 211 | std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() << |
| 212 | std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 213 | _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 214 | cancelScheduledExpiringEvent((*it).getExpiringEventId()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | for (std::list<NextHop>::iterator nhit = |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 216 | (*it).getNexthopList().getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 217 | nhit != (*it).getNexthopList().getNextHops().end(); nhit++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | //Remove entry from NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 219 | if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) { |
| 220 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 221 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 222 | else { |
| 223 | if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() != |
| 224 | nhit->getConnectingFaceUri()) { |
| 225 | unregisterPrefix(it->getName(), nhit->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 226 | } |
| 227 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 228 | } |
| 229 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 230 | if (m_table.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 231 | m_table.clear(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | int |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 236 | Fib::getNumberOfFacesForName(NexthopList& nextHopList, |
| 237 | uint32_t maxFacesPerPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | { |
| 239 | int endFace = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 240 | if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 241 | return nextHopList.getSize(); |
| 242 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 243 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 244 | return maxFacesPerPrefix; |
| 245 | } |
| 246 | return endFace; |
| 247 | } |
| 248 | |
| 249 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 250 | Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 251 | const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 252 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 253 | for (std::list<NextHop>::iterator it = nl.getNextHops().begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 254 | it != nl.getNextHops().end(); ++it) { |
| 255 | if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 256 | //Remove FIB Entry from NDN-FIB |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 257 | if (!m_nlsr.getAdjacencyList().isNeighbor(name)) { |
| 258 | unregisterPrefix(name, it->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 259 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 260 | else { |
| 261 | if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() != |
| 262 | it->getConnectingFaceUri()) { |
| 263 | unregisterPrefix(name, it->getConnectingFaceUri()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 264 | } |
| 265 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 271 | Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri, |
| 272 | uint64_t faceCost, uint64_t timeout) |
| 273 | { |
| 274 | ndn::nfd::ControlParameters faceParameters; |
| 275 | faceParameters |
| 276 | .setUri(faceUri); |
| 277 | |
| 278 | m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, |
| 279 | ndn::bind(&Fib::registerPrefixInNfd, this,_1, |
| 280 | namePrefix, faceCost, timeout), |
| 281 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 282 | "Failed in name registration")); |
| 283 | |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, |
| 288 | const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 289 | { |
| 290 | ndn::nfd::ControlParameters controlParameters; |
| 291 | controlParameters |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 292 | .setName(namePrefix) |
| 293 | .setFaceId(faceCreateResult.getFaceId()) |
| 294 | .setCost(faceCost) |
| 295 | .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000)) |
| 296 | .setOrigin(128); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 297 | m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 298 | ndn::bind(&Fib::onRegistration, this, _1, |
| 299 | "Successful in name registration", |
| 300 | faceCreateResult.getUri()), |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 301 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 302 | "Failed in name registration")); |
| 303 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 304 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 305 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 306 | Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 307 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 308 | uint32_t faceId = m_faceMap.getFaceId(faceUri); |
| 309 | if (faceId > 0) { |
| 310 | ndn::nfd::ControlParameters controlParameters; |
| 311 | controlParameters |
| 312 | .setName(namePrefix) |
| 313 | .setFaceId(faceId) |
| 314 | .setOrigin(128); |
| 315 | m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 316 | ndn::bind(&Fib::onSuccess, this, _1, |
| 317 | "Successful in unregistering name"), |
| 318 | ndn::bind(&Fib::onFailure, this, _1, _2, |
| 319 | "Failed in unregistering name")); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 320 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 324 | Fib::setStrategy(const ndn::Name& name, const std::string& strategy) |
| 325 | { |
| 326 | ndn::nfd::ControlParameters parameters; |
| 327 | parameters |
| 328 | .setName(name) |
| 329 | .setStrategy(strategy); |
| 330 | |
| 331 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters, |
| 332 | bind(&Fib::onSuccess, this, _1, |
| 333 | "Successfully set strategy choice"), |
| 334 | bind(&Fib::onFailure, this, _1, _2, |
| 335 | "Failed to set strategy choice")); |
| 336 | } |
| 337 | |
| 338 | void |
| 339 | Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 340 | const std::string& message, const std::string& faceUri) |
| 341 | { |
| 342 | //std::cout << message << ": " << commandSuccessResult << std::endl; |
| 343 | m_faceMap.update(faceUri, commandSuccessResult.getFaceId()); |
| 344 | m_faceMap.print(); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 349 | Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, |
| 350 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 351 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 352 | //std::cout << message << ": " << commandSuccessResult << std::endl; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 356 | Fib::onFailure(uint32_t code, const std::string& error, |
| 357 | const std::string& message) |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 358 | { |
| 359 | std::cout << message << ": " << error << " (code: " << code << ")"; |
| 360 | } |
| 361 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 362 | void |
| 363 | Fib::writeLog() |
| 364 | { |
| 365 | _LOG_DEBUG("-------------------FIB-----------------------------"); |
| 366 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
| 367 | ++it) { |
| 368 | (*it).writeLog(); |
| 369 | } |
| 370 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 371 | |
| 372 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 373 | Fib::print() |
| 374 | { |
| 375 | cout << "-------------------FIB-----------------------------" << endl; |
| 376 | for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 377 | ++it) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 378 | cout << (*it); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | } //namespace nlsr |