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" |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 26 | #include "logger.hpp" |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 27 | #include "utility/name-helper.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 | |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 31 | const std::string NLSR_COMPONENT = "nlsr"; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 32 | const std::string LSA_COMPONENT = "LSA"; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 33 | |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 34 | INIT_LOGGER(SyncLogicHandler); |
| 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) |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 48 | : onNewLsa(std::make_unique<OnNewLsa>()) |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 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 | 4810107 | 2018-05-30 17:53:06 -0500 | [diff] [blame] | 56 | SyncLogicHandler::createSyncLogic(const ndn::Name& syncPrefix, const ndn::time::milliseconds& syncInterestLifetime) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | { |
Ashlesh Gawande | 4810107 | 2018-05-30 17:53:06 -0500 | [diff] [blame] | 58 | if (m_syncLogic != nullptr) { |
| 59 | NLSR_LOG_WARN("Trying to create Sync Logic object, but Sync Logic object already exists"); |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 63 | // Build LSA sync update prefix |
| 64 | buildUpdatePrefix(); |
| 65 | |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 66 | NLSR_LOG_DEBUG("Creating Sync Logic object. Sync Prefix: " << syncPrefix); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 67 | |
Ashlesh Gawande | 4810107 | 2018-05-30 17:53:06 -0500 | [diff] [blame] | 68 | // The face's lifetime is managed in main.cpp; Logic should not manage the memory |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 69 | // of the object |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 70 | std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>()); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 71 | |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 72 | m_syncLogic = std::make_shared<SyncProtocolAdapter>(*facePtr, |
| 73 | m_confParam.getSyncProtocol(), |
| 74 | syncPrefix, |
| 75 | m_nameLsaUserPrefix, |
| 76 | syncInterestLifetime, |
| 77 | std::bind(&SyncLogicHandler::processUpdate, this, _1, _2)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 78 | |
| 79 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 80 | m_syncLogic->addUserNode(m_adjLsaUserPrefix); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 81 | } |
laqinfan | ac4b656 | 2017-12-08 11:24:21 +0000 | [diff] [blame] | 82 | else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 83 | m_syncLogic->addUserNode(m_coorLsaUserPrefix); |
laqinfan | ac4b656 | 2017-12-08 11:24:21 +0000 | [diff] [blame] | 84 | } |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 85 | else { |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 86 | m_syncLogic->addUserNode(m_adjLsaUserPrefix); |
| 87 | m_syncLogic->addUserNode(m_coorLsaUserPrefix); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 88 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 92 | SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | { |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 94 | NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 95 | |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 96 | int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT); |
| 97 | int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 98 | |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 99 | if (nlsrPosition < 0 || lsaPosition < 0) { |
| 100 | NLSR_LOG_WARN("Received malformed sync update"); |
| 101 | return; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | } |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 103 | |
| 104 | ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1); |
| 105 | ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1); |
| 106 | |
| 107 | ndn::Name originRouter = networkName; |
| 108 | originRouter.append(routerName); |
| 109 | |
| 110 | processUpdateFromSync(originRouter, updateName, highSeq); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 114 | SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter, |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 115 | const ndn::Name& updateName, uint64_t seqNo) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 116 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 117 | NLSR_LOG_DEBUG("Origin Router of update: " << originRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 119 | // A router should not try to fetch its own LSA |
| 120 | if (originRouter != m_confParam.getRouterPrefix()) { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 121 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 122 | Lsa::Type lsaType; |
| 123 | std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 124 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 125 | NLSR_LOG_DEBUG("Received sync update with higher " << lsaType |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 126 | << " sequence number than entry in LSDB"); |
| 127 | |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 128 | if (m_isLsaNew(originRouter, lsaType, seqNo)) { |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 129 | if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 && |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 130 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 131 | NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing" |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 132 | << " is enabled. Not going to fetch."); |
| 133 | return; |
| 134 | } |
| 135 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 136 | if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 && |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 137 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 138 | NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state" |
Nick Gordon | 0f1bf1d | 2017-06-22 15:40:27 -0500 | [diff] [blame] | 139 | << " is enabled. Not going to fetch."); |
| 140 | return; |
| 141 | } |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 142 | (*onNewLsa)(updateName, seqNo); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 143 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 147 | void |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 148 | SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 149 | { |
Ashlesh Gawande | 4810107 | 2018-05-30 17:53:06 -0500 | [diff] [blame] | 150 | if (m_syncLogic == nullptr) { |
| 151 | NLSR_LOG_FATAL("Cannot publish routing update; SyncLogic does not exist"); |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 152 | |
Ashlesh Gawande | 4810107 | 2018-05-30 17:53:06 -0500 | [diff] [blame] | 153 | BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncLogic does not exist")); |
Vince Lehman | 9d09780 | 2015-03-16 17:55:59 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 156 | switch (type) { |
| 157 | case Lsa::Type::ADJACENCY: |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 158 | m_syncLogic->publishUpdate(m_adjLsaUserPrefix, seqNo); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 159 | break; |
| 160 | case Lsa::Type::COORDINATE: |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 161 | m_syncLogic->publishUpdate(m_coorLsaUserPrefix, seqNo); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 162 | break; |
| 163 | case Lsa::Type::NAME: |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame^] | 164 | m_syncLogic->publishUpdate(m_nameLsaUserPrefix, seqNo); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 165 | break; |
| 166 | default: |
| 167 | break; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 168 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 172 | SyncLogicHandler::buildUpdatePrefix() |
| 173 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 174 | ndn::Name updatePrefix = m_confParam.getLsaPrefix(); |
| 175 | updatePrefix.append(m_confParam.getSiteName()); |
| 176 | updatePrefix.append(m_confParam.getRouterName()); |
| 177 | |
| 178 | m_nameLsaUserPrefix = updatePrefix; |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 179 | m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 180 | |
| 181 | m_adjLsaUserPrefix = updatePrefix; |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 182 | m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 183 | |
| 184 | m_coorLsaUserPrefix = updatePrefix; |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 185 | m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE)); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 186 | } |
| 187 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 188 | } // namespace nlsr |