blob: 2e09d2bf78259669677bf10a133cca90b62446cf [file] [log] [blame]
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande30d96e42021-03-21 19:15:33 -07002/*
Alexander Afanasyev135288c2022-04-23 23:06:56 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -05004 * 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 Gawande30d96e42021-03-21 19:15:33 -070020 */
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050021
22#include "sync-protocol-adapter.hpp"
23#include "logger.hpp"
24
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050025namespace nlsr {
26
Davide Pesavento1954a0c2022-09-30 15:56:04 -040027INIT_LOGGER(SyncProtocolAdapter);
28
29#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050030const auto FIXED_SESSION = ndn::name::Component::fromNumber(0);
Davide Pesavento1954a0c2022-09-30 15:56:04 -040031#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050032
33SyncProtocolAdapter::SyncProtocolAdapter(ndn::Face& face,
Davide Pesavento1954a0c2022-09-30 15:56:04 -040034 ndn::KeyChain& keyChain,
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070035 SyncProtocol syncProtocol,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050036 const ndn::Name& syncPrefix,
37 const ndn::Name& userPrefix,
38 ndn::time::milliseconds syncInterestLifetime,
Davide Pesavento1954a0c2022-09-30 15:56:04 -040039 SyncUpdateCallback syncUpdateCallback)
40 : m_syncProtocol(syncProtocol)
41 , m_syncUpdateCallback(std::move(syncUpdateCallback))
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050042{
Davide Pesavento1954a0c2022-09-30 15:56:04 -040043 switch (m_syncProtocol) {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070044#ifdef HAVE_CHRONOSYNC
Davide Pesavento1954a0c2022-09-30 15:56:04 -040045 case SyncProtocol::CHRONOSYNC:
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050046 NDN_LOG_DEBUG("Using ChronoSync");
47 m_chronoSyncLogic = std::make_shared<chronosync::Logic>(face,
48 syncPrefix,
49 userPrefix,
Davide Pesavento1954a0c2022-09-30 15:56:04 -040050 [this] (auto&&... args) { onChronoSyncUpdate(std::forward<decltype(args)>(args)...); },
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050051 chronosync::Logic::DEFAULT_NAME,
52 chronosync::Logic::DEFAULT_VALIDATOR,
53 chronosync::Logic::DEFAULT_RESET_TIMER,
54 chronosync::Logic::DEFAULT_CANCEL_RESET_TIMER,
55 chronosync::Logic::DEFAULT_RESET_INTEREST_LIFETIME,
56 syncInterestLifetime,
57 chronosync::Logic::DEFAULT_SYNC_REPLY_FRESHNESS,
58 chronosync::Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME,
59 FIXED_SESSION);
Davide Pesavento1954a0c2022-09-30 15:56:04 -040060 break;
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070061#endif
Davide Pesavento1954a0c2022-09-30 15:56:04 -040062 case SyncProtocol::PSYNC:
63 NDN_LOG_DEBUG("Using PSync");
64 m_psyncLogic = std::make_shared<psync::FullProducer>(face,
65 keyChain,
66 80,
67 syncPrefix,
68 userPrefix,
69 [this] (auto&&... args) { onPSyncUpdate(std::forward<decltype(args)>(args)...); },
70 syncInterestLifetime);
71 break;
72 default:
73 NDN_CXX_UNREACHABLE;
74 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050075}
76
77void
78SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix)
79{
Davide Pesavento1954a0c2022-09-30 15:56:04 -040080 switch (m_syncProtocol) {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070081#ifdef HAVE_CHRONOSYNC
Davide Pesavento1954a0c2022-09-30 15:56:04 -040082 case SyncProtocol::CHRONOSYNC:
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050083 m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION);
Davide Pesavento1954a0c2022-09-30 15:56:04 -040084 break;
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070085#endif
Davide Pesavento1954a0c2022-09-30 15:56:04 -040086 case SyncProtocol::PSYNC:
87 m_psyncLogic->addUserNode(userPrefix);
88 break;
89 default:
90 NDN_CXX_UNREACHABLE;
91 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050092}
93
94void
95SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq)
96{
Davide Pesavento1954a0c2022-09-30 15:56:04 -040097 switch (m_syncProtocol) {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070098#ifdef HAVE_CHRONOSYNC
Davide Pesavento1954a0c2022-09-30 15:56:04 -040099 case SyncProtocol::CHRONOSYNC:
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500100 m_chronoSyncLogic->updateSeqNo(seq, userPrefix);
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400101 break;
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700102#endif
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400103 case SyncProtocol::PSYNC:
104 m_psyncLogic->publishName(userPrefix, seq);
105 break;
106 default:
107 NDN_CXX_UNREACHABLE;
108 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500109}
110
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700111#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500112void
113SyncProtocolAdapter::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates)
114{
115 NLSR_LOG_TRACE("Received ChronoSync update event");
116
117 for (const auto& update : updates) {
118 // Remove FIXED_SESSION
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400119 m_syncUpdateCallback(update.session.getPrefix(-1), update.high, 0);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500120 }
121}
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700122#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500123
124void
125SyncProtocolAdapter::onPSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
126{
127 NLSR_LOG_TRACE("Received PSync update event");
128
129 for (const auto& update : updates) {
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400130 m_syncUpdateCallback(update.prefix, update.highSeq, update.incomingFace);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500131 }
132}
133
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400134} // namespace nlsr