Files
qt5-legatus-legacy/Lts/Common/LtsWebSocket/client_message_factory.h
T
2026-08-13 18:33:10 +02:00

86 lines
2.3 KiB
C++
Executable File

/**
* @RDE_HEADER@
* LTS web socket server - Abstract Client Message Factory
* $Id$
* $HeadURL$
* $Author$
* Created by Philipp Dassow, SLST, 2017-11-28
*/
#pragma once
#include "client_message.h"
#include "reply_message.h"
#include <LtsWebSocket/ltswebsocketcommon_export.h>
#include <QString>
#include <QJsonParseError>
namespace NGL
{
class INGLLogger;
}
namespace LtsWebSocket
{
/**
* @brief API for the client message factory that has to be provided to the LtsWebSocketServer
* */
class LTSWEBSOCKETCOMMON_EXPORT AbstractClientMessageFactory
{
public:
explicit AbstractClientMessageFactory(NGL::INGLLogger &logger)
: m_logger(logger)
{}
virtual ~AbstractClientMessageFactory() {}
/**
* @brief parseMessage parses the message for TYPE, UUID and DATA
* @param message to be parsed
* @return QJsonParseError
*/
QJsonParseError parseMessage(const QString &message);
/**
* @brief createMessage Creates the ClientMessage which will process the message
* Since the message is not created in the main thread, nothing should be done here!
* @param client Handler for replys
* @return new client message processor
*/
virtual AbstractClientMessage *createMessage(ReplyHandler *client, const QString &message) = 0;
protected:
AbstractClientMessage *checkAttributes(ReplyHandler *client,
const QString &message) const;
AbstractClientMessage *checkResult(ReplyHandler *client,
const QString &message,
AbstractClientMessage *result) const;
protected: // attributes of currently processed message
QString m_type;
int m_typeId;
QString m_uuid;
QJsonValue m_data;
NGL::INGLLogger &m_logger;
}; // class AbstractClientMessageFactory
class PongMessage final : public ReplyMessage
{
public:
static QString strPong() { return QStringLiteral("pong"); }
static int idPong() { return 2; }
explicit PongMessage(const QString &payload)
: ReplyMessage(strPong(), idPong(), QString())
, m_payload(payload)
{}
QString toJsonString() const override
{
return m_payload.isEmpty() ? strPong() : QStringLiteral("%1 %2").arg(strPong(), m_payload);
}
private:
QString m_payload;
};
} // namespace LtsWebSocket