Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide torrent creation feature via WebAPI #20366

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ add_library(qbt_base STATIC
bittorrent/torrent.h
bittorrent/torrentcontenthandler.h
bittorrent/torrentcontentlayout.h
bittorrent/torrentcreationmanager.h
bittorrent/torrentcreationtask.h
bittorrent/torrentcreator.h
bittorrent/torrentdescriptor.h
bittorrent/torrentimpl.h
Expand Down Expand Up @@ -137,6 +139,8 @@ add_library(qbt_base STATIC
bittorrent/speedmonitor.cpp
bittorrent/torrent.cpp
bittorrent/torrentcontenthandler.cpp
bittorrent/torrentcreationmanager.cpp
bittorrent/torrentcreationtask.cpp
bittorrent/torrentcreator.cpp
bittorrent/torrentdescriptor.cpp
bittorrent/torrentimpl.cpp
Expand Down
134 changes: 134 additions & 0 deletions src/base/bittorrent/torrentcreationmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2024 Radu Carpa <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#include "torrentcreationmanager.h"

#include <utility>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/indexed_by.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
glassez marked this conversation as resolved.
Show resolved Hide resolved

#include <QUuid>

#define SETTINGS_KEY(name) u"TorrentCreator/Manager/" name

namespace BitTorrent
{
using namespace boost::multi_index;

class TorrentCreationManager::TaskSet final : public boost::multi_index_container<
std::shared_ptr<TorrentCreationTask>,
indexed_by<
ordered_unique<tag<struct ByID>, const_mem_fun<TorrentCreationTask, QString, &TorrentCreationTask::id>>,
ordered_non_unique<tag<struct ByCompletion>, composite_key<
TorrentCreationTask,
const_mem_fun<TorrentCreationTask, bool, &TorrentCreationTask::isFinished>,
const_mem_fun<TorrentCreationTask, QDateTime, &TorrentCreationTask::timeAdded>>>>>
{
};
}

BitTorrent::TorrentCreationManager::TorrentCreationManager(IApplication *app, QObject *parent)
: ApplicationComponent(app, parent)
, m_maxTasks {SETTINGS_KEY(u"MaxTasks"_s), 256}
, m_numThreads {SETTINGS_KEY(u"NumThreads"_s), 1}
, m_tasks {std::make_unique<TaskSet>()}
{
if (m_numThreads > 0)
m_threadPool.setMaxThreadCount(m_numThreads);
}

BitTorrent::TorrentCreationManager::~TorrentCreationManager() = default;

std::shared_ptr<BitTorrent::TorrentCreationTask> BitTorrent::TorrentCreationManager::createTask(const TorrentCreatorParams &params, bool startSeeding)
{
if (std::cmp_greater_equal(m_tasks->size(), m_maxTasks.get()))
{
// Try to delete old finished tasks to stay under target
auto &tasksByCompletion = m_tasks->get<ByCompletion>();
auto [iter, endIter] = tasksByCompletion.equal_range(std::make_tuple(true));
while ((iter != endIter) && std::cmp_greater_equal(m_tasks->size(), m_maxTasks.get()))
{
iter = tasksByCompletion.erase(iter);
}
}
if (std::cmp_greater_equal(m_tasks->size(), m_maxTasks.get()))
return {};

const QString taskID = generateTaskID();

auto *torrentCreator = new TorrentCreator(params, this);
auto creationTask = std::make_shared<TorrentCreationTask>(app(), taskID, torrentCreator, startSeeding);
connect(creationTask.get(), &QObject::destroyed, torrentCreator, &BitTorrent::TorrentCreator::requestInterruption);

m_tasks->get<ByID>().insert(creationTask);
m_threadPool.start(torrentCreator);

return creationTask;
}

QString BitTorrent::TorrentCreationManager::generateTaskID() const
{
const auto &tasksByID = m_tasks->get<ByID>();
QString taskID = QUuid::createUuid().toString(QUuid::WithoutBraces);
while (tasksByID.find(taskID) != tasksByID.end())
taskID = QUuid::createUuid().toString(QUuid::WithoutBraces);

return taskID;
}

std::shared_ptr<BitTorrent::TorrentCreationTask> BitTorrent::TorrentCreationManager::getTask(const QString &id) const
{
const auto &tasksByID = m_tasks->get<ByID>();
const auto iter = tasksByID.find(id);
if (iter == tasksByID.end())
return nullptr;

return *iter;
}

QList<std::shared_ptr<BitTorrent::TorrentCreationTask>> BitTorrent::TorrentCreationManager::tasks() const
{
const auto &tasksByCompletion = m_tasks->get<ByCompletion>();
return {tasksByCompletion.cbegin(), tasksByCompletion.cend()};
}

bool BitTorrent::TorrentCreationManager::deleteTask(const QString &id)
{
auto &tasksByID = m_tasks->get<ByID>();
const auto iter = tasksByID.find(id);
if (iter == tasksByID.end())
return false;

tasksByID.erase(iter);
return true;
}
70 changes: 70 additions & 0 deletions src/base/bittorrent/torrentcreationmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2024 Radu Carpa <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#pragma once

#include <memory>

#include <QtContainerFwd>
#include <QObject>
#include <QThreadPool>

#include "base/applicationcomponent.h"
#include "base/settingvalue.h"
#include "torrentcreationtask.h"
#include "torrentcreator.h"

namespace BitTorrent
{
class TorrentCreationManager final : public ApplicationComponent<QObject>
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(TorrentCreationManager)

public:
explicit TorrentCreationManager(IApplication *app, QObject *parent = nullptr);
~TorrentCreationManager() override;

std::shared_ptr<TorrentCreationTask> createTask(const TorrentCreatorParams &params, bool startSeeding = true);
std::shared_ptr<TorrentCreationTask> getTask(const QString &id) const;
QList<std::shared_ptr<TorrentCreationTask>> tasks() const;
bool deleteTask(const QString &id);

private:
QString generateTaskID() const;

CachedSettingValue<qint32> m_maxTasks;
CachedSettingValue<qint32> m_numThreads;

class TaskSet;
std::unique_ptr<TaskSet> m_tasks;

QThreadPool m_threadPool;
};
}
149 changes: 149 additions & 0 deletions src/base/bittorrent/torrentcreationtask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2024 Radu Carpa <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#include "torrentcreationtask.h"

#include "base/addtorrentmanager.h"
#include "base/interfaces/iapplication.h"
#include "base/bittorrent/addtorrentparams.h"

BitTorrent::TorrentCreationTask::TorrentCreationTask(IApplication *app, const QString &id
, TorrentCreator *torrentCreator, bool startSeeding, QObject *parent)
: ApplicationComponent(app, parent)
, m_id {id}
, m_params {torrentCreator->params()}
, m_timeAdded {QDateTime::currentDateTime()}
{
Q_ASSERT(torrentCreator);

connect(torrentCreator, &BitTorrent::TorrentCreator::started, this, [this]
{
m_timeStarted = QDateTime::currentDateTime();
});

connect(torrentCreator, &BitTorrent::TorrentCreator::progressUpdated, this
, [this](const int progress)
{
m_progress = progress;
});

connect(torrentCreator, &BitTorrent::TorrentCreator::creationSuccess, this
, [this, app, startSeeding](const TorrentCreatorResult &result)
{
m_timeFinished = QDateTime::currentDateTime();
m_result = result;

if (!startSeeding)
return;

BitTorrent::AddTorrentParams params;
params.savePath = result.savePath;
params.skipChecking = true;
params.useAutoTMM = false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path

if (!app->addTorrentManager()->addTorrent(result.torrentFilePath.data(), params))
m_errorMsg = tr("Failed to start seeding.");
});

connect(torrentCreator, &BitTorrent::TorrentCreator::creationFailure, this
, [this](const QString &errorMsg)
{
m_timeFinished = QDateTime::currentDateTime();
m_errorMsg = errorMsg;
});
}

QString BitTorrent::TorrentCreationTask::id() const
{
return m_id;
}

const BitTorrent::TorrentCreatorParams &BitTorrent::TorrentCreationTask::params() const
{
return m_params;
}

BitTorrent::TorrentCreationTask::State BitTorrent::TorrentCreationTask::state() const
{
if (m_timeStarted.isNull())
return Queued;
if (m_timeFinished.isNull())
return Running;
return Finished;
}

bool BitTorrent::TorrentCreationTask::isQueued() const
{
return (state() == Queued);
}

bool BitTorrent::TorrentCreationTask::isRunning() const
{
return (state() == Running);
}

bool BitTorrent::TorrentCreationTask::isFinished() const
{
return (state() == Finished);
}

bool BitTorrent::TorrentCreationTask::isFailed() const
{
return !m_errorMsg.isEmpty();
}

int BitTorrent::TorrentCreationTask::progress() const
{
return m_progress;
}

const BitTorrent::TorrentCreatorResult &BitTorrent::TorrentCreationTask::result() const
{
return m_result;
}

QString BitTorrent::TorrentCreationTask::errorMsg() const
{
return m_errorMsg;
}

QDateTime BitTorrent::TorrentCreationTask::timeAdded() const
{
return m_timeAdded;
}

QDateTime BitTorrent::TorrentCreationTask::timeStarted() const
{
return m_timeStarted;
}

QDateTime BitTorrent::TorrentCreationTask::timeFinished() const
{
return m_timeFinished;
}
Loading
Loading