23 #include "standardserverconsole.h" 25 #include <QMessageBox> 29 #include <QTextStream> 32 StandardServerConsole::StandardServerConsole(
const QIcon &icon,
const QString &program,
const QStringList &arguments)
35 setAttribute(Qt::WA_DeleteOnClose);
38 setWindowTitle(
"Server Console");
44 setCentralWidget(console);
47 process =
new QProcess();
48 process->start(program, arguments);
49 if (process->waitForStarted())
52 connect(console, SIGNAL(messageSent(
const QString&)),
this, SLOT(writeToStandardInput(
const QString&)));
53 connect(process, SIGNAL(readyReadStandardError()),
this, SLOT(errorDataReady()));
54 connect(process, SIGNAL(readyReadStandardOutput()),
this, SLOT(outputDataReady()));
55 connect(process, SIGNAL(finished(
int,QProcess::ExitStatus)),
this, SLOT(finish(
int,QProcess::ExitStatus)));
61 StandardServerConsole::~StandardServerConsole()
64 if (process->pid() != 0)
70 auto ps =
new QProcess();
71 ps->start(QString(
"ps h --ppid %1 -o pid").arg(process->pid()));
72 ps->waitForFinished();
73 QByteArray psOutput = ps->readAllStandardOutput();
74 QTextStream stream(&psOutput);
75 stream.skipWhiteSpace();
76 while (!stream.atEnd())
80 stream.skipWhiteSpace();
87 process->disconnect(
this);
89 process->waitForFinished(1000);
91 process->waitForFinished();
95 void StandardServerConsole::errorDataReady()
97 console->appendMessage(QString(process->readAllStandardError()));
100 void StandardServerConsole::finish(
int exitCode, QProcess::ExitStatus exitStatus)
102 if (exitStatus == QProcess::CrashExit && exitCode != 0)
103 QMessageBox::critical(
this,
"Server crash", QString(
"The server terminated unexpectedly with exit code: %1").arg(exitCode));
108 void StandardServerConsole::outputDataReady()
110 console->appendMessage(QString(process->readAllStandardOutput()));
113 void StandardServerConsole::writeToStandardInput(
const QString &message)
115 process->write((message +
"\n").toUtf8());