akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 31 | INIT_LOGGER("SyncLogicHandler"); |
| 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 | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame^] | 46 | SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, ConfParameter& conf) |
| 47 | : onNewLsa(ndn::make_unique<OnNewLsa>()) |
| 48 | , m_syncFace(face) |
| 49 | , m_isLsaNew(isLsaNew) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 50 | , m_confParam(conf) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 51 | { |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 52 | } |
| 53 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | void |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 55 | SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 56 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 57 | if (m_syncSocket != nullptr) { |
| 58 | _LOG_WARN("Trying to create Sync socket, but Sync socket already exists"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | m_syncPrefix = syncPrefix; |
| 63 | |
| 64 | // Build LSA sync update prefix |
| 65 | buildUpdatePrefix(); |
| 66 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 67 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 68 | |
| 69 | // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory |
| 70 | // of the object |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 71 | std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 72 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 73 | m_syncSocket = std::make_shared<chronosync::Socket>(m_syncPrefix, m_nameLsaUserPrefix, *facePtr, |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 74 | std::bind(&SyncLogicHandler::onChronoSyncUpdate, this, _1)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 75 | |
| 76 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
| 77 | m_syncSocket->addSyncNode(m_adjLsaUserPrefix); |
| 78 | } |
| 79 | else { |
| 80 | m_syncSocket->addSyncNode(m_coorLsaUserPrefix); |
| 81 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 85 | SyncLogicHandler::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& v) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | { |
Ashlesh Gawande | 7600c90 | 2017-06-21 13:28:35 -0500 | [diff] [blame] | 87 | _LOG_DEBUG("Received ChronoSync update event"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 88 | |
| 89 | for (size_t i = 0; i < v.size(); i++){ |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 90 | ndn::Name updateName = v[i].session.getPrefix(-1); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 91 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 92 | _LOG_DEBUG("Update Name: " << updateName << " Seq no: " << v[i].high); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 93 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 94 | int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT); |
| 95 | int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT); |
| 96 | |
| 97 | if (nlsrPosition < 0 || lsaPosition < 0) { |
| 98 | _LOG_WARN("Received malformed sync update"); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1); |
| 103 | ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1); |
| 104 | |
| 105 | ndn::Name originRouter = networkName; |
| 106 | originRouter.append(routerName); |
| 107 | |
| 108 | processUpdateFromSync(originRouter, updateName, v[i].high); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 113 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter, |
| 114 | const ndn::Name& updateName, const uint64_t& seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 115 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 116 | _LOG_DEBUG("Origin Router of update: " << originRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 118 | // A router should not try to fetch its own LSA |
| 119 | if (originRouter != m_confParam.getRouterPrefix()) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 120 | |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 121 | std::string lsaType = updateName.get(updateName.size()-1).toUri(); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 122 | |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 123 | _LOG_DEBUG("Received sync update with higher " << lsaType |
| 124 | << " sequence number than entry in LSDB"); |
| 125 | |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame^] | 126 | if (m_isLsaNew(originRouter, lsaType, seqNo)) { |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 127 | if (lsaType == AdjLsa::TYPE_STRING && seqNo != 0 && |
| 128 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 129 | _LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing" |
| 130 | << " is enabled. Not going to fetch."); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (lsaType == CoordinateLsa::TYPE_STRING && seqNo != 0 && |
| 135 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
| 136 | _LOG_ERROR("Got an update for coordinate LSA when link-state" |
| 137 | << " is enabled. Not going to fetch."); |
| 138 | return; |
| 139 | } |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame^] | 140 | (*onNewLsa)(updateName, seqNo); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 141 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 145 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 146 | SyncLogicHandler::publishRoutingUpdate(const ndn::Name& type, const uint64_t& seqNo) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 147 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 148 | if (m_syncSocket == nullptr) { |
| 149 | _LOG_FATAL("Cannot publish routing update; SyncSocket does not exist"); |
| 150 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 151 | 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] | 152 | } |
| 153 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 154 | if (type == NameLsa::TYPE_STRING) { |
| 155 | publishSyncUpdate(m_nameLsaUserPrefix, seqNo); |
| 156 | } |
| 157 | else if (type == AdjLsa::TYPE_STRING) { |
| 158 | publishSyncUpdate(m_adjLsaUserPrefix, seqNo); |
| 159 | } |
| 160 | else { |
| 161 | publishSyncUpdate(m_coorLsaUserPrefix, seqNo); |
| 162 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 166 | SyncLogicHandler::buildUpdatePrefix() |
| 167 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 168 | ndn::Name updatePrefix = m_confParam.getLsaPrefix(); |
| 169 | updatePrefix.append(m_confParam.getSiteName()); |
| 170 | updatePrefix.append(m_confParam.getRouterName()); |
| 171 | |
| 172 | m_nameLsaUserPrefix = updatePrefix; |
| 173 | m_nameLsaUserPrefix.append(NameLsa::TYPE_STRING); |
| 174 | |
| 175 | m_adjLsaUserPrefix = updatePrefix; |
| 176 | m_adjLsaUserPrefix.append(AdjLsa::TYPE_STRING); |
| 177 | |
| 178 | m_coorLsaUserPrefix = updatePrefix; |
| 179 | m_coorLsaUserPrefix.append(CoordinateLsa::TYPE_STRING); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 183 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo) |
| 184 | { |
| 185 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo); |
| 186 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 187 | ndn::Name updateName(updatePrefix); |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 188 | std::string data("NoData"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 189 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 190 | m_syncSocket->publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), |
| 191 | ndn::time::milliseconds(1000), seqNo, updateName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 194 | } // namespace nlsr |