AusweisApp2/src/websocket/UIPlugInWebSocket.cpp

160 lines
3.5 KiB
C++
Raw Normal View History

2017-07-03 09:30:10 +02:00
/*!
2018-03-28 15:10:51 +02:00
* \copyright Copyright (c) 2016-2018 Governikus GmbH & Co. KG, Germany
2017-07-03 09:30:10 +02:00
*/
#include "UIPlugInWebSocket.h"
#include "view/UILoader.h"
#include <QCoreApplication>
2017-07-03 09:33:28 +02:00
#include <QFile>
2017-07-03 09:30:10 +02:00
#include <QLoggingCategory>
#include <QPluginLoader>
Q_DECLARE_LOGGING_CATEGORY(websocket)
using namespace governikus;
2017-07-03 09:33:28 +02:00
quint16 UIPlugInWebSocket::cWebSocketPort = UIPlugInWebSocket::WEBSOCKET_DEFAULT_PORT;
2017-07-03 09:30:10 +02:00
UIPlugInWebSocket::UIPlugInWebSocket()
: UIPlugIn()
, mServer(QCoreApplication::applicationName() + QLatin1Char('/') + QCoreApplication::applicationVersion(), QWebSocketServer::NonSecureMode)
, mConnection(nullptr)
, mJsonApi(nullptr)
2017-07-03 09:33:28 +02:00
, mContext()
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
if (!UILoader::getInstance().load(UIPlugInName::UIPlugInJsonApi))
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
qWarning(websocket) << "Cannot start WebSocket because JSON-API is missing";
return;
2017-07-03 09:30:10 +02:00
}
2017-07-03 09:33:28 +02:00
mJsonApi = qobject_cast<UIPlugInJsonApi*>(UILoader::getInstance().getLoaded(UIPlugInName::UIPlugInJsonApi));
Q_ASSERT(mJsonApi);
connect(&mServer, &QWebSocketServer::newConnection, this, &UIPlugInWebSocket::onNewConnection);
qDebug(websocket) << "Starting WebSocket..." << mServer.listen(QHostAddress::LocalHost, cWebSocketPort);
if (!mServer.isListening())
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
qCritical(websocket) << mServer.errorString();
return;
2017-07-03 09:30:10 +02:00
}
2017-07-03 09:33:28 +02:00
const quint16 port = mServer.serverPort();
qDebug(websocket) << "Listening on port" << port;
#ifndef QT_NO_DEBUG
if (cWebSocketPort == 0)
{
const QString path = WEBSOCKET_PORT_FILENAME(QCoreApplication::applicationPid());
QFile file(path);
if (file.open(QIODevice::WriteOnly))
{
QTextStream(&file) << port;
qDebug(websocket) << "Stored port number to info file" << path;
}
else
{
qCritical(websocket) << "Failed to store port number to info file" << path;
}
}
#endif
2017-07-03 09:30:10 +02:00
}
UIPlugInWebSocket::~UIPlugInWebSocket()
{
}
2017-07-03 09:33:28 +02:00
void UIPlugInWebSocket::setPort(quint16 pPort)
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
cWebSocketPort = pPort;
2017-07-03 09:30:10 +02:00
}
2017-07-03 09:33:28 +02:00
quint16 UIPlugInWebSocket::getPort()
2017-07-03 09:30:10 +02:00
{
2017-07-03 09:33:28 +02:00
return cWebSocketPort;
2017-07-03 09:30:10 +02:00
}
void UIPlugInWebSocket::onWorkflowStarted(QSharedPointer<WorkflowContext> pContext)
{
2017-07-03 09:33:28 +02:00
mContext = pContext;
2017-07-03 09:30:10 +02:00
}
void UIPlugInWebSocket::onWorkflowFinished(QSharedPointer<WorkflowContext> pContext)
{
2017-07-03 09:33:28 +02:00
Q_UNUSED(pContext);
mContext.clear();
2017-07-03 09:30:10 +02:00
}
void UIPlugInWebSocket::onNewConnection()
{
if (mServer.hasPendingConnections())
{
auto connection = mServer.nextPendingConnection();
if (mConnection)
{
qDebug(websocket) << "Client is already connected...";
connection->close(QWebSocketProtocol::CloseCodePolicyViolated);
return;
}
mConnection.reset(connection);
connect(mConnection.data(), &QWebSocket::disconnected, this, &UIPlugInWebSocket::onClientDisconnected);
connect(mConnection.data(), &QWebSocket::textMessageReceived, this, &UIPlugInWebSocket::onTextMessageReceived);
connect(mJsonApi, &UIPlugInJsonApi::fireMessage, this, &UIPlugInWebSocket::onJsonApiMessage);
}
}
void UIPlugInWebSocket::onClientDisconnected()
{
qDebug(websocket) << "Client disconnected...";
2017-07-03 09:33:28 +02:00
if (mContext)
{
const QSignalBlocker blocker(mJsonApi);
Q_EMIT mContext->fireCancelWorkflow();
}
2017-07-03 09:30:10 +02:00
mConnection.reset();
disconnect(mJsonApi, &UIPlugInJsonApi::fireMessage, this, &UIPlugInWebSocket::onJsonApiMessage);
}
void UIPlugInWebSocket::onTextMessageReceived(const QString& pMessage)
{
if (mConnection)
{
mJsonApi->doMessageProcessing(pMessage.toUtf8());
}
}
void UIPlugInWebSocket::onJsonApiMessage(const QByteArray& pMessage)
{
if (mConnection)
{
mConnection->sendTextMessage(QString::fromUtf8(pMessage));
}
}
void UIPlugInWebSocket::doShutdown()
{
if (mConnection)
{
mConnection->close(QWebSocketProtocol::CloseCodeGoingAway);
}
}