Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNERunPythonTool.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-2023 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// Thread for run tool
19/****************************************************************************/
20
24
25#include "GNEPythonTool.h"
26#include "GNERunPythonTool.h"
27
28// ============================================-===============================
29// member method definitions
30// ===========================================================================
31
33 MFXSingleEventThread(runToolDialog->getGNEApp()->getApp(), runToolDialog->getGNEApp()),
34 myEventQueue(eq),
35 myEventThrow(ev) {
36}
37
38
40
41
42void
44 myPythonTool = tool;
45 // reset flags
46 myRunning = false;
47 myErrorOccurred = false;
48 start();
49}
50
51
52void
54 if (myRunning) {
55 // cancel thread
56 cancel();
57 // reset flags
58 myRunning = false;
59 myErrorOccurred = false;
60 // show info
61 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("cancelled by user\n"))));
63 }
64}
65
66
67bool
69 return myRunning;
70}
71
72
73bool
77
78
79FXint
81 // declare buffer
82 char buffer[128];
83 for (int i = 0; i < 128; i++) {
84 buffer[i] = '\0';
85 }
86 // open process showing std::err in console
87#ifdef WIN32
88 myPipe = _popen((myPythonTool->getCommand() + " 2>&1").c_str(), "r");
89#else
90 myPipe = popen((myPythonTool->getCommand() + " 2>&1").c_str(), "r");
91#endif
92 if (!myPipe) {
93 // set error ocurred flag
94 myErrorOccurred = true;
98 return 1;
99 } else {
100 // set running flag
101 myRunning = true;
102 // Show command
104 // start process
105 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("starting process...\n"))));
107 try {
108 // add buffer
109 while (fgets(buffer, sizeof buffer, myPipe) != NULL) {
112 }
113 } catch (...) {
114 // close process
115#ifdef WIN32
116 _pclose(myPipe);
117#else
118 pclose(myPipe);
119#endif
120 // set flags
121 myRunning = false;
122 myErrorOccurred = true;
123 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("error processing command\n"))));
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 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("process finished\n"))));
140 return 1;
141}
142
143/****************************************************************************/
@ MESSAGE_OCCURRED
send when a message occured
@ ERROR_OCCURRED
send when a error occured
@ OUTPUT_OCCURRED
send when a tool produces output
@ TOOL_ENDED
send when a tool finishes
#define TL(string)
Definition MsgHandler.h:287
virtual std::string getCommand() const
get command (python + script + arguments)
Abstract dialog for tools.
GNERunPythonTool(GNERunPythonToolDialog *runToolDialog, MFXSynchQue< GUIEvent * > &eq, FXEX::MFXThreadEvent &ev)
Constructor.
MFXSynchQue< GUIEvent * > & myEventQueue
event Queue
FXint run()
starts the thread. The thread ends after the tool is finished
const GNEPythonTool * myPythonTool
tool
bool isRunning() const
check if tool is running
bool myErrorOccurred
flag for check if during execution an error was Occurred
void abortTool()
abort tool running
FXEX::MFXThreadEvent & myEventThrow
event throw
bool myRunning
flag for check if tool is running
void runTool(const GNEPythonTool *tool)
run tool
bool errorOccurred() const
check if during execution an error was Occurred
FILE * myPipe
pipe file
~GNERunPythonTool()
destructor
void push_back(T what)