Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Proxies/HighLevelArchitecture/Guez/web_socket_handler.cpp
T
torstenandCopilot b774f4035c Qt6 Migration: DDS-Stubs, Logger-Fixes, Export-Header Korrekturen
- NGL_ADD_LIBRARY2: 87 CMakeLists.txt konvertiert für Export-Header Generierung
- 44 Export-Header Include-Pfade korrigiert (_export.h)
- 81 DDS-Stub-Header generiert (*_dtc.h, *_fts.h, *_dts.h, *_ftc.h)
- INGLLogger: Duplikat gelöscht, m_logger Member entfernt, variadische Makros
- Q_FUNC_INFO Redefinition behoben (Qt6 definiert bereits)
- FATAL_LOG Makro hinzugefügt
- InfrastructureManagement DDS-Stubs (ism.hpp, ism_enums.hpp, ism_ftc.h)
- FilterManagement DDS-Stubs (flt_ftc.h, flt_dts.h)
- AreaEffectSimulation DDS-Stubs (aes.hpp, aes_enums.hpp)
- SymbolAppearance Klasse vollständig definiert mit content() Methode
- BuildingComponentFilter Klasse definiert mit Copy-Konstruktor
- Ftc Factory: nullptr Return (abstract class)
- AesFilter Klasse in aes.hpp hinzugefügt
- Dtc Klasse in ppm.hpp mit participantChanged Signal
- AesType Enum: alle 15 At* Werte definiert
- AesEffectState_def: EsReceived hinzugefügt
- DataModification Enum: eAdded/eModified/eRemoved
- function_reply_object.h → core_types.h Include-Pfad (72 Dateien)
- Enum-Class-Streaming Template-Operator in INGLLogger.h
- ism_mounting_point.h: QImage → QtGui/QImage, QGuiApplication/QScreen
- ism_data_plugin_interface.h: QSet Include, Include-Reihenfolge
- QRegExp → QRegularExpression in tpm_js_parser.cpp
- .underlying() Aufrufe zu direktem Enum-Vergleich korrigiert
- Include-Pfade: aes.hpp in symbol_appearance_settings_impl.h und aes_display_filter_impl.h

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-19 06:06:24 +02:00

257 lines
6.6 KiB
C++
Executable File

