Add support for switching to a different DataFeed upon Init msg

This commit is contained in:
Gary Talent 2021-03-08 20:39:39 -06:00
parent 8160055aa1
commit b6033f48dc
3 changed files with 33 additions and 11 deletions

View File

@ -10,9 +10,13 @@
#include "server.hpp" #include "server.hpp"
DataFeed::DataFeed(QIODevice *dev): QObject(dev) { DataFeed::DataFeed(QIODevice *dev, bool skipInit): QObject(dev) {
m_dev = dev; m_dev = std::unique_ptr<QIODevice>(dev);
connect(m_dev, &QIODevice::readyRead, this, &DataFeed::handleInit); if (!skipInit) {
connect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::handleInit);
} else {
connect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::read);
}
} }
const QSharedPointer<ProcessData> &DataFeed::procData() { const QSharedPointer<ProcessData> &DataFeed::procData() {
@ -24,21 +28,24 @@ void DataFeed::handleInit() {
if (doc.isObject()) { if (doc.isObject()) {
auto msg = doc.object(); auto msg = doc.object();
if (msg["type"].toString() == "Init") { if (msg["type"].toString() == "Init") {
disconnect(m_dev, &QIODevice::readyRead, this, &DataFeed::handleInit); disconnect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::handleInit);
connect(m_dev, &QIODevice::readyRead, this, &DataFeed::read); connect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::read);
} }
} }
} }
void DataFeed::read() { void DataFeed::read() {
while (m_dev->bytesAvailable()) { while (m_dev && m_dev->bytesAvailable()) {
auto json = m_dev->readLine(); const auto json = m_dev->readLine();
const auto doc = QJsonDocument::fromJson(json); const auto doc = QJsonDocument::fromJson(json);
if (m_procData) { if (m_procData) {
const auto msg = doc.object(); const auto msg = doc.object();
if (msg["type"] == "TraceEvent") { if (msg["type"] == "TraceEvent") {
m_procData->traceEvents.push_back(msg["data"].toObject()); m_procData->traceEvents.push_back(msg["data"].toObject());
emit m_procData->traceEvent(m_procData->traceEvents.last()); emit m_procData->traceEvent(m_procData->traceEvents.last());
} else if (msg["type"] == "Init") {
emit feedEnd(m_dev.get());
m_dev.release();
} else { } else {
qDebug().noquote() << "Bad message:" << json; qDebug().noquote() << "Bad message:" << json;
} }
@ -56,5 +63,13 @@ void LogServer::handleConnection() {
auto conn = m_server->nextPendingConnection(); auto conn = m_server->nextPendingConnection();
connect(conn, &QAbstractSocket::disconnected, conn, &QObject::deleteLater); connect(conn, &QAbstractSocket::disconnected, conn, &QObject::deleteLater);
connect(this, &QObject::destroyed, conn, &QObject::deleteLater); connect(this, &QObject::destroyed, conn, &QObject::deleteLater);
emit newDataFeed(new DataFeed(conn)); auto feed = new DataFeed(conn);
connect(feed, &DataFeed::feedEnd, this, &LogServer::setupDataFeed);
emit newDataFeed(feed);
}
void LogServer::setupDataFeed(QIODevice *conn) {
auto feed = new DataFeed(conn, true);
connect(feed, &DataFeed::feedEnd, this, &LogServer::setupDataFeed);
emit newDataFeed(feed);
} }

View File

@ -23,10 +23,10 @@ class DataFeed: public QObject {
private: private:
QSharedPointer<ProcessData> m_procData = QSharedPointer<ProcessData>(new ProcessData); QSharedPointer<ProcessData> m_procData = QSharedPointer<ProcessData>(new ProcessData);
QIODevice *m_dev = nullptr; std::unique_ptr<QIODevice> m_dev;
public: public:
DataFeed(QIODevice *dev); DataFeed(QIODevice *dev, bool skipInit = false);
const QSharedPointer<ProcessData> &procData(); const QSharedPointer<ProcessData> &procData();
@ -34,6 +34,10 @@ class DataFeed: public QObject {
void handleInit(); void handleInit();
void read(); void read();
signals:
void feedEnd(QIODevice*);
}; };
class LogServer: public QObject { class LogServer: public QObject {
@ -48,6 +52,9 @@ class LogServer: public QObject {
public slots: public slots:
void handleConnection(); void handleConnection();
void setupDataFeed(QIODevice *conn);
signals: signals:
void newDataFeed(DataFeed*); void newDataFeed(DataFeed*);
}; };

View File

@ -34,7 +34,7 @@ class TraceView: public QWidget {
public: public:
TraceView(QWidget *parent = nullptr); TraceView(QWidget *parent = nullptr);
~TraceView(); ~TraceView() override;
void setProcessData(ProcessData *data); void setProcessData(ProcessData *data);