Skip to content

Commit a3df365

Browse files
committed
ticket:4175 Start the tcp server from the main thread.
1 parent ce0dd87 commit a3df365

File tree

5 files changed

+170
-128
lines changed

5 files changed

+170
-128
lines changed

OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.cpp

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <QMenu>
4949
#include <QDesktopWidget>
5050
#include <QClipboard>
51+
#include <QTcpSocket>
5152

5253
/*!
5354
* \class SimulationOutputTree
@@ -302,6 +303,11 @@ SimulationOutputWidget::SimulationOutputWidget(SimulationOptions simulationOptio
302303
// create the ArchivedSimulationItem
303304
mpArchivedSimulationItem = new ArchivedSimulationItem(mSimulationOptions, this);
304305
MainWindow::instance()->getSimulationDialog()->getArchivedSimulationsTreeWidget()->addTopLevelItem(mpArchivedSimulationItem);
306+
// start the tcp server
307+
mpTcpServer = new QTcpServer;
308+
mSocketDisconnected = true;
309+
mpTcpServer->listen(QHostAddress(QHostAddress::LocalHost));
310+
connect(mpTcpServer, SIGNAL(newConnection()), SLOT(createSimulationProgressSocket()));
305311
// create the thread
306312
mpSimulationProcessThread = new SimulationProcessThread(this);
307313
connect(mpSimulationProcessThread, SIGNAL(sendCompilationStarted()), SLOT(compilationProcessStarted()));
@@ -321,6 +327,9 @@ SimulationOutputWidget::~SimulationOutputWidget()
321327
if (mpSimulationOutputHandler) {
322328
delete mpSimulationOutputHandler;
323329
}
330+
if (mpTcpServer) {
331+
delete mpTcpServer;
332+
}
324333
}
325334

326335
void SimulationOutputWidget::addGeneratedFileTab(QString fileName)
@@ -344,9 +353,10 @@ void SimulationOutputWidget::addGeneratedFileTab(QString fileName)
344353
}
345354

346355
/*!
347-
Writes the simulation output in a formatted text form.\n
348-
\param - pSimulationMessage - the simulation output message.
349-
*/
356+
* \brief SimulationOutputWidget::writeSimulationMessage
357+
* Writes the simulation output in a formatted text form.\n
358+
* \param pSimulationMessage - the simulation output message.
359+
*/
350360
void SimulationOutputWidget::writeSimulationMessage(SimulationMessage *pSimulationMessage)
351361
{
352362
static QString lastSream;
@@ -385,9 +395,57 @@ void SimulationOutputWidget::writeSimulationMessage(SimulationMessage *pSimulati
385395
}
386396

387397
/*!
388-
Slot activated when SimulationProcessThread sendCompilationStarted signal is raised.\n
389-
Updates the progress label, bar and button controls.
390-
*/
398+
* \brief SimulationOutputWidget::createSimulationProgressSocket
399+
* Slot activated when QTcpServer newConnection SIGNAL is raised.\n
400+
* Accepts the incoming connection and connects to readyRead SIGNAL of QTcpSocket.
401+
*/
402+
void SimulationOutputWidget::createSimulationProgressSocket()
403+
{
404+
if (sender()) {
405+
QTcpServer *pTcpServer = qobject_cast<QTcpServer*>(const_cast<QObject*>(sender()));
406+
if (pTcpServer && pTcpServer->hasPendingConnections()) {
407+
QTcpSocket *pTcpSocket = pTcpServer->nextPendingConnection();
408+
mSocketDisconnected = false;
409+
connect(pTcpSocket, SIGNAL(readyRead()), SLOT(readSimulationProgress()));
410+
connect(pTcpSocket, SIGNAL(disconnected()), SLOT(socketDisconnected()));
411+
disconnect(pTcpServer, SIGNAL(newConnection()), this, SLOT(createSimulationProgressSocket()));
412+
}
413+
}
414+
}
415+
416+
/*!
417+
* \brief SimulationProcessThread::readSimulationProgress
418+
* Slot activated when QTcpSocket readyRead or disconnected SIGNAL is raised.\n
419+
* Sends the recieved data to xml parser.
420+
*/
421+
void SimulationOutputWidget::readSimulationProgress()
422+
{
423+
if (sender()) {
424+
QTcpSocket *pTcpSocket = qobject_cast<QTcpSocket*>(const_cast<QObject*>(sender()));
425+
if (pTcpSocket) {
426+
QString output = QString(pTcpSocket->readAll());
427+
if (!output.isEmpty()) {
428+
writeSimulationOutput(output, StringHandler::Unknown, false);
429+
}
430+
}
431+
}
432+
}
433+
434+
/*!
435+
* \brief SimulationOutputWidget::socketDisconnected
436+
* Slot activated when QTcpSocket disconnected SIGNAL is raised.\n
437+
* Writes the exit status and exit code of the simulation process.
438+
*/
439+
void SimulationOutputWidget::socketDisconnected()
440+
{
441+
mSocketDisconnected = true;
442+
}
443+
444+
/*!
445+
* \brief SimulationOutputWidget::compilationProcessStarted
446+
* Slot activated when SimulationProcessThread sendCompilationStarted signal is raised.\n
447+
* Updates the progress label, bar and button controls.
448+
*/
391449
void SimulationOutputWidget::compilationProcessStarted()
392450
{
393451
mpProgressLabel->setText(tr("Compiling <b>%1</b>. Please wait for a while.").arg(mSimulationOptions.getClassName()));
@@ -398,9 +456,12 @@ void SimulationOutputWidget::compilationProcessStarted()
398456
}
399457

400458
/*!
401-
Slot activated when SimulationProcessThread sendCompilationStandardOutput signal is raised.\n
402-
Writes the compilation standard output to the compilation output text box.
403-
*/
459+
* \brief SimulationOutputWidget::writeCompilationOutput
460+
* Slot activated when SimulationProcessThread sendCompilationStandardOutput signal is raised.\n
461+
* Writes the compilation standard output to the compilation output text box.
462+
* \param output
463+
* \param color
464+
*/
404465
void SimulationOutputWidget::writeCompilationOutput(QString output, QColor color)
405466
{
406467
mpGeneratedFilesTabWidget->setTabEnabled(1, true);
@@ -412,9 +473,12 @@ void SimulationOutputWidget::writeCompilationOutput(QString output, QColor color
412473
}
413474

414475
/*!
415-
Slot activated when SimulationProcessThread sendCompilationFinished signal is raised.\n
416-
Calls the Transformational Debugger or Algorithmic Debugger depending on the user selections.
417-
*/
476+
* \brief SimulationOutputWidget::compilationProcessFinished
477+
* Slot activated when SimulationProcessThread sendCompilationFinished signal is raised.\n
478+
* Calls the Transformational Debugger or Algorithmic Debugger depending on the user selections.
479+
* \param exitCode
480+
* \param exitStatus
481+
*/
418482
void SimulationOutputWidget::compilationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
419483
{
420484
mpProgressLabel->setText(tr("Compilation of <b>%1</b> is finished.").arg(mSimulationOptions.getClassName()));
@@ -433,9 +497,10 @@ void SimulationOutputWidget::compilationProcessFinished(int exitCode, QProcess::
433497
}
434498

435499
/*!
436-
Slot activated when SimulationProcessThread sendSimulationStarted signal is raised.\n
437-
Updates the progress label, bar and button controls.
438-
*/
500+
* \brief SimulationOutputWidget::simulationProcessStarted
501+
* Slot activated when SimulationProcessThread sendSimulationStarted signal is raised.\n
502+
* Updates the progress label, bar and button controls.
503+
*/
439504
void SimulationOutputWidget::simulationProcessStarted()
440505
{
441506
mpProgressLabel->setText(tr("Running simulation of <b>%1</b>. Please wait for a while.").arg(mSimulationOptions.getClassName()));
@@ -506,9 +571,12 @@ void SimulationOutputWidget::writeSimulationOutput(QString output, StringHandler
506571
}
507572

508573
/*!
509-
Slot activated when SimulationProcessThread sendSimulationFinished signal is raised.\n
510-
Reads the result variables, populates the variables browser and shows the plotting view.
511-
*/
574+
* \brief SimulationOutputWidget::simulationProcessFinished
575+
* Slot activated when SimulationProcessThread sendSimulationFinished signal is raised.\n
576+
* Reads the result variables, populates the variables browser and shows the plotting view.
577+
* \param exitCode
578+
* \param exitStatus
579+
*/
512580
void SimulationOutputWidget::simulationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
513581
{
514582
Q_UNUSED(exitCode);
@@ -521,9 +589,10 @@ void SimulationOutputWidget::simulationProcessFinished(int exitCode, QProcess::E
521589
}
522590

523591
/*!
524-
Slot activated when mpCancelButton clicked signal is raised.\n
525-
Cancels a running compilaiton/simulation by killing the compilation/simulation process.
526-
*/
592+
* \brief SimulationOutputWidget::cancelCompilationOrSimulation
593+
* Slot activated when mpCancelButton clicked signal is raised.\n
594+
* Cancels a running compilaiton/simulation by killing the compilation/simulation process.
595+
*/
527596
void SimulationOutputWidget::cancelCompilationOrSimulation()
528597
{
529598
if (mpSimulationProcessThread->isCompilationProcessRunning()) {
@@ -543,13 +612,14 @@ void SimulationOutputWidget::cancelCompilationOrSimulation()
543612
}
544613

545614
/*!
546-
Slot activated when a link is clicked from simulation output.\n
547-
Parses the url and loads the TransformationsWidget with the used equation.
548-
\param url - the url that is clicked
549-
*/
615+
* \brief SimulationOutputWidget::openTransformationBrowser
616+
* Slot activated when a link is clicked from simulation output.\n
617+
* Parses the url and loads the TransformationsWidget with the used equation.
618+
* \param url - the url that is clicked
619+
*/
550620
/*
551-
<a href="omedittransformationsbrowser://model_info.json?index=4></a>"
552-
*/
621+
* <a href="omedittransformationsbrowser://model_info.json?index=4></a>"
622+
*/
553623
void SimulationOutputWidget::openTransformationBrowser(QUrl url)
554624
{
555625
/* read the file name */

OMEdit/OMEditGUI/Simulation/SimulationOutputWidget.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <QTextBrowser>
4545
#include <QProcess>
4646
#include <QDateTime>
47+
#include <QTcpServer>
4748

4849
class Label;
4950
class SimulationProcessThread;
@@ -86,6 +87,8 @@ class SimulationOutputWidget : public QWidget
8687
bool isOutputStructured() {return mIsOutputStructured;}
8788
SimulationOutputTree* getSimulationOutputTree() {return mpSimulationOutputTree;}
8889
QPlainTextEdit* getCompilationOutputTextBox() {return mpCompilationOutputTextBox;}
90+
QTcpServer* getTcpServer() {return mpTcpServer;}
91+
bool isSocketDisconnected() {return mSocketDisconnected;}
8992
SimulationProcessThread* getSimulationProcessThread() {return mpSimulationProcessThread;}
9093
void addGeneratedFileTab(QString fileName);
9194
void writeSimulationMessage(SimulationMessage *pSimulationMessage);
@@ -101,9 +104,14 @@ class SimulationOutputWidget : public QWidget
101104
SimulationOutputTree *mpSimulationOutputTree;
102105
QPlainTextEdit *mpCompilationOutputTextBox;
103106
ArchivedSimulationItem *mpArchivedSimulationItem;
107+
QTcpServer *mpTcpServer;
108+
bool mSocketDisconnected;
104109
SimulationProcessThread *mpSimulationProcessThread;
105110
QDateTime mResultFileLastModifiedDateTime;
106111
public slots:
112+
void createSimulationProgressSocket();
113+
void readSimulationProgress();
114+
void socketDisconnected();
107115
void compilationProcessStarted();
108116
void writeCompilationOutput(QString output, QColor color);
109117
void compilationProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);

0 commit comments

Comments
 (0)