void ProcessInputChannel::initialize() { _backgroundOutputPipe = new QLocalSocket(); QObject::connect(_backgroundOutputPipe, SIGNAL(connected()), this, SLOT(onOutputPipeConnectionMade())); _backgroundOutputPipe->connectToServer(_mainProcessServerName,QLocalSocket::ReadWrite); _backgroundIPCServer = new QLocalServer(); QObject::connect(_backgroundIPCServer,SIGNAL(newConnection()),this,SLOT(onNewConnectionPending())); QString serverName; { QTemporaryFile tmpf(QDir::tempPath() + QDir::separator() + NATRON_APPLICATION_NAME "_INPUT_SOCKET" + QString::number(QCoreApplication::applicationPid())); tmpf.open(); serverName = tmpf.fileName(); } _backgroundIPCServer->listen(serverName); if(!_backgroundOutputPipe->waitForConnected(5000)){ //< blocking, we wait for the server to respond std::cout << "WARNING: The GUI application failed to respond, canceling this process will not be possible" " unless it finishes or you kill it." << std::endl; } writeToOutputChannel(QString(kBgProcessServerCreatedShort) + _backgroundIPCServer->fullServerName()); ///we wait for the GUI app to connect its socket to this server, we let it 5 sec to reply _backgroundIPCServer->waitForNewConnection(5000); ///since we're still not returning the event loop, just process them manually in case ///Qt didn't caught the new connection pending. QCoreApplication::processEvents(); }
void ProcessInputChannel::initialize() { _backgroundOutputPipe = new QLocalSocket(); QObject::connect( _backgroundOutputPipe, SIGNAL(connected()), this, SLOT(onOutputPipeConnectionMade()) ); _backgroundOutputPipe->connectToServer(_mainProcessServerName,QLocalSocket::ReadWrite); std::cout << "Attempting connection to " << _mainProcessServerName.toStdString() << std::endl; _backgroundIPCServer = new QLocalServer(); QObject::connect( _backgroundIPCServer,SIGNAL(newConnection()),this,SLOT(onNewConnectionPending()) ); QString tmpFileName; #if defined(Q_OS_WIN) tmpFileName += QString::fromUtf8("//./pipe"); tmpFileName += QLatin1Char('/'); tmpFileName += QString::fromUtf8(NATRON_APPLICATION_NAME); tmpFileName += QString::fromUtf8("_INPUT_SOCKET"); #endif { #if defined(Q_OS_UNIX) QTemporaryFile tmpf(tmpFileName); tmpFileName = tmpf.fileName(); tmpf.remove(); #else QTemporaryFile tmpf; tmpf.open(); QString tmpFilePath = tmpf.fileName(); QString baseName; int lastSlash = tmpFilePath.lastIndexOf(QLatin1Char('/')); if (lastSlash != -1 && lastSlash < tmpFilePath.size() - 1) { baseName = tmpFilePath.mid(lastSlash + 1); } else { baseName = tmpFilePath; } tmpFileName += baseName; tmpf.remove(); #endif } _backgroundIPCServer->listen(tmpFileName); if ( !_backgroundOutputPipe->waitForConnected(5000) ) { //< blocking, we wait for the server to respond std::cout << "WARNING: The GUI application failed to respond, canceling this process will not be possible" " unless it finishes or you kill it." << std::endl; } writeToOutputChannel( QString::fromUtf8(kBgProcessServerCreatedShort) + tmpFileName ); ///we wait for the GUI app to connect its socket to this server, we let it 5 sec to reply _backgroundIPCServer->waitForNewConnection(5000); ///since we're still not returning the event loop, just process them manually in case ///Qt didn't caught the new connection pending. QCoreApplication::processEvents(); }
ProcessHandler::ProcessHandler(AppInstance* app, const QString& projectPath, Natron::OutputEffectInstance* writer) : _app(app) ,_process(new QProcess) ,_writer(writer) ,_ipcServer(0) ,_bgProcessOutputSocket(0) ,_bgProcessInputSocket(0) ,_earlyCancel(false) ,_processLog() { ///setup the server used to listen the output of the background process _ipcServer = new QLocalServer(); QObject::connect(_ipcServer,SIGNAL(newConnection()),this,SLOT(onNewConnectionPending())); QString serverName; int randomNumber = std::rand(); { QTemporaryFile tmpf(NATRON_APPLICATION_NAME "_OUTPUT_PIPE_" + QString::number(randomNumber)); tmpf.open(); serverName = tmpf.fileName(); tmpf.remove(); } _ipcServer->listen(serverName); QStringList processArgs; processArgs << projectPath << "-b" << "-w" << writer->getName().c_str(); processArgs << "--IPCpipe" << (_ipcServer->fullServerName()); ///connect the useful slots of the process QObject::connect(_process,SIGNAL(readyReadStandardOutput()),this,SLOT(onStandardOutputBytesWritten())); QObject::connect(_process,SIGNAL(readyReadStandardError()),this,SLOT(onStandardErrorBytesWritten())); QObject::connect(_process,SIGNAL(error(QProcess::ProcessError)),this,SLOT(onProcessError(QProcess::ProcessError))); QObject::connect(_process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(onProcessEnd(int,QProcess::ExitStatus))); ///start the process _processLog.push_back("Starting background rendering: " + QCoreApplication::applicationFilePath()); _processLog.push_back(" "); for (int i = 0; i < processArgs.size(); ++i) { _processLog.push_back(processArgs[i] + " "); } _process->start(QCoreApplication::applicationFilePath(),processArgs); }
ProcessHandler::ProcessHandler(const QString & projectPath, const NodePtr& writer) : _process(new QProcess) , _writer(writer) , _ipcServer(0) , _bgProcessOutputSocket(0) , _bgProcessInputSocket(0) , _earlyCancel(false) , _processLog() , _processArgs() { ///setup the server used to listen the output of the background process _ipcServer = new QLocalServer(); QObject::connect( _ipcServer, SIGNAL(newConnection()), this, SLOT(onNewConnectionPending()) ); QString tmpFileName; #if defined(Q_OS_WIN) tmpFileName += QString::fromUtf8("//./pipe"); tmpFileName += QLatin1Char('/'); tmpFileName += QString::fromUtf8(NATRON_APPLICATION_NAME); tmpFileName += QString::fromUtf8("_INPUT_SOCKET"); #endif { #if defined(Q_OS_UNIX) QTemporaryFile tmpf(tmpFileName); tmpf.open(); tmpFileName = tmpf.fileName(); tmpf.remove(); #else QTemporaryFile tmpf; tmpf.open(); QString tmpFilePath = tmpf.fileName(); QString baseName; int lastSlash = tmpFilePath.lastIndexOf( QLatin1Char('/') ); if ( (lastSlash != -1) && (lastSlash < tmpFilePath.size() - 1) ) { baseName = tmpFilePath.mid(lastSlash + 1); } else { baseName = tmpFilePath; } tmpFileName += baseName; tmpf.remove(); #endif } _ipcServer->listen(tmpFileName); _processArgs << QString::fromUtf8("-b") << QString::fromUtf8("-w") << QString::fromUtf8( writer->getScriptName_mt_safe().c_str() ); _processArgs << QString::fromUtf8("--IPCpipe") << tmpFileName; _processArgs << projectPath; ///connect the useful slots of the process QObject::connect( _process, SIGNAL(readyReadStandardOutput()), this, SLOT(onStandardOutputBytesWritten()) ); QObject::connect( _process, SIGNAL(readyReadStandardError()), this, SLOT(onStandardErrorBytesWritten()) ); QObject::connect( _process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onProcessError(QProcess::ProcessError)) ); QObject::connect( _process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onProcessEnd(int,QProcess::ExitStatus)) ); ///start the process _processLog.push_back( tr("Starting background rendering: %1 %2") .arg( QCoreApplication::applicationFilePath() ) .arg( _processArgs.join( QString::fromUtf8(" ") ) ) ); }