152 lines
3.6 KiB
C++
152 lines
3.6 KiB
C++
|
|
#include "torrentsession.h"
|
||
|
|
#include "mainscreen.h"
|
||
|
|
|
||
|
|
#include <sstream>
|
||
|
|
|
||
|
|
TorrentSession::TorrentSession(SearchItem it, std::filesystem::path download_path, QObject *parent)
|
||
|
|
{
|
||
|
|
this->item = new SearchItem(it);
|
||
|
|
this->download_path = download_path;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
int TorrentSession::start()
|
||
|
|
{
|
||
|
|
auto pid = fork();
|
||
|
|
if(pid != 0) {
|
||
|
|
qDebug() << "Process id: " << getpid() << "forked.";
|
||
|
|
return pid;
|
||
|
|
}
|
||
|
|
qDebug() << "Process id: " << getpid() <<
|
||
|
|
"created for torrent session with download path: " << download_path.string();
|
||
|
|
|
||
|
|
manager = new SocketManager();
|
||
|
|
socket_manager_thread = new QThread(this);
|
||
|
|
manager->moveToThread(socket_manager_thread);
|
||
|
|
socket_manager_thread->start();
|
||
|
|
connect(this, &TorrentSession::startSocketManager, manager, &SocketManager::start);
|
||
|
|
connect(this, &TorrentSession::sendStatusSignalToSocketManager, manager, &SocketManager::statusSignalFromTorrentSession);
|
||
|
|
connect(manager, &SocketManager::status, this, &TorrentSession::sendStatus);
|
||
|
|
connect(manager, &SocketManager::pause, this, &TorrentSession::pause);
|
||
|
|
connect(manager, &SocketManager::resume, this, &TorrentSession::resume);
|
||
|
|
connect(manager, &SocketManager::remove, this, &TorrentSession::remove);
|
||
|
|
|
||
|
|
emit startSocketManager();
|
||
|
|
connect(&start_timer, &QTimer::timeout, this, &TorrentSession::launchSession);
|
||
|
|
start_timer.setInterval(300);
|
||
|
|
start_timer.setSingleShot(true);
|
||
|
|
start_timer.start();
|
||
|
|
return pid;
|
||
|
|
}
|
||
|
|
|
||
|
|
void TorrentSession::sendStatus()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void TorrentSession::pause()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void TorrentSession::resume()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void TorrentSession::remove()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//libtorrent stuff starts here.
|
||
|
|
void TorrentSession::launchSession()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
SocketManager::SocketManager(QObject *parent)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void SocketManager::start()
|
||
|
|
{
|
||
|
|
server = new QLocalServer(this);
|
||
|
|
connect(server, &QLocalServer::newConnection, this, &SocketManager::addConnection);
|
||
|
|
auto unique_path = testDir("/tmp/nekodl", ".sock");
|
||
|
|
bool f = server->listen(QString::fromStdString(unique_path.string()));
|
||
|
|
if(!f) {
|
||
|
|
qDebug() << "Failed to start server";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SocketManager::statusSignalFromTorrentSession(QByteArray data)
|
||
|
|
{
|
||
|
|
if(socket && socket->isOpen() && socket->isValid()) {
|
||
|
|
socket->write(data);
|
||
|
|
socket->flush();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SocketManager::addConnection()
|
||
|
|
{
|
||
|
|
if(server->hasPendingConnections()) {
|
||
|
|
if(socket) {
|
||
|
|
socket->deleteLater();
|
||
|
|
}
|
||
|
|
socket = server->nextPendingConnection();
|
||
|
|
connect(socket, &QLocalSocket::readyRead, this, &SocketManager::commandFromClient);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SocketManager::commandFromClient()
|
||
|
|
{
|
||
|
|
if(socket) {
|
||
|
|
QByteArray big_data;
|
||
|
|
while(socket->bytesAvailable()) {
|
||
|
|
auto data = socket->readAll();
|
||
|
|
big_data += data;
|
||
|
|
}
|
||
|
|
QString data(big_data);
|
||
|
|
sendSignalToTorrentSession(data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SocketManager::sendSignalToTorrentSession(QString command)
|
||
|
|
{
|
||
|
|
if(command == "status") {
|
||
|
|
emit status();
|
||
|
|
}
|
||
|
|
if(command == "pause") {
|
||
|
|
emit pause();
|
||
|
|
}
|
||
|
|
if(command == "resume") {
|
||
|
|
emit resume();
|
||
|
|
}
|
||
|
|
if(command == "remove") {
|
||
|
|
emit remove();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string SocketManager::numToStr(int num)
|
||
|
|
{
|
||
|
|
std::stringstream ss;
|
||
|
|
ss << num;
|
||
|
|
return ss.str();
|
||
|
|
}
|
||
|
|
|
||
|
|
std::filesystem::path SocketManager::testDir(std::string file_name, std::string file_format)
|
||
|
|
{
|
||
|
|
int i = 1;
|
||
|
|
while(true) {
|
||
|
|
std::filesystem::path final_path = std::filesystem::path(file_name + "_" + numToStr(i++) + file_format);
|
||
|
|
qDebug() << "Checking path: " << final_path.string();
|
||
|
|
if(!std::filesystem::exists(final_path)) {
|
||
|
|
return final_path;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|