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

View file

@ -16,6 +16,7 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#include <QCoreApplication>
#include "SMSubServer.h" #include "SMSubServer.h"
#include "smsub.h" #include "smsub.h"
@ -27,48 +28,82 @@ SMSubServer::SMSubServer(const QString &socket)
void SMSubServer::incomingConnection(quintptr socketDescriptor) void SMSubServer::incomingConnection(quintptr socketDescriptor)
{ {
QLocalSocket *localSocket = new QLocalSocket(); QLocalSocket *socket = new QLocalSocket();
localSocket->setSocketDescriptor(socketDescriptor); socket->setSocketDescriptor(socketDescriptor);
QObject::connect(localSocket, SIGNAL(readyRead()), this, SLOT(readyRead())); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
QObject::connect(localSocket, SIGNAL(disconnected()), this, SLOT(deleteSocket())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(deleteSocket()));
sockets << localSocket; sockets << socket;
// Initial open writing
socket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
} }
void SMSubServer::readyRead() void SMSubServer::readyRead()
{ {
// Manage client input
QLocalSocket *socket = (QLocalSocket*)sender(); QLocalSocket *socket = (QLocalSocket*)sender();
while (socket->canReadLine()) { while (socket->canReadLine()) {
const QByteArray readData = socket->readLine().trimmed(); 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); socket->setProperty("ReceiveLog", true);
debugOutput(socket, "Log output enabled!");
} }
else if (readData.startsWith("-log")) { else if (readData.startsWith("-log")) {
socket->setProperty("ReceiveLog", false); socket->setProperty("ReceiveLog", false);
debugOutput(socket, "Log output disabled!");
} }
else if (readData.startsWith("kill")) { else if (readData.startsWith("kill")) {
emit killRequested(); emit killRequested();
debugOutput(socket, "Killing server!");
} }
else if (readData.startsWith("stop")) { else if (readData.startsWith("stop")) {
emit stopRequested(); emit stopRequested();
debugOutput(socket, "Stopping server!");
} }
else if (readData.startsWith("wl")) { 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")) { 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() void SMSubServer::deleteSocket()
{ {
// Delete socket and remove from index
QLocalSocket *socket = (QLocalSocket*)sender(); QLocalSocket *socket = (QLocalSocket*)sender();
sockets.removeAll(socket); sockets.removeAll(socket);
socket->deleteLater(); 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) 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 it = sockets.constBegin();
QVector<QLocalSocket*>::const_iterator end = sockets.constEnd(); QVector<QLocalSocket*>::const_iterator end = sockets.constEnd();
while (it != end) { while (it != end) {

View file

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