Refactor the HelpMessageSender
parent
5ca5dfbcef
commit
10831bfe50
@ -1,107 +1,171 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014-2020 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "helpmessagewindow.h"
|
#include "helpmessagewindow.h"
|
||||||
|
#include "ui_helpmessagewindow.h"
|
||||||
|
|
||||||
|
#include <QNetworkConfigurationManager>
|
||||||
|
#include <QNetworkSession>
|
||||||
|
#include <QSettings>
|
||||||
|
#include <QTcpSocket>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
lcHelpMessageWindow::lcHelpMessageWindow( const QString &argServerIP, const unsigned short int &argServerPort, QWidget *argParent ) :
|
lc::HelpMessageWindow::HelpMessageWindow(const QString &argServerIP,
|
||||||
QMainWindow{ argParent },
|
const uint16_t argServerPort,
|
||||||
helpMessageSocket { new QTcpSocket{ this } },
|
QWidget *argParent)
|
||||||
serverPort{ argServerPort },
|
: QMainWindow{argParent}, helpMessageSocket{new QTcpSocket{this}},
|
||||||
serverAddress{ argServerIP },
|
|
||||||
ui{ new Ui::HelpMessageWindow }
|
serverAddress{argServerIP},
|
||||||
{
|
serverPort{argServerPort}, ui{new Ui::HelpMessageWindow} {
|
||||||
ui->setupUi( this );
|
ui->setupUi(this);
|
||||||
connect( ui->PBAskForHelp, &QPushButton::clicked, this, &lcHelpMessageWindow::RequestHelp );
|
|
||||||
connect( helpMessageSocket, &QTcpSocket::readyRead, this, &lcHelpMessageWindow::ReadHelpReply );
|
connect(ui->PBAskForHelp, &QPushButton::clicked, this,
|
||||||
connect( helpMessageSocket, SIGNAL( error( QAbstractSocket::SocketError ) ),
|
&lc::HelpMessageWindow::RequestHelp);
|
||||||
this, SLOT( DisplayError( QAbstractSocket::SocketError ) ) );
|
connect(helpMessageSocket, &QTcpSocket::readyRead, this,
|
||||||
|
&lc::HelpMessageWindow::ReadHelpReply);
|
||||||
QNetworkConfigurationManager manager;
|
connect(helpMessageSocket,
|
||||||
if ( manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired ) {
|
static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(
|
||||||
// Get saved network configuration
|
&QAbstractSocket::error),
|
||||||
QSettings settings{ QSettings::UserScope, QLatin1String{ "QtProject" } };
|
this, &HelpMessageWindow::DisplayError);
|
||||||
settings.beginGroup( QLatin1String{ "QtNetwork" } );
|
|
||||||
const QString id = settings.value( QLatin1String{ "DefaultNetworkConfiguration" } ).toString();
|
QNetworkConfigurationManager manager;
|
||||||
settings.endGroup();
|
if (manager.capabilities() &
|
||||||
|
QNetworkConfigurationManager::NetworkSessionRequired) {
|
||||||
// If the saved network configuration is not currently discovered use the system default
|
// Get saved network configuration
|
||||||
QNetworkConfiguration config = manager.configurationFromIdentifier( id );
|
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
|
||||||
if ( ( config.state() & QNetworkConfiguration::Discovered ) != QNetworkConfiguration::Discovered ) {
|
settings.beginGroup(QLatin1String{"QtNetwork"});
|
||||||
config = manager.defaultConfiguration();
|
const QString id =
|
||||||
}
|
settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString();
|
||||||
|
settings.endGroup();
|
||||||
networkSession = new QNetworkSession{ config, this };
|
|
||||||
connect( networkSession, &QNetworkSession::opened, this, &lcHelpMessageWindow::OpenedSession );
|
// If the saved network configuration is not currently discovered use the
|
||||||
|
// system default
|
||||||
networkSession->open();
|
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
|
||||||
|
if ((config.state() & QNetworkConfiguration::Discovered) !=
|
||||||
|
QNetworkConfiguration::Discovered) {
|
||||||
|
config = manager.defaultConfiguration();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
lcHelpMessageWindow::~lcHelpMessageWindow() {
|
networkSession = new QNetworkSession{config, this};
|
||||||
delete ui;
|
connect(networkSession, &QNetworkSession::opened, this,
|
||||||
}
|
&lc::HelpMessageWindow::OpenedSession);
|
||||||
|
|
||||||
void lcHelpMessageWindow::DisplayError( QAbstractSocket::SocketError socketError ) {
|
networkSession->open();
|
||||||
QString errorMessage;
|
}
|
||||||
switch ( socketError ) {
|
|
||||||
case QAbstractSocket::RemoteHostClosedError:
|
|
||||||
return;
|
|
||||||
case QAbstractSocket::HostNotFoundError:
|
|
||||||
errorMessage = tr( "An error occurred: The server could not be found for error reporting:\n" );
|
|
||||||
break;
|
|
||||||
case QAbstractSocket::ConnectionRefusedError:
|
|
||||||
errorMessage = tr( "An error occurred: The connection was refused by the laboratory server:\n" );
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
errorMessage = tr( "The following error occured:\n" );
|
|
||||||
}
|
|
||||||
errorMessage.append( tr("%1").arg( helpMessageSocket->errorString() ) );
|
|
||||||
errorMessage.append( "\n\nPlease raise your hand to notify the experimenters." );
|
|
||||||
ui->LSendingSuccess->setText( errorMessage );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcHelpMessageWindow::OpenedSession() {
|
/*!
|
||||||
// Save the used configuration
|
* \brief Destroy the HelpMessageWindow instance
|
||||||
QNetworkConfiguration config = networkSession->configuration();
|
*/
|
||||||
QString id;
|
lc::HelpMessageWindow::~HelpMessageWindow() { delete ui; }
|
||||||
if ( config.type() == QNetworkConfiguration::UserChoice )
|
|
||||||
id = networkSession->sessionProperty( QLatin1String{ "UserChoiceConfiguration" } ).toString();
|
/*!
|
||||||
else
|
* \brief Display an error message to the user if requesting help failed
|
||||||
id = config.identifier();
|
*
|
||||||
|
* \param socketError The error that occurred upon requesting help
|
||||||
QSettings settings{ QSettings::UserScope, QLatin1String{ "QtProject" } };
|
*/
|
||||||
settings.beginGroup( QLatin1String{ "QtNetwork" } );
|
void lc::HelpMessageWindow::DisplayError(
|
||||||
settings.setValue( QLatin1String{ "DefaultNetworkConfiguration" }, id );
|
const QAbstractSocket::SocketError socketError) {
|
||||||
settings.endGroup();
|
QString errorMessage;
|
||||||
|
|
||||||
|
switch (socketError) {
|
||||||
|
case QAbstractSocket::RemoteHostClosedError:
|
||||||
|
return;
|
||||||
|
case QAbstractSocket::HostNotFoundError:
|
||||||
|
errorMessage = tr("An error occurred: The server could not be found for "
|
||||||
|
"error reporting:\n");
|
||||||
|
break;
|
||||||
|
case QAbstractSocket::ConnectionRefusedError:
|
||||||
|
errorMessage = tr("An error occurred: The connection was refused by the "
|
||||||
|
"laboratory server:\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
errorMessage = tr("The following error occured:\n");
|
||||||
|
}
|
||||||
|
errorMessage.append(tr("%1").arg(helpMessageSocket->errorString()));
|
||||||
|
errorMessage.append(
|
||||||
|
"\n\nPlease raise your hand to notify the experimenters.");
|
||||||
|
|
||||||
|
ui->LSendingSuccess->setText(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcHelpMessageWindow::ReadHelpReply() {
|
/*!
|
||||||
QDataStream in( helpMessageSocket );
|
* \brief Handle the opening of a new QNetworkSession session
|
||||||
in.setVersion( QDataStream::Qt_5_2 );
|
*/
|
||||||
|
void lc::HelpMessageWindow::OpenedSession() {
|
||||||
if ( blockSize == 0 ) {
|
// Save the used configuration
|
||||||
if ( helpMessageSocket->bytesAvailable() < ( int )sizeof( quint16 ) )
|
QNetworkConfiguration config = networkSession->configuration();
|
||||||
return;
|
QString id;
|
||||||
|
if (config.type() == QNetworkConfiguration::UserChoice)
|
||||||
in >> blockSize;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
if ( helpMessageSocket->bytesAvailable() < blockSize ) {
|
/*!
|
||||||
return;
|
* \brief Read help request reply from laboratory server and notify user
|
||||||
|
*/
|
||||||
|
void lc::HelpMessageWindow::ReadHelpReply() {
|
||||||
|
QDataStream in{helpMessageSocket};
|
||||||
|
in.setVersion(QDataStream::Qt_5_2);
|
||||||
|
|
||||||
|
if (blockSize == 0) {
|
||||||
|
if (helpMessageSocket->bytesAvailable() <
|
||||||
|
static_cast<int>(sizeof(quint16))) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString serverAnswer;
|
in >> blockSize;
|
||||||
in >> serverAnswer;
|
}
|
||||||
|
|
||||||
if ( serverAnswer == "Help demand retrieved." ) {
|
if (helpMessageSocket->bytesAvailable() < blockSize) {
|
||||||
ui->LSendingSuccess->setText( tr( "Help message successfully sent.\nPlease wait for the experimenter to show up at your booth." ) );
|
return;
|
||||||
ui->PBAskForHelp->setEnabled( false );
|
}
|
||||||
} else {
|
|
||||||
ui->LSendingSuccess->setText( tr( "An error occurred sending the help message. Please raise your arm.\n\n'%1'" ).arg( serverAnswer ) );
|
QString serverAnswer;
|
||||||
}
|
in >> serverAnswer;
|
||||||
|
|
||||||
|
if (serverAnswer == "Help demand retrieved.") {
|
||||||
|
ui->LSendingSuccess->setText(
|
||||||
|
tr("Help message successfully sent.\nPlease wait for the experimenter "
|
||||||
|
"to show up at your booth."));
|
||||||
|
ui->PBAskForHelp->setEnabled(false);
|
||||||
|
} else {
|
||||||
|
ui->LSendingSuccess->setText(tr("An error occurred sending the help "
|
||||||
|
"message. Please raise your arm.\n\n'%1'")
|
||||||
|
.arg(serverAnswer));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcHelpMessageWindow::RequestHelp() {
|
/*!
|
||||||
blockSize = 0;
|
* \brief Request help from the laboratory server just by connecting to it
|
||||||
helpMessageSocket->abort();
|
*/
|
||||||
helpMessageSocket->connectToHost( serverAddress, serverPort );
|
void lc::HelpMessageWindow::RequestHelp() {
|
||||||
|
blockSize = 0;
|
||||||
|
helpMessageSocket->abort();
|
||||||
|
helpMessageSocket->connectToHost(serverAddress, serverPort);
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014-2020 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef HELPMESSAGEWINDOW_H
|
#ifndef HELPMESSAGEWINDOW_H
|
||||||
#define HELPMESSAGEWINDOW_H
|
#define HELPMESSAGEWINDOW_H
|
||||||
|
|
||||||
#include "ui_helpmessagewindow.h"
|
#include <QAbstractSocket>
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QMessageBox>
|
|
||||||
#include <QtNetwork>
|
class QNetworkSession;
|
||||||
|
class QTcpSocket;
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class HelpMessageWindow;
|
class HelpMessageWindow;
|
||||||
}
|
} // namespace Ui
|
||||||
|
|
||||||
class lcHelpMessageWindow : public QMainWindow
|
namespace lc {
|
||||||
{
|
|
||||||
Q_OBJECT
|
class HelpMessageWindow : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit lcHelpMessageWindow( const QString &argServerIP, const unsigned short int &argServerPort, QWidget *argParent = nullptr );
|
explicit HelpMessageWindow(const QString &argServerIP, uint16_t argServerPort,
|
||||||
~lcHelpMessageWindow();
|
QWidget *argParent = nullptr);
|
||||||
|
~HelpMessageWindow() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
quint16 blockSize = 0;
|
quint16 blockSize = 0;
|
||||||
QTcpSocket *helpMessageSocket = nullptr;
|
QTcpSocket *const helpMessageSocket = nullptr;
|
||||||
QNetworkSession *networkSession = nullptr;
|
QNetworkSession *networkSession = nullptr;
|
||||||
const quint16 serverPort = 0;
|
const QString serverAddress;
|
||||||
const QHostAddress serverAddress;
|
const quint16 serverPort = 0;
|
||||||
Ui::HelpMessageWindow *ui;
|
Ui::HelpMessageWindow *const ui = nullptr;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void RequestHelp();
|
void DisplayError(QAbstractSocket::SocketError socketError);
|
||||||
void ReadHelpReply();
|
void OpenedSession();
|
||||||
void DisplayError( QAbstractSocket::SocketError socketError );
|
void ReadHelpReply();
|
||||||
void OpenedSession();
|
void RequestHelp();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace lc
|
||||||
|
|
||||||
#endif // HELPMESSAGEWINDOW_H
|
#endif // HELPMESSAGEWINDOW_H
|
||||||
|
@ -1,12 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014-2020 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "helpmessagewindow.h"
|
#include "helpmessagewindow.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication a{argc, argv};
|
||||||
|
QSettings labSettings{"Labcontrol", "Labclient"};
|
||||||
|
|
||||||
|
const auto serverIP{labSettings.value("server_ip").toString()};
|
||||||
|
if (serverIP.isEmpty()) {
|
||||||
|
qDebug() << "Invalid laboratory server ip \"" + serverIP + "\" given";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto serverPortStr{labSettings.value("server_port").toString()};
|
||||||
|
bool convSuccess = false;
|
||||||
|
const auto serverPort = serverPortStr.toUInt(&convSuccess);
|
||||||
|
if ((false == convSuccess) || ((serverPort < 1) || (serverPort > 65535))) {
|
||||||
|
qDebug() << "Invalid laboratory server port \"" + serverPortStr +
|
||||||
|
"\" given";
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
int main( int argc, char *argv[] )
|
lc::HelpMessageWindow w{serverIP, static_cast<uint16_t>(serverPort)};
|
||||||
{
|
w.show();
|
||||||
QApplication a( argc, argv );
|
|
||||||
QSettings labSettings{ "Economic Laboratory", "Labcontrol" };
|
|
||||||
lcHelpMessageWindow w{ labSettings.value( "server_ip", "127.0.0.1" ).toString(), labSettings.value( "client_help_server_port", "0" ).toUInt() };
|
|
||||||
w.show();
|
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue