akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #include "sync-logic-handler.hpp" |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 23 | #include "common.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 24 | #include "conf-parameter.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 25 | #include "lsa.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 26 | #include "utility/name-helper.hpp" |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 27 | #include "logger.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 28 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | namespace nlsr { |
| 30 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 31 | INIT_LOGGER(SyncLogicHandler); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 32 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 33 | const std::string NLSR_COMPONENT = "NLSR"; |
| 34 | const std::string LSA_COMPONENT = "LSA"; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 35 | |
Alexander Afanasyev | 7c8882f | 2014-10-28 12:12:15 -0700 | [diff] [blame] | 36 | template<class T> |
| 37 | class NullDeleter |
| 38 | { |
| 39 | public: |
| 40 | void |
| 41 | operator()(T*) |
| 42 | { |
| 43 | } |
| 44 | }; |
| 45 | |
Nick Gordon | 563611e | 2018-01-23 13:44:36 -0600 | [diff] [blame] | 46 | SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, |
| 47 | const ConfParameter& conf) |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 48 | : onNewLsa(ndn::make_unique<OnNewLsa>()) |
| 49 | , m_syncFace(face) |
| 50 | , m_isLsaNew(isLsaNew) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 51 | , m_confParam(conf) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 52 | { |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 53 | } |
| 54 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | void |
Ashlesh Gawande | f7da9c5 | 2018-02-06 17:36:46 -0600 | [diff] [blame] | 56 | SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix, const ndn::time::milliseconds& syncInterestLifetime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 58 | if (m_syncSocket != nullptr) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 59 | NLSR_LOG_WARN("Trying to create Sync socket, but Sync socket already exists"); |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
| 63 | m_syncPrefix = syncPrefix; |
| 64 | |
| 65 | // Build LSA sync update prefix |
| 66 | buildUpdatePrefix(); |
| 67 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 68 | NLSR_LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 69 | |
| 70 | // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory |
| 71 | // of the object |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 72 | std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 73 | |
Alexander Afanasyev | b0bf8df | 2018-02-19 12:25:44 -0500 | [diff] [blame^] | 74 | const auto fixedSession = ndn::name::Component::fromNumber(0); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 75 | m_syncSocket = std::make_shared<chronosync::Socket>(m_syncPrefix, m_nameLsaUserPrefix, *facePtr, |
Ashlesh Gawande | f7da9c5 | 2018-02-06 17:36:46 -0600 | [diff] [blame] | 76 | std::bind(&SyncLogicHandler::onChronoSyncUpdate, this, _1), |
| 77 | chronosync::Socket::DEFAULT_NAME, chronosync::Socket::DEFAULT_VALIDATOR, |
Alexander Afanasyev | b0bf8df | 2018-02-19 12:25:44 -0500 | [diff] [blame^] | 78 | syncInterestLifetime, fixedSession); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 79 | |
| 80 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
Alexander Afanasyev | b0bf8df | 2018-02-19 12:25:44 -0500 | [diff] [blame^] | 81 | m_syncSocket->addSyncNode(m_adjLsaUserPrefix, chronosync::Socket::DEFAULT_NAME, fixedSession); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 82 | } |
laqinfan | ac4b656 | 2017-12-08 11:24:21 +0000 | [diff] [blame] | 83 | else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
Alexander Afanasyev | b0bf8df | 2018-02-19 12:25:44 -0500 | [diff] [blame^] | 84 | m_syncSocket->addSyncNode(m_coorLsaUserPrefix, chronosync::Socket::DEFAULT_NAME, fixedSession); |
laqinfan | ac4b656 | 2017-12-08 11:24:21 +0000 | [diff] [blame] | 85 | } |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 86 | else { |
Alexander Afanasyev | b0bf8df | 2018-02-19 12:25:44 -0500 | [diff] [blame^] | 87 | m_syncSocket->addSyncNode(m_adjLsaUserPrefix, chronosync::Socket::DEFAULT_NAME, fixedSession); |
| 88 | m_syncSocket->addSyncNode(m_coorLsaUserPrefix, chronosync::Socket::DEFAULT_NAME, fixedSession); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 89 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 93 | SyncLogicHandler::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& v) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 95 | NLSR_LOG_DEBUG("Received ChronoSync update event"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 96 | |
| 97 | for (size_t i = 0; i < v.size(); i++){ |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 98 | ndn::Name updateName = v[i].session.getPrefix(-1); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 99 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 100 | NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << v[i].high); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 101 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 102 | int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT); |
| 103 | int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT); |
| 104 | |
| 105 | if (nlsrPosition < 0 || lsaPosition < 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 106 | NLSR_LOG_WARN("Received malformed sync update"); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
| 110 | ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1); |
| 111 | ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1); |
| 112 | |
| 113 | ndn::Name originRouter = networkName; |
| 114 | originRouter.append(routerName); |
| 115 | |
| 116 | processUpdateFromSync(originRouter, updateName, v[i].high); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 121 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter, |
| 122 | const ndn::Name& updateName, const uint64_t& seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 124 | NLSR_LOG_DEBUG("Origin Router of update: " << originRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 126 | // A router should not try to fetch its own LSA |
| 127 | if (originRouter != m_confParam.getRouterPrefix()) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 128 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 129 | Lsa::Type lsaType; |
| 130 | std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 131 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 132 | NLSR_LOG_DEBUG("Received sync update with higher " << lsaType |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 133 | << " sequence number than entry in LSDB"); |
| 134 | |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 135 | if (m_isLsaNew(originRouter, lsaType, seqNo)) { |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 136 | if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 && |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 137 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 138 | NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing" |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 139 | << " is enabled. Not going to fetch."); |
| 140 | return; |
| 141 | } |
| 142 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 143 | if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 && |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 144 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 145 | NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state" |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 146 | << " is enabled. Not going to fetch."); |
| 147 | return; |
| 148 | } |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 149 | (*onNewLsa)(updateName, seqNo); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 150 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 154 | void |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 155 | SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 156 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 157 | if (m_syncSocket == nullptr) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 158 | NLSR_LOG_FATAL("Cannot publish routing update; SyncSocket does not exist"); |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 159 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 160 | BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncSocket does not exist")); |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 163 | switch (type) { |
| 164 | case Lsa::Type::ADJACENCY: |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 165 | publishSyncUpdate(m_adjLsaUserPrefix, seqNo); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 166 | break; |
| 167 | case Lsa::Type::COORDINATE: |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 168 | publishSyncUpdate(m_coorLsaUserPrefix, seqNo); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 169 | break; |
| 170 | case Lsa::Type::NAME: |
| 171 | publishSyncUpdate(m_nameLsaUserPrefix, seqNo); |
| 172 | break; |
| 173 | default: |
| 174 | break; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 175 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 179 | SyncLogicHandler::buildUpdatePrefix() |
| 180 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 181 | ndn::Name updatePrefix = m_confParam.getLsaPrefix(); |
| 182 | updatePrefix.append(m_confParam.getSiteName()); |
| 183 | updatePrefix.append(m_confParam.getRouterName()); |
| 184 | |
| 185 | m_nameLsaUserPrefix = updatePrefix; |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 186 | m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 187 | |
| 188 | m_adjLsaUserPrefix = updatePrefix; |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 189 | m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 190 | |
| 191 | m_coorLsaUserPrefix = updatePrefix; |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 192 | m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE)); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 196 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo) |
| 197 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 198 | NLSR_LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 199 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | ndn::Name updateName(updatePrefix); |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 201 | std::string data("NoData"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 202 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 203 | m_syncSocket->publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), |
| 204 | ndn::time::milliseconds(1000), seqNo, updateName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 207 | } // namespace nlsr |