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#include <config.h>
21
22#ifdef HAVE_BOOST
23#ifdef _MSC_VER
24#pragma warning(push, 0)
25#define WIN32_LEAN_AND_MEAN
26#define NOMINMAX
27#include <windows.h>
28#include <boost/process.hpp>
29#include <boost/process/v1/child.hpp>
30#include <boost/process/v1/io.hpp>
31#pragma warning(pop)
32#else
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wall"
35#pragma GCC diagnostic ignored "-Wextra"
36#include <boost/process.hpp>
37#include <boost/process/v1/child.hpp>
38#include <boost/process/v1/io.hpp>
39#pragma GCC diagnostic pop
40#endif
41#endif
42
46
47#include "GNEExternalRunner.h"
48
49// ============================================-===============================
50// member method definitions
51// ===========================================================================
52
54 MFXSingleEventThread(applicationWindow->getApp(), applicationWindow) {
55 // set external runner in application window
56 applicationWindow->setExternalRunner(this);
57}
58
59
61
62
63void
65 // first abort any running process
66 abort();
67 // set run dialog
68 myRunDialog = runDialog;
69 // set flags
70 myRunning = false;
71 myErrorOccurred = false;
72 // start thread
73 start();
74}
75
76
77void
79 if (myRunning) {
80 // cancel thread
81 cancel();
82 // reset flags
83 myRunning = false;
84 myErrorOccurred = false;
85 // add event in runDialog
86 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("cancelled by user\n"))), true);
87 }
88}
89
90
91bool
93 return myRunning;
94}
95
96
97bool
101
102
103FXint
105// check if use boost version, or the "classic" version
106#ifdef HAVE_BOOST
107 try {
108 // declare both streams for read out and err
109 boost::process::v1::opstream in;
110 boost::process::v1::ipstream out;
111 boost::process::v1::ipstream err;
112 // declare run command
113 const auto runCommand = myRunDialog->getRunCommand();
114 // Show command
116 // run command derivating the std_out to out and std_err to err
117 boost::process::v1::child c(runCommand,
118 boost::process::v1::std_in < in,
119 boost::process::v1::std_out > out,
120 boost::process::v1::std_err > err);
121 // declare a stdout reader thread
122 std::thread outReaderThread([&out, this]() {
123 std::string buffer;
124 // read until a \n appears
125 while (std::getline(out, buffer)) {
126 // clear '\r' character
127 if (!buffer.empty() && (buffer.back() == '\r')) {
128 buffer.pop_back();
129 }
131 }
132 });
133 // declare a stderr reader thread
134 std::thread errReaderThread([&err, this]() {
135 std::string buffer;
136 // read until a \n appears
137 while (std::getline(err, buffer)) {
138 // clear '\r' character
139 if (!buffer.empty() && (buffer.back() == '\r')) {
140 buffer.pop_back();
141 }
143 }
144 });
145 // wait until child process is finish
146 c.wait();
147 // use readers for read output
148 if (outReaderThread.joinable()) {
149 outReaderThread.join();
150 }
151 if (errReaderThread.joinable()) {
152 errReaderThread.join();
153 }
154 // add a end of line
156 // return exit code
157 return c.exit_code();
158 } catch (...) {
159 return EXIT_FAILURE;
160 }
161#else
162 // get run command
163 const std::string runCommand = myRunDialog->getRunCommand();
164 // declare buffer
165 char buffer[128];
166 for (int i = 0; i < 128; i++) {
167 buffer[i] = '\0';
168 }
169 // open process showing std::err in console
170#ifdef WIN32
171 myPipe = _popen(StringUtils::transcodeToLocal(runCommand + " 2>&1").c_str(), "r");
172#else
173 myPipe = popen((runCommand + " 2>&1").c_str(), "r");
174#endif
175 if (!myPipe) {
176 // set error ocurred flag
177 myErrorOccurred = true;
178 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, "popen() failed!"), false);
180 return 1;
181 } else {
182 // set running flag
183 myRunning = true;
184 // Show command
186 // start process
187 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("starting process...\n"))), true);
188 try {
189 // add buffer
190 while (fgets(buffer, sizeof buffer, myPipe) != NULL) {
192 }
193 } catch (...) {
194 // close process
195#ifdef WIN32
196 _pclose(myPipe);
197#else
198 pclose(myPipe);
199#endif
200 // set flags
201 myRunning = false;
202 myErrorOccurred = true;
203 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("error processing command\n"))), true);
204 return 1;
205 }
206 }
207 // close process
208#ifdef WIN32
209 _pclose(myPipe);
210#else
211 pclose(myPipe);
212#endif
213 myPipe = nullptr;
214 // set running flag
215 myRunning = false;
216 // end process
217 myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("process finished\n"))), false);
219 return 1;
220#endif
221}
222
223/****************************************************************************/
@ 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:304
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