1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*
SPDX-FileCopyrightText: 2005-2007 Joris Guisson <joris.guisson@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "torrentservice.h"
#include <QRandomGenerator>
#include <interfaces/torrentinterface.h>
#include <peer/peerid.h>
#include <torrent/globals.h>
#include <torrent/server.h>
#include <util/log.h>
#include <util/sha1hash.h>
using namespace bt;
namespace kt
{
TorrentService::TorrentService(TorrentInterface *tc)
: tc(tc)
, srv(nullptr)
, browser(nullptr)
{
}
TorrentService::~TorrentService()
{
stop(nullptr);
}
void TorrentService::onPublished(bool ok)
{
if (ok)
Out(SYS_ZCO | LOG_NOTICE) << "ZC: " << tc->getStats().torrent_name << " was published" << endl;
else
Out(SYS_ZCO | LOG_NOTICE) << "ZC: failed to publish " << tc->getStats().torrent_name << endl;
}
void TorrentService::stop(bt::WaitJob *wjob)
{
Q_UNUSED(wjob)
if (srv) {
srv->stop();
srv->deleteLater();
srv = nullptr;
}
if (browser) {
browser->deleteLater();
browser = nullptr;
}
}
void TorrentService::start()
{
bt::Uint16 port = bt::ServerInterface::getPort();
QString name = QStringLiteral("%1__%2%3")
.arg(tc->getOwnPeerID().toString())
.arg(QRandomGenerator::global()->bounded(26) + 65)
.arg(QRandomGenerator::global()->bounded(26) + 65);
QStringList subtypes;
subtypes << QLatin1Char('_') + tc->getInfoHash().toString() + QStringLiteral("._sub._bittorrent._tcp");
if (!srv) {
srv = new KDNSSD::PublicService();
srv->setPort(port);
srv->setServiceName(name);
srv->setType(QStringLiteral("_bittorrent._tcp"));
srv->setSubTypes(subtypes);
connect(srv, &KDNSSD::PublicService::published, this, &TorrentService::onPublished);
srv->publishAsync();
}
if (!browser) {
browser = new KDNSSD::ServiceBrowser(QLatin1Char('_') + tc->getInfoHash().toString() + QStringLiteral("._sub._bittorrent._tcp"), true);
connect(browser, &KDNSSD::ServiceBrowser::serviceAdded, this, &TorrentService::onServiceAdded);
browser->startBrowse();
}
}
void TorrentService::onServiceAdded(KDNSSD::RemoteService::Ptr ptr)
{
// let us not connect to ourselves
if (!ptr->serviceName().startsWith(tc->getOwnPeerID().toString())) {
QString host = ptr->hostName();
bt::Uint16 port = ptr->port();
Out(SYS_ZCO | LOG_NOTICE) << "ZC: found local peer " << host << ":" << port << endl;
// resolve host name before adding it
// clang-format off
net::AddressResolver::resolve(host, port, this, SLOT(hostResolved(net::AddressResolver*)));
// clang-format on
}
}
void TorrentService::hostResolved(net::AddressResolver *ar)
{
if (ar->succeeded()) {
addPeer(ar->address(), true);
Q_EMIT peersReady(this);
}
}
void TorrentService::aboutToBeDestroyed()
{
Q_EMIT serviceDestroyed(this);
}
}
#include "moc_torrentservice.cpp"
|