#include <netedit/GNEApplicationWindow.h>
#include <netedit/dialogs/run/GNERunDialog.h>
#include <utils/gui/events/GUIEvent_Message.h>
#include "GNEExternalRunner.h"
GNEExternalRunner::GNEExternalRunner(GNEApplicationWindow* applicationWindow) :
MFXSingleEventThread(applicationWindow->getApp(), applicationWindow) {
applicationWindow->setExternalRunner(this);
}
GNEExternalRunner::~GNEExternalRunner() {}
void
GNEExternalRunner::runTool(GNERunDialog* runDialog) {
abort();
myRunDialog = runDialog;
myRunning = false;
myErrorOccurred = false;
start();
}
void
GNEExternalRunner::abort() {
if (myRunning) {
cancel();
myRunning = false;
myErrorOccurred = false;
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("cancelled by user\n"))), true);
}
}
bool
GNEExternalRunner::isRunning() const {
return myRunning;
}
bool
GNEExternalRunner::errorOccurred() const {
return myErrorOccurred;
}
FXint
GNEExternalRunner::run() {
const std::string runCommand = myRunDialog->getRunCommand();
char buffer[128];
for (int i = 0; i < 128; i++) {
buffer[i] = '\0';
}
#ifdef WIN32
myPipe = _popen(StringUtils::transcodeToLocal(runCommand + " 2>&1").c_str(), "r");
#else
myPipe = popen((runCommand + " 2>&1").c_str(), "r");
#endif
if (!myPipe) {
myErrorOccurred = true;
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, "popen() failed!"), false);
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::TOOL_ENDED, ""), true);
return 1;
} else {
myRunning = true;
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::OUTPUT_OCCURRED, runCommand + "\n"), false);
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("starting process...\n"))), true);
try {
while (fgets(buffer, sizeof buffer, myPipe) != NULL) {
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::OUTPUT_OCCURRED, buffer), true);
}
} catch (...) {
#ifdef WIN32
_pclose(myPipe);
#else
pclose(myPipe);
#endif
myRunning = false;
myErrorOccurred = true;
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("error processing command\n"))), true);
return 1;
}
}
#ifdef WIN32
_pclose(myPipe);
#else
pclose(myPipe);
#endif
myPipe = nullptr;
myRunning = false;
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("process finished\n"))), false);
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::TOOL_ENDED, ""), true);
return 1;
}