blob: ae08e3ae053026bd82d61276b0e1d9eecb9a9e1f [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/*
3 * Copyright (c) 2014-2021, 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
25INIT_LOGGER(SyncProtocolAdapter);
26
27namespace nlsr {
28
29const auto FIXED_SESSION = ndn::name::Component::fromNumber(0);
30
31SyncProtocolAdapter::SyncProtocolAdapter(ndn::Face& face,
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070032 SyncProtocol syncProtocol,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050033 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 Gawande30d96e42021-03-21 19:15:33 -070040 NLSR_LOG_TRACE("SyncProtocol value: " << m_syncProtocol);
41
42#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050043 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 Gawande30d96e42021-03-21 19:15:33 -070058 return;
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050059 }
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070060#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 Gawande32ec3fd2018-07-18 13:42:32 -050069}
70
71void
72SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix)
73{
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070074#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050075 if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
76 m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION);
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070077 return;
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050078 }
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070079#endif
80
81 m_psyncLogic->addUserNode(userPrefix);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050082}
83
84void
85SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq)
86{
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070087#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050088 if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
89 m_chronoSyncLogic->updateSeqNo(seq, userPrefix);
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070090 return;
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050091 }
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070092#endif
93 m_psyncLogic->publishName(userPrefix, seq);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050094}
95
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070096#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050097void
98SyncProtocolAdapter::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
104 m_syncUpdateCallback(update.session.getPrefix(-1), update.high);
105 }
106}
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700107#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500108
109void
110SyncProtocolAdapter::onPSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
111{
112 NLSR_LOG_TRACE("Received PSync update event");
113
114 for (const auto& update : updates) {
115 m_syncUpdateCallback(update.prefix, update.highSeq);
116 }
117}
118
119} // namespace nlsr