/* * Copyright 2014-2016 Markus Prasser * * This file is part of Labcontrol. * * Labcontrol is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Labcontrol is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Labcontrol. If not, see . */ #include "clienthelpnotificationserver.h" lc::ClientHelpNotificationServer::ClientHelpNotificationServer( const QMap< QString, Client* > * const argClientIPsToClientsMap, const QString &argServerIP, const unsigned short int &argServerPort, QObject *argParent ) : QObject{ argParent }, clientIPsToClientsMap{ argClientIPsToClientsMap }, hostAddress{ argServerIP }, serverPort{ argServerPort } { QNetworkConfigurationManager manager; 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(); 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 ) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession{ config, this }; connect( networkSession, &QNetworkSession::opened, this, &ClientHelpNotificationServer::OpenSession ); networkSession->open(); } else { OpenSession(); } connect( helpMessageServer, &QTcpServer::newConnection, this, &ClientHelpNotificationServer::SendReply ); } void lc::ClientHelpNotificationServer::OpenSession() { // Save the used configuration if ( networkSession ) { QNetworkConfiguration config = networkSession->configuration(); QString id; if ( config.type() == QNetworkConfiguration::UserChoice ) { id = networkSession->sessionProperty( QLatin1String{ "UserChoiceConfiguration" } ).toString(); } else { id = config.identifier(); } QSettings settings( QSettings::UserScope, QLatin1String{ "QtProject" } ); settings.beginGroup( QLatin1String{ "QtNetwork" } ); settings.setValue( QLatin1String{ "DefaultNetworkConfiguration" }, id ); settings.endGroup(); } helpMessageServer = new QTcpServer{ this }; if ( !helpMessageServer->listen( hostAddress, serverPort ) ) { QMessageBox messageBox{ QMessageBox::Critical, tr( "Unable to start the client help notification server" ), tr( "Unable to start the client help notification server.\nThe following error occurred:\n\n%1." ).arg( helpMessageServer->errorString() ), QMessageBox::Ok }; messageBox.exec(); return; } } void lc::ClientHelpNotificationServer::SendReply() { QByteArray block; QDataStream out{ &block, QIODevice::WriteOnly }; out.setVersion( QDataStream::Qt_5_2 ); out << ( quint16 )0; out << QString{ "Help demand retrieved." }; out.device()->seek( 0 ); out << ( quint16 )( block.size() - sizeof( quint16 ) ); QTcpSocket *clientConnection = helpMessageServer->nextPendingConnection(); QString peerAddress = clientConnection->peerAddress().toString(); QString peerName; bool unknownClient = false; if ( clientIPsToClientsMap->contains( peerAddress ) ) { peerName = ( *clientIPsToClientsMap )[ peerAddress ]->name; } else { unknownClient = true; } 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 }; requestReceivedBox.exec(); } else { QMessageBox requestReceivedBox{ QMessageBox::Information, tr( "'%1' asked for help.").arg( peerName ), tr( "'%1' asked for help.").arg( peerName ), QMessageBox::Ok }; requestReceivedBox.exec(); } }