Ring Daemon
Loading...
Searching...
No Matches
sync_channel_handler.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
18#include <opendht/thread_pool.h>
19
20static constexpr const char SYNC_SCHEME[] {"sync://"};
21
22namespace jami {
23
24SyncChannelHandler::SyncChannelHandler(const std::shared_ptr<JamiAccount>& acc, dhtnet::ConnectionManager& cm)
26 , account_(acc)
27 , connectionManager_(cm)
28{}
29
31
32void
34 const std::string&,
35 ConnectCb&& cb,
36 const std::string& /*connectionType*/,
37 bool /*forceNewConnection*/)
38{
39 auto channelName = SYNC_SCHEME + deviceId.toString();
40 if (connectionManager_.isConnecting(deviceId, channelName)) {
41 JAMI_LOG("Already connecting to {}", deviceId);
42 return;
43 }
44 connectionManager_.connectDevice(deviceId, channelName, std::move(cb));
45}
46
47bool
48SyncChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& /* name */)
49{
50 auto acc = account_.lock();
51 if (!cert || !cert->issuer || !acc)
52 return false;
53 return cert->issuer->getId().toString() == acc->getUsername();
54}
55
56void
57SyncChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>& cert,
58 const std::string&,
59 std::shared_ptr<dhtnet::ChannelSocket> channel)
60{
61 auto acc = account_.lock();
62 if (!cert || !cert->issuer || !acc)
63 return;
64 auto deviceId = channel->deviceId();
65 if (auto sm = acc->syncModule())
66 sm->cacheSyncConnection(std::move(channel), cert->issuer->getId().toString(), cert->getLongId());
67 dht::ThreadPool::io().run([account = account_, deviceId = deviceId.toString()]() {
68 if (auto acc = account.lock())
69 acc->sendProfile("", acc->getUsername(), deviceId);
70 });
71}
72
73} // namespace jami
A Channel handler is used to make the link between JamiAccount and ConnectionManager Its role is to m...
void onReady(const std::shared_ptr< dht::crypto::Certificate > &peer, const std::string &name, std::shared_ptr< dhtnet::ChannelSocket > channel) override
Launch sync process.
void connect(const DeviceId &deviceId, const std::string &, ConnectCb &&cb, const std::string &connectionType="", bool forceNewConnection=false) override
Ask for a new sync channel.
SyncChannelHandler(const std::shared_ptr< JamiAccount > &acc, dhtnet::ConnectionManager &cm)
bool onRequest(const std::shared_ptr< dht::crypto::Certificate > &peer, const std::string &name) override
Determine if we accept or not the sync request.
#define JAMI_LOG(formatstr,...)
Definition logger.h:237
std::function< void(std::shared_ptr< dhtnet::ChannelSocket >, const DeviceId &)> ConnectCb
dht::PkId DeviceId
void emitSignal(Args... args)
Definition jami_signal.h:64
static constexpr const char SYNC_SCHEME[]