gui: Introduce IPCPreviewClient

This commit is contained in:
Albert S. 2022-05-27 09:29:56 +02:00
parent 3bdcb76d8e
commit 8f2e77b152
3 changed files with 170 additions and 5 deletions

View File

@ -23,7 +23,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
ipcclient.cpp \
ipcpreviewclient.cpp \
ipcpreviewworker.cpp \
ipcserver.cpp \
main.cpp \
mainwindow.cpp \
@ -35,11 +36,13 @@ SOURCES += \
previewresult.cpp \
previewresultpdf.cpp \
previewresultplaintext.cpp \
previewworker.cpp
renderconfig.cpp \
rendertarget.cpp
HEADERS += \
ipc.h \
ipcclient.h \
ipcpreviewclient.h \
ipcpreviewworker.h \
ipcserver.h \
mainwindow.h \
clicklabel.h \
@ -50,8 +53,8 @@ HEADERS += \
previewresult.h \
previewresultpdf.h \
previewresultplaintext.h \
previewworker.h \
renderconfig.h
renderconfig.h \
rendertarget.h
FORMS += \
mainwindow.ui

126
gui/ipcpreviewclient.cpp Normal file
View File

@ -0,0 +1,126 @@
#include <QLocalSocket>
#include <QApplication>
#include "ipc.h"
#include "ipcpreviewclient.h"
#include "previewresultpdf.h"
#include "previewresultplaintext.h"
bool IPCPreviewClient::connect()
{
if(socket->state() == QLocalSocket::ConnectedState)
{
socket->disconnectFromServer();
if(socket->state() == QLocalSocket::ConnectedState)
{
socket->waitForDisconnected(100);
}
}
socket->connectToServer(socketPath);
socket->waitForConnected(100);
return socket->state() == QLocalSocket::ConnectedState;
}
QSharedPointer<PreviewResult> IPCPreviewClient::deserialize(QByteArray &array)
{
QDataStream stream{&array, QIODevice::ReadOnly};
PreviewResultType type;
stream >> type;
if(type == PreviewResultType::PDF)
{
return PreviewResultPdf::deserialize(array);
}
if(type == PreviewResultType::PlainText)
{
return PreviewResultPlainText::deserialize(array);
}
return QSharedPointer<PreviewResult>(nullptr);
}
IPCPreviewClient::IPCPreviewClient()
{
this->socket = new QLocalSocket(this);
}
void IPCPreviewClient::setSocketPath(QString socketPath)
{
this->socketPath = socketPath;
}
void IPCPreviewClient::startGeneration(RenderConfig config, const QVector<RenderTarget> &targets)
{
this->start(config, targets);
}
void IPCPreviewClient::start(RenderConfig config, const QVector<RenderTarget> &targets)
{
if(targets.count() == 0)
{
return;
}
if(!connect() || !socket->isOpen())
{
// TODO: ERROR
}
if(socket->isOpen() && socket->isWritable())
{
QDataStream stream(socket);
stream << GeneratePreviews;
stream << config;
stream << targets;
socket->flush();
int numTarget = 0;
if(socket->isOpen() && socket->isReadable() && socket->state() == QLocalSocket::ConnectedState)
{
do
{
socket->waitForReadyRead(100);
stream.startTransaction();
stream >> numTarget;
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
if(numTarget != targets.count())
{
throw std::runtime_error("Server reports less targets than it should");
}
}
else
{
// TODO: ERROR
}
int processed = 0;
++this->currentPreviewGeneration;
while(socket->isOpen() && socket->isReadable() && processed < targets.count())
{
QByteArray array;
do
{
socket->waitForReadyRead(100);
stream.startTransaction();
stream >> array;
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
emit previewReceived(deserialize(array), this->currentPreviewGeneration);
++processed;
}
if(processed != targets.count())
{
// TODO: ERROR
}
}
socket->disconnectFromServer();
emit finished();
}
void IPCPreviewClient::stopGeneration()
{
if(!connect() || !socket->isOpen())
{
// TODO: ERROR
}
QDataStream stream(socket);
stream << StopGeneratePreviews;
socket->flush();
}

36
gui/ipcpreviewclient.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef IPCPREVIEWCLIENT_H
#define IPCPREVIEWCLIENT_H
#include <QObject>
#include <QLocalSocket>
#include "previewresult.h"
#include "renderconfig.h"
#include "rendertarget.h"
class IPCPreviewClient : public QObject
{
Q_OBJECT
private:
unsigned int currentPreviewGeneration = 1;
QLocalSocket *socket;
QString socketPath;
bool connect();
QSharedPointer<PreviewResult> deserialize(QByteArray &array);
public:
IPCPreviewClient();
~IPCPreviewClient()
{
delete socket;
}
void setSocketPath(QString socketPath);
public slots:
void start(RenderConfig config, const QVector<RenderTarget> &targets);
void startGeneration(RenderConfig config, const QVector<RenderTarget> &targets);
void stopGeneration();
signals:
void previewReceived(QSharedPointer<PreviewResult> previewResult, unsigned int currentPreviewGeneration);
void finished();
};
#endif // IPCPREVIEWCLIENT_H