sync-protocol-adapter.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, The University of Memphis,
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/>.
20  */
21 
23 #include "logger.hpp"
24 
25 namespace nlsr {
26 
27 INIT_LOGGER(SyncProtocolAdapter);
28 
29 #ifdef HAVE_CHRONOSYNC
30 const auto FIXED_SESSION = ndn::name::Component::fromNumber(0);
31 #endif
32 
34  ndn::KeyChain& keyChain,
35  SyncProtocol syncProtocol,
36  const ndn::Name& syncPrefix,
37  const ndn::Name& userPrefix,
38  ndn::time::milliseconds syncInterestLifetime,
39  SyncUpdateCallback syncUpdateCallback)
40  : m_syncProtocol(syncProtocol)
41  , m_syncUpdateCallback(std::move(syncUpdateCallback))
42 {
43  switch (m_syncProtocol) {
44 #ifdef HAVE_CHRONOSYNC
46  NDN_LOG_DEBUG("Using ChronoSync");
47  m_chronoSyncLogic = std::make_shared<chronosync::Logic>(face,
48  syncPrefix,
49  userPrefix,
50  [this] (auto&&... args) { onChronoSyncUpdate(std::forward<decltype(args)>(args)...); },
51  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);
60  break;
61 #endif // HAVE_CHRONOSYNC
62 #ifdef HAVE_PSYNC
63  case SyncProtocol::PSYNC: {
64  NDN_LOG_DEBUG("Using PSync");
65  psync::FullProducer::Options opts;
66  opts.onUpdate = [this] (auto&&... args) { onPSyncUpdate(std::forward<decltype(args)>(args)...); };
67  opts.syncInterestLifetime = syncInterestLifetime;
68  m_psyncLogic = std::make_shared<psync::FullProducer>(face, keyChain, syncPrefix, opts);
69  m_psyncLogic->addUserNode(userPrefix);
70  break;
71  }
72 #endif // HAVE_PSYNC
73 #ifdef HAVE_SVS
74  case SyncProtocol::SVS:
75  NDN_LOG_DEBUG("Using SVS");
76  m_svsCore = std::make_shared<ndn::svs::SVSyncCore>(face,
77  syncPrefix,
78  [this] (auto&&... args) { onSvsUpdate(std::forward<decltype(args)>(args)...); });
79  break;
80 #endif // HAVE_SVS
81  default:
82  NDN_CXX_UNREACHABLE;
83  }
84 }
85 
86 void
87 SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix)
88 {
89  switch (m_syncProtocol) {
90 #ifdef HAVE_CHRONOSYNC
92  m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION);
93  break;
94 #endif // HAVE_CHRONOSYNC
95 #ifdef HAVE_PSYNC
97  m_psyncLogic->addUserNode(userPrefix);
98  break;
99 #endif // HAVE_PSYNC
100 #ifdef HAVE_SVS
101  case SyncProtocol::SVS:
102  break;
103 #endif // HAVE_SVS
104  default:
105  NDN_CXX_UNREACHABLE;
106  }
107 }
108 
109 void
110 SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq)
111 {
112  switch (m_syncProtocol) {
113 #ifdef HAVE_CHRONOSYNC
115  m_chronoSyncLogic->updateSeqNo(seq, userPrefix);
116  break;
117 #endif // HAVE_CHRONOSYNC
118 #ifdef HAVE_PSYNC
119  case SyncProtocol::PSYNC:
120  m_psyncLogic->publishName(userPrefix, seq);
121  break;
122 #endif // HAVE_PSYNC
123 #ifdef HAVE_SVS
124  case SyncProtocol::SVS:
125  m_svsCore->updateSeqNo(seq, userPrefix);
126  break;
127 #endif // HAVE_SVS
128  default:
129  NDN_CXX_UNREACHABLE;
130  }
131 }
132 
133 #ifdef HAVE_CHRONOSYNC
134 void
135 SyncProtocolAdapter::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates)
136 {
137  NLSR_LOG_TRACE("Received ChronoSync update event");
138 
139  for (const auto& update : updates) {
140  // Remove FIXED_SESSION
141  m_syncUpdateCallback(update.session.getPrefix(-1), update.high, 0);
142  }
143 }
144 #endif // HAVE_CHRONOSYNC
145 
146 #ifdef HAVE_PSYNC
147 void
148 SyncProtocolAdapter::onPSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
149 {
150  NLSR_LOG_TRACE("Received PSync update event");
151 
152  for (const auto& update : updates) {
153  m_syncUpdateCallback(update.prefix, update.highSeq, update.incomingFace);
154  }
155 }
156 #endif // HAVE_PSYNC
157 
158 #ifdef HAVE_SVS
159 void
160 SyncProtocolAdapter::onSvsUpdate(const std::vector<ndn::svs::MissingDataInfo>& updates)
161 {
162  NLSR_LOG_TRACE("Received SVS update event");
163 
164  for (const auto& update : updates) {
165  m_syncUpdateCallback(update.nodeId, update.high, update.incomingFace);
166  }
167 }
168 #endif // HAVE_SVS
169 
170 } // namespace nlsr
void publishUpdate(const ndn::Name &userPrefix, uint64_t seq)
Publish update to Sync.
SyncProtocolAdapter(ndn::Face &face, ndn::KeyChain &keyChain, SyncProtocol syncProtocol, const ndn::Name &syncPrefix, const ndn::Name &userPrefix, ndn::time::milliseconds syncInterestLifetime, SyncUpdateCallback syncUpdateCallback)
void addUserNode(const ndn::Name &userPrefix)
Add user node to Sync.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::function< void(const ndn::Name &updateName, uint64_t seqNo, uint64_t incomingFaceId)> SyncUpdateCallback