|
|
@ -7,9 +7,9 @@ |
|
|
|
*/ |
|
|
|
TorrentClientInterface::TorrentClientInterface(TorrentClient *client) |
|
|
|
: QObject(), |
|
|
|
m_torrentClient(client), |
|
|
|
m_metaObject(staticMetaObject), |
|
|
|
m_metaEnum(m_metaObject.enumerator(m_metaObject.indexOfEnumerator("RequestType"))) |
|
|
|
m_metaEnum(m_metaObject.enumerator(m_metaObject.indexOfEnumerator("RequestType"))), |
|
|
|
m_torrentClient(client) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
@ -31,14 +31,15 @@ void TorrentClientInterface::exec(QByteArray data) |
|
|
|
qDebug() << "Invalid request received"; |
|
|
|
return; |
|
|
|
} |
|
|
|
if (!requestPacket["type"].isString() || !requestPacket["data"].isObject()) { |
|
|
|
if (!requestPacket["type"].isString()) { |
|
|
|
qDebug() << "Malformed request received"; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Perform the request
|
|
|
|
QString requestType = requestPacket["type"].toString(); |
|
|
|
QJsonValueRef body = requestPacket["body"]; |
|
|
|
QJsonValueRef body = requestPacket["data"]; |
|
|
|
QJsonValue *responseData = request(requestType, body); |
|
|
|
if (responseData == nullptr) { |
|
|
|
return; |
|
|
@ -52,7 +53,7 @@ void TorrentClientInterface::exec(QByteArray data) |
|
|
|
delete responseData; |
|
|
|
} |
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
// Request Handling --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match and perform the provided request |
|
|
@ -75,14 +76,99 @@ QJsonValue* TorrentClientInterface::request(const QString type, const QJsonValue |
|
|
|
*/ |
|
|
|
QJsonValue* TorrentClientInterface::request(const RequestType type, const QJsonValueRef &body) |
|
|
|
{ |
|
|
|
qDebug() << "Requesting:" << type << body; |
|
|
|
switch(type) { |
|
|
|
case request_add : return requestAdd(body); |
|
|
|
case request_list: return requestList(body); |
|
|
|
default: |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------
|
|
|
|
// Response Generation -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an error response |
|
|
|
* |
|
|
|
* @param message |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
QJsonValue* TorrentClientInterface::createErrorResponse(const int code, const char* message) |
|
|
|
{ |
|
|
|
return createErrorResponse(code, QString::fromStdString(message)); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an error response |
|
|
|
* |
|
|
|
* @param message |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
QJsonValue* TorrentClientInterface::createErrorResponse(const int code, std::string message) |
|
|
|
{ |
|
|
|
return createErrorResponse(code, QString::fromStdString(message)); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an error response |
|
|
|
* |
|
|
|
* @param message |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
QJsonValue* TorrentClientInterface::createErrorResponse(const int code, const QString message) |
|
|
|
{ |
|
|
|
QJsonObject response; |
|
|
|
response["status"] = "error"; |
|
|
|
response["message"] = message; |
|
|
|
response["error_code"] = code; |
|
|
|
return new QJsonValue(response); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a successful response |
|
|
|
* |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
QJsonValue* TorrentClientInterface::createResponse(QJsonValue data) |
|
|
|
{ |
|
|
|
QJsonObject response; |
|
|
|
response["status"] = "success"; |
|
|
|
response["data"] = data; |
|
|
|
return new QJsonValue(response); |
|
|
|
} |
|
|
|
|
|
|
|
// Request Implementations -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a torrent to the client |
|
|
|
* |
|
|
|
* @param body |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
QJsonValue* TorrentClientInterface::requestAdd(const QJsonValueRef &body) |
|
|
|
{ |
|
|
|
qDebug() << "Adding torrent"; |
|
|
|
|
|
|
|
if (!body.isString()) { |
|
|
|
return createErrorResponse(-1, "Value is not a string"); |
|
|
|
} |
|
|
|
|
|
|
|
// Add the torrent
|
|
|
|
QString magnetLink = body.toString(); |
|
|
|
lt::sha1_hash infoHash; |
|
|
|
lt::error_code error; |
|
|
|
m_torrentClient->addTorrent(magnetLink, infoHash, error); |
|
|
|
|
|
|
|
// Check for an error
|
|
|
|
if (error.value() != lt::errors::no_error) { |
|
|
|
return createErrorResponse(error.value(), error.message()); |
|
|
|
} |
|
|
|
|
|
|
|
QJsonObject response; |
|
|
|
response["info_hash"] = QString::fromStdString(infoHash.to_string()); |
|
|
|
return createResponse(QJsonValue(response)); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* Request the list of torrents from the client |
|
|
|