Line data Source code
1 : /* 2 : * Copyright (C) 2021-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU General Public License as published by 8 : * the Free Software Foundation; either version 3 of the License, or 9 : * (at your option) any later version. 10 : * 11 : * This program is distributed in the hope that it will be useful, 12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : * GNU General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program; if not, write to the Free Software 18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : */ 20 : 21 : #include "jamidht/sync_channel_handler.h" 22 : 23 : #include <opendht/thread_pool.h> 24 : 25 : static constexpr const char SYNC_URI[] {"sync://"}; 26 : 27 : namespace jami { 28 : 29 461 : SyncChannelHandler::SyncChannelHandler(const std::shared_ptr<JamiAccount>& acc, 30 461 : dhtnet::ConnectionManager& cm) 31 : : ChannelHandlerInterface() 32 461 : , account_(acc) 33 461 : , connectionManager_(cm) 34 461 : {} 35 : 36 922 : SyncChannelHandler::~SyncChannelHandler() {} 37 : 38 : void 39 61 : SyncChannelHandler::connect(const DeviceId& deviceId, const std::string&, ConnectCb&& cb) 40 : { 41 61 : auto channelName = SYNC_URI + deviceId.toString(); 42 61 : if (connectionManager_.isConnecting(deviceId, channelName)) { 43 0 : JAMI_INFO("Already connecting to %s", deviceId.to_c_str()); 44 0 : return; 45 : } 46 122 : connectionManager_.connectDevice(deviceId, 47 : channelName, 48 61 : std::move(cb)); 49 61 : } 50 : 51 : bool 52 56 : SyncChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, 53 : const std::string& /* name */) 54 : { 55 56 : auto acc = account_.lock(); 56 56 : if (!cert || !cert->issuer || !acc) 57 0 : return false; 58 56 : return cert->issuer->getId().toString() == acc->getUsername(); 59 56 : } 60 : 61 : void 62 112 : SyncChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>& cert, 63 : const std::string&, 64 : std::shared_ptr<dhtnet::ChannelSocket> channel) 65 : { 66 112 : auto acc = account_.lock(); 67 112 : if (!cert || !cert->issuer || !acc) 68 0 : return; 69 112 : if (auto sm = acc->syncModule()) 70 112 : sm->cacheSyncConnection(std::move(channel), 71 224 : cert->issuer->getId().toString(), 72 112 : cert->getLongId()); 73 111 : dht::ThreadPool::io().run([account=account_, channel]() { 74 112 : if (auto acc = account.lock()) 75 112 : acc->sendProfile("", acc->getUsername(), channel->deviceId().toString()); 76 112 : }); 77 112 : } 78 : 79 : } // namespace jami