Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, The University of Memphis, |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 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/>. |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 20 | */ |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 21 | |
| 22 | #include "sync-protocol-adapter.hpp" |
| 23 | #include "logger.hpp" |
| 24 | |
| 25 | INIT_LOGGER(SyncProtocolAdapter); |
| 26 | |
| 27 | namespace nlsr { |
| 28 | |
| 29 | const auto FIXED_SESSION = ndn::name::Component::fromNumber(0); |
| 30 | |
| 31 | SyncProtocolAdapter::SyncProtocolAdapter(ndn::Face& face, |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 32 | SyncProtocol syncProtocol, |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 33 | const ndn::Name& syncPrefix, |
| 34 | const ndn::Name& userPrefix, |
| 35 | ndn::time::milliseconds syncInterestLifetime, |
| 36 | const SyncUpdateCallback& syncUpdateCallback) |
| 37 | : m_syncProtocol(syncProtocol) |
| 38 | , m_syncUpdateCallback(syncUpdateCallback) |
| 39 | { |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 40 | NLSR_LOG_TRACE("SyncProtocol value: " << m_syncProtocol); |
| 41 | |
| 42 | #ifdef HAVE_CHRONOSYNC |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 43 | if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) { |
| 44 | NDN_LOG_DEBUG("Using ChronoSync"); |
| 45 | m_chronoSyncLogic = std::make_shared<chronosync::Logic>(face, |
| 46 | syncPrefix, |
| 47 | userPrefix, |
| 48 | std::bind(&SyncProtocolAdapter::onChronoSyncUpdate, this, _1), |
| 49 | chronosync::Logic::DEFAULT_NAME, |
| 50 | chronosync::Logic::DEFAULT_VALIDATOR, |
| 51 | chronosync::Logic::DEFAULT_RESET_TIMER, |
| 52 | chronosync::Logic::DEFAULT_CANCEL_RESET_TIMER, |
| 53 | chronosync::Logic::DEFAULT_RESET_INTEREST_LIFETIME, |
| 54 | syncInterestLifetime, |
| 55 | chronosync::Logic::DEFAULT_SYNC_REPLY_FRESHNESS, |
| 56 | chronosync::Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME, |
| 57 | FIXED_SESSION); |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 58 | return; |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 59 | } |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | NDN_LOG_DEBUG("Using PSync"); |
| 63 | m_psyncLogic = std::make_shared<psync::FullProducer>(80, |
| 64 | face, |
| 65 | syncPrefix, |
| 66 | userPrefix, |
| 67 | std::bind(&SyncProtocolAdapter::onPSyncUpdate, this, _1), |
| 68 | syncInterestLifetime); |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix) |
| 73 | { |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 74 | #ifdef HAVE_CHRONOSYNC |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 75 | if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) { |
| 76 | m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION); |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 77 | return; |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 78 | } |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | m_psyncLogic->addUserNode(userPrefix); |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq) |
| 86 | { |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 87 | #ifdef HAVE_CHRONOSYNC |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 88 | if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) { |
| 89 | m_chronoSyncLogic->updateSeqNo(seq, userPrefix); |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 90 | return; |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 91 | } |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 92 | #endif |
| 93 | m_psyncLogic->publishName(userPrefix, seq); |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 96 | #ifdef HAVE_CHRONOSYNC |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 97 | void |
| 98 | SyncProtocolAdapter::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates) |
| 99 | { |
| 100 | NLSR_LOG_TRACE("Received ChronoSync update event"); |
| 101 | |
| 102 | for (const auto& update : updates) { |
| 103 | // Remove FIXED_SESSION |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 104 | m_syncUpdateCallback(update.session.getPrefix(-1), update.high, 0); |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 107 | #endif |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 108 | |
| 109 | void |
| 110 | SyncProtocolAdapter::onPSyncUpdate(const std::vector<psync::MissingDataInfo>& updates) |
| 111 | { |
| 112 | NLSR_LOG_TRACE("Received PSync update event"); |
| 113 | |
| 114 | for (const auto& update : updates) { |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 115 | m_syncUpdateCallback(update.prefix, update.highSeq, update.incomingFace); |
Ashlesh Gawande | 32ec3fd | 2018-07-18 13:42:32 -0500 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 119 | } // namespace nlsr |