2022-07-04 21:14:13 +00:00
|
|
|
#include "SignalsManager.h"
|
2022-10-14 10:38:41 +00:00
|
|
|
#include "StatusGoEvent.h"
|
2022-07-04 21:14:13 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2022-07-07 18:16:59 +00:00
|
|
|
#include <libstatus.h>
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2022-07-04 21:14:13 +00:00
|
|
|
using namespace std::string_literals;
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
namespace Status::StatusGo
|
|
|
|
{
|
2022-07-04 21:14:13 +00:00
|
|
|
|
|
|
|
std::map<std::string, SignalType> SignalsManager::signalMap;
|
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
EventData::EventData(nlohmann::json eventInfo, bool error)
|
|
|
|
: m_eventInfo(std::move(eventInfo))
|
|
|
|
, m_hasError(error)
|
|
|
|
{ }
|
|
|
|
|
2022-07-04 21:14:13 +00:00
|
|
|
// TODO: make me thread safe or better refactor into broadcasting mechanism
|
|
|
|
SignalsManager* SignalsManager::instance()
|
|
|
|
{
|
|
|
|
static SignalsManager manager;
|
|
|
|
return &manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
SignalsManager::SignalsManager()
|
|
|
|
: QObject(nullptr)
|
|
|
|
{
|
2022-10-14 10:38:41 +00:00
|
|
|
// Don't allow async signal processing in attept to debug the the linux running tests issue
|
|
|
|
m_threadPool.setMaxThreadCount(1);
|
|
|
|
|
2022-07-04 21:14:13 +00:00
|
|
|
SetSignalEventCallback((void*)&SignalsManager::signalCallback);
|
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
signalMap = {
|
|
|
|
{"node.ready"s, SignalType::NodeReady},
|
|
|
|
{"node.started"s, SignalType::NodeStarted},
|
|
|
|
{"node.stopped"s, SignalType::NodeStopped},
|
|
|
|
{"node.login"s, SignalType::NodeLogin},
|
|
|
|
{"node.crashed"s, SignalType::NodeCrashed},
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
{"discovery.started"s, SignalType::DiscoveryStarted},
|
|
|
|
{"discovery.stopped"s, SignalType::DiscoveryStopped},
|
|
|
|
{"discovery.summary"s, SignalType::DiscoverySummary},
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
{"mediaserver.started"s, SignalType::MailserverStarted},
|
|
|
|
{"mailserver.changed"s, SignalType::MailserverChanged},
|
|
|
|
{"mailserver.available"s, SignalType::MailserverAvailable},
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
{"history.request.started"s, SignalType::HistoryRequestStarted},
|
|
|
|
{"history.request.batch.processed"s, SignalType::HistoryRequestBatchProcessed},
|
|
|
|
{"history.request.completed"s, SignalType::HistoryRequestCompleted},
|
|
|
|
|
|
|
|
{"wallet"s, SignalType::WalletEvent},
|
|
|
|
};
|
2022-07-04 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
SignalsManager::~SignalsManager() { }
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
void SignalsManager::processSignal(const char* statusSignalData)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
2022-10-14 10:38:41 +00:00
|
|
|
// TODO: overkill, use some kind of message broker
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
auto dataStrPtr = std::make_shared<std::string>(statusSignalData);
|
|
|
|
m_threadPool.start(QRunnable::create([dataStrPtr, this]() {
|
|
|
|
try
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
2022-10-14 10:38:41 +00:00
|
|
|
StatusGoEvent event = json::parse(*dataStrPtr);
|
|
|
|
if(event.error != std::nullopt)
|
|
|
|
{
|
|
|
|
qWarning() << "Error in signal" << event.type.c_str() << "; error" << event.error.value();
|
|
|
|
// TODO report event error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString signalError;
|
|
|
|
if(event.event.contains("error")) signalError = event.event["error"].get<QString>();
|
|
|
|
|
|
|
|
dispatch(event.type, std::move(event.event), signalError);
|
2022-07-04 21:14:13 +00:00
|
|
|
}
|
2022-10-14 10:38:41 +00:00
|
|
|
catch(const std::exception& e)
|
|
|
|
{
|
|
|
|
qWarning() << "Error decoding signal, err: " << e.what() << "; signal data: " << dataStrPtr->c_str();
|
|
|
|
}
|
|
|
|
}));
|
2022-07-04 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
void SignalsManager::dispatch(const std::string& type, json signalEvent, const QString& signalError)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
|
|
|
SignalType signalType(Unknown);
|
2022-10-14 10:38:41 +00:00
|
|
|
if(!signalMap.contains(type))
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
2022-10-14 10:38:41 +00:00
|
|
|
qWarning() << "Unknown signal received: " << type.c_str();
|
2022-07-04 21:14:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:38:41 +00:00
|
|
|
signalType = signalMap[type];
|
2022-07-04 21:14:13 +00:00
|
|
|
switch(signalType)
|
|
|
|
{
|
|
|
|
// TODO: create extractor functions like in nim
|
2022-10-19 13:41:53 +00:00
|
|
|
case NodeLogin: emit nodeLogin(signalError); break;
|
|
|
|
case NodeReady: emit nodeReady(signalError); break;
|
|
|
|
case NodeStarted: emit nodeStarted(signalError); break;
|
|
|
|
case NodeStopped: emit nodeStopped(signalError); break;
|
2022-07-04 21:14:13 +00:00
|
|
|
case NodeCrashed:
|
|
|
|
qWarning() << "node.crashed, error: " << signalError;
|
|
|
|
emit nodeCrashed(signalError);
|
|
|
|
break;
|
2022-10-19 13:41:53 +00:00
|
|
|
case DiscoveryStarted: emit discoveryStarted(signalError); break;
|
|
|
|
case DiscoveryStopped: emit discoveryStopped(signalError); break;
|
2022-10-14 10:38:41 +00:00
|
|
|
case DiscoverySummary: emit discoverySummary(signalEvent.array().size(), signalError); break;
|
|
|
|
case MailserverStarted: emit mailserverStarted(signalError); break;
|
2022-10-19 13:41:53 +00:00
|
|
|
case MailserverChanged: emit mailserverChanged(signalError); break;
|
|
|
|
case MailserverAvailable: emit mailserverAvailable(signalError); break;
|
|
|
|
case HistoryRequestStarted: emit historyRequestStarted(signalError); break;
|
|
|
|
case HistoryRequestBatchProcessed: emit historyRequestBatchProcessed(signalError); break;
|
|
|
|
case HistoryRequestCompleted: emit historyRequestCompleted(signalError); break;
|
2022-10-14 10:38:41 +00:00
|
|
|
case WalletEvent: emit wallet(EventDataQPtr(new EventData(std::move(signalEvent), false))); break;
|
2022-07-04 21:14:13 +00:00
|
|
|
case Unknown: assert(false); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignalsManager::signalCallback(const char* data)
|
|
|
|
{
|
2022-10-14 10:38:41 +00:00
|
|
|
SignalsManager::instance()->processSignal(data);
|
2022-07-04 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
} // namespace Status::StatusGo
|