Initial commit

This commit is contained in:
torsten committed 2026-08-13 18:33:10 +02:00
commit b7d90b9afe
16245 files changed
+3396666

No files matched your search

@@ -0,0 +1,42 @@
/**
* @RDE_HEADER@
* The MessageHandler class processes messages from the client an creates appropriate replys
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#include "message_handler.h"
#include "message_transmitter.h"
#include "ws_client_message.h"
#include <memory>
namespace WarehouseGuez
{
namespace WsBiteInterface
{
namespace Messages
{
MessageHandler::MessageHandler(DataManager *dataManager)
: m_dataManager(dataManager)
, m_logger(getLogger("MessageHandler"))
{
Q_ASSERT(dataManager);
}
void MessageHandler::onNewTextMessage(MessageTransmitter *transmitter, const QString &message)
{
Q_ASSERT(transmitter);
std::unique_ptr<ClientMessage> clientMessage(ClientMessage::create(message, m_logger));
if (!clientMessage)
return;
clientMessage->process(m_dataManager, transmitter);
}
} // namespace Messages
} // namespace WsBiteInterface
} // namespace WarehouseGuez
@@ -0,0 +1,43 @@
/**
* @RDE_HEADER@
* The MessageHandler class processes messages from the client an creates appropriate replys
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#pragma once
#include <QtCore/QObject>
#include "logger.h"
namespace WarehouseGuez
{
namespace WsBiteInterface
{
class DataManager;
namespace Messages
{
class MessageTransmitter;
class MessageHandler
: public QObject
{
Q_OBJECT
public:
explicit MessageHandler(DataManager *dataManager);
public Q_SLOTS:
void onNewTextMessage(MessageTransmitter *transmitter, const QString &message);
private:
DataManager *m_dataManager;
LOGGER_T m_logger;
};
} // namespace Messages
} // namespace WsBiteInterface
} // namespace WarehouseGuez
@@ -0,0 +1,26 @@
/**
* @RDE_HEADER@
* The MessageTransmitter Interface offers an extremely lightweight API for a class to just write textmessages to
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#pragma once
#include "ws_reply_message.h"
namespace WarehouseGuez
{
namespace WsBiteInterface
{
namespace Messages
{
class MessageTransmitter
{
public:
virtual void addMessage(const ReplyMessage &message) = 0;
};
} // namespace Messages
} // namespace WsBiteInterface
} // namespace WarehouseGuez
@@ -0,0 +1,161 @@
/**
* @RDE_HEADER@
* The ClientMessage class is an abstract base class for concrete ClientMessage objects
* $Id: client_message.cpp 58958 2023-02-13 11:26:20Z fritsche_h $
* $HeadURL: svn://slssvn/TDS/branches/product/stable/guez2/Lts/Services/WarehouseGuez/WsBiteInterface/Messages/client_message.cpp $
* $Author: fritsche_h $
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#include "ws_client_message.h"
#include "ws_reply_message.h"
#include "WsBiteInterface/bite_data_manager.h"
#include "WsBiteInterface/Messages/message_transmitter.h"
#include "WsBiteInterface/json_keys.h"
#include <QtCore/QJsonDocument>
#include <QtCore/QUuid>
#include <chrono>
/*
*python testing:
import websocket
import json
import time
ws = websocket.create_connection("ws://127.0.0.1:2081")
d = { 'type' : 'getEssentials', 'uuid': 'blub' }
ws.send(json.dumps(d))
d = { 'type' : 'getConfigCNR', 'uuid': 'blub' }
ws.send(json.dumps(d))
d = { 'type' : 'getPositionsCNR', 'uuid': 'blub', 'data': {'repeat': 10}}
ws.send(json.dumps(d))
while True:
ws.recv()
*/
namespace WarehouseGuez
{
namespace WsBiteInterface
{
using namespace JsonKeys;
namespace Messages
{
// ####### ClientMessage begin #######
ClientMessage *ClientMessage::create(const QString &message, NGL::INGLLogger &logger)
{
ClientMessage *result = nullptr;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error);
if (error.error != QJsonParseError::NoError)
{
ERROR_LOG(logger) << "Got message which is not JSON conform: " << message;
return new InvalidMessage(QString(), QStringLiteral("Got message which is not JSON conform"));
}
QJsonObject obj = doc.object();
QJsonObject dataPart = obj.value(DATA()).toObject();
QString type = obj.value(TYPE()).toString();
QString uuid = obj.value(UUID_()).toString();
if (type.isEmpty())
{
ERROR_LOG(logger) << "Got message without type: " << message;
result = new InvalidMessage(uuid, QStringLiteral("Message has no type"));
}
else if (type == ERROR_() && dataPart.contains(ERROR_MESSAGE()) && dataPart.contains(ERROR_CLASS()) && dataPart.contains(UUID_()))
{
ERROR_LOG(logger) << "Got error message: " << QJsonDocument(dataPart).toJson();
return nullptr;
}
else if (uuid.isEmpty())
{
ERROR_LOG(logger) << "Got message without uuid: " << message;
result = new InvalidMessage(QString(), QStringLiteral("Got message without uuid"));
}
else if (type == GET_BITE_DATA())
result = new GetBiteDataMessage(type, uuid, dataPart);
if (result == nullptr)
{
ERROR_LOG(logger) << "Got message with invalid type: " << message;
result = new InvalidMessage(uuid, QStringLiteral("Invalid type"));
}
if (!result->hasValidData())
{
ERROR_LOG(logger) << "Got message with invalid data part: " << message;
delete result;
result = new InvalidMessage(uuid, QStringLiteral("Has invalid data part"));
}
return result;
}
ClientMessage::ClientMessage(const QString &type, const QString &uuid, const QJsonObject &data)
: m_type(type)
, m_uuid(uuid)
, m_data(data)
{
}
// ####### ClientMessage end #######
InvalidMessage::InvalidMessage(const QString &uuid, const QString &errorText)
: ClientMessage(QString(), uuid)
, m_errorText(errorText)
{}
// ####### InvalidMessage begin #######
void InvalidMessage::process(DataManager *dataManager, MessageTransmitter *transmitter)
{
Q_UNUSED(dataManager);
ErrorMessage::ErrorData errorData;
errorData.errorClass = 1; // ??????????
errorData.errorMessage = m_errorText;
errorData.uuid = m_uuid;
transmitter->addMessage(ErrorMessage(errorData));
}
bool InvalidMessage::hasValidData() const
{
// always true because there is no data part
return true;
}
// ####### InvalidMessage end #######
// ####### GetBiteDataMessage begin #######
void GetBiteDataMessage::process(DataManager *dataManager, MessageTransmitter *transmitter)
{
using namespace Messages;
int equipNo = m_data.value(EQUIP_NO()).toInt();
int boxId = m_data.value(BOX()).toInt();
dataManager->setEquipNoBoxId(equipNo, boxId, transmitter, m_uuid);
OkMessage::OkData okMsg;
okMsg.messageType = GET_BITE_DATA();
okMsg.uuid = m_uuid;
okMsg.info = QStringLiteral("Received");
OkMessage msg(okMsg);
transmitter->addMessage(msg);
}
// ####### GetBiteDataMessage end #######
} // namespace Messages
} // namespace WsBiteInterface
} // namespace WarehouseGuez
@@ -0,0 +1,71 @@
/**
* @RDE_HEADER@
* The ClientMessage class is an abstract base class for concrete ClientMessage objects
* $Id: client_message.h 58958 2023-02-13 11:26:20Z fritsche_h $
* $HeadURL: svn://slssvn/TDS/branches/product/stable/guez2/Lts/Services/WarehouseGuez/WsBiteInterface/Messages/client_message.h $
* $Author: fritsche_h $
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#pragma once
#include <QtCore/QJsonObject>
#include "logger.h"
namespace WarehouseGuez
{
namespace WsBiteInterface
{
class DataManager;
namespace Messages
{
class MessageTransmitter;
class ClientMessage
{
public:
static ClientMessage *create(const QString &message, NGL::INGLLogger &logger);
virtual void process(DataManager *dataManager, MessageTransmitter *transmitter) = 0;
virtual bool hasValidData() const = 0;
virtual ~ClientMessage() = default;
ClientMessage(const QString &type, const QString &uuid, const QJsonObject &data = QJsonObject());
protected: // attributes
const QString m_type;
const QString m_uuid;
const QJsonObject m_data;
}; // class ClientMessage
class GetBiteDataMessage final : public ClientMessage
{
public:
void process(DataManager *dataManager, MessageTransmitter *transmitter) override;
bool hasValidData() const override { return true; }
using ClientMessage::ClientMessage;
}; // class GetBiteDataMessage
class InvalidMessage final : public ClientMessage
{
friend class ClientMessage;
explicit InvalidMessage(const QString &uuid, const QString &errorText);
public:
void process(DataManager *dataManager, MessageTransmitter *transmitter) override;
bool hasValidData() const override;
using ClientMessage::ClientMessage;
private: // attributes
QString m_errorText;
}; // class InvalidMessage
} // namespace Messages
} // namespace WsBiteInterface
} // namespace WarehouseGuez
@@ -0,0 +1,79 @@
/**
* @RDE_HEADER@
* The ReplyMessage class is an abstract base class for all possible messages which are sent to the client
* $Id: reply_message.cpp 58543 2022-12-05 13:40:10Z rathje $
* $HeadURL: svn://slssvn/TDS/branches/product/stable/guez2/Lts/Services/WarehouseGuez/WsBiteInterface/Messages/reply_message.cpp $
* $Author: rathje $
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#include "ws_reply_message.h"
#include "WsBiteInterface/json_keys.h"
#include <QtCore/QUuid>
#include <QtCore/QJsonDocument>
#include <QtGlobal>
namespace WarehouseGuez
{
namespace WsBiteInterface
{
using namespace JsonKeys;
namespace Messages
{
// ####### ReplyMessage begin #######
QString ReplyMessage::toJsonString() const
{
return QString::fromUtf8(QJsonDocument(m_jsonRepresentation).toJson(QJsonDocument::Compact));
}
ReplyMessage::ReplyMessage(const QString &type, const QString &uuid)
{
m_jsonRepresentation.insert(TYPE(), type);
Q_ASSERT(!uuid.isEmpty());
m_jsonRepresentation.insert(UUID_(), uuid);
}
// ####### ReplyMessage end #######
// ####### OkMessage begin #######
OkMessage::OkMessage(const OkMessage::OkData &data)
: ReplyMessage(OK(), data.uuid)
{
m_jsonRepresentation.insert(DATA(),
QJsonObject({
{MESSAGE_TYPE(), data.messageType},
{INFO(), data.info}
}));
}
// ####### OkMessage end #######
// ####### ErrorMessage begin #######
ErrorMessage::ErrorMessage(const ErrorMessage::ErrorData &data)
: ReplyMessage(ERROR_(), data.uuid)
{
m_jsonRepresentation.insert(DATA(),
QJsonObject({
{ERROR_CLASS(), data.errorClass},
{ERROR_MESSAGE(), data.errorMessage}
}));
}
// ####### ErrorMessage end #######
// ####### BiteDataMessage begin #######
BiteDataMessage::BiteDataMessage(const QJsonObject &data, const QString &uuid)
: ReplyMessage(BITE_DATA(), uuid)
{
m_jsonRepresentation.insert(DATA(), data);
}
// ####### BiteDataMessage end #######
} // namespace Messages
} // namespace WsBiteInterface
} // namespace CombatNetRadio
@@ -0,0 +1,103 @@
/**
* @RDE_HEADER@
* The ReplyMessage class is an abstract base class for all possible messages which are sent to the client
* $Id: reply_message.h 58543 2022-12-05 13:40:10Z rathje $
* $HeadURL: svn://slssvn/TDS/branches/product/stable/guez2/Lts/Services/WarehouseGuez/WsBiteInterface/Messages/reply_message.h $
* $Author: rathje $
* Created by Hannes Fritsche, SLSE, 2017-09-07
*/
#pragma once
#include <QtCore/QVector>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
#include <QtCore/QString>
namespace WarehouseGuez
{
namespace WsBiteInterface
{
namespace Messages
{
class ReplyMessage
{
public:
ReplyMessage() = default; //!< QVector requires this; don't use directly
virtual ~ReplyMessage() = default;
virtual QString toJsonString() const;
protected:
template<typename T>
static QJsonArray vecToPath(const QVector<T> &data)
{
QJsonArray result;
for (const T &p : data)
result.append(p.toJson());
return result;
}
protected: // methods
explicit ReplyMessage(const QString &type, const QString &uuid);
protected: // attributes
QJsonObject m_jsonRepresentation;
}; // class Reply Message
class ErrorMessage final
: public ReplyMessage
{
public:
struct ErrorData
{
int errorClass = 0;
QString uuid;
QString errorMessage;
ErrorData() = default;
explicit ErrorData(int errorClass_, const QString &uuid_, const QString &errorMessage_)
: errorClass(errorClass_)
, uuid(uuid_)
, errorMessage(errorMessage_)
{}
};
explicit ErrorMessage(const ErrorData &data);
explicit ErrorMessage(int errorClass, const QString &uuid, const QString &errorMessage)
: ErrorMessage(ErrorData(errorClass, uuid, errorMessage))
{}
}; // class ErrorMessage
class OkMessage final
: public ReplyMessage
{
public:
struct OkData
{
QString uuid;
QString messageType;
QString info;
};
explicit OkMessage(const OkData &data);
}; // class OkMessage
class BiteDataMessage final
: public ReplyMessage
{
public:
explicit BiteDataMessage(const QJsonObject &data, const QString &uuid);
}; // BiteDataMessage
} // namespace Messages
} // namespace WsBiteInterface
} // namespace WarehouseGuez