aboutsummaryrefslogtreecommitdiffstats
path: root/mock-server/MockServer.cpp
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-03-23 16:39:33 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-03-23 16:39:33 +0100
commit61debe99a269bf7e87f6ba2f8d2a376e619fcf12 (patch)
tree41cf942329760f8a5f16d98ea202124fcae8a0f3 /mock-server/MockServer.cpp
parent46e96b3318900b561928be304f4a9e252f2785a4 (diff)
downloadQtXpl2-61debe99a269bf7e87f6ba2f8d2a376e619fcf12.tar.gz
QtXpl2-61debe99a269bf7e87f6ba2f8d2a376e619fcf12.zip
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.
Diffstat (limited to 'mock-server/MockServer.cpp')
-rw-r--r--mock-server/MockServer.cpp356
1 files changed, 188 insertions, 168 deletions
diff --git a/mock-server/MockServer.cpp b/mock-server/MockServer.cpp
index 87279b4..08eb116 100644
--- a/mock-server/MockServer.cpp
+++ b/mock-server/MockServer.cpp
@@ -1,11 +1,10 @@
/**
* @file MockServer.cpp
- * @brief Mock XPL2 server — listens on all three protocol ports.
+ * @brief Mock XPL2 server — connects out to the client on three protocol
+ * ports.
*/
#include "MockServer.h"
-#include <QTcpSocket>
-
bool MockServer::s_wireDebug = false;
void
@@ -14,19 +13,22 @@ MockServer::enableWireDebug ()
s_wireDebug = true;
}
-MockServer::MockServer (QObject *parent) : QObject (parent)
+MockServer::MockServer (const QString &host, QObject *parent)
+ : QObject (parent), m_host (host)
{
setupPort (m_command, "Command", 9110);
setupPort (m_imaging, "Imaging", 9111);
setupPort (m_status, "Status", 9112);
- connect (&m_pingTimer, &QTimer::timeout, this, &MockServer::sendKaPing);
- m_pingTimer.start (1000);
+ connect (&m_tickTimer, &QTimer::timeout, this, &MockServer::tick);
+ m_tickTimer.start (1000);
connect (&m_jcStatusTimer, &QTimer::timeout, this,
&MockServer::sendJcStatusMsg);
connect (&m_phStatusTimer, &QTimer::timeout, this,
&MockServer::sendPhStatusMsg);
+
+ connectAll ();
}
void
@@ -35,81 +37,102 @@ MockServer::setupPort (Port &port, const char *name, quint16 number)
port.name = name;
port.number = number;
- connect (&port.server, &QTcpServer::newConnection, this,
- &MockServer::onNewConnection);
+ connect (&port.socket, &QTcpSocket::connected, this,
+ &MockServer::onSocketConnected);
+ connect (&port.socket, &QTcpSocket::disconnected, this,
+ &MockServer::onSocketDisconnected);
+ connect (&port.socket, &QTcpSocket::readyRead, this,
+ &MockServer::onSocketMessageReady);
+ connect (&port.socket, &QAbstractSocket::errorOccurred, this,
+ [this, &port] (QAbstractSocket::SocketError)
+ {
+ qDebug ("%s Connection error: %s",
+ qPrintable (logTag (&port.socket)),
+ qPrintable (port.socket.errorString ()));
+ });
+}
- if (!port.server.listen (QHostAddress::Any, number))
- qCritical ("%s Failed to listen: %s", qPrintable (logTag (number)),
- qPrintable (port.server.errorString ()));
- else
- qInfo ("%s Listening", qPrintable (logTag (number)));
+void
+MockServer::connectAll ()
+{
+ for (auto *port : { &m_command, &m_imaging, &m_status })
+ if (port->socket.state () == QAbstractSocket::UnconnectedState)
+ port->socket.connectToHost (m_host, port->number);
}
QString
-MockServer::logTag (quint16 localPort) const
+MockServer::logTag (const QTcpSocket *socket) const
{
const char *name = "Unknown";
- if (localPort == m_command.number)
- name = m_command.name;
- else if (localPort == m_imaging.number)
- name = m_imaging.name;
- else if (localPort == m_status.number)
- name = m_status.name;
+ quint16 number = 0;
+ if (socket == &m_command.socket)
+ {
+ name = m_command.name;
+ number = m_command.number;
+ }
+ else if (socket == &m_imaging.socket)
+ {
+ name = m_imaging.name;
+ number = m_imaging.number;
+ }
+ else if (socket == &m_status.socket)
+ {
+ name = m_status.name;
+ number = m_status.number;
+ }
/* Fixed-width tag: "[Command:9110]" = 14 chars, left-padded to 15. */
- return QStringLiteral ("[%1:%2]").arg (name).arg (localPort).leftJustified (
- 15);
+ return QStringLiteral ("[%1:%2]").arg (name).arg (number).leftJustified (15);
}
void
-MockServer::onNewConnection ()
+MockServer::onSocketConnected ()
{
- auto *server = qobject_cast<QTcpServer *> (sender ());
- while (auto *sock = server->nextPendingConnection ())
- {
- quint16 lp = sock->localPort ();
- qInfo ("%s Client connected", qPrintable (logTag (lp)));
- m_clients.append (sock);
- connect (sock, &QTcpSocket::readyRead, this,
- &MockServer::onClientMessageReady);
- connect (sock, &QTcpSocket::disconnected, this,
- &MockServer::onClientDisconnected);
- }
+ auto *socket = qobject_cast<QTcpSocket *> (sender ());
+ qInfo ("%s Connected to client", qPrintable (logTag (socket)));
}
void
-MockServer::onClientMessageReady ()
+MockServer::onSocketDisconnected ()
{
- auto *sock = qobject_cast<QTcpSocket *> (sender ());
- while (sock->canReadLine ())
- {
- QByteArray line = sock->readLine ();
- handleCommand (sock, line);
- }
+ auto *socket = qobject_cast<QTcpSocket *> (sender ());
+ qInfo ("%s Disconnected from client", qPrintable (logTag (socket)));
}
void
-MockServer::onClientDisconnected ()
+MockServer::onSocketMessageReady ()
{
- auto *sock = qobject_cast<QTcpSocket *> (sender ());
- quint16 lp = sock->localPort ();
- qInfo ("%s Client disconnected", qPrintable (logTag (lp)));
- m_clients.removeOne (sock);
- sock->deleteLater ();
+ auto *socket = qobject_cast<QTcpSocket *> (sender ());
+ Port *port = nullptr;
+ if (socket == &m_command.socket)
+ port = &m_command;
+ else if (socket == &m_imaging.socket)
+ port = &m_imaging;
+ else
+ port = &m_status;
+
+ while (socket->canReadLine ())
+ {
+ QByteArray line = socket->readLine ();
+ handleCommand (*port, line);
+ }
}
void
-MockServer::sendKaPing ()
+MockServer::tick ()
{
- for (auto *client : m_clients)
+ /* Send KA_PING on connected sockets, retry connection on disconnected. */
+ for (auto *port : { &m_command, &m_imaging, &m_status })
{
- if (client->state () == QAbstractSocket::ConnectedState)
- sendReply (client, "KA_PING\n");
+ if (port->socket.state () == QAbstractSocket::ConnectedState)
+ sendReply (port->socket, "KA_PING\n");
+ else if (port->socket.state () == QAbstractSocket::UnconnectedState)
+ port->socket.connectToHost (m_host, port->number);
}
}
void
-MockServer::handleCommand (QTcpSocket *client, const QByteArray &line)
+MockServer::handleCommand (Port &port, const QByteArray &line)
{
QByteArray trimmed = line.trimmed ();
if (trimmed.isEmpty ())
@@ -121,37 +144,37 @@ MockServer::handleCommand (QTcpSocket *client, const QByteArray &line)
QByteArray params = (comma >= 0) ? trimmed.mid (comma + 1) : QByteArray ();
if (cmd == "KA_PING")
- handleKaPing (client, params);
+ handleKaPing (port, params);
else if (cmd == "GS_JC_VERSION")
- handleGsJcVersion (client);
+ handleGsJcVersion (port.socket);
else if (cmd == "GS_PH_VERSION")
- handleGsPhVersion (client, params);
+ handleGsPhVersion (port.socket, params);
/* CN_ JC success shape (no params needed) */
else if (cmd == "CN_JETTING_ALL_ON" || cmd == "CN_JETTING_ON"
|| cmd == "CN_JETTING_OFF" || cmd == "CN_JC_ID_LED_ON"
|| cmd == "CN_JC_ID_LED_OFF" || cmd == "CN_JC_CALIBRATION"
|| cmd == "CN_JC_RESET_FAULT_CODES")
- handleCnJcSuccess (client, cmd);
+ handleCnJcSuccess (port.socket, cmd);
/* CN_ PH success shape (params carry printheadId) */
else if (cmd == "CN_PH_JETTING_ON" || cmd == "CN_PH_JETTING_OFF"
|| cmd == "CN_PH_ID_LED_ON" || cmd == "CN_PH_ID_LED_OFF"
|| cmd == "CN_PH_CALIBRATION" || cmd == "CN_PH_RESET_FAULT_CODES"
|| cmd == "CN_PH_NOZZLES_DISABLED")
- handleCnPhSuccess (client, cmd, params);
+ handleCnPhSuccess (port.socket, cmd, params);
/* CN_ custom-shape commands */
else if (cmd == "CN_PH_CALIBRATION_DATA")
- handleCnPhCalibrationData (client, params);
+ handleCnPhCalibrationData (port.socket, params);
else if (cmd == "CN_PH_CALIBRATION_RAW_DATA")
- handleCnPhCalibrationRawData (client, params);
+ handleCnPhCalibrationRawData (port.socket, params);
else if (cmd == "CN_PH_CALIBRATED_BASE_FREQUENCY")
- handleCnPhCalibratedBaseFrequency (client, params);
+ handleCnPhCalibratedBaseFrequency (port.socket, params);
/* CN_ status messaging (arrives on status port) */
else if (cmd == "CN_JC_STATUS_MESSAGING_START"
|| cmd == "CN_PH_STATUS_MESSAGING_START")
- handleCnStatusMessagingStart (client, cmd, params);
+ handleCnStatusMessagingStart (port.socket, cmd, params);
else if (cmd == "CN_JC_STATUS_MESSAGING_STOP"
|| cmd == "CN_PH_STATUS_MESSAGING_STOP")
- handleCnStatusMessagingStop (client, cmd);
+ handleCnStatusMessagingStop (port.socket, cmd);
/* CF_ JC success shape */
else if (cmd == "CF_PH_DEASSIGN_ID" || cmd == "CF_JC_SAVE_CALIBRATION"
|| cmd == "CF_JC_RESET_CALIBRATION"
@@ -161,152 +184,151 @@ MockServer::handleCommand (QTcpSocket *client, const QByteArray &line)
|| cmd == "CF_JC_RESET_CONTROLLER_SOFTWARE"
|| cmd == "CF_JC_RESTART" || cmd == "CF_JC_SHUTDOWN"
|| cmd == "CF_JC_SAVE_ALL_PRINTHEAD_SETTINGS")
- handleCnJcSuccess (client, cmd);
+ handleCnJcSuccess (port.socket, cmd);
/* CF_ PH success shape */
else if (cmd == "CF_PH_SET_ID" || cmd == "CF_PH_SAVE_CALIBRATION"
|| cmd == "CF_PH_RESET_CALIBRATION"
|| cmd == "CF_PH_RESET_ALL_SETTINGS" || cmd == "CF_PH_REBOOT"
|| cmd == "CF_PH_SAVE_SETTINGS")
- handleCnPhSuccess (client, cmd, params);
+ handleCnPhSuccess (port.socket, cmd, params);
/* CF_ custom-shape commands */
else if (cmd == "CF_JC_SET_PURGE_SETTINGS")
- handleCfJcSetPurgeSettings (client, params);
+ handleCfJcSetPurgeSettings (port.socket, params);
else if (cmd == "CF_JC_SET_JETTING_PARAMS")
- handleCfJcSetJettingParams (client, params);
+ handleCfJcSetJettingParams (port.socket, params);
else if (cmd == "CF_PH_SET_JETTING_PARAMS")
- handleCfPhJettingParams (client, cmd, params);
+ handleCfPhJettingParams (port.socket, cmd, params);
else if (cmd == "CF_PH_GET_JETTING_PARAMS")
- handleCfPhJettingParams (client, cmd, params);
+ handleCfPhJettingParams (port.socket, cmd, params);
else if (cmd == "CF_JC_SETTER")
- handleCfJcSetter (client, params);
+ handleCfJcSetter (port.socket, params);
else if (cmd == "CF_PH_SETTER")
- handleCfPhSetter (client, params);
+ handleCfPhSetter (port.socket, params);
else if (cmd == "CF_JC_GETTER")
- handleCfJcGetter (client, params);
+ handleCfJcGetter (port.socket, params);
else if (cmd == "CF_PH_GETTER")
- handleCfPhGetter (client, params);
+ handleCfPhGetter (port.socket, params);
/* Imaging commands (port 2) */
else if (cmd == "m2")
- handleImagingStart (client, params);
+ handleImagingStart (port.socket, params);
else if (cmd == "m4")
- handleImagingStop (client);
+ handleImagingStop (port.socket);
else if (cmd == "m0" || cmd == "m5")
- handleImagingMaskStart (client, cmd, params);
+ handleImagingMaskStart (port.socket, cmd, params);
else if (cmd == "m1" || cmd == "m6")
- handleImagingMaskEnd (client, cmd, params);
+ handleImagingMaskEnd (port.socket, cmd, params);
else if (cmd == "m3")
- handleImageCount (client);
+ handleImageCount (port.socket);
else
- qWarning ("%s Unknown command: %s",
- qPrintable (logTag (client->localPort ())), cmd.constData ());
+ qWarning ("%s Unknown command: %s", qPrintable (logTag (&port.socket)),
+ cmd.constData ());
}
void
-MockServer::sendReply (QTcpSocket *client, const QByteArray &data)
+MockServer::sendReply (QTcpSocket &socket, const QByteArray &data)
{
- client->write (data);
+ socket.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 ());
+ qDebug ("%s TX %s%s", qPrintable (logTag (&socket)), cmd.constData (),
+ wire.constData ());
}
void
-MockServer::handleKaPing (QTcpSocket *client, const QByteArray &params)
+MockServer::handleKaPing (Port &port, const QByteArray &params)
{
QByteArray wire;
if (s_wireDebug)
wire = " << KA_PING," + params;
- qDebug ("%s RX KA_PING ACK%s", qPrintable (logTag (client->localPort ())),
+ qDebug ("%s RX KA_PING ACK%s", qPrintable (logTag (&port.socket)),
wire.constData ());
}
void
-MockServer::handleGsJcVersion (QTcpSocket *client)
+MockServer::handleGsJcVersion (QTcpSocket &socket)
{
- qDebug ("%s RX GS_JC_VERSION", qPrintable (logTag (client->localPort ())));
- sendReply (client, "GS_JC_VERSION,1,\"1.05\",\"2.00\",15\n");
+ qDebug ("%s RX GS_JC_VERSION", qPrintable (logTag (&socket)));
+ sendReply (socket, "GS_JC_VERSION,1,\"1.05\",\"2.00\",15\n");
}
void
-MockServer::handleGsPhVersion (QTcpSocket *client, const QByteArray &params)
+MockServer::handleGsPhVersion (QTcpSocket &socket, const QByteArray &params)
{
- qDebug ("%s RX GS_PH_VERSION,%s", qPrintable (logTag (client->localPort ())),
+ qDebug ("%s RX GS_PH_VERSION,%s", qPrintable (logTag (&socket)),
params.constData ());
/* Echo back canned version info for whatever printhead ID was requested. */
- sendReply (client, QByteArray ("GS_PH_VERSION,1,") + params
+ sendReply (socket, QByteArray ("GS_PH_VERSION,1,") + params
+ ",\"3.10\",\"1.00\",\"Standard\","
"\"2.05\",\"1.02\",\"0.9.1\"\n");
}
void
-MockServer::handleCnJcSuccess (QTcpSocket *client, const QByteArray &cmd)
+MockServer::handleCnJcSuccess (QTcpSocket &socket, const QByteArray &cmd)
{
- qDebug ("%s RX %s", qPrintable (logTag (client->localPort ())),
- cmd.constData ());
- sendReply (client, cmd + ",1,1\n");
+ qDebug ("%s RX %s", qPrintable (logTag (&socket)), cmd.constData ());
+ sendReply (socket, cmd + ",1,1\n");
}
void
-MockServer::handleCnPhSuccess (QTcpSocket *client, const QByteArray &cmd,
+MockServer::handleCnPhSuccess (QTcpSocket &socket, const QByteArray &cmd,
const QByteArray &params)
{
- qDebug ("%s RX %s,%s", qPrintable (logTag (client->localPort ())),
- cmd.constData (), params.constData ());
+ qDebug ("%s RX %s,%s", qPrintable (logTag (&socket)), cmd.constData (),
+ params.constData ());
/* Extract printhead ID (first field before any comma). */
int comma = params.indexOf (',');
QByteArray phId = (comma >= 0) ? params.left (comma) : params;
- sendReply (client, cmd + ",1," + phId + ",1\n");
+ sendReply (socket, cmd + ",1," + phId + ",1\n");
}
void
-MockServer::handleCnPhCalibrationData (QTcpSocket *client,
+MockServer::handleCnPhCalibrationData (QTcpSocket &socket,
const QByteArray &params)
{
- qDebug ("%s RX CN_PH_CALIBRATION_DATA,%s",
- qPrintable (logTag (client->localPort ())), params.constData ());
+ qDebug ("%s RX CN_PH_CALIBRATION_DATA,%s", qPrintable (logTag (&socket)),
+ params.constData ());
/* Reply: controllerId, phId, then 48 floats (-1 = uncalibrated). */
QByteArray reply = "CN_PH_CALIBRATION_DATA,1," + params;
for (int i = 0; i < 48; ++i)
reply += ",-1";
reply += '\n';
- sendReply (client, reply);
+ sendReply (socket, reply);
}
void
-MockServer::handleCnPhCalibrationRawData (QTcpSocket *client,
+MockServer::handleCnPhCalibrationRawData (QTcpSocket &socket,
const QByteArray &params)
{
- qDebug ("%s RX CN_PH_CALIBRATION_RAW_DATA,%s",
- qPrintable (logTag (client->localPort ())), params.constData ());
+ qDebug ("%s RX CN_PH_CALIBRATION_RAW_DATA,%s", qPrintable (logTag (&socket)),
+ params.constData ());
QByteArray reply = "CN_PH_CALIBRATION_RAW_DATA,1," + params;
for (int i = 0; i < 48; ++i)
reply += ",-1";
reply += '\n';
- sendReply (client, reply);
+ sendReply (socket, reply);
}
void
-MockServer::handleCnPhCalibratedBaseFrequency (QTcpSocket *client,
+MockServer::handleCnPhCalibratedBaseFrequency (QTcpSocket &socket,
const QByteArray &params)
{
qDebug ("%s RX CN_PH_CALIBRATED_BASE_FREQUENCY,%s",
- qPrintable (logTag (client->localPort ())), params.constData ());
- sendReply (client,
+ qPrintable (logTag (&socket)), params.constData ());
+ sendReply (socket,
"CN_PH_CALIBRATED_BASE_FREQUENCY,1," + params + ",10.0,10.0\n");
}
void
-MockServer::handleCnStatusMessagingStart (QTcpSocket *client,
+MockServer::handleCnStatusMessagingStart (QTcpSocket &socket,
const QByteArray &cmd,
const QByteArray &params)
{
- qDebug ("%s RX %s,%s", qPrintable (logTag (client->localPort ())),
- cmd.constData (), params.constData ());
+ qDebug ("%s RX %s,%s", qPrintable (logTag (&socket)), cmd.constData (),
+ params.constData ());
/* Parse level and interval from params: "level,interval" */
QList<QByteArray> parts = params.split (',');
@@ -324,89 +346,91 @@ MockServer::handleCnStatusMessagingStart (QTcpSocket *client,
m_phStatusTimer.start (interval);
}
- sendReply (client, cmd + ",1," + params + ",1\n");
+ sendReply (socket, cmd + ",1," + params + ",1\n");
}
void
-MockServer::handleCnStatusMessagingStop (QTcpSocket *client,
+MockServer::handleCnStatusMessagingStop (QTcpSocket &socket,
const QByteArray &cmd)
{
- qDebug ("%s RX %s", qPrintable (logTag (client->localPort ())),
- cmd.constData ());
+ qDebug ("%s RX %s", qPrintable (logTag (&socket)), cmd.constData ());
if (cmd == "CN_JC_STATUS_MESSAGING_STOP")
m_jcStatusTimer.stop ();
else
m_phStatusTimer.stop ();
- sendReply (client, cmd + ",1,1\n");
+ sendReply (socket, cmd + ",1,1\n");
}
void
-MockServer::handleCfJcSetPurgeSettings (QTcpSocket *client,
+MockServer::handleCfJcSetPurgeSettings (QTcpSocket &socket,
const QByteArray &params)
{
- qDebug ("%s RX CF_JC_SET_PURGE_SETTINGS,%s",
- qPrintable (logTag (client->localPort ())), params.constData ());
- sendReply (client, "CF_JC_SET_PURGE_SETTINGS,1," + params + ",1\n");
+ qDebug ("%s RX CF_JC_SET_PURGE_SETTINGS,%s", qPrintable (logTag (&socket)),
+ params.constData ());
+ sendReply (socket, "CF_JC_SET_PURGE_SETTINGS,1," + params + ",1\n");
}
void
-MockServer::handleCfJcSetJettingParams (QTcpSocket *client,
+MockServer::handleCfJcSetJettingParams (QTcpSocket &socket,
const QByteArray &params)
{
- qDebug ("%s RX CF_JC_SET_JETTING_PARAMS,%s",
- qPrintable (logTag (client->localPort ())), params.constData ());
- sendReply (client, "CF_JC_SET_JETTING_PARAMS,1," + params + ",1\n");
+ qDebug ("%s RX CF_JC_SET_JETTING_PARAMS,%s", qPrintable (logTag (&socket)),
+ params.constData ());
+ sendReply (socket, "CF_JC_SET_JETTING_PARAMS,1," + params + ",1\n");
}
void
-MockServer::handleCfPhJettingParams (QTcpSocket *client, const QByteArray &cmd,
+MockServer::handleCfPhJettingParams (QTcpSocket &socket, const QByteArray &cmd,
const QByteArray &params)
{
- qDebug ("%s RX %s,%s", qPrintable (logTag (client->localPort ())),
- cmd.constData (), params.constData ());
+ qDebug ("%s RX %s,%s", qPrintable (logTag (&socket)), cmd.constData (),
+ params.constData ());
if (cmd == "CF_PH_GET_JETTING_PARAMS")
- sendReply (client, cmd + ",1," + params + ",0,0,0,0,0,1\n");
+ sendReply (socket, cmd + ",1," + params + ",0,0,0,0,0,1\n");
else
- sendReply (client, cmd + ",1," + params + ",1\n");
+ sendReply (socket, cmd + ",1," + params + ",1\n");
}
void
-MockServer::handleCfJcSetter (QTcpSocket *client, const QByteArray &params)
+MockServer::handleCfJcSetter (QTcpSocket &socket, const QByteArray &params)
{
- qDebug ("%s RX CF_JC_SETTER,%s", qPrintable (logTag (client->localPort ())),
+ qDebug ("%s RX CF_JC_SETTER,%s", qPrintable (logTag (&socket)),
params.constData ());
- sendReply (client, "CF_JC_SETTER,1," + params + ",1\n");
+ sendReply (socket, "CF_JC_SETTER,1," + params + ",1\n");
}
void
-MockServer::handleCfPhSetter (QTcpSocket *client, const QByteArray &params)
+MockServer::handleCfPhSetter (QTcpSocket &socket, const QByteArray &params)
{
- qDebug ("%s RX CF_PH_SETTER,%s", qPrintable (logTag (client->localPort ())),
+ qDebug ("%s RX CF_PH_SETTER,%s", qPrintable (logTag (&socket)),
params.constData ());
- sendReply (client, "CF_PH_SETTER,1," + params + ",1\n");
+ sendReply (socket, "CF_PH_SETTER,1," + params + ",1\n");
}
void
-MockServer::handleCfJcGetter (QTcpSocket *client, const QByteArray &params)
+MockServer::handleCfJcGetter (QTcpSocket &socket, const QByteArray &params)
{
- qDebug ("%s RX CF_JC_GETTER,%s", qPrintable (logTag (client->localPort ())),
+ qDebug ("%s RX CF_JC_GETTER,%s", qPrintable (logTag (&socket)),
params.constData ());
- sendReply (client, "CF_JC_GETTER,1," + params + ",\"0\",1\n");
+ sendReply (socket, "CF_JC_GETTER,1," + params + ",\"0\",1\n");
}
void
-MockServer::handleCfPhGetter (QTcpSocket *client, const QByteArray &params)
+MockServer::handleCfPhGetter (QTcpSocket &socket, const QByteArray &params)
{
- qDebug ("%s RX CF_PH_GETTER,%s", qPrintable (logTag (client->localPort ())),
+ qDebug ("%s RX CF_PH_GETTER,%s", qPrintable (logTag (&socket)),
params.constData ());
- sendReply (client, "CF_PH_GETTER,1," + params + ",\"0\",1\n");
+ sendReply (socket, "CF_PH_GETTER,1," + params + ",\"0\",1\n");
}
void
MockServer::sendJcStatusMsg ()
{
+ if (m_status.socket.state () != QAbstractSocket::ConnectedState)
+ return;
+
static int tick = 0;
++tick;
bool odd = (tick % 2) != 0;
@@ -432,15 +456,15 @@ MockServer::sendJcStatusMsg ()
}
base += '\n';
- for (auto *client : m_clients)
- if (client->state () == QAbstractSocket::ConnectedState
- && client->localPort () == m_status.number)
- sendReply (client, base);
+ sendReply (m_status.socket, base);
}
void
MockServer::sendPhStatusMsg ()
{
+ if (m_status.socket.state () != QAbstractSocket::ConnectedState)
+ return;
+
static int tick = 0;
++tick;
bool odd = (tick % 2) != 0;
@@ -475,47 +499,43 @@ MockServer::sendPhStatusMsg ()
}
base += '\n';
- for (auto *client : m_clients)
- if (client->state () == QAbstractSocket::ConnectedState
- && client->localPort () == m_status.number)
- sendReply (client, base);
+ sendReply (m_status.socket, base);
}
void
-MockServer::handleImagingStart (QTcpSocket *client, const QByteArray &params)
+MockServer::handleImagingStart (QTcpSocket &socket, const QByteArray &params)
{
- qDebug ("%s RX m2,%s", qPrintable (logTag (client->localPort ())),
- params.constData ());
- sendReply (client, "n,\"A\"\n");
+ qDebug ("%s RX m2,%s", qPrintable (logTag (&socket)), params.constData ());
+ sendReply (socket, "n,\"A\"\n");
}
void
-MockServer::handleImagingStop (QTcpSocket *client)
+MockServer::handleImagingStop (QTcpSocket &socket)
{
- qDebug ("%s RX m4", qPrintable (logTag (client->localPort ())));
- sendReply (client, "m4,1\n");
+ qDebug ("%s RX m4", qPrintable (logTag (&socket)));
+ sendReply (socket, "m4,1\n");
}
void
-MockServer::handleImagingMaskStart (QTcpSocket *client, const QByteArray &cmd,
+MockServer::handleImagingMaskStart (QTcpSocket &socket, const QByteArray &cmd,
const QByteArray &params)
{
- qDebug ("%s RX %s,%s", qPrintable (logTag (client->localPort ())),
- cmd.constData (), params.constData ());
+ qDebug ("%s RX %s,%s", qPrintable (logTag (&socket)), cmd.constData (),
+ params.constData ());
}
void
-MockServer::handleImagingMaskEnd (QTcpSocket *client, const QByteArray &cmd,
+MockServer::handleImagingMaskEnd (QTcpSocket &socket, const QByteArray &cmd,
const QByteArray &params)
{
- qDebug ("%s RX %s,%s", qPrintable (logTag (client->localPort ())),
- cmd.constData (), params.constData ());
- sendReply (client, "n,\"A\"\n");
+ qDebug ("%s RX %s,%s", qPrintable (logTag (&socket)), cmd.constData (),
+ params.constData ());
+ sendReply (socket, "n,\"A\"\n");
}
void
-MockServer::handleImageCount (QTcpSocket *client)
+MockServer::handleImageCount (QTcpSocket &socket)
{
- qDebug ("%s RX m3", qPrintable (logTag (client->localPort ())));
- sendReply (client, "n,\"0\"\n");
+ qDebug ("%s RX m3", qPrintable (logTag (&socket)));
+ sendReply (socket, "n,\"0\"\n");
}