Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNERunNetgenerate.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 netgenerate tool
19/****************************************************************************/
20
24
25#include "GNERunNetgenerate.h"
26
27// ============================================-===============================
28// member method definitions
29// ===========================================================================
30
32 MFXSingleEventThread(runDialog->getGNEApp()->getApp(), runDialog->getGNEApp()),
33 myEventQueue(eq),
34 myEventThrow(ev) {
35}
36
37
39
40
41void
42GNERunNetgenerate::run(const OptionsCont* netgenerateOptions) {
43 // set command
44
45#ifdef WIN32
46 std::string exePath = "netgenerate.exe";
47#else
48 std::string exePath = "netgenerate";
49#endif
50 const char* sumoHomeEnv = getenv("SUMO_HOME");
51 std::string sumoHome = "";
52 if (sumoHomeEnv != nullptr && sumoHomeEnv != std::string("")) {
53 sumoHome = std::string(sumoHomeEnv);
54 // prevent double quotes
55 if (sumoHome.front() == '"') {
56 sumoHome.erase(sumoHome.begin());
57 }
58 if (sumoHome.size() > 0 && sumoHome.back() == '"') {
59 sumoHome.pop_back();
60 }
61 sumoHome += "/bin/";
62 }
63 // quote to handle spaces. note that this differs from GNEPythonTool because the python interpreter is a bit smarter
64 // when handling quoted parts within a path
65 myNetgenerateCommand = "\"" + sumoHome + exePath + "\"";
66
67 // iterate over all topics
68 for (const auto& topic : netgenerateOptions->getSubTopics()) {
69 // ignore configuration
70 if (topic != "Configuration") {
71 const std::vector<std::string> entries = netgenerateOptions->getSubTopicsEntries(topic);
72 for (const auto& entry : entries) {
73 if (!netgenerateOptions->isDefault(entry)) {
74 myNetgenerateCommand += " --" + entry + " " + netgenerateOptions->getValueString(entry);
75 }
76 }
77 }
78 }
79 // reset flags
80 myRunning = false;
81 myErrorOccurred = false;
82 start();
83}
84
85
86void
88 if (myRunning) {
89 // cancel thread
90 cancel();
91 // reset flags
92 myRunning = false;
93 myErrorOccurred = false;
94 // show info
95 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("cancelled by user\n"))));
97 }
98}
99
100
101bool
103 return myRunning;
104}
105
106
107bool
111
112
113FXint
115 // declare buffer
116 char buffer[128];
117 for (int i = 0; i < 128; i++) {
118 buffer[i] = '\0';
119 }
120 // open process showing std::err in console
121#ifdef WIN32
122 myPipe = _popen((myNetgenerateCommand + " 2>&1").c_str(), "r");
123#else
124 myPipe = popen((myNetgenerateCommand + " 2>&1").c_str(), "r");
125#endif
126 if (!myPipe) {
127 // set error ocurred flag
128 myErrorOccurred = true;
132 return 1;
133 } else {
134 // set running flag
135 myRunning = true;
136 // Show command
138 // start process
139 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("starting process...\n"))));
141 try {
142 // add buffer
143 while (fgets(buffer, sizeof buffer, myPipe) != NULL) {
146 }
147 } catch (...) {
148 // close process
149#ifdef WIN32
150 _pclose(myPipe);
151#else
152 pclose(myPipe);
153#endif
154 // set flags
155 myRunning = false;
156 myErrorOccurred = true;
157 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::ERROR_OCCURRED, std::string(TL("error processing command\n"))));
159 return 1;
160 }
161 }
162 // close process
163#ifdef WIN32
164 _pclose(myPipe);
165#else
166 pclose(myPipe);
167#endif
168 myPipe = nullptr;
169 // set running flag
170 myRunning = false;
171 // end process
172 myEventQueue.push_back(new GUIEvent_Message(GUIEventType::MESSAGE_OCCURRED, std::string(TL("process finished\n"))));
175 return 1;
176}
177
178/****************************************************************************/
@ 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
Abstract dialog for tools.
FXEX::MFXThreadEvent & myEventThrow
event throw
bool myErrorOccurred
flag for check if during execution an error was Occurred
FILE * myPipe
pipe file
bool errorOccurred() const
check if during execution an error was Occurred
void abort()
abort netgenerate running
bool myRunning
flag for check if netgenerate is running
~GNERunNetgenerate()
destructor
FXint run()
starts the thread. The thread ends after the netgenerate is finished
MFXSynchQue< GUIEvent * > & myEventQueue
event Queue
GNERunNetgenerate(GNERunNetgenerateDialog *runDialog, MFXSynchQue< GUIEvent * > &eq, FXEX::MFXThreadEvent &ev)
Constructor.
bool isRunning() const
check if netgenerate is running
std::string myNetgenerateCommand
netgenerate command
void push_back(T what)
A storage for options typed value containers)
Definition OptionsCont.h:89
const std::vector< std::string > & getSubTopics() const
return the list of subtopics
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
std::string getValueString(const std::string &name) const
Returns the string-value of the named option (all options)
std::vector< std::string > getSubTopicsEntries(const std::string &subtopic) const
return the list of entries for the given subtopic