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 | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 23 | #include "nlsr.hpp" |
| 24 | #include "lsdb.hpp" |
| 25 | #include "hello-protocol.hpp" |
| 26 | #include "utility/name-helper.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 27 | #include "logger.hpp" |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
| 30 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | INIT_LOGGER("HelloProtocol"); |
| 32 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 33 | const std::string HelloProtocol::INFO_COMPONENT="info"; |
| 34 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 35 | void |
| 36 | HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds) |
| 37 | { |
| 38 | std::cout << "Expressing Interest :" << interestName << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 39 | _LOG_DEBUG("Expressing Interest :" << interestName); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 40 | ndn::Interest i(interestName); |
| 41 | i.setInterestLifetime(ndn::time::seconds(seconds)); |
| 42 | i.setMustBeFresh(true); |
| 43 | m_nlsr.getNlsrFace().expressInterest(i, |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 44 | ndn::bind(&HelloProtocol::onContent, |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 45 | this, |
| 46 | _1, _2), |
| 47 | ndn::bind(&HelloProtocol::processInterestTimedOut, |
| 48 | this, _1)); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | HelloProtocol::sendScheduledInterest(uint32_t seconds) |
| 53 | { |
| 54 | std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList(); |
| 55 | for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 56 | ++it) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 57 | ndn::Name interestName = (*it).getName() ; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 58 | interestName.append(INFO_COMPONENT); |
| 59 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 60 | expressInterest(interestName, |
| 61 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 62 | } |
| 63 | scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval()); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | HelloProtocol::scheduleInterest(uint32_t seconds) |
| 68 | { |
| 69 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds), |
| 70 | ndn::bind(&HelloProtocol::sendScheduledInterest, |
| 71 | this, seconds)); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | HelloProtocol::processInterest(const ndn::Name& name, |
| 76 | const ndn::Interest& interest) |
| 77 | { |
| 78 | const ndn::Name interestName = interest.getName(); |
| 79 | std::cout << "Interest Received for Name: " << interestName << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 80 | _LOG_DEBUG("Interest Received for Name: " << interestName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | if (interestName.get(-2).toUri() != INFO_COMPONENT) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 82 | return; |
| 83 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 84 | ndn::Name neighbor; |
| 85 | neighbor.wireDecode(interestName.get(-1).blockFromValue()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 86 | std::cout << "Neighbor: " << neighbor << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 87 | _LOG_DEBUG("Neighbor: " << neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 88 | if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 89 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
| 90 | data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 91 | data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()), |
| 92 | INFO_COMPONENT.size()); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 93 | m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 94 | std::cout << ">> D: " << data << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 95 | _LOG_DEBUG("Sending out data for name: " << data.getName()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 96 | m_nlsr.getNlsrFace().put(data); |
| 97 | int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 98 | if (status == 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 99 | ndn::Name interestName(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | interestName.append(INFO_COMPONENT); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 101 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix()); |
| 102 | expressInterest(interestName, |
| 103 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | HelloProtocol::processInterestTimedOut(const ndn::Interest& interest) |
| 110 | { |
| 111 | const ndn::Name interestName(interest.getName()); |
| 112 | std::cout << "Interest timed out for Name: " << interestName << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 113 | _LOG_DEBUG("Interest timed out for Name: " << interestName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 114 | if (interestName.get(-2).toUri() != INFO_COMPONENT) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 115 | return; |
| 116 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 117 | ndn::Name neighbor = interestName.getPrefix(-2); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 118 | std::cout << "Neighbor: " << neighbor << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 119 | _LOG_DEBUG("Neighbor: " << neighbor); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 120 | m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor); |
| 121 | int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 122 | uint32_t infoIntTimedOutCount = |
| 123 | m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor); |
| 124 | std::cout << "Neighbor: " << neighbor << std::endl; |
| 125 | std::cout << "Status: " << status << std::endl; |
| 126 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 127 | _LOG_DEBUG("Status: " << status); |
| 128 | _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 129 | if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 130 | ndn::Name interestName(neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 131 | interestName.append(INFO_COMPONENT); |
| 132 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 133 | expressInterest(interestName, |
| 134 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 135 | } |
| 136 | else if ((status == 1) && |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 137 | (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 138 | m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0); |
| 139 | m_nlsr.incrementAdjBuildCount(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 140 | if (m_nlsr.getIsBuildAdjLsaSheduled() == false) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 141 | _LOG_DEBUG("Scheduling scheduledAdjLsaBuild"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 142 | m_nlsr.setIsBuildAdjLsaSheduled(true); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 143 | // event here |
| 144 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 145 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, |
| 146 | &m_nlsr.getLsdb())); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 151 | void |
| 152 | HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data) |
| 153 | { |
| 154 | m_nlsr.getValidator().validate(data, |
| 155 | ndn::bind(&HelloProtocol::onContentValidated, this, _1), |
| 156 | ndn::bind(&HelloProtocol::onContentValidationFailed, |
| 157 | this, _1, _2)); |
| 158 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 159 | |
| 160 | void |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 161 | HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data) |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 162 | { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 163 | ndn::Name dataName = data->getName(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 164 | std::cout << "Data received for name: " << dataName << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 165 | _LOG_DEBUG("Data received for name: " << dataName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 166 | if (dataName.get(-3).toUri() == INFO_COMPONENT) { |
| 167 | ndn::Name neighbor = dataName.getPrefix(-3); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 168 | int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 169 | int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount( |
| 170 | neighbor); |
| 171 | //debugging purpose start |
| 172 | std::cout << "Before Updates: " << std::endl; |
| 173 | std::cout << "Neighbor : " << neighbor << std::endl; |
| 174 | std::cout << "Status: " << oldStatus << std::endl; |
| 175 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
| 176 | //debugging purpose end |
| 177 | m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1); |
| 178 | m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0); |
| 179 | int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 180 | infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount( |
| 181 | neighbor); |
| 182 | //debugging purpose |
| 183 | std::cout << "After Updates: " << std::endl; |
| 184 | std::cout << "Neighbor : " << neighbor << std::endl; |
| 185 | std::cout << "Status: " << newStatus << std::endl; |
| 186 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 187 | _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 188 | //debugging purpose end |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 189 | // change in Adjacency list |
| 190 | if ((oldStatus - newStatus) != 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 191 | m_nlsr.incrementAdjBuildCount(); |
| 192 | /* Need to schedule event for Adjacency LSA building */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 193 | if (m_nlsr.getIsBuildAdjLsaSheduled() == false) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 194 | _LOG_DEBUG("Scheduling scheduledAdjLsaBuild"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 195 | m_nlsr.setIsBuildAdjLsaSheduled(true); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 196 | // event here |
| 197 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 198 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 199 | ndn::ref(m_nlsr.getLsdb()))); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 205 | void |
| 206 | HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 207 | const std::string& msg) |
| 208 | { |
| 209 | _LOG_DEBUG("Validation Error: " << msg); |
| 210 | } |
| 211 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 212 | } //namespace nlsr |