Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/GNEExternalRunner.cpp
169666 views
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
/****************************************************************************/
14
/// @file GNEExternalRunner.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Mar 2023
17
///
18
// External runner for python and external tools
19
/****************************************************************************/
20
21
#include <netedit/GNEApplicationWindow.h>
22
#include <netedit/dialogs/run/GNERunDialog.h>
23
#include <utils/gui/events/GUIEvent_Message.h>
24
25
#include "GNEExternalRunner.h"
26
27
// ============================================-===============================
28
// member method definitions
29
// ===========================================================================
30
31
GNEExternalRunner::GNEExternalRunner(GNEApplicationWindow* applicationWindow) :
32
MFXSingleEventThread(applicationWindow->getApp(), applicationWindow) {
33
// set external runner in application window
34
applicationWindow->setExternalRunner(this);
35
}
36
37
38
GNEExternalRunner::~GNEExternalRunner() {}
39
40
41
void
42
GNEExternalRunner::runTool(GNERunDialog* runDialog) {
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
55
void
56
GNEExternalRunner::abort() {
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
69
bool
70
GNEExternalRunner::isRunning() const {
71
return myRunning;
72
}
73
74
75
bool
76
GNEExternalRunner::errorOccurred() const {
77
return myErrorOccurred;
78
}
79
80
81
FXint
82
GNEExternalRunner::run() {
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;
99
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, "popen() failed!"), false);
100
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::TOOL_ENDED, ""), true);
101
return 1;
102
} else {
103
// set running flag
104
myRunning = true;
105
// Show command
106
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::OUTPUT_OCCURRED, runCommand + "\n"), false);
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) {
112
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::OUTPUT_OCCURRED, buffer), true);
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);
139
myRunDialog->addEvent(new GUIEvent_Message(GUIEventType::TOOL_ENDED, ""), true);
140
return 1;
141
}
142
143
/****************************************************************************/
144
145