Skip to content
Snippets Groups Projects
DapCommandController.cpp 10.48 KiB
#include "DapCommandController.h"

#include <DapNodeType.h>
#include <QDataStream>

/// Overloaded constructor.
/// @param apIODevice Data transfer device.
/// @param apParent Parent.
DapCommandController::DapCommandController(QIODevice *apIODevice, QObject *apParent)
    :  DapRpcService(apParent)
{
    // Socket initialization
    m_DAPRpcSocket = new DapRpcSocket(apIODevice, this);
    // Signal-slot connection initiating the execution of the method called by the service
    connect(m_DAPRpcSocket, SIGNAL(messageReceived(DapRpcMessage)), SLOT(messageProcessing(DapRpcMessage)));

    addService(this);
}

/// Process incoming message.
/// @param asMessage Incoming message.
void DapCommandController::messageProcessing(const DapRpcMessage &asMessage)
{
    DapRpcSocket *socket = static_cast<DapRpcSocket*>(sender());
    if (!socket) {
        qDebug() << "Called without service socket";
        return;
    }

    processMessage(socket, asMessage);
}

/// Process the result of the command execution.
void DapCommandController::processCommandResult()
{
    qInfo() << "processCommandResult()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply) {
        qWarning() << "Invalid response received";
        return;
    }
    emit sigCommandResult(reply->response().toJsonValue());
}

/// Get node logs.
void DapCommandController::getNodeLogs()
{
    qInfo() << QString("getNodeLogs()");
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getNodeLogs");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetNodeLogs()));
}

void DapCommandController::getHistory()
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getHistory");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetHistory()));

}

void DapCommandController::setNewHistory(const QVariant& aData)
{
    emit sendHistory(aData);
}

void DapCommandController::requestConsole(const QString& aQueue)
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getQueryResult", aQueue);
    connect(reply, SIGNAL(finished()), this, SLOT(processResponseConsole()));
}
void DapCommandController::getCmdHistory()
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getCmdHistory");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetCmdHistory()));
}

void DapCommandController::changeCurrentNetwork(const QString& aNetwork)
{
    m_DAPRpcSocket->invokeRemoteMethod("RPCServer.changeCurrentNetwork", aNetwork);
}

#include "DapChainWallet.h"
void DapCommandController::setNewWalletData(const QVariant& aData)
{
    emit sigWalletData(QByteArray::fromHex(aData.toByteArray()));
}

void DapCommandController::requestWalletData()
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.walletData");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetWalletData()));
}

void DapCommandController::processChangedLog()
{
    emit onChangeLogModel();
}

/// Handling service response for receiving node logs.
void DapCommandController::processGetNodeLogs()
{
    qInfo() << "processGetNodeLogs()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply) {
        qWarning() << "Invalid response received";
        return;
    }
    emit sigCommandResult(reply->response().toJsonValue());
    emit sigNodeLogsReceived(reply->response().toJsonValue().toVariant().toStringList());
}

///
void DapCommandController::processAddWallet()
{
    qInfo() << "processAddWallet()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply) {
        qWarning() << "Invalid response received";
        return;
    }
    emit sigCommandResult(reply->response().toJsonValue());
    auto name = reply->response().toJsonValue().toVariant().toStringList().at(0);
    auto address = reply->response().toJsonValue().toVariant().toStringList().at(1);
    emit sigWalletAdded(name, address);
}

void DapCommandController::processSendToken()
{
    qInfo() << "processSendToken()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply) {
        qWarning() << "Invalid response received";
        return;
    }
    qInfo() << reply->response();
    emit sigCommandResult(reply->response().toJsonValue());
    auto answer = reply->response().toJsonValue().toVariant().toString();
    emit onTokenSended(answer);
}
void DapCommandController::processGetWallets()
{
    qInfo() << "processGetWallets()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply) {
        qWarning() << "Invalid response received";
        return;
    }
    emit sigCommandResult(reply->response().toJsonValue());
    emit sigWalletsReceived(reply->response().toJsonValue().toVariant().toMap());
}

void DapCommandController::processGetWalletInfo()
{
    qInfo() << "processGetWalletInfo()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply || reply->response().toJsonValue().toVariant().toStringList().count() <= 0) {
        qWarning() << "Invalid response received";
        return;
    }
    emit sigCommandResult(reply->response().toJsonValue());
    QString name = reply->response().toJsonValue().toVariant().toStringList().at(0);
    QString address = reply->response().toJsonValue().toVariant().toStringList().at(1);
    QStringList temp = reply->response().toJsonValue().toVariant().toStringList();
    QStringList tokens = temp.mid(3, temp.count());
    
    emit sigWalletInfoChanged(name, address, QStringList(), tokens);
}

