From b6033f48dc9cf49072b1edb6581380d7a8d09481 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 8 Mar 2021 20:39:39 -0600 Subject: [PATCH] Add support for switching to a different DataFeed upon Init msg --- src/server.cpp | 31 +++++++++++++++++++++++-------- src/server.hpp | 11 +++++++++-- src/traceview.hpp | 2 +- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/server.cpp b/src/server.cpp index 94ea2d2..2021880 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -10,9 +10,13 @@ #include "server.hpp" -DataFeed::DataFeed(QIODevice *dev): QObject(dev) { - m_dev = dev; - connect(m_dev, &QIODevice::readyRead, this, &DataFeed::handleInit); +DataFeed::DataFeed(QIODevice *dev, bool skipInit): QObject(dev) { + m_dev = std::unique_ptr(dev); + if (!skipInit) { + connect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::handleInit); + } else { + connect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::read); + } } const QSharedPointer &DataFeed::procData() { @@ -24,21 +28,24 @@ void DataFeed::handleInit() { if (doc.isObject()) { auto msg = doc.object(); if (msg["type"].toString() == "Init") { - disconnect(m_dev, &QIODevice::readyRead, this, &DataFeed::handleInit); - connect(m_dev, &QIODevice::readyRead, this, &DataFeed::read); + disconnect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::handleInit); + connect(m_dev.get(), &QIODevice::readyRead, this, &DataFeed::read); } } } void DataFeed::read() { - while (m_dev->bytesAvailable()) { - auto json = m_dev->readLine(); + while (m_dev && m_dev->bytesAvailable()) { + const auto json = m_dev->readLine(); const auto doc = QJsonDocument::fromJson(json); if (m_procData) { const auto msg = doc.object(); if (msg["type"] == "TraceEvent") { m_procData->traceEvents.push_back(msg["data"].toObject()); emit m_procData->traceEvent(m_procData->traceEvents.last()); + } else if (msg["type"] == "Init") { + emit feedEnd(m_dev.get()); + m_dev.release(); } else { qDebug().noquote() << "Bad message:" << json; } @@ -56,5 +63,13 @@ void LogServer::handleConnection() { auto conn = m_server->nextPendingConnection(); connect(conn, &QAbstractSocket::disconnected, 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); } diff --git a/src/server.hpp b/src/server.hpp index 1c1d906..77a9576 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -23,10 +23,10 @@ class DataFeed: public QObject { private: QSharedPointer m_procData = QSharedPointer(new ProcessData); - QIODevice *m_dev = nullptr; + std::unique_ptr m_dev; public: - DataFeed(QIODevice *dev); + DataFeed(QIODevice *dev, bool skipInit = false); const QSharedPointer &procData(); @@ -34,6 +34,10 @@ class DataFeed: public QObject { void handleInit(); void read(); + + signals: + void feedEnd(QIODevice*); + }; class LogServer: public QObject { @@ -48,6 +52,9 @@ class LogServer: public QObject { public slots: void handleConnection(); + void setupDataFeed(QIODevice *conn); + signals: void newDataFeed(DataFeed*); + }; diff --git a/src/traceview.hpp b/src/traceview.hpp index 75d9312..0eff0df 100644 --- a/src/traceview.hpp +++ b/src/traceview.hpp @@ -34,7 +34,7 @@ class TraceView: public QWidget { public: TraceView(QWidget *parent = nullptr); - ~TraceView(); + ~TraceView() override; void setProcessData(ProcessData *data);