#include "torrentclientinterface.h" /** * Create a new torrent client interface instance * * @param client */ TorrentClientInterface::TorrentClientInterface(TorrentClient *client) : QObject(), m_metaObject(staticMetaObject), m_metaEnum(m_metaObject.enumerator(m_metaObject.indexOfEnumerator("RequestType"))), m_torrentClient(client) { } /** * Parse and execute the given request and sending any provided responses * * @param data */ void TorrentClientInterface::exec(QByteArray data) { QJsonDocument json = QJsonDocument::fromJson(data); if (!json.isObject()) { qDebug() << "Unable to parse request"; return; } QJsonObject requestPacket = json.object(); if (requestPacket.count() != 2 || !requestPacket.contains("type") || !requestPacket.contains("data")) { qDebug() << "Invalid request received"; return; } if (!requestPacket["type"].isString()) { qDebug() << "Malformed request received"; return; } // Perform the request QString requestType = requestPacket["type"].toString(); QJsonValueRef body = requestPacket["data"]; QJsonValue *responseData = request(requestType, body); if (responseData == nullptr) { return; } // Construct and write the response QJsonObject response; response["type"] = requestType; response["data"] = *responseData; writeBytes(QJsonDocument(response).toJson(QJsonDocument::Compact)); delete responseData; } // Request Handling -------------------------------------------------------------------------------- /** * Match and perform the provided request * * @param type * @param body * @return */ QJsonValue* TorrentClientInterface::request(const QString type, const QJsonValueRef &body) { return request((RequestType)m_metaEnum.keyToValue(("request_" + type).toLatin1()), body); } /** * Match and perform the provided request * * @param type * @param body * @return */ 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 * * @param body * @return */ QJsonValue* TorrentClientInterface::requestList(const QJsonValueRef &body) { return nullptr; }