Skip to content

Commit

Permalink
Removed the unnecessary SimulationServerCheckThread class.
Browse files Browse the repository at this point in the history
Do not create unnecessary QTcpSocket objects.
  • Loading branch information
adeas31 committed Oct 19, 2017
1 parent dbc50ee commit d5f420a
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 92 deletions.
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp
Expand Up @@ -30,6 +30,7 @@ bool OpcUaClient::connectToServer()
mpClient = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout);
UA_StatusCode returnValue = UA_Client_connect(mpClient, UA_ClientConnectionTCP, endPoint.c_str());

qDebug() << returnValue;
if (returnValue != UA_STATUSCODE_GOOD) {
UA_Client_disconnect(mpClient);
UA_Client_delete(mpClient);
Expand Down
22 changes: 0 additions & 22 deletions OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp
Expand Up @@ -1112,16 +1112,7 @@ SimulationOptions SimulationDialog::createSimulationOptions()
simulationFlags.append("-embeddedServer=opc-ua");
// embedded server port
if (mpInteractiveSimulationPortNumberTextBox->text().isEmpty()) {
// look for an available port
QTcpSocket *pSocket = new QTcpSocket();
qint16 port = 4841;
// find a port not previously used by another opc ua server
pSocket->connectToHost(QHostAddress::LocalHost, port);
while (pSocket->waitForConnected(10)) {
port++;
pSocket->abort();
pSocket->connectToHost(QHostAddress::LocalHost, port);
}
simulationOptions.setInteractiveSimulationPortNumber(port);
simulationFlags.append(QString("-embeddedServerPort=").append(QString::number(port)));
} else {
Expand Down Expand Up @@ -1641,19 +1632,6 @@ void SimulationDialog::simulationProcessRunning(SimulationOptions simulationOpti
}
}

/*!
* \brief SimulationDialog::embeddedServerError
* \param simulationOptions
* Handles what should be done if the OPC UA client is unable to bind to the server \n
* over the selected port. \n
*/
void SimulationDialog::embeddedServerError(QString error)
{
// make the user aware of the problem
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information),
"Embedded simulation server: " + error, Helper::ok);
}

OpcUaClient* SimulationDialog::getOpcUaClient(int port)
{
return mOpcUaClientsMap.value(port);
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditGUI/Simulation/SimulationDialog.h
Expand Up @@ -231,7 +231,6 @@ class SimulationDialog : public QDialog
void showAlgorithmicDebugger(SimulationOptions simulationOptions);
void simulationProcessFinished(SimulationOptions simulationOptions, QDateTime resultFileLastModifiedDateTime);
void simulationProcessRunning(SimulationOptions simulationOptions);
void embeddedServerError(QString error);
public slots:
void numberOfIntervalsRadioToggled(bool toggle);
void intervalRadioToggled(bool toggle);
Expand Down
11 changes: 0 additions & 11 deletions OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.cpp
Expand Up @@ -351,7 +351,6 @@ SimulationOutputWidget::SimulationOutputWidget(SimulationOptions simulationOptio
SLOT(compilationProcessFinished(int,QProcess::ExitStatus)));
connect(mpSimulationProcessThread, SIGNAL(sendSimulationStarted()), SLOT(simulationProcessStarted()));
connect(mpSimulationProcessThread, SIGNAL(sendEmbeddedServerReady()), SLOT(embeddedServerReady()));
connect(mpSimulationProcessThread, SIGNAL(sendEmbeddedServerError(QString)), SLOT(embeddedServerError(QString)));
connect(mpSimulationProcessThread, SIGNAL(sendSimulationOutput(QString,StringHandler::SimulationMessageType,bool)),
SLOT(writeSimulationOutput(QString,StringHandler::SimulationMessageType,bool)));
connect(mpSimulationProcessThread, SIGNAL(sendSimulationFinished(int,QProcess::ExitStatus)),
Expand Down Expand Up @@ -577,16 +576,6 @@ void SimulationOutputWidget::embeddedServerReady()
MainWindow::instance()->getSimulationDialog()->simulationProcessRunning(mSimulationOptions);
}

/*!
* \brief SimulationOutputWidget::embeddedServerReady
* Slot activated when SimulationProcessThread sendembeddedServerError signal is raised.\n
* The provided port is unbound and can be used for communication between the client and remote.
*/
void SimulationOutputWidget::embeddedServerError(QString error)
{
MainWindow::instance()->getSimulationDialog()->embeddedServerError(error);
}

/*!
* \brief SimulationOutputWidget::writeSimulationOutput
* Slot activated when SimulationProcessThread sendSimulationOutput signal is raised.\n
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.h
Expand Up @@ -118,7 +118,6 @@ public slots:
void compilationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
void simulationProcessStarted();
void embeddedServerReady();
void embeddedServerError(QString error);
void writeSimulationOutput(QString output, StringHandler::SimulationMessageType type, bool textFormat);
void simulationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
void cancelCompilationOrSimulation();
Expand Down
40 changes: 1 addition & 39 deletions OMEdit/OMEditGUI/Simulation/SimulationProcessThread.cpp
Expand Up @@ -35,7 +35,6 @@
#include "Options/OptionsDialog.h"

#include <QDir>
#include <QTcpSocket>

SimulationProcessThread::SimulationProcessThread(SimulationOutputWidget *pSimulationOutputWidget)
: QThread(pSimulationOutputWidget), mpSimulationOutputWidget(pSimulationOutputWidget)
Expand Down Expand Up @@ -235,47 +234,10 @@ void SimulationProcessThread::simulationProcessStarted()
emit sendSimulationStarted();

if (mpSimulationOutputWidget->getSimulationOptions().isInteractiveSimulation()) {
// make sure that the embedded server is ready to be connected to
int port = mpSimulationOutputWidget->getSimulationOptions().getInteractiveSimulationPortNumber();
SimulationServerCheckThread *pEmbeddedServerCheckThread = new SimulationServerCheckThread(port);
connect(pEmbeddedServerCheckThread, SIGNAL(sendEmbeddedServerReady()), this, SLOT(embeddedServerReady()));
connect(pEmbeddedServerCheckThread, SIGNAL(sendEmbeddedServerError(QString)), this, SLOT(embeddedServerError(QString)));
connect(pEmbeddedServerCheckThread, SIGNAL(finished()), pEmbeddedServerCheckThread, SLOT(deleteLater()));
pEmbeddedServerCheckThread->start();
emit sendEmbeddedServerReady();
}
}

/*!
* \brief SimulationServerCheckThread::run
* Raises the sendEmbeddedServerReady signal when the OPC UA server is ready to be connectd to. \n
*/
void SimulationServerCheckThread::run()
{
QTcpSocket *pTcpSocket = new QTcpSocket;
while (pTcpSocket->state() != QAbstractSocket::ConnectedState) {
pTcpSocket->connectToHost(QHostAddress::LocalHost, mPort);
msleep(10);
if (pTcpSocket->waitForConnected()) {
emit sendEmbeddedServerReady();
}
}
// make the user aware of known socket errors
if (pTcpSocket->error() && pTcpSocket->error() != QAbstractSocket::UnknownSocketError) {
emit sendEmbeddedServerError(pTcpSocket->errorString());
}
}

void SimulationProcessThread::embeddedServerReady()
{
emit sendEmbeddedServerReady();
}

void SimulationProcessThread::embeddedServerError(QString error)
{
// if, somehow, the client is unable to bind to the server over the specified port
emit sendEmbeddedServerError(error);
}

/*!
* \brief SimulationProcessThread::readSimulationStandardOutput
* Slot activated when mpSimulationProcess readyReadStandardOutput signal is raised.\n
Expand Down
18 changes: 0 additions & 18 deletions OMEdit/OMEditGUI/Simulation/SimulationProcessThread.h
Expand Up @@ -77,33 +77,15 @@ private slots:
void readSimulationStandardError();
void simulationProcessError(QProcess::ProcessError error);
void simulationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
void embeddedServerReady();
void embeddedServerError(QString error);
signals:
void sendCompilationStarted();
void sendCompilationOutput(QString, QColor);
void sendCompilationFinished(int, QProcess::ExitStatus);
void sendSimulationStarted();
void sendEmbeddedServerReady();
void sendEmbeddedServerError(QString);
void sendEstablishConnectionRunning();
void sendSimulationOutput(QString, StringHandler::SimulationMessageType type, bool);
void sendSimulationFinished(int, QProcess::ExitStatus);
};

class SimulationServerCheckThread : public QThread
{
Q_OBJECT
public:
SimulationServerCheckThread(int port)
: mPort(port) {}
~SimulationServerCheckThread(){}
virtual void run();
private:
int mPort;
signals:
void sendEmbeddedServerReady();
void sendEmbeddedServerError(QString);
};

#endif // SIMULATIONPROCESSTHREAD_H

0 comments on commit d5f420a

Please sign in to comment.