Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Proxies/TrainingAreaBase/Plugins/AdlerJson/ws_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

240 lines
6.0 KiB
C++
Executable File

#include "ws_handler.h"
#include <QWebSocket>
#include <QJsonDocument>
namespace TrainingAreaBase {
WebSocketHandler::WebSocketHandler(QObject *parent)
: WebSocketInterface(parent)
, m_webSocketServer(new QWebSocketServer(QStringLiteral("WebsocketServer_SAA"), QWebSocketServer::NonSecureMode,this))
, m_logger(AdlerJson::getLogger("WebsocketHandler"))
{}
WebSocketHandler::~WebSocketHandler(){}
bool WebSocketHandler::init(quint16 listenPort)
{
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
{
return false;
}
connect(m_webSocketServer, &QWebSocketServer::newConnection,
this, &WebSocketHandler::onNewConnection);
return true;
}
bool WebSocketHandler::isConnected() const
{
return !m_connections.isEmpty();
}
quint16 WebSocketHandler::getPort() const
{
return m_webSocketServer->serverPort();
}
bool WebSocketHandler::sendTelegram(int typeId, const QJsonObject *data, int exerciseId)
{
Q_UNUSED(exerciseId);
QJsonObject obj;
obj[QStringLiteral("typeId")] = typeId;
obj[QStringLiteral("data")] = *data;
const QString jsonStr = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact));
bool bOk = false;
return bOk;
}
bool WebSocketHandler::sendTelegram(const int wsId, const QJsonObject *data)
{
DEBUG_LOG() << "Send Telegram esId=" << wsId
<< ", m_connections.size=" << m_connections.size();
for (auto it = m_connections.begin(); it != m_connections.end(); it++)
DEBUG_LOG() << "key: " << it.key();
const QString jsonStr = QString::fromUtf8(QJsonDocument(*data).toJson(QJsonDocument::Compact));
if(!m_connections.contains(wsId))
{
DEBUG_LOG() << "!m_connections.contains(" << wsId << ") return false;";
return false;
}
std::shared_ptr<WebSocketWrapper> ws = m_connections.value(wsId);
DEBUG_LOG() << "Send Telegram to SAA: " << jsonStr<< " \n LocalPort" << ws->webSocket()->localAddress().toString()
<< "." << ws->webSocket()->localPort()
<< " PeerPort " << ws->webSocket()->peerAddress().toString()
<< ":" <<ws->webSocket()->peerPort();
DEBUG_LOG() << "VERSION " << ws->webSocket()->version();
return ws->sendTextMessage(jsonStr);
}
bool WebSocketHandler::disconnectWs(int wsId)
{
if(!m_connections.contains(wsId))
{
DEBUG_LOG() << "!m_connections.contains(" << wsId << ") return false;";
return false;
}
std::shared_ptr<WebSocketWrapper> ws = m_connections.value(wsId);
ws->webSocket()->close(QWebSocketProtocol::CloseCodeGoingAway, QStringLiteral("Timeout"));
m_connections.remove(wsId);
return true;
}
QString WebSocketHandler::getPeerAddress(int wsId) const
{
if (!m_connections.contains(wsId))
return QString();
const auto ws = m_connections[wsId];
QHostAddress address = ws->webSocket()->peerAddress();
bool ok = false;
quint32 ipv4 = address.toIPv4Address(&ok);
if (ok)
address = QHostAddress(ipv4);
return address.toString();
}
QUrl WebSocketHandler::getPeerUrl(int wsId) const
{
if (!m_connections.contains(wsId))
return {};
const auto ws = m_connections[wsId];
return ws->webSocket()->requestUrl();
}
int WebSocketHandler::getExerciseId(int wsId) const
{
if (!m_connections.contains(wsId))
return -1;
const auto ws = m_connections[wsId];
return ws->exerciseId();
}
void WebSocketHandler::onTextMessageReceived(const QByteArray &baJson)
{
DEBUG_LOG() << "Receive Telegram from SAA <<<<< " << baJson;
using namespace TrainingAreaBase::TetraIcd;
WebSocketWrapper *wsw = qobject_cast<WebSocketWrapper *>(sender());
int wsId = getWSIdFromWS(wsw);
if (!wsw)
return;
if (baJson.isEmpty())
return;
const QJsonObject obj = QJsonDocument::fromJson(baJson).object();
emit recivedTelegram(wsId, obj);
}
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);
int wsId = m_nextWsId;
connect(wsw.get(), &WebSocketWrapper::textMessageReceived,
this, &WebSocketHandler::onTextMessageReceived);
connect(ws, &QWebSocket::disconnected,
this, &WebSocketHandler::onWebSocketDisconnected);
wsw->setWsId(wsId);
emit newConnection( wsId, ws->peerAddress().toString(),ws->peerPort());
m_connections.insert(wsId ,wsw);
DEBUG_LOG() << "m_connections.insert"
<< ", wsId :" << wsId;
m_nextWsId++;
}
int WebSocketHandler::getWSIdFromWS( WebSocketWrapper* wsw ){
for(auto pair : m_connections)
{
if(pair.get() == wsw){return pair->getWsId();}
}
return -1;
}
void WebSocketHandler::onWebSocketDisconnected()
{
QWebSocket *ws = qobject_cast<QWebSocket *>(sender());
if (!ws)
return;
auto copy = m_connections;
for (auto it = copy.begin(); it != copy.end(); ++it)
{
auto wsw = *it;
DEBUG_LOG() << "peerPort :" << wsw->webSocket()->peerPort() << ", localPort :" << wsw->webSocket()->localPort();
if (wsw->webSocket() == ws)
{
DEBUG_LOG() << it.key() << " Emit Disconnect ------------------------------------- esId=" << it.key();
emit wsDisconnected(it.key());
m_connections.remove(it.key());
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)
{
m_webSocket->sendTextMessage(msg);
return true;
}
void WebSocketWrapper::setExerciseId(int exerciseId)
{
m_exerciseId = exerciseId;
}
void WebSocketWrapper::onTextMessageReceived(const QString &data)
{
emit textMessageReceived(data.toUtf8());
}
}