107 lines
3.7 KiB
C++
107 lines
3.7 KiB
C++
#include "worker.h"
|
|
#include "QDebug"
|
|
#include "sharedipc.h"
|
|
#include "converterdata.h"
|
|
|
|
Worker::Worker(const QString &program, const QStringList &arguments, IProccessMonitor * monitor,QObject *parent)
|
|
: QObject(parent), process(new QProcess(this)), program(program), arguments(arguments), monitor(monitor)
|
|
{
|
|
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
|
this, &Worker::onProcessFinished);
|
|
connect(process, &QProcess::errorOccurred, this, [this](QProcess::ProcessError error){
|
|
qDebug() << "Process error:" << error << ", description:" << process->errorString();
|
|
});
|
|
connect(process, &QProcess::stateChanged, [](QProcess::ProcessState state){
|
|
qDebug() << "Process state changed:" << state;
|
|
});
|
|
|
|
connect(process, &QProcess::readyReadStandardError, this, &Worker::readyReadStandardError);
|
|
connect(process, &QProcess::readyReadStandardOutput, this, &Worker::readyReadStandardOutput);
|
|
|
|
#if _DEBUG
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); // получить текущие системные переменные
|
|
QString path = env.value("PATH");
|
|
path = "D:\\Qt6-8\\Tools\\CMake_64\\bin;" + path; // добавить нужный путь в начало PATH
|
|
env.insert("PATH", path);
|
|
process->setProcessEnvironment(env);
|
|
#endif
|
|
|
|
isLogStandartOutput = ConverterData::Get().isGetLogStansartOutput();
|
|
}
|
|
|
|
void Worker::start() {
|
|
qDebug() << "Starting process:" << program << arguments.join(" ");
|
|
|
|
factory = createIPCControllerFactory();
|
|
IPCController* masterCtrl = factory->getController(masterMemory.toStdString());
|
|
masterCtrl->sendMessage("Hello from Master!");
|
|
|
|
// Slave
|
|
slaveCtrl = factory->getController(slaveMemory.toStdString());
|
|
// slaveCtrl->setMessageCallback([](const std::string& msg) {
|
|
// qDebug() << "Received: " << msg;
|
|
// });
|
|
slaveCtrl->setMessageCallback(monitor->messageCallbackHandling);
|
|
slaveCtrl->startListening();
|
|
arguments << "-masterMemory" << masterMemory.toLocal8Bit() << "-slaveMemory" << slaveMemory.toLocal8Bit();
|
|
msg = program + arguments.join(" ");
|
|
qDebug() << "Starting process:" << msg;
|
|
|
|
isStarted = true;
|
|
process->start(program, arguments);
|
|
|
|
qDebug() << process->state();
|
|
}
|
|
|
|
void Worker::stop() {
|
|
if (process->state() != QProcess::NotRunning) {
|
|
qDebug() << "Stopping process...";
|
|
process->kill();
|
|
process->waitForFinished();
|
|
}
|
|
}
|
|
|
|
void Worker::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
|
|
if(isStarted == false) return;
|
|
isStarted = false;
|
|
slaveCtrl->stopListening();
|
|
factory->releaseController(masterMemory.toStdString());
|
|
factory->releaseController(slaveMemory.toStdString());
|
|
monitor->setRetCode(exitCode);
|
|
qDebug() << "Process finished with:"
|
|
<< "\n Exit code:" << exitCode
|
|
<< "\n Status:" << (exitStatus == QProcess::NormalExit ? "Normal" : "Crash");
|
|
|
|
// if (exitCode) {
|
|
// monitor->setPercentComplete(0);
|
|
// } else {
|
|
monitor->setPercentComplete(1);
|
|
// }
|
|
emit finished();
|
|
}
|
|
|
|
void Worker::readyReadStandardOutput()
|
|
{
|
|
if(isLogStandartOutput){
|
|
QString str = process->readAllStandardOutput();
|
|
|
|
std::string str_std = str.toStdString();
|
|
if (monitor->logErrorOutput != nullptr) {
|
|
monitor->logErrorOutput(str_std);
|
|
}
|
|
qDebug()<< "Output:" << str;
|
|
}else {
|
|
qDebug()<< "Output:" << process->readAllStandardOutput();
|
|
}
|
|
}
|
|
|
|
void Worker::readyReadStandardError()
|
|
{
|
|
QString str = process->readAllStandardError();
|
|
std::string str_std = str.toStdString();
|
|
if (monitor->logErrorOutput != nullptr) {
|
|
monitor->logErrorOutput(str_std);
|
|
}
|
|
qDebug()<< "OutputError:" << str;
|
|
}
|