From 2ae8c9b0b2f6853116ace484b16496793ab24697 Mon Sep 17 00:00:00 2001 From: adityapk00 Date: Wed, 16 Jan 2019 13:05:21 -0800 Subject: [PATCH] Refactor websockets --- src/rpc.cpp | 9 ++++--- src/rpc.h | 4 --- src/txtablemodel.cpp | 18 ++++++++++--- src/txtablemodel.h | 9 ++++--- src/websockets.cpp | 64 +++++++++++++++++++++++++++++++++++++++----- src/websockets.h | 39 +++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 20 deletions(-) diff --git a/src/rpc.cpp b/src/rpc.cpp index f3848ff..8905fd2 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -5,6 +5,7 @@ #include "senttxstore.h" #include "turnstile.h" #include "version.h" +#include "websockets.h" using json = nlohmann::json; @@ -712,9 +713,11 @@ void RPC::refreshBalances() { // 1. Get the Balances getBalance([=] (json reply) { - balT = QString::fromStdString(reply["transparent"]).toDouble(); - balZ = QString::fromStdString(reply["private"]).toDouble(); - balTotal = QString::fromStdString(reply["total"]).toDouble(); + auto balT = QString::fromStdString(reply["transparent"]).toDouble(); + auto balZ = QString::fromStdString(reply["private"]).toDouble(); + auto balTotal = QString::fromStdString(reply["total"]).toDouble(); + + AppDataModel::getInstance()->setBalances(balT, balZ); ui->balSheilded ->setText(Settings::getZECDisplayFormat(balZ)); ui->balTransparent->setText(Settings::getZECDisplayFormat(balT)); diff --git a/src/rpc.h b/src/rpc.h index 4a05efe..7f0c18d 100644 --- a/src/rpc.h +++ b/src/rpc.h @@ -71,10 +71,6 @@ public: Turnstile* getTurnstile() { return turnstile; } Connection* getConnection() { return conn; } - double balT; - double balZ; - double balTotal; - private: void refreshBalances(); diff --git a/src/txtablemodel.cpp b/src/txtablemodel.cpp index a294f66..9d44c00 100644 --- a/src/txtablemodel.cpp +++ b/src/txtablemodel.cpp @@ -186,14 +186,26 @@ void TxTableModel::updateAllData() { return QVariant(); } -QString TxTableModel::getTxId(int row) { +QString TxTableModel::getTxId(int row) const { return modeldata->at(row).txid; } -QString TxTableModel::getMemo(int row) { +QString TxTableModel::getMemo(int row) const { return modeldata->at(row).memo; } -QString TxTableModel::getAddr(int row) { +QString TxTableModel::getAddr(int row) const { return modeldata->at(row).address.trimmed(); +} + +qint64 TxTableModel::getDate(int row) const { + return modeldata->at(row).datetime; +} + +QString TxTableModel::getType(int row) const { + return modeldata->at(row).type; +} + +QString TxTableModel::getAmt(int row) const { + return Settings::getDecimalString(modeldata->at(row).amount); } \ No newline at end of file diff --git a/src/txtablemodel.h b/src/txtablemodel.h index a2e3e91..362b621 100644 --- a/src/txtablemodel.h +++ b/src/txtablemodel.h @@ -15,9 +15,12 @@ public: void addZSentData(const QList& data); void addZRecvData(const QList& data); - QString getTxId(int row); - QString getMemo(int row); - QString getAddr(int row); + QString getTxId(int row) const; + QString getMemo(int row) const; + QString getAddr(int row) const; + qint64 getDate(int row) const; + QString getType(int row) const; + QString getAmt (int row) const; bool exportToCsv(QString fileName) const; diff --git a/src/websockets.cpp b/src/websockets.cpp index 2b1dad2..f1e4d04 100644 --- a/src/websockets.cpp +++ b/src/websockets.cpp @@ -43,12 +43,7 @@ void WSServer::processTextMessage(QString message) if (m_debug) qDebug() << "Message received:" << message; if (pClient) { - QJsonDocument json(QJsonObject { - {"saplingAddress", m_mainWindow->getRPC()->getDefaultSaplingAddress()}, - {"balance", m_mainWindow->getRPC()->balTotal}, - {"zecprice", Settings::getInstance()->getZECPrice()} - }); - + auto json = AppDataServer::processMessage(message, m_mainWindow); pClient->sendTextMessage(json.toJson()); } } @@ -72,4 +67,59 @@ void WSServer::socketDisconnected() m_clients.removeAll(pClient); pClient->deleteLater(); } -} \ No newline at end of file +} + +// ============================== +// AppDataServer +// ============================== +QJsonDocument AppDataServer::processMessage(QString message, MainWindow* mainWindow) { + // First, extract the command from the message + auto msg = QJsonDocument::fromJson(message.toUtf8()); + if (!msg.object().contains("command")) { + return QJsonDocument(QJsonObject{ + {"errorCode", -1}, + {"errorMessage", "Unknown JSON format"} + }); + } + + if (msg["command"] == "getInfo") { + return processGetInfo(mainWindow); + } + else if (msg["command"] == "getTransactions") { + return processGetTransactions(mainWindow); + } + else { + return QJsonDocument(QJsonObject{ + {"errorCode", -1}, + {"errorMessage", "Command not found:" + msg["command"].toString()} + }); + } +} + +QJsonDocument AppDataServer::processGetInfo(MainWindow* mainWindow) { + return QJsonDocument(QJsonObject{ + {"version", 1.0}, + {"saplingAddress", mainWindow->getRPC()->getDefaultSaplingAddress()}, + {"balance", AppDataModel::getInstance()->getTotalBalance()}, + {"zecprice", Settings::getInstance()->getZECPrice()} + }); +} + +QJsonDocument AppDataServer::processGetTransactions(MainWindow* mainWindow) { + QJsonArray txns; + auto model = mainWindow->getRPC()->getTransactionsModel(); + for (int i = 0; i < model->rowCount(QModelIndex()); i++) { + txns.append(QJsonObject{ + {"type", model->getType(i)}, + {"datetime", model->getDate(i)}, + {"amount", model->getAmt(i)} + }); + } + + return QJsonDocument(txns); +} + +// ============================== +// AppDataModel +// ============================== +AppDataModel* AppDataModel::instance = NULL; \ No newline at end of file diff --git a/src/websockets.h b/src/websockets.h index 284ac11..cb6d831 100644 --- a/src/websockets.h +++ b/src/websockets.h @@ -31,4 +31,43 @@ private: bool m_debug; }; +class AppDataServer { +public: + static QJsonDocument processMessage(QString message, MainWindow* mainWindow); + static QJsonDocument processGetInfo(MainWindow* mainWindow); + static QJsonDocument processGetTransactions(MainWindow* mainWindow); +}; + +class AppDataModel { +public: + static AppDataModel* getInstance() { + if (instance == NULL) + instance = new AppDataModel(); + + return instance; + } + + double getTBalance() { return balTransparent; } + double getZBalance() { return balShielded; } + double getTotalBalance() { return balTotal; } + + void setBalances(double transparent, double shielded) { + balTransparent = transparent; + balShielded = shielded; + balTotal = balTransparent + balShielded; + } + +private: + AppDataModel() = default; // Private, for singleton + + double balTransparent; + double balShielded; + double balTotal; + double balMaxSingle; + + QString saplingAddress; + + static AppDataModel* instance; +}; + #endif // WEBSOCKETS_H \ No newline at end of file