From dbc50ee19b3f5aa553068cd25a2375d4ed85c17c Mon Sep 17 00:00:00 2001 From: Christoffer Fors Johansson Date: Thu, 6 Jul 2017 11:51:18 +0200 Subject: [PATCH] Fixed variable names. Better port handling. --- OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp | 2 +- .../OMEditGUI/Simulation/SimulationDialog.cpp | 14 +++++---- .../OMEditGUI/Simulation/SimulationDialog.h | 2 +- .../Simulation/SimulationOutputWidget.cpp | 8 ++--- .../Simulation/SimulationOutputWidget.h | 2 +- .../Simulation/SimulationProcessThread.cpp | 31 +++++++------------ .../Simulation/SimulationProcessThread.h | 6 ++-- 7 files changed, 30 insertions(+), 35 deletions(-) diff --git a/OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp b/OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp index b1cf313af50..da019bd8e25 100644 --- a/OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp +++ b/OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp @@ -69,7 +69,7 @@ QStringList OpcUaClient::fetchVariableNamesFromServer() UA_ReferenceDescription *ref = &(browseResponse.results[i].references[j]); if (ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) { int nodeId = ref->nodeId.nodeId.identifier.numeric; - QString variableName = (char *)ref->browseName.name.data; + QString variableName = QString::fromUtf8((char *)ref->browseName.name.data, ref->browseName.name.length); if (!variableName.startsWith("$")) { Variable *pVariable = new Variable(nodeId, variableIsWritable(nodeId)); if (variableIsReal(nodeId)) { diff --git a/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp b/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp index 947bc9bb70c..17aa6356ef2 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp +++ b/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp @@ -1115,9 +1115,12 @@ SimulationOptions SimulationDialog::createSimulationOptions() // look for an available port QTcpSocket *pSocket = new QTcpSocket(); qint16 port = 4841; - // bind exclusively - while(!pSocket->bind(port, QAbstractSocket::DontShareAddress)) { + // 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))); @@ -1644,12 +1647,11 @@ void SimulationDialog::simulationProcessRunning(SimulationOptions simulationOpti * 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(SimulationOptions simulationOptions) +void SimulationDialog::embeddedServerError(QString error) { // make the user aware of the problem - QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), - tr("Client unable to bind to the embedded simulation server over the port: %1.") - .arg(simulationOptions.getInteractiveSimulationPortNumber()), Helper::ok); + QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), + "Embedded simulation server: " + error, Helper::ok); } OpcUaClient* SimulationDialog::getOpcUaClient(int port) diff --git a/OMEdit/OMEditGUI/Simulation/SimulationDialog.h b/OMEdit/OMEditGUI/Simulation/SimulationDialog.h index 5a3ddc696bf..927f38c0670 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationDialog.h +++ b/OMEdit/OMEditGUI/Simulation/SimulationDialog.h @@ -231,7 +231,7 @@ class SimulationDialog : public QDialog void showAlgorithmicDebugger(SimulationOptions simulationOptions); void simulationProcessFinished(SimulationOptions simulationOptions, QDateTime resultFileLastModifiedDateTime); void simulationProcessRunning(SimulationOptions simulationOptions); - void embeddedServerError(SimulationOptions simulationOptions); + void embeddedServerError(QString error); public slots: void numberOfIntervalsRadioToggled(bool toggle); void intervalRadioToggled(bool toggle); diff --git a/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.cpp b/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.cpp index d3ef81d1f35..d2d732b9932 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.cpp +++ b/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.cpp @@ -351,7 +351,7 @@ 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()), SLOT(embeddedServerError())); + 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)), @@ -579,12 +579,12 @@ void SimulationOutputWidget::embeddedServerReady() /*! * \brief SimulationOutputWidget::embeddedServerReady - * Slot activated when SimulationProcessThread sendembeddedServerReady signal is raised.\n + * 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() +void SimulationOutputWidget::embeddedServerError(QString error) { - MainWindow::instance()->getSimulationDialog()->embeddedServerError(mSimulationOptions); + MainWindow::instance()->getSimulationDialog()->embeddedServerError(error); } /*! diff --git a/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.h b/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.h index 18fdadc2678..917830d4cd5 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.h +++ b/OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.h @@ -118,7 +118,7 @@ public slots: void compilationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); void simulationProcessStarted(); void embeddedServerReady(); - void embeddedServerError(); + void embeddedServerError(QString error); void writeSimulationOutput(QString output, StringHandler::SimulationMessageType type, bool textFormat); void simulationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); void cancelCompilationOrSimulation(); diff --git a/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.cpp b/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.cpp index c5cb05e3922..e016fbe52e0 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.cpp +++ b/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.cpp @@ -239,7 +239,7 @@ void SimulationProcessThread::simulationProcessStarted() int port = mpSimulationOutputWidget->getSimulationOptions().getInteractiveSimulationPortNumber(); SimulationServerCheckThread *pEmbeddedServerCheckThread = new SimulationServerCheckThread(port); connect(pEmbeddedServerCheckThread, SIGNAL(sendEmbeddedServerReady()), this, SLOT(embeddedServerReady())); - connect(pEmbeddedServerCheckThread, SIGNAL(sendEmbeddedServerError()), this, SLOT(embeddedServerError())); + connect(pEmbeddedServerCheckThread, SIGNAL(sendEmbeddedServerError(QString)), this, SLOT(embeddedServerError(QString))); connect(pEmbeddedServerCheckThread, SIGNAL(finished()), pEmbeddedServerCheckThread, SLOT(deleteLater())); pEmbeddedServerCheckThread->start(); } @@ -247,29 +247,22 @@ void SimulationProcessThread::simulationProcessStarted() /*! * \brief SimulationServerCheckThread::run - * Raises the sendEmbeddedServerReady signal when the OPC UA server is ready to be connectd to.\n + * Raises the sendEmbeddedServerReady signal when the OPC UA server is ready to be connectd to. \n */ void SimulationServerCheckThread::run() { QTcpSocket *pTcpSocket = new QTcpSocket; - if (!pTcpSocket->bind(mPort, QAbstractSocket::DontShareAddress)) { - emit sendEmbeddedServerError(); - } else { - pTcpSocket->abort(); + while (pTcpSocket->state() != QAbstractSocket::ConnectedState) { pTcpSocket->connectToHost(QHostAddress::LocalHost, mPort); - - while (pTcpSocket->state() != QAbstractSocket::ConnectedState) { - msleep(100); - if (pTcpSocket->waitForConnected()) { - pTcpSocket->disconnectFromHost(); - if (pTcpSocket->state() == QAbstractSocket::UnconnectedState || pTcpSocket->waitForDisconnected()) { - emit sendEmbeddedServerReady(); - } - } else { - break; - } + 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() @@ -277,10 +270,10 @@ void SimulationProcessThread::embeddedServerReady() emit sendEmbeddedServerReady(); } -void SimulationProcessThread::embeddedServerError() +void SimulationProcessThread::embeddedServerError(QString error) { // if, somehow, the client is unable to bind to the server over the specified port - emit sendEmbeddedServerError(); + emit sendEmbeddedServerError(error); } /*! diff --git a/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.h b/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.h index ab23974315f..197cf5d4ceb 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.h +++ b/OMEdit/OMEditGUI/Simulation/SimulationProcessThread.h @@ -78,14 +78,14 @@ private slots: void simulationProcessError(QProcess::ProcessError error); void simulationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); void embeddedServerReady(); - void embeddedServerError(); + void embeddedServerError(QString error); signals: void sendCompilationStarted(); void sendCompilationOutput(QString, QColor); void sendCompilationFinished(int, QProcess::ExitStatus); void sendSimulationStarted(); void sendEmbeddedServerReady(); - void sendEmbeddedServerError(); + void sendEmbeddedServerError(QString); void sendEstablishConnectionRunning(); void sendSimulationOutput(QString, StringHandler::SimulationMessageType type, bool); void sendSimulationFinished(int, QProcess::ExitStatus); @@ -103,7 +103,7 @@ class SimulationServerCheckThread : public QThread int mPort; signals: void sendEmbeddedServerReady(); - void sendEmbeddedServerError(); + void sendEmbeddedServerError(QString); }; #endif // SIMULATIONPROCESSTHREAD_H