|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2014-2016 Markus Prasser
|
|
|
|
|
* Copyright 2014-2018 Markus Prasser, Tobias Weiss
|
|
|
|
|
*
|
|
|
|
|
* This file is part of Labcontrol.
|
|
|
|
|
*
|
|
|
|
@ -17,11 +17,17 @@
|
|
|
|
|
* along with Labcontrol. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include "clienthelpnotificationserver.h"
|
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QNetworkConfigurationManager>
|
|
|
|
|
#include <QNetworkSession>
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
extern std::unique_ptr<lc::Settings> settings;
|
|
|
|
|
|
|
|
|
|
lc::ClientHelpNotificationServer::ClientHelpNotificationServer(QObject *argParent) :
|
|
|
|
@ -29,16 +35,18 @@ lc::ClientHelpNotificationServer::ClientHelpNotificationServer( QObject *argPare
|
|
|
|
|
hostAddress{settings->serverIP}
|
|
|
|
|
{
|
|
|
|
|
QNetworkConfigurationManager manager;
|
|
|
|
|
if ( manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired ) {
|
|
|
|
|
if (manager.capabilities()
|
|
|
|
|
& QNetworkConfigurationManager::NetworkSessionRequired) {
|
|
|
|
|
// Get saved network configuration
|
|
|
|
|
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
|
|
|
|
|
settings.beginGroup(QLatin1String{"QtNetwork"});
|
|
|
|
|
const QString id = settings.value( QLatin1String{ "DefaultNetworkConfiguration" } ).toString();
|
|
|
|
|
const QString id{settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString()};
|
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
|
|
// If the saved network configuration is not currently discovered use the system default
|
|
|
|
|
QNetworkConfiguration config = manager.configurationFromIdentifier( id );
|
|
|
|
|
if ( ( config.state() & QNetworkConfiguration::Discovered ) != QNetworkConfiguration::Discovered ) {
|
|
|
|
|
QNetworkConfiguration config{manager.configurationFromIdentifier(id)};
|
|
|
|
|
if ((config.state() & QNetworkConfiguration::Discovered)
|
|
|
|
|
!= QNetworkConfiguration::Discovered) {
|
|
|
|
|
config = manager.defaultConfiguration();
|
|
|
|
|
}
|
|
|
|
|
networkSession = new QNetworkSession{config, this};
|
|
|
|
@ -53,10 +61,11 @@ lc::ClientHelpNotificationServer::ClientHelpNotificationServer( QObject *argPare
|
|
|
|
|
this, &ClientHelpNotificationServer::SendReply);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void lc::ClientHelpNotificationServer::OpenSession() {
|
|
|
|
|
void lc::ClientHelpNotificationServer::OpenSession()
|
|
|
|
|
{
|
|
|
|
|
// Save the used configuration
|
|
|
|
|
if (networkSession) {
|
|
|
|
|
QNetworkConfiguration config = networkSession->configuration();
|
|
|
|
|
QNetworkConfiguration config{networkSession->configuration()};
|
|
|
|
|
QString id;
|
|
|
|
|
if (config.type() == QNetworkConfiguration::UserChoice) {
|
|
|
|
|
id = networkSession->sessionProperty(QLatin1String{"UserChoiceConfiguration"}).toString();
|
|
|
|
@ -64,7 +73,7 @@ void lc::ClientHelpNotificationServer::OpenSession() {
|
|
|
|
|
id = config.identifier();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSettings settings( QSettings::UserScope, QLatin1String{ "QtProject" } );
|
|
|
|
|
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
|
|
|
|
|
settings.beginGroup(QLatin1String{"QtNetwork"});
|
|
|
|
|
settings.setValue(QLatin1String{"DefaultNetworkConfiguration"}, id);
|
|
|
|
|
settings.endGroup();
|
|
|
|
@ -79,16 +88,17 @@ void lc::ClientHelpNotificationServer::OpenSession() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void lc::ClientHelpNotificationServer::SendReply() {
|
|
|
|
|
void lc::ClientHelpNotificationServer::SendReply()
|
|
|
|
|
{
|
|
|
|
|
QByteArray block;
|
|
|
|
|
QDataStream out{&block, QIODevice::WriteOnly};
|
|
|
|
|
out.setVersion(QDataStream::Qt_5_2);
|
|
|
|
|
out << ( quint16 )0;
|
|
|
|
|
out << static_cast<quint16>(0);
|
|
|
|
|
out << QString{"Help demand retrieved."};
|
|
|
|
|
out.device()->seek(0);
|
|
|
|
|
out << ( quint16 )( block.size() - sizeof( quint16 ) );
|
|
|
|
|
out << static_cast<quint16>(block.size() - sizeof(quint16));
|
|
|
|
|
|
|
|
|
|
QTcpSocket *clientConnection = helpMessageServer->nextPendingConnection();
|
|
|
|
|
auto clientConnection = helpMessageServer->nextPendingConnection();
|
|
|
|
|
QString peerAddress = clientConnection->peerAddress().toString();
|
|
|
|
|
QString peerName;
|
|
|
|
|
bool unknownClient = false;
|
|
|
|
@ -98,17 +108,23 @@ void lc::ClientHelpNotificationServer::SendReply() {
|
|
|
|
|
unknownClient = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect( clientConnection, &QTcpSocket::disconnected, clientConnection, &QTcpSocket::deleteLater );
|
|
|
|
|
connect(clientConnection, &QTcpSocket::disconnected,
|
|
|
|
|
clientConnection, &QTcpSocket::deleteLater);
|
|
|
|
|
clientConnection->write(block);
|
|
|
|
|
clientConnection->disconnectFromHost();
|
|
|
|
|
|
|
|
|
|
if (unknownClient) {
|
|
|
|
|
QMessageBox requestReceivedBox{ QMessageBox::Information, tr( "Unknown client asked for help."),
|
|
|
|
|
tr( "An unknown client with IP '%1' asked for help.").arg( peerAddress ), QMessageBox::Ok };
|
|
|
|
|
QMessageBox requestReceivedBox{QMessageBox::Information,
|
|
|
|
|
tr("Unknown client asked for help."),
|
|
|
|
|
tr("An unknown client with IP '%1' asked"
|
|
|
|
|
" for help.").arg(peerAddress),
|
|
|
|
|
QMessageBox::Ok};
|
|
|
|
|
requestReceivedBox.exec();
|
|
|
|
|
} else {
|
|
|
|
|
QMessageBox requestReceivedBox{ QMessageBox::Information, tr( "'%1' asked for help.").arg( peerName ),
|
|
|
|
|
tr( "'%1' asked for help.").arg( peerName ), QMessageBox::Ok };
|
|
|
|
|
QMessageBox requestReceivedBox{QMessageBox::Information,
|
|
|
|
|
tr("'%1' asked for help.").arg( peerName ),
|
|
|
|
|
tr("'%1' asked for help.").arg(peerName),
|
|
|
|
|
QMessageBox::Ok};
|
|
|
|
|
requestReceivedBox.exec();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|