From 61debe99a269bf7e87f6ba2f8d2a376e619fcf12 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Mon, 23 Mar 2026 16:39:33 +0100 Subject: Invert TCP connection model: client listens, server connects The XPL2 protocol specifies that the JI2 controller initiates connections to the client, not the other way around. Xpl2Client now listens on 3 ports via QTcpServer and accepts inbound connections; MockServer connects out with auto-retry on the 1s KA_PING tick. QML API: startListening/stopListening, listening+connected properties, host property removed. Mock server gains --host CLI arg. --- mock-server/MockServer.h | 77 ++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 39 deletions(-) (limited to 'mock-server/MockServer.h') diff --git a/mock-server/MockServer.h b/mock-server/MockServer.h index 74b2731..242cb32 100644 --- a/mock-server/MockServer.h +++ b/mock-server/MockServer.h @@ -1,88 +1,87 @@ /** * @file MockServer.h - * @brief Mock XPL2 server — listens on all three protocol ports. + * @brief Mock XPL2 server — connects out to the client on three protocol + * ports. */ #pragma once -#include #include -#include +#include #include -class QTcpSocket; - class MockServer : public QObject { Q_OBJECT public: - explicit MockServer (QObject *parent = nullptr); + explicit MockServer (const QString &host, QObject *parent = nullptr); static void enableWireDebug (); private slots: - void onNewConnection (); - void onClientMessageReady (); - void onClientDisconnected (); - /* Send KA_PING keepalive to all connected clients. */ - void sendKaPing (); + void onSocketConnected (); + void onSocketDisconnected (); + void onSocketMessageReady (); + /* Send KA_PING on connected sockets, retry connection on disconnected. */ + void tick (); void sendJcStatusMsg (); void sendPhStatusMsg (); private: struct Port { - QTcpServer server; + QTcpSocket socket; const char *name = nullptr; quint16 number = 0; }; void setupPort (Port &port, const char *name, quint16 number); + void connectAll (); /* Return a fixed-width "[Name:port]" tag for log lines. */ - QString logTag (quint16 localPort) const; + QString logTag (const QTcpSocket *socket) 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 ¶ms); - void handleGsJcVersion (QTcpSocket *client); - void handleGsPhVersion (QTcpSocket *client, const QByteArray ¶ms); + void handleCommand (Port &port, const QByteArray &line); + void sendReply (QTcpSocket &socket, const QByteArray &data); + void handleKaPing (Port &port, const QByteArray ¶ms); + void handleGsJcVersion (QTcpSocket &socket); + void handleGsPhVersion (QTcpSocket &socket, const QByteArray ¶ms); /* CN_ control command handlers */ - void handleCnJcSuccess (QTcpSocket *client, const QByteArray &cmd); - void handleCnPhSuccess (QTcpSocket *client, const QByteArray &cmd, + void handleCnJcSuccess (QTcpSocket &socket, const QByteArray &cmd); + void handleCnPhSuccess (QTcpSocket &socket, const QByteArray &cmd, const QByteArray ¶ms); - void handleCnPhCalibrationData (QTcpSocket *client, + void handleCnPhCalibrationData (QTcpSocket &socket, const QByteArray ¶ms); - void handleCnPhCalibrationRawData (QTcpSocket *client, + void handleCnPhCalibrationRawData (QTcpSocket &socket, const QByteArray ¶ms); - void handleCnPhCalibratedBaseFrequency (QTcpSocket *client, + void handleCnPhCalibratedBaseFrequency (QTcpSocket &socket, const QByteArray ¶ms); - void handleCnStatusMessagingStart (QTcpSocket *client, const QByteArray &cmd, + void handleCnStatusMessagingStart (QTcpSocket &socket, const QByteArray &cmd, const QByteArray ¶ms); - void handleCnStatusMessagingStop (QTcpSocket *client, const QByteArray &cmd); + void handleCnStatusMessagingStop (QTcpSocket &socket, const QByteArray &cmd); /* CF_ configuration command handlers */ - void handleCfJcSetPurgeSettings (QTcpSocket *client, + void handleCfJcSetPurgeSettings (QTcpSocket &socket, const QByteArray ¶ms); - void handleCfJcSetJettingParams (QTcpSocket *client, + void handleCfJcSetJettingParams (QTcpSocket &socket, const QByteArray ¶ms); - void handleCfPhJettingParams (QTcpSocket *client, const QByteArray &cmd, + void handleCfPhJettingParams (QTcpSocket &socket, const QByteArray &cmd, const QByteArray ¶ms); - void handleCfJcSetter (QTcpSocket *client, const QByteArray ¶ms); - void handleCfPhSetter (QTcpSocket *client, const QByteArray ¶ms); - void handleCfJcGetter (QTcpSocket *client, const QByteArray ¶ms); - void handleCfPhGetter (QTcpSocket *client, const QByteArray ¶ms); + void handleCfJcSetter (QTcpSocket &socket, const QByteArray ¶ms); + void handleCfPhSetter (QTcpSocket &socket, const QByteArray ¶ms); + void handleCfJcGetter (QTcpSocket &socket, const QByteArray ¶ms); + void handleCfPhGetter (QTcpSocket &socket, const QByteArray ¶ms); /* Imaging command handlers */ - void handleImagingStart (QTcpSocket *client, const QByteArray ¶ms); - void handleImagingStop (QTcpSocket *client); - void handleImagingMaskStart (QTcpSocket *client, const QByteArray &cmd, + void handleImagingStart (QTcpSocket &socket, const QByteArray ¶ms); + void handleImagingStop (QTcpSocket &socket); + void handleImagingMaskStart (QTcpSocket &socket, const QByteArray &cmd, const QByteArray ¶ms); - void handleImagingMaskEnd (QTcpSocket *client, const QByteArray &cmd, + void handleImagingMaskEnd (QTcpSocket &socket, const QByteArray &cmd, const QByteArray ¶ms); - void handleImageCount (QTcpSocket *client); + void handleImageCount (QTcpSocket &socket); + QString m_host; Port m_command; Port m_imaging; Port m_status; - QList m_clients; - QTimer m_pingTimer; + QTimer m_tickTimer; QTimer m_jcStatusTimer; QTimer m_phStatusTimer; int m_jcStatusLevel = 1; -- cgit v1.2.3