aboutsummaryrefslogtreecommitdiffstats
path: root/mock-server
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-03-13 18:20:35 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-03-13 18:20:35 +0100
commit87169e10cb7ebe732ef388552bb0c057c09767ef (patch)
treefba2c9e2255cb02553c43fb37d653a47574824ba /mock-server
parent02fe86ab2a04a02b114d7ca8ce4374a29a1d5f45 (diff)
downloadQtXpl2-87169e10cb7ebe732ef388552bb0c057c09767ef.tar.gz
QtXpl2-87169e10cb7ebe732ef388552bb0c057c09767ef.zip
Unified socket slots, --wire-debug CLI flag, fix mock TX and disconnect logging
Collapse 12 per-socket slots into 4 sender()-based slots. Replace runtime wireDebug QML property with static --wire-debug CLI flag on both demo and mock server. Route MockServer::sendKaPing through sendReply so KA_PING TX shows wire bytes. Simplify sendReply to deduce command token from wire data. Fix Xpl2Client disconnect logging (logTag takes pointer, null-sender fallback).
Diffstat (limited to 'mock-server')
-rw-r--r--mock-server/MockServer.cpp90
-rw-r--r--mock-server/MockServer.h10
-rw-r--r--mock-server/main.cpp11
3 files changed, 82 insertions, 29 deletions
diff --git a/mock-server/MockServer.cpp b/mock-server/MockServer.cpp
index 52db5a1..d58e58e 100644
--- a/mock-server/MockServer.cpp
+++ b/mock-server/MockServer.cpp
@@ -6,6 +6,14 @@
#include <QTcpSocket>
+bool MockServer::s_wireDebug = false;
+
+void
+MockServer::enableWireDebug ()
+{
+ s_wireDebug = true;
+}
+
MockServer::MockServer (QObject *parent) : QObject (parent)
{
setupPort (m_command, "Command", 9110);
@@ -26,22 +34,26 @@ MockServer::setupPort (Port &port, const char *name, quint16 number)
&MockServer::onNewConnection);
if (!port.server.listen (QHostAddress::Any, number))
- qCritical ("Failed to listen on %s port %d: %s", name, number,
+ qCritical ("%s Failed to listen: %s", qPrintable (logTag (number)),
qPrintable (port.server.errorString ()));
else
- qInfo ("Listening on %s port %d", name, number);
+ qInfo ("%s Listening", qPrintable (logTag (number)));
}
-const char *
-MockServer::portName (quint16 localPort) const
+QString
+MockServer::logTag (quint16 localPort) const
{
+ const char *name = "Unknown";
if (localPort == m_command.number)
- return m_command.name;
- if (localPort == m_imaging.number)
- return m_imaging.name;
- if (localPort == m_status.number)
- return m_status.name;
- return "Unknown";
+ name = m_command.name;
+ else if (localPort == m_imaging.number)
+ name = m_imaging.name;
+ else if (localPort == m_status.number)
+ name = m_status.name;
+
+ /* Fixed-width tag: "[Command:9110]" = 14 chars, left-padded to 15. */
+ return QStringLiteral ("[%1:%2]").arg (name).arg (localPort).leftJustified (
+ 15);
}
void
@@ -51,7 +63,7 @@ MockServer::onNewConnection ()
while (auto *sock = server->nextPendingConnection ())
{
quint16 lp = sock->localPort ();
- qInfo ("[%s:%d] client connected", portName (lp), lp);
+ qInfo ("%s Client connected", qPrintable (logTag (lp)));
m_clients.append (sock);
connect (sock, &QTcpSocket::readyRead, this,
&MockServer::onClientMessageReady);
@@ -76,7 +88,7 @@ MockServer::onClientDisconnected ()
{
auto *sock = qobject_cast<QTcpSocket *> (sender ());
quint16 lp = sock->localPort ();
- qInfo ("[%s:%d] client disconnected", portName (lp), lp);
+ qInfo ("%s Client disconnected", qPrintable (logTag (lp)));
m_clients.removeOne (sock);
sock->deleteLater ();
}
@@ -87,7 +99,7 @@ MockServer::sendKaPing ()
for (auto *client : m_clients)
{
if (client->state () == QAbstractSocket::ConnectedState)
- client->write ("KA_PING\n");
+ sendReply (client, "KA_PING\n");
}
}
@@ -98,25 +110,47 @@ MockServer::handleCommand (QTcpSocket *client, const QByteArray &line)
if (trimmed.isEmpty ())
return;
- quint16 lp = client->localPort ();
-
- /* Split on first comma to get command token. */
+ /* Split on first comma to get command token and remaining params. */
int comma = trimmed.indexOf (',');
QByteArray cmd = (comma >= 0) ? trimmed.left (comma) : trimmed;
+ QByteArray params = (comma >= 0) ? trimmed.mid (comma + 1) : QByteArray ();
if (cmd == "KA_PING")
- {
- qDebug ("[%s:%d] KA_PING ACK received", portName (lp), lp);
- return;
- }
+ handleKaPing (client, params);
+ else if (cmd == "GS_JC_VERSION")
+ handleGsJcVersion (client);
+ else
+ qWarning ("%s Unknown command: %s",
+ qPrintable (logTag (client->localPort ())), cmd.constData ());
+}
- if (cmd == "GS_JC_VERSION")
- {
- qInfo ("[%s:%d] -> GS_JC_VERSION reply", portName (lp), lp);
- client->write ("GS_JC_VERSION,1,\"1.05\",\"2.00\",15\n");
- return;
- }
+void
+MockServer::sendReply (QTcpSocket *client, const QByteArray &data)
+{
+ client->write (data);
+ QByteArray trimmed = data.trimmed ();
+ int comma = trimmed.indexOf (',');
+ QByteArray cmd = (comma >= 0) ? trimmed.left (comma) : trimmed;
+ QByteArray wire;
+ if (s_wireDebug)
+ wire = " >> " + trimmed;
+ qDebug ("%s TX %s%s", qPrintable (logTag (client->localPort ())),
+ cmd.constData (), wire.constData ());
+}
- qWarning ("[%s:%d] Unknown command: %s", portName (lp), lp,
- trimmed.constData ());
+void
+MockServer::handleKaPing (QTcpSocket *client, const QByteArray &params)
+{
+ QByteArray wire;
+ if (s_wireDebug)
+ wire = " << KA_PING," + params;
+ qDebug ("%s RX KA_PING ACK%s", qPrintable (logTag (client->localPort ())),
+ wire.constData ());
+}
+
+void
+MockServer::handleGsJcVersion (QTcpSocket *client)
+{
+ qDebug ("%s RX GS_JC_VERSION", qPrintable (logTag (client->localPort ())));
+ sendReply (client, "GS_JC_VERSION,1,\"1.05\",\"2.00\",15\n");
}
diff --git a/mock-server/MockServer.h b/mock-server/MockServer.h
index 21d4d32..13bb2cd 100644
--- a/mock-server/MockServer.h
+++ b/mock-server/MockServer.h
@@ -17,11 +17,13 @@ class MockServer : public QObject
public:
explicit MockServer (QObject *parent = nullptr);
+ static void enableWireDebug ();
private slots:
void onNewConnection ();
void onClientMessageReady ();
void onClientDisconnected ();
+ /* Send KA_PING keepalive to all connected clients. */
void sendKaPing ();
private:
@@ -33,12 +35,18 @@ private:
};
void setupPort (Port &port, const char *name, quint16 number);
- const char *portName (quint16 localPort) const;
+ /* Return a fixed-width "[Name:port]" tag for log lines. */
+ QString logTag (quint16 localPort) const;
+ /* Parse incoming line, log RX, and dispatch to the matching handler. */
void handleCommand (QTcpSocket *client, const QByteArray &line);
+ void sendReply (QTcpSocket *client, const QByteArray &data);
+ void handleKaPing (QTcpSocket *client, const QByteArray &params);
+ void handleGsJcVersion (QTcpSocket *client);
Port m_command;
Port m_imaging;
Port m_status;
QList<QTcpSocket *> m_clients;
QTimer m_pingTimer;
+ static bool s_wireDebug;
};
diff --git a/mock-server/main.cpp b/mock-server/main.cpp
index b104685..5613462 100644
--- a/mock-server/main.cpp
+++ b/mock-server/main.cpp
@@ -4,13 +4,24 @@
*/
#include "MockServer.h"
+#include <QCommandLineParser>
#include <QCoreApplication>
int
main (int argc, char *argv[])
{
+ qSetMessagePattern ("MockServer [%{time HH:mm:ss.zzz}] %{message}");
+
QCoreApplication app (argc, argv);
+ QCommandLineParser parser;
+ parser.addOption ({ "wire-debug", "Log raw wire TX/RX to dev log" });
+ parser.addHelpOption ();
+ parser.process (app);
+
+ if (parser.isSet ("wire-debug"))
+ MockServer::enableWireDebug ();
+
new MockServer (&app);
return app.exec ();