add +dbg -dbg for Debug messages, greet the Client with SMBus Version

This commit is contained in:
Syping 2020-07-16 20:02:21 +02:00
parent fe7b2ab13d
commit 9df6b11a16
3 changed files with 50 additions and 8 deletions

View file

@ -40,6 +40,7 @@ void SMSubProcess::start()
void SMSubProcess::readyRead()
{
// Read process output and emit event
while (process.canReadLine()) {
const QByteArray readData = process.readLine().trimmed();
emit outputWritten(readData + '\n');
@ -48,12 +49,14 @@ void SMSubProcess::readyRead()
void SMSubProcess::processExit(int exitCode)
{
// Exit with the same exit code as the process
emit processStopped();
QCoreApplication::exit(exitCode);
}
void SMSubProcess::processError(QProcess::ProcessError error)
{
// Handle process errors
if (likely(error == QProcess::FailedToStart)) {
QTextStream(stderr) << "Process failed to start!" << endl;
QCoreApplication::exit(1);
@ -66,15 +69,18 @@ void SMSubProcess::processError(QProcess::ProcessError error)
void SMSubProcess::killProcess()
{
// Kill process as requested
process.kill();
}
void SMSubProcess::stopProcess()
{
// Terminate process as requested
process.terminate();
}
void SMSubProcess::writeInput(const QByteArray &input)
{
// Write input from Unix/IPC socket to process
process.write(input);
}

View file

@ -16,6 +16,7 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <QCoreApplication>
#include "SMSubServer.h"
#include "smsub.h"
@ -27,48 +28,82 @@ SMSubServer::SMSubServer(const QString &socket)
void SMSubServer::incomingConnection(quintptr socketDescriptor)
{
QLocalSocket *localSocket = new QLocalSocket();
localSocket->setSocketDescriptor(socketDescriptor);
QObject::connect(localSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
QObject::connect(localSocket, SIGNAL(disconnected()), this, SLOT(deleteSocket()));
sockets << localSocket;
QLocalSocket *socket = new QLocalSocket();
socket->setSocketDescriptor(socketDescriptor);
QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(deleteSocket()));
sockets << socket;
// Initial open writing
socket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
}
void SMSubServer::readyRead()
{
// Manage client input
QLocalSocket *socket = (QLocalSocket*)sender();
while (socket->canReadLine()) {
const QByteArray readData = socket->readLine().trimmed();
if (readData.startsWith("+log")) {
if (readData.startsWith("+dbg")) {
socket->setProperty("ReceiveDbgMsg", true);
socket->write("Debug messages enabled!\n");
}
else if (readData.startsWith("-dbg")) {
socket->setProperty("ReceiveDbgMsg", false);
socket->write("Debug messages disabled!\n");
}
else if (readData.startsWith("+log")) {
socket->setProperty("ReceiveLog", true);
debugOutput(socket, "Log output enabled!");
}
else if (readData.startsWith("-log")) {
socket->setProperty("ReceiveLog", false);
debugOutput(socket, "Log output disabled!");
}
else if (readData.startsWith("kill")) {
emit killRequested();
debugOutput(socket, "Killing server!");
}
else if (readData.startsWith("stop")) {
emit stopRequested();
debugOutput(socket, "Stopping server!");
}
else if (readData.startsWith("wl")) {
emit inputWritten(readData.mid(3) + '\n');
const QByteArray writeData = readData.mid(3);
emit inputWritten(writeData + '\n');
debugOutput(socket, "Write line \"" + writeData + "\"!");
}
else if (readData.startsWith("w")) {
emit inputWritten(readData.mid(2));
const QByteArray writeData = readData.mid(2);
emit inputWritten(writeData);
debugOutput(socket, "Write \"" + writeData + "\"!");
}
}
}
void SMSubServer::deleteSocket()
{
// Delete socket and remove from index
QLocalSocket *socket = (QLocalSocket*)sender();
sockets.removeAll(socket);
socket->deleteLater();
}
void SMSubServer::debugOutput(QLocalSocket *socket, const QByteArray &message)
{
// Only send debug messages when the client opted-in
const QVariant variant = socket->property("ReceiveDbgMsg");
if (unlikely(variant.type() == QVariant::Bool)) {
bool receiveDbgMsg = variant.toBool();
if (likely(receiveDbgMsg)) {
socket->write(message + '\n');
}
}
}
void SMSubServer::writeOutput(const QByteArray &output)
{
// Read process output when client opted-in for log
QVector<QLocalSocket*>::const_iterator it = sockets.constBegin();
QVector<QLocalSocket*>::const_iterator end = sockets.constEnd();
while (it != end) {

View file

@ -38,6 +38,7 @@ private slots:
void readyRead();
private:
inline void debugOutput(QLocalSocket *socket, const QByteArray &message);
QVector<QLocalSocket*> sockets;
signals: