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"
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<QIODevice>(dev);
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() {
@ -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);
}

View File

@ -23,10 +23,10 @@ class DataFeed: public QObject {
private:
QSharedPointer<ProcessData> m_procData = QSharedPointer<ProcessData>(new ProcessData);
QIODevice *m_dev = nullptr;
std::unique_ptr<QIODevice> m_dev;
public:
DataFeed(QIODevice *dev);
DataFeed(QIODevice *dev, bool skipInit = false);
const QSharedPointer<ProcessData> &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*);
};

View File

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