void DapCommandController::processGetNodeNetwork()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    emit sendNodeNetwork(reply->response().toJsonValue().toVariant());
}

void DapCommandController::processGetNodeStatus()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    emit sendNodeStatus(reply->response().toJsonValue().toVariant());
}

void DapCommandController::processExecuteCommand()
{
    qInfo() << "processGetWalletInfo()";
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    if (!reply || reply->response().toJsonValue().toVariant().toStringList().isEmpty()) {

        QString result = "Invalid response received";
        qWarning() << result;
        emit executeCommandChanged(result);
        return;
    }
    emit sigCommandResult(reply->response().toJsonValue());
    QString result = reply->response().toJsonValue().toVariant().toStringList().at(0);
    emit executeCommandChanged(result);
}

void DapCommandController::processGetHistory()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    QVariant result = reply->response().toJsonValue().toArray().toVariantList();
    emit sendHistory(result);
}

void DapCommandController::processResponseConsole()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    QString result = reply->response().toJsonValue().toVariant().toString();
    emit responseConsole(result);
}

void DapCommandController::processGetCmdHistory()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    QString result = reply->response().toJsonValue().toVariant().toString();
    emit sigCmdHistory(result);
}

void DapCommandController::processGetWalletData()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    QByteArray result = reply->response().toJsonValue().toVariant().toByteArray();
    emit sigWalletData(QByteArray::fromHex(result));
}

void DapCommandController::processGetNetworkList()
{
    DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender());
    QStringList result = reply->response().toJsonValue().toVariant().toStringList();
    emit sendNetworkList(result);
}

/// Show or hide GUI client by clicking on the tray icon.
/// @param aIsActivated Accepts true - when requesting to 
/// display a client, falso - when requesting to hide a client.
void DapCommandController::activateClient(bool aIsActivated)
{
    emit onClientActivate(aIsActivated);
}

/// Shut down client.
void DapCommandController::closeClient()
{
    emit onClientClose();
}

void DapCommandController::addWallet(const QString &asWalletName)
{
     qInfo() << QString("addWallet(%1)").arg(asWalletName);
     DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.addWallet", asWalletName);
     connect(reply, SIGNAL(finished()), this, SLOT(processAddWallet()));
}

void DapCommandController::removeWallet(const QString &asWalletName)
{
    qInfo() << QString("removeWallet(%1)").arg(asWalletName);
    m_DAPRpcSocket->invokeRemoteMethod("RPCServer.removeWallet", asWalletName);
}

void DapCommandController::sendToken(const QString &asSendWallet, const QString &asAddressReceiver, const QString &asToken, const QString &aAmount)
{
    qInfo() << QString("sendToken(%1, %2, %3, %4)").arg(asSendWallet).arg(asAddressReceiver).arg(asToken).arg(aAmount);
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.sendToken", asSendWallet, asAddressReceiver, asToken, aAmount);
    connect(reply, SIGNAL(finished()), this, SLOT(processSendToken()));
}

void DapCommandController::getWallets()
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getWallets");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetWallets()));
}

void DapCommandController::getWalletInfo(const QString& asWalletName)
{
    qInfo() << QString("getWalletInfo(%1)").arg(asWalletName);
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getWalletInfo", asWalletName);
    connect(reply, SIGNAL(finished()), this, SLOT(processGetWalletInfo()));
}

void DapCommandController::getNodeNetwork()
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getNodeNetwork");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetNodeNetwork()));
}

void DapCommandController::getNetworkList()
{
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getNetworkList");
    connect(reply, SIGNAL(finished()), this, SLOT(processGetNetworkList()));
}

void DapCommandController::setNodeStatus(const bool aIsOnline)
{
    m_DAPRpcSocket->invokeRemoteMethod("RPCServer.setNodeStatus", aIsOnline);
}

void DapCommandController::executeCommand(const QString &command)
{
    qInfo() << QString("rpc executeCommand(%1)").arg(command);
    DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.executeCommand", command);
    connect(reply, SIGNAL(finished()), this, SLOT(processExecuteCommand()));
}