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-2024 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
25
26#include "GNEPythonTool.h"
27#include "GNERunPythonTool.h"
28
29// ============================================-===============================
30// member method definitions
31// ===========================================================================
32
34 MFXSingleEventThread(runToolDialog->getGNEApp()->getApp(), runToolDialog->getGNEApp()),
35 myEventQueue(eq),
36 myEventThrow(ev) {
37}
38
39
41
42
43void
45 myPythonTool = tool;
46 // reset flags
47 myRunning = false;
48 myErrorOccurred = false;
49 start();
50}
51
52
53void
55 if (myRunning) {
56 // cancel thread
57 cancel();
58 // reset flags
59 myRunning = false;
60 myErrorOccurred = false;
61 // show info
62 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("cancelled by user\n"))));
64 }
65}
66
67
68bool
70 return myRunning;
71}
72
73
74bool
78
79
80FXint
82 // declare buffer
83 char buffer[128];
84 for (int i = 0; i < 128; i++) {
85 buffer[i] = '\0';
86 }
87 // open process showing std::err in console
88#ifdef WIN32
89 myPipe = _popen(StringUtils::transcodeToLocal(myPythonTool->getCommand() + " 2>&1").c_str(), "r");
90#else
91 myPipe = popen((myPythonTool->getCommand() + " 2>&1").c_str(), "r");
92#endif
93 if (!myPipe) {
94 // set error ocurred flag
95 myErrorOccurred = true;
99 return 1;
100 } else {
101 // set running flag
102 myRunning = true;
103 // Show command
105 // start process
106 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("starting process...\n"))));
108 try {
109 // add buffer
110 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 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("error processing command\n"))));
126 return 1;
127 }
128 }
129 // close process
130#ifdef WIN32
131 _pclose(myPipe);
132#else
133 pclose(myPipe);
134#endif
135 myPipe = nullptr;
136 // set running flag
137 myRunning = false;
138 // end process
139 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("process finished\n"))));
141 return 1;
142}
143
144/****************************************************************************/
@ 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:315
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)
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage