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