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