#include "web_socket_handler.h"
#include <utility>
#include "main_handler.h"
#include "ngs_handler.h"
#include "ppm_handler.h"
#include <Common/settings.h>
#include <QJsonDocument>
#include <QWebSocket>
namespace HighLevelArchitecture
{
WebSocketHandler::WebSocketHandler(int exerciseId, MainHandler *mainHandler, QObject *parent)
: QObject(parent)
, m_webSocketServer(new QWebSocketServer(QStringLiteral("WLS"), QWebSocketServer::NonSecureMode, this))
, m_mainHandler(mainHandler)
, m_exerciseId(exerciseId)
{}
WebSocketHandler::~WebSocketHandler()
{
for (const auto &s : std::as_const(m_webSockets))
s->disconnect(this);
}
bool WebSocketHandler::init(quint16 listenPort)
{
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
{
WARNING_LOG() << "Can not listen on port " << listenPort << " - aborting";
return false;
}
connect(m_webSocketServer, &QWebSocketServer::newConnection,
this, &WebSocketHandler::onNewConnection);
return true;
}
bool WebSocketHandler::sendTelegramInternal(const TrainingAreaBase::TetraIcd::TrainingAreaTelegram *data,
const QString &type, int exerciseId)
{
QJsonObject obj;
obj[QStringLiteral("data")] = data->toJson();
obj[QStringLiteral("type")] = type;
obj[QStringLiteral("typeId")] = data->id();
const QString jsonStr = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact));
DEBUG_LOG() << "jsonStr: " << jsonStr;
bool bOk = false;
for (const auto &wsw : std::as_const(m_webSockets))
{
if (wsw->sendTextMessage(jsonStr, exerciseId))
bOk = true;
}
return bOk;
}
void WebSocketHandler::sendHlaConnectionState()
{
TrainingAreaBase::TetraIcd::ProxyConnectionState connState;
connState.connectedToSystem = m_mainHandler->isConnectedToHla();
sendTelegram(&connState, m_exerciseId);
DEBUG_LOG() << "sendHlaConnectionState, isConnected: " << connState.connectedToSystem;
}
void WebSocketHandler::sendParticipantConnectionStates()
{
DEBUG_LOG() << "sendParticipantConnectionStates";
TrainingAreaBase::TetraIcd::WlsParticipantsConnectionState partConnState;
partConnState.equipmentNumber = -1;
partConnState.dataCommunicationPossible = false;
sendTelegram(&partConnState, m_exerciseId);
if (!m_mainHandler->isConnectedToHla())
{
INFO_LOG() << "We are (not) yet connected to HLA - don't send participant connection states";
return;
}
const auto externalEquipmentNumbers = m_mainHandler->ngsHandler()->ppmHandler()->externalEquipmentNumbers();
for (int equipNo : externalEquipmentNumbers)
{
partConnState.equipmentNumber = equipNo;
partConnState.dataCommunicationPossible = true;
sendTelegram(&partConnState, m_exerciseId);
}
}
void WebSocketHandler::onExternalEquipmentNumberAdded(int equipNo)
{
sendDataCommunicationState(equipNo, true);
}
void WebSocketHandler::onExternalEquipmentNumberRemoved(int equipNo)
{
sendDataCommunicationState(equipNo, false);
}
void WebSocketHandler::sendDataCommunicationState(int equipNo, bool bIsAvailable)
{
TrainingAreaBase::TetraIcd::WlsParticipantsConnectionState tele;
tele.equipmentNumber = equipNo;
tele.dataCommunicationPossible = bIsAvailable;
sendTelegram(&tele, m_exerciseId);
}
void WebSocketHandler::onNewConnection()
{
QWebSocket *ws = m_webSocketServer->nextPendingConnection();
if (!ws)
return;
DEBUG_LOG() << "New websocket connection from " << ws->peerAddress().toString() << ":" << ws->peerPort();
auto wsw = std::make_shared<WebSocketWrapper>(ws);
connect(wsw.get(), &WebSocketWrapper::textMessageReceived,
this, &WebSocketHandler::onTextMessageReceived);
connect(ws, &QWebSocket::disconnected,
this, &WebSocketHandler::onWebSocketDisconnected);
m_webSockets += wsw;
}
void WebSocketHandler::onTextMessageReceived(const QByteArray &baJson)
{
using namespace TrainingAreaBase::TetraIcd;
TRACE_LOG() << "WebSocketHandler::onTextMessageReceived: " << baJson;
WebSocketWrapper *wsw = qobject_cast<WebSocketWrapper *>(sender());
if (!wsw)
{
ERROR_LOG() << "Internal error - sender is no WebSocketWrapper";
return;
}
if (baJson.isEmpty())
return;
const QJsonObject obj = QJsonDocument::fromJson(baJson).object();
const QJsonValue typeId = obj.value(QStringLiteral("typeId"));
if (!typeId.isDouble())
{
ERROR_LOG() << "Can not read type id: " << baJson;
return;
}
const QJsonObject data = obj.value(QStringLiteral("data")).toObject();
try
{
switch (typeId.toInt())
{
case Constraints<ExerciseStateData>::id():
wsw->setExerciseId(ExerciseStateData::fromJson(data).exerciseId);
if (m_exerciseId == wsw->exerciseId())
{
sendParticipantConnectionStates();
sendHlaConnectionState();
}
break;
case Constraints<ExerciseEquipmentAssignmentComplete>::id():
break;
case Constraints<Init>::id():
emit initRequest(Init::fromJson(data));
break;
case Constraints<DemandBite>::id():
emit biteRequest(DemandBite::fromJson(data));
break;
case Constraints<DemandVulnerabilityStateChange>::id():
emit vulnerabilityStateRequest(DemandVulnerabilityStateChange::fromJson(data));
break;
default:
WARNING_LOG() << "Handler for telegram Id " << typeId.toInt()
<< " not yet implemented (" << baJson << ")";
return;
}
}
catch (const std::exception &e)
{
WARNING_LOG() << "Could not parse json: " << e.what() << " (" << baJson << ")";
return;
}
}
void WebSocketHandler::onWebSocketDisconnected()
{
QWebSocket *ws = qobject_cast<QWebSocket *>(sender());
if (!ws)
return;
for (auto it = m_webSockets.begin(); it != m_webSockets.end(); ++it)
{
auto wsw = *it;
if (wsw->webSocket() == ws)
{
m_webSockets.erase(it);
break;
}
}
}
WebSocketWrapper::WebSocketWrapper(QWebSocket *ws)
: m_webSocket(ws)
, m_exerciseId(-1)
{
connect(m_webSocket, &QWebSocket::textMessageReceived,
this, &WebSocketWrapper::onTextMessageReceived);
}
WebSocketWrapper::~WebSocketWrapper()
{
if (m_webSocket)
{
m_webSocket->disconnect(this);
m_webSocket->close();
}
delete m_webSocket;
}
bool WebSocketWrapper::sendTextMessage(const QString &msg, int exerciseId)
{
if (m_exerciseId != exerciseId && exerciseId != 0 && m_exerciseId != -1)
return false;
m_webSocket->sendTextMessage(msg);
return true;
}
void WebSocketWrapper::setExerciseId(int exerciseId)
{
m_exerciseId = exerciseId;
}
void WebSocketWrapper::onTextMessageReceived(const QString &data)
{
emit textMessageReceived(data.toUtf8());
}
}