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