Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEExternalRunner.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// External runner for python and external tools
19/****************************************************************************/
20
24
25#include "GNEExternalRunner.h"
26
27// ============================================-===============================
28// member method definitions
29// ===========================================================================
30
32 MFXSingleEventThread(applicationWindow->getApp(), applicationWindow) {
33 // set external runner in application window
34 applicationWindow->setExternalRunner(this);
35}
36
37
39
40
41void
43 // first abort any running process
44 abort();
45 // set run dialog
46 myRunDialog = runDialog;
47 // set flags
48 myRunning = false;
49 myErrorOccurred = false;
50 // start thread
51 start();
52}
53
54
55void
57 if (myRunning) {
58 // cancel thread
59 cancel();
60 // reset flags
61 myRunning = false;
62 myErrorOccurred = false;
63 // add event in runDialog
64 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("cancelled by user\n"))), true);
65 }
66}
67
68
69bool
71 return myRunning;
72}
73
74
75bool
79
80
81FXint
83 // get run command
84 const std::string runCommand = myRunDialog->getRunCommand();
85 // declare buffer
86 char buffer[128];
87 for (int i = 0; i < 128; i++) {
88 buffer[i] = '\0';
89 }
90 // open process showing std::err in console
91#ifdef WIN32
92 myPipe = _popen(StringUtils::transcodeToLocal(runCommand + " 2>&1").c_str(), "r");
93#else
94 myPipe = popen((runCommand + " 2>&1").c_str(), "r");
95#endif
96 if (!myPipe) {
97 // set error ocurred flag
98 myErrorOccurred = true;
101 return 1;
102 } else {
103 // set running flag
104 myRunning = true;
105 // Show command
107 // start process
108 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("starting process...\n"))), true);
109 try {
110 // add buffer
111 while (fgets(buffer, sizeof buffer, myPipe) != NULL) {
113 }
114 } catch (...) {
115 // close process
116#ifdef WIN32
117 _pclose(myPipe);
118#else
119 pclose(myPipe);
120#endif
121 // set flags
122 myRunning = false;
123 myErrorOccurred = true;
124 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("error processing command\n"))), true);
125 return 1;
126 }
127 }
128 // close process
129#ifdef WIN32
130 _pclose(myPipe);
131#else
132 pclose(myPipe);
133#endif
134 myPipe = nullptr;
135 // set running flag
136 myRunning = false;
137 // end process
138 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("process finished\n"))), false);
140 return 1;
141}
142
143/****************************************************************************/
@ MESSAGE_OCCURRED
send when a message occurred
@ ERROR_OCCURRED
send when a error occurred
@ OUTPUT_OCCURRED
send when a tool produces output
@ TOOL_ENDED
send when a tool finishes
#define TL(string)
Definition MsgHandler.h:305
The main window of Netedit.
void setExternalRunner(GNEExternalRunner *externalRunner)
set external runner
bool myErrorOccurred
flag for check if during execution an error was Occurred
~GNEExternalRunner()
destructor
GNERunDialog * myRunDialog
pointer to current run dialog
bool errorOccurred() const
check if during execution an error was Occurred
GNEExternalRunner(GNEApplicationWindow *applicationWindow)
Constructor.
FILE * myPipe
pipe file
bool isRunning() const
check if is running
void runTool(GNERunDialog *runDialog)
run tool called from dialog
void abort()
abort running
FXint run()
starts the thread. The thread ends after the tool is finished
bool myRunning
flag for check if we have a running process
void addEvent(GUIEvent *event, const bool signal)
add event in the queue
virtual std::string getRunCommand() const =0
get run command
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage