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 | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 23 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 24 | #include "common.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 25 | #include "conf-parameter.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 26 | #include "logger.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 27 | #include "lsa.hpp" |
| 28 | #include "lsdb.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 29 | #include "utility/name-helper.hpp" |
| 30 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | namespace nlsr { |
| 32 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 33 | INIT_LOGGER("SyncLogicHandler"); |
| 34 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 35 | using namespace ndn; |
| 36 | using namespace std; |
| 37 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 38 | const std::string NLSR_COMPONENT = "NLSR"; |
| 39 | const std::string LSA_COMPONENT = "LSA"; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 40 | |
Alexander Afanasyev | 7c8882f | 2014-10-28 12:12:15 -0700 | [diff] [blame] | 41 | template<class T> |
| 42 | class NullDeleter |
| 43 | { |
| 44 | public: |
| 45 | void |
| 46 | operator()(T*) |
| 47 | { |
| 48 | } |
| 49 | }; |
| 50 | |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 51 | SyncLogicHandler::SyncLogicHandler(ndn::Face& face, |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 52 | Lsdb& lsdb, ConfParameter& conf) |
| 53 | : m_syncFace(face) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 54 | , m_lsdb(lsdb) |
| 55 | , m_confParam(conf) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 56 | { |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 57 | } |
| 58 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | void |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 60 | SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 61 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 62 | if (m_syncSocket != nullptr) { |
| 63 | _LOG_WARN("Trying to create Sync socket, but Sync socket already exists"); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | m_syncPrefix = syncPrefix; |
| 68 | |
| 69 | // Build LSA sync update prefix |
| 70 | buildUpdatePrefix(); |
| 71 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 72 | _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 73 | |
| 74 | // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory |
| 75 | // of the object |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 76 | std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 77 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 78 | |
| 79 | m_syncSocket = std::make_shared<chronosync::Socket>(m_syncPrefix, m_nameLsaUserPrefix, *facePtr, |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 80 | bind(&SyncLogicHandler::onNsyncUpdate, this, _1)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 81 | |
| 82 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
| 83 | m_syncSocket->addSyncNode(m_adjLsaUserPrefix); |
| 84 | } |
| 85 | else { |
| 86 | m_syncSocket->addSyncNode(m_coorLsaUserPrefix); |
| 87 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 91 | SyncLogicHandler::onNsyncUpdate(const vector<chronosync::MissingDataInfo>& v) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 93 | _LOG_DEBUG("Received Nsync update event"); |
| 94 | |
| 95 | for (size_t i = 0; i < v.size(); i++){ |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 96 | ndn::Name updateName = v[i].session.getPrefix(-1); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 97 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 98 | _LOG_DEBUG("Update Name: " << updateName << " Seq no: " << v[i].high); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 99 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 100 | int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT); |
| 101 | int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT); |
| 102 | |
| 103 | if (nlsrPosition < 0 || lsaPosition < 0) { |
| 104 | _LOG_WARN("Received malformed sync update"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1); |
| 109 | ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1); |
| 110 | |
| 111 | ndn::Name originRouter = networkName; |
| 112 | originRouter.append(routerName); |
| 113 | |
| 114 | processUpdateFromSync(originRouter, updateName, v[i].high); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 119 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter, |
| 120 | const ndn::Name& updateName, const uint64_t& seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 122 | _LOG_DEBUG("Origin Router of update: " << originRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 124 | // A router should not try to fetch its own LSA |
| 125 | if (originRouter != m_confParam.getRouterPrefix()) { |
| 126 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 127 | std::string lsaType = updateName.get(updateName.size()-1).toUri(); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 128 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 129 | _LOG_DEBUG("Received sync update with higher " << lsaType |
| 130 | << " sequence number than entry in LSDB"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 131 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 132 | if (isLsaNew(originRouter, lsaType, seqNo)) { |
| 133 | if (lsaType == AdjLsa::TYPE_STRING && seqNo != 0 && |
| 134 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 135 | _LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing" |
| 136 | << " is enabled. Not going to fetch."); |
| 137 | return; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 138 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 139 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 140 | if (lsaType == CoordinateLsa::TYPE_STRING && seqNo != 0 && |
| 141 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
| 142 | _LOG_ERROR("Got an update for coordinate LSA when link-state" |
| 143 | << " is enabled. Not going to fetch."); |
| 144 | return; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 145 | } |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 146 | expressInterestForLsa(updateName, seqNo); |
| 147 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 151 | bool |
| 152 | SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType, |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 153 | const uint64_t& seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 155 | ndn::Name lsaKey = originRouter; |
| 156 | lsaKey.append(lsaType); |
| 157 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 158 | if (lsaType == NameLsa::TYPE_STRING) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 159 | { |
| 160 | return m_lsdb.isNameLsaNew(lsaKey, seqNo); |
| 161 | } |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 162 | else if (lsaType == AdjLsa::TYPE_STRING) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 163 | { |
| 164 | return m_lsdb.isAdjLsaNew(lsaKey, seqNo); |
| 165 | } |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 166 | else if (lsaType == CoordinateLsa::TYPE_STRING) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 167 | { |
| 168 | return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo); |
| 169 | } |
| 170 | |
| 171 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | } |
| 173 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 175 | SyncLogicHandler::expressInterestForLsa(const ndn::Name& updateName, |
| 176 | const uint64_t& seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 178 | ndn::Name interest(updateName); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 179 | interest.appendNumber(seqNo); |
| 180 | |
| 181 | m_lsdb.expressInterest(interest, 0); |
| 182 | } |
| 183 | |
| 184 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 185 | SyncLogicHandler::publishRoutingUpdate(const ndn::Name& type, const uint64_t& seqNo) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 186 | { |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 187 | if (m_syncSocket == nullptr) { |
| 188 | _LOG_FATAL("Cannot publish routing update; SyncSocket does not exist"); |
| 189 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 190 | 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] | 191 | } |
| 192 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 193 | if (type == NameLsa::TYPE_STRING) { |
| 194 | publishSyncUpdate(m_nameLsaUserPrefix, seqNo); |
| 195 | } |
| 196 | else if (type == AdjLsa::TYPE_STRING) { |
| 197 | publishSyncUpdate(m_adjLsaUserPrefix, seqNo); |
| 198 | } |
| 199 | else { |
| 200 | publishSyncUpdate(m_coorLsaUserPrefix, seqNo); |
| 201 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 205 | SyncLogicHandler::buildUpdatePrefix() |
| 206 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame^] | 207 | ndn::Name updatePrefix = m_confParam.getLsaPrefix(); |
| 208 | updatePrefix.append(m_confParam.getSiteName()); |
| 209 | updatePrefix.append(m_confParam.getRouterName()); |
| 210 | |
| 211 | m_nameLsaUserPrefix = updatePrefix; |
| 212 | m_nameLsaUserPrefix.append(NameLsa::TYPE_STRING); |
| 213 | |
| 214 | m_adjLsaUserPrefix = updatePrefix; |
| 215 | m_adjLsaUserPrefix.append(AdjLsa::TYPE_STRING); |
| 216 | |
| 217 | m_coorLsaUserPrefix = updatePrefix; |
| 218 | m_coorLsaUserPrefix.append(CoordinateLsa::TYPE_STRING); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 222 | SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo) |
| 223 | { |
| 224 | _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo); |
| 225 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 226 | ndn::Name updateName(updatePrefix); |
| 227 | string data("NoData"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 228 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 229 | m_syncSocket->publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), |
| 230 | ndn::time::milliseconds(1000), seqNo, updateName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 233 | } // namespace nlsr |