feather/src/utils/WebsocketNotifier.cpp

119 lines
3.4 KiB
C++
Raw Normal View History

2021-05-02 18:22:38 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2023-01-02 19:30:11 +00:00
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
2021-05-02 18:22:38 +00:00
#include "WebsocketNotifier.h"
2021-07-06 19:36:27 +00:00
#include "Utils.h"
2021-05-25 13:06:38 +00:00
#include "utils/os/tails.h"
#include "utils/os/whonix.h"
2023-11-30 14:01:39 +00:00
#include "plugins/PluginRegistry.h"
2021-05-02 18:22:38 +00:00
#include <QJsonObject>
WebsocketNotifier::WebsocketNotifier(QObject *parent)
: QObject(parent)
, websocketClient(new WebsocketClient(this))
{
2023-03-01 12:28:01 +00:00
connect(websocketClient, &WebsocketClient::WSMessage, this, &WebsocketNotifier::onWSMessage);
2023-11-30 14:01:39 +00:00
for (const auto& plugin : PluginRegistry::getPlugins()) {
m_pluginSubscriptions << plugin->socketData();
}
2021-05-02 18:22:38 +00:00
}
QPointer<WebsocketNotifier> WebsocketNotifier::m_instance(nullptr);
void WebsocketNotifier::onWSMessage(const QJsonObject &msg) {
QString cmd = msg.value("cmd").toString();
2022-02-23 22:27:20 +00:00
m_lastMessageReceived = QDateTime::currentDateTimeUtc();
2021-05-18 15:59:18 +00:00
m_cache[cmd] = msg;
2021-05-02 18:22:38 +00:00
if (cmd == "blockheights") {
QJsonObject data = msg.value("data").toObject();
int mainnet = data.value("mainnet").toInt();
int stagenet = data.value("stagenet").toInt();
emit BlockHeightsReceived(mainnet, stagenet);
}
else if(cmd == "nodes") {
this->onWSNodes(msg.value("data").toArray());
}
else if(cmd == "crypto_rates") {
QJsonArray crypto_rates = msg.value("data").toArray();
emit CryptoRatesReceived(crypto_rates);
}
else if(cmd == "fiat_rates") {
QJsonObject fiat_rates = msg.value("data").toObject();
emit FiatRatesReceived(fiat_rates);
}
else if(cmd == "txFiatHistory") {
auto txFiatHistory_data = msg.value("data").toObject();
emit TxFiatHistoryReceived(txFiatHistory_data);
}
#if defined(CHECK_UPDATES)
else if (cmd == "updates") {
this->onWSUpdates(msg.value("data").toObject());
}
#endif
2023-11-30 14:01:39 +00:00
else if (m_pluginSubscriptions.contains(cmd)) {
emit dataReceived(cmd, msg.value("data"));
2021-05-02 18:22:38 +00:00
}
}
2021-05-18 15:59:18 +00:00
void WebsocketNotifier::emitCache() {
for (const auto &msg : m_cache) {
this->onWSMessage(msg);
}
}
2022-02-23 22:27:20 +00:00
bool WebsocketNotifier::stale(int minutes) {
return m_lastMessageReceived < QDateTime::currentDateTimeUtc().addSecs(-(minutes*60));
}
2021-05-02 18:22:38 +00:00
void WebsocketNotifier::onWSNodes(const QJsonArray &nodes) {
// TODO: Refactor, should be filtered client side
QList<FeatherNode> l;
for (auto &&entry: nodes) {
auto obj = entry.toObject();
auto nettype = obj.value("nettype");
auto type = obj.value("type");
2021-05-18 15:59:18 +00:00
auto networkType = constants::networkType;
2021-05-02 18:22:38 +00:00
// filter remote node network types
if(nettype == "mainnet" && networkType != NetworkType::MAINNET)
continue;
if(nettype == "stagenet" && networkType != NetworkType::STAGENET)
continue;
if(nettype == "testnet" && networkType != NetworkType::TESTNET)
continue;
FeatherNode node{obj.value("address").toString(),
obj.value("height").toInt(),
obj.value("target_height").toInt(),
obj.value("online").toBool()};
l.append(node);
}
emit NodesReceived(l);
}
void WebsocketNotifier::onWSUpdates(const QJsonObject &updates) {
emit UpdatesReceived(updates);
}
WebsocketNotifier* WebsocketNotifier::instance()
{
if (!m_instance) {
m_instance = new WebsocketNotifier(QCoreApplication::instance());
}
return m_instance;
}