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 | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 33 | const std::string HelloProtocol::INFO_COMPONENT = "INFO"; |
| 34 | const std::string HelloProtocol::NLSR_COMPONENT = "NLSR"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 35 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 36 | void |
| 37 | HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds) |
| 38 | { |
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 | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 57 | /* interest name: /<neighbor>/NLSR/INFO/<router> */ |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 58 | ndn::Name interestName = (*it).getName() ; |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 59 | interestName.append(NLSR_COMPONENT); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 60 | interestName.append(INFO_COMPONENT); |
| 61 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 62 | expressInterest(interestName, |
| 63 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 64 | } |
| 65 | scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval()); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | HelloProtocol::scheduleInterest(uint32_t seconds) |
| 70 | { |
| 71 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds), |
| 72 | ndn::bind(&HelloProtocol::sendScheduledInterest, |
| 73 | this, seconds)); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | HelloProtocol::processInterest(const ndn::Name& name, |
| 78 | const ndn::Interest& interest) |
| 79 | { |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 80 | /* interest name: /<neighbor>/NLSR/INFO/<router> */ |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 81 | const ndn::Name interestName = interest.getName(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 82 | _LOG_DEBUG("Interest Received for Name: " << interestName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 83 | if (interestName.get(-2).toUri() != INFO_COMPONENT) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 84 | return; |
| 85 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 86 | ndn::Name neighbor; |
| 87 | neighbor.wireDecode(interestName.get(-1).blockFromValue()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 88 | _LOG_DEBUG("Neighbor: " << neighbor); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 89 | if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 90 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
| 91 | data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 92 | data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()), |
| 93 | INFO_COMPONENT.size()); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 94 | m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName()); |
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 | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 99 | /* interest name: /<neighbor>/NLSR/INFO/<router> */ |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 100 | ndn::Name interestName(neighbor); |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 101 | interestName.append(NLSR_COMPONENT); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 102 | interestName.append(INFO_COMPONENT); |
akmhoque | d57f367 | 2014-06-10 10:41:32 -0500 | [diff] [blame] | 103 | interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 104 | expressInterest(interestName, |
| 105 | m_nlsr.getConfParameter().getInterestResendTime()); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | HelloProtocol::processInterestTimedOut(const ndn::Interest& interest) |
| 112 | { |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 113 | /* interest name: /<neighbor>/NLSR/INFO/<router> */ |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 114 | const ndn::Name interestName(interest.getName()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 115 | _LOG_DEBUG("Interest timed out for Name: " << interestName); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 116 | if (interestName.get(-2).toUri() != INFO_COMPONENT) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 117 | return; |
| 118 | } |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 119 | ndn::Name neighbor = interestName.getPrefix(-3); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 120 | _LOG_DEBUG("Neighbor: " << neighbor); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 121 | m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor); |
| 122 | int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 123 | uint32_t infoIntTimedOutCount = |
| 124 | m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 125 | _LOG_DEBUG("Status: " << status); |
| 126 | _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 127 | if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) { |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 128 | /* interest name: /<neighbor>/NLSR/INFO/<router> */ |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 129 | ndn::Name interestName(neighbor); |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 130 | interestName.append(NLSR_COMPONENT); |
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 | { |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 163 | /* data name: /<neighbor>/NLSR/INFO/<router>/<version> */ |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 164 | ndn::Name dataName = data->getName(); |
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) { |
akmhoque | 93f1a07 | 2014-06-19 16:24:28 -0500 | [diff] [blame] | 167 | ndn::Name neighbor = dataName.getPrefix(-4); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 168 | int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 169 | m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1); |
| 170 | m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0); |
| 171 | int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 172 | _LOG_DEBUG("Neighbor : " << neighbor); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 173 | _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 174 | // change in Adjacency list |
| 175 | if ((oldStatus - newStatus) != 0) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 176 | m_nlsr.incrementAdjBuildCount(); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 177 | // Need to schedule event for Adjacency LSA building |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 178 | if (m_nlsr.getIsBuildAdjLsaSheduled() == false) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 179 | _LOG_DEBUG("Scheduling scheduledAdjLsaBuild"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 180 | m_nlsr.setIsBuildAdjLsaSheduled(true); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 181 | // event here |
| 182 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 183 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 184 | ndn::ref(m_nlsr.getLsdb()))); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 190 | void |
| 191 | HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 192 | const std::string& msg) |
| 193 | { |
| 194 | _LOG_DEBUG("Validation Error: " << msg); |
| 195 | } |
| 196 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 197 | } //namespace nlsr |