Revise ClientHelpNotificationServer

remotes/origin/HEAD
markuspg 6 years ago
parent 10831bfe50
commit c212472e74

@ -17,13 +17,21 @@
* 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 <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QTcpServer>
#include <QTcpSocket>
extern std::unique_ptr<lc::Settings> settings; extern std::unique_ptr<lc::Settings> settings;
/*!
* \brief Create a new instance and start listening as soon as possible
*
* \param argParent The instance's parent QObject
*/
lc::ClientHelpNotificationServer::ClientHelpNotificationServer( lc::ClientHelpNotificationServer::ClientHelpNotificationServer(
QObject *argParent) QObject *argParent)
: QObject{argParent}, hostAddress{settings->serverIP} { : QObject{argParent}, hostAddress{settings->serverIP} {
@ -31,11 +39,12 @@ lc::ClientHelpNotificationServer::ClientHelpNotificationServer(
if (manager.capabilities() & if (manager.capabilities() &
QNetworkConfigurationManager::NetworkSessionRequired) { QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration // Get saved network configuration
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}}; QSettings qSettings{QSettings::UserScope, QLatin1String{"QtProject"}};
settings.beginGroup(QLatin1String{"QtNetwork"}); qSettings.beginGroup(QLatin1String{"QtNetwork"});
const QString id = const QString id =
settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString(); qSettings.value(QLatin1String{"DefaultNetworkConfiguration"})
settings.endGroup(); .toString();
qSettings.endGroup();
// If the saved network configuration is not currently discovered use the // If the saved network configuration is not currently discovered use the
// system default // system default
@ -56,6 +65,10 @@ lc::ClientHelpNotificationServer::ClientHelpNotificationServer(
&ClientHelpNotificationServer::SendReply); &ClientHelpNotificationServer::SendReply);
} }
/*!
* \brief Open a new network session and start listening for incoming
* connections
*/
void lc::ClientHelpNotificationServer::OpenSession() { void lc::ClientHelpNotificationServer::OpenSession() {
// Save the used configuration // Save the used configuration
if (networkSession) { if (networkSession) {
@ -69,14 +82,14 @@ void lc::ClientHelpNotificationServer::OpenSession() {
id = config.identifier(); id = config.identifier();
} }
QSettings settings(QSettings::UserScope, QLatin1String{"QtProject"}); QSettings qSettings(QSettings::UserScope, QLatin1String{"QtProject"});
settings.beginGroup(QLatin1String{"QtNetwork"}); qSettings.beginGroup(QLatin1String{"QtNetwork"});
settings.setValue(QLatin1String{"DefaultNetworkConfiguration"}, id); qSettings.setValue(QLatin1String{"DefaultNetworkConfiguration"}, id);
settings.endGroup(); qSettings.endGroup();
} }
helpMessageServer = new QTcpServer{this}; helpMessageServer = new QTcpServer{this};
if (!helpMessageServer->listen(hostAddress, if (!helpMessageServer->listen(QHostAddress{hostAddress},
settings->clientHelpNotificationServerPort)) { settings->clientHelpNotificationServerPort)) {
QMessageBox messageBox{ QMessageBox messageBox{
QMessageBox::Critical, QMessageBox::Critical,
@ -94,19 +107,17 @@ 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>(static_cast<unsigned int>(block.size()) -
sizeof(quint16));
QTcpSocket *clientConnection = helpMessageServer->nextPendingConnection(); const auto clientConnection = helpMessageServer->nextPendingConnection();
QString peerAddress = clientConnection->peerAddress().toString(); const auto peerAddress = clientConnection->peerAddress().toString();
QString peerName; QString peerName;
bool unknownClient = false;
if (settings->clIPsToClMap.contains(peerAddress)) { if (settings->clIPsToClMap.contains(peerAddress)) {
peerName = settings->clIPsToClMap[peerAddress]->name; peerName = settings->clIPsToClMap[peerAddress]->name;
} else {
unknownClient = true;
} }
connect(clientConnection, &QTcpSocket::disconnected, clientConnection, connect(clientConnection, &QTcpSocket::disconnected, clientConnection,
@ -114,7 +125,7 @@ void lc::ClientHelpNotificationServer::SendReply() {
clientConnection->write(block); clientConnection->write(block);
clientConnection->disconnectFromHost(); clientConnection->disconnectFromHost();
if (unknownClient) { if (peerName.isEmpty()) {
QMessageBox requestReceivedBox{ QMessageBox requestReceivedBox{
QMessageBox::Information, tr("Unknown client asked for help."), QMessageBox::Information, tr("Unknown client asked for help."),
tr("An unknown client with IP '%1' asked for help.").arg(peerAddress), tr("An unknown client with IP '%1' asked for help.").arg(peerAddress),

@ -20,27 +20,26 @@
#ifndef CLIENTHELPNOTIFICATIONSERVER_H #ifndef CLIENTHELPNOTIFICATIONSERVER_H
#define CLIENTHELPNOTIFICATIONSERVER_H #define CLIENTHELPNOTIFICATIONSERVER_H
#include "src/Lib/client.h"
#include <QHostAddress>
#include <QMessageBox>
#include <QObject> #include <QObject>
#include <QtNetwork>
class QNetworkSession;
class QTcpServer;
namespace lc { namespace lc {
/*!
* \brief A server listing for connections from clients which will be
* interpreted as help requests
*/
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 QString hostAddress;
QNetworkSession *networkSession = nullptr; QNetworkSession *networkSession = nullptr;
private slots: private slots:

Loading…
Cancel
Save