Reformat ClientHelpNotificationServer

remotes/origin/HEAD
markuspg 8 years ago
parent e533ede28b
commit d839f7f227

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 Markus Prasser * Copyright 2014-2018 Markus Prasser, Tobias Weiss
* *
* This file is part of Labcontrol. * This file is part of Labcontrol.
* *
@ -20,24 +20,20 @@
#ifndef CLIENTHELPNOTIFICATIONSERVER_H #ifndef CLIENTHELPNOTIFICATIONSERVER_H
#define CLIENTHELPNOTIFICATIONSERVER_H #define CLIENTHELPNOTIFICATIONSERVER_H
#include "src/Lib/client.h"
#include <QObject>
#include <QHostAddress> #include <QHostAddress>
#include <QMessageBox>
#include <QtNetwork> class QNetworkSession;
class QTcpServer;
namespace lc { namespace lc {
class ClientHelpNotificationServer : public QObject { class ClientHelpNotificationServer : public QObject
{
Q_OBJECT Q_OBJECT
public: public:
explicit ClientHelpNotificationServer(QObject *argParent = nullptr); explicit ClientHelpNotificationServer(QObject *argParent = nullptr);
signals:
public slots:
private: private:
QTcpServer *helpMessageServer = nullptr; QTcpServer *helpMessageServer = nullptr;
const QHostAddress hostAddress; const QHostAddress hostAddress;
@ -48,6 +44,6 @@ private slots:
void SendReply(); void SendReply();
}; };
} } // namespace lc
#endif // CLIENTHELPNOTIFICATIONSERVER_H #endif // CLIENTHELPNOTIFICATIONSERVER_H

Loading…
Cancel
Save