Line data Source code
1 : /* 2 : * Copyright (C) 2004-2025 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 575 : SyncChannelHandler::SyncChannelHandler(const std::shared_ptr<JamiAccount>& acc, 25 575 : dhtnet::ConnectionManager& cm) 26 : : ChannelHandlerInterface() 27 575 : , account_(acc) 28 575 : , connectionManager_(cm) 29 575 : {} 30 : 31 1150 : SyncChannelHandler::~SyncChannelHandler() {} 32 : 33 : void 34 107 : SyncChannelHandler::connect(const DeviceId& deviceId, 35 : const std::string&, 36 : ConnectCb&& cb, 37 : const std::string& connectionType, 38 : bool forceNewConnection) 39 : { 40 107 : auto channelName = SYNC_SCHEME + deviceId.toString(); 41 107 : if (connectionManager_.isConnecting(deviceId, channelName)) { 42 150 : JAMI_LOG("Already connecting to {}", deviceId); 43 50 : return; 44 : } 45 57 : connectionManager_.connectDevice(deviceId, channelName, std::move(cb)); 46 107 : } 47 : 48 : bool 49 53 : SyncChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, 50 : const std::string& /* name */) 51 : { 52 53 : auto acc = account_.lock(); 53 53 : if (!cert || !cert->issuer || !acc) 54 0 : return false; 55 53 : return cert->issuer->getId().toString() == acc->getUsername(); 56 53 : } 57 : 58 : void 59 106 : SyncChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>& cert, 60 : const std::string&, 61 : std::shared_ptr<dhtnet::ChannelSocket> channel) 62 : { 63 106 : auto acc = account_.lock(); 64 106 : if (!cert || !cert->issuer || !acc) 65 0 : return; 66 106 : if (auto sm = acc->syncModule()) 67 106 : sm->cacheSyncConnection(std::move(channel), 68 212 : cert->issuer->getId().toString(), 69 106 : cert->getLongId()); 70 106 : dht::ThreadPool::io().run([account = account_, channel]() { 71 106 : if (auto acc = account.lock()) 72 106 : acc->sendProfile("", acc->getUsername(), channel->deviceId().toString()); 73 106 : }); 74 106 : } 75 : 76 : } // namespace jami