Line data Source code
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 : */
17 : #include "jamidht/sync_channel_handler.h"
18 : #include <opendht/thread_pool.h>
19 :
20 : static constexpr const char SYNC_SCHEME[] {"sync://"};
21 :
22 : namespace jami {
23 :
24 672 : SyncChannelHandler::SyncChannelHandler(const std::shared_ptr<JamiAccount>& acc, dhtnet::ConnectionManager& cm)
25 : : ChannelHandlerInterface()
26 672 : , account_(acc)
27 672 : , connectionManager_(cm)
28 672 : {}
29 :
30 1344 : SyncChannelHandler::~SyncChannelHandler() {}
31 :
32 : void
33 68 : SyncChannelHandler::connect(const DeviceId& deviceId,
34 : const std::string&,
35 : ConnectCb&& cb,
36 : const std::string& /*connectionType*/,
37 : bool /*forceNewConnection*/)
38 : {
39 68 : auto channelName = SYNC_SCHEME + deviceId.toString();
40 68 : connectionManager_.connectDevice(deviceId,
41 : channelName,
42 68 : std::move(cb),
43 136 : dhtnet::ConnectDeviceOptions {.forceNewSocket = false, .uniqueName = true});
44 68 : }
45 :
46 : bool
47 68 : SyncChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& /* name */)
48 : {
49 68 : auto acc = account_.lock();
50 68 : if (!cert || !cert->issuer || !acc)
51 0 : return false;
52 68 : return cert->issuer->getId().toString() == acc->getUsername();
53 68 : }
54 :
55 : void
56 135 : SyncChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>& cert,
57 : const std::string&,
58 : std::shared_ptr<dhtnet::ChannelSocket> channel)
59 : {
60 135 : auto acc = account_.lock();
61 135 : if (!cert || !cert->issuer || !acc)
62 0 : return;
63 135 : auto deviceId = channel->deviceId();
64 135 : if (auto sm = acc->syncModule())
65 135 : sm->cacheSyncConnection(std::move(channel), cert->issuer->getId().toString(), cert->getLongId());
66 135 : dht::ThreadPool::io().run([account = account_, deviceId = deviceId.toString()]() {
67 135 : if (auto acc = account.lock())
68 405 : acc->sendProfile("", acc->getUsername(), deviceId);
69 135 : });
70 135 : }
71 :
72 : } // namespace jami
|