SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUILoadThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Class describing the thread that performs the loading of a simulation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <ctime>
40 #include <utils/options/Option.h>
47 #include <utils/xml/XMLSubSys.h>
48 #include <guisim/GUINet.h>
49 #include <guisim/GUIEventControl.h>
51 #include <netload/NLBuilder.h>
52 #include <netload/NLHandler.h>
58 #include <microsim/MSFrame.h>
59 #include "GUIApplicationWindow.h"
60 #include "GUILoadThread.h"
61 #include "GUIGlobals.h"
63 
64 #ifdef HAVE_INTERNAL
65 #include <mesogui/GUIMEVehicleControl.h>
66 #endif
67 
68 #ifndef NO_TRACI
70 #include "TraCIServerAPI_GUI.h"
71 #endif
72 
73 #ifdef CHECK_MEMORY_LEAKS
74 #include <foreign/nvwa/debug_new.h>
75 #endif // CHECK_MEMORY_LEAKS
76 
77 
78 // ===========================================================================
79 // member method definitions
80 // ===========================================================================
83  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
84  myEventThrow(ev) {
89 }
90 
91 
93  delete myErrorRetriever;
94  delete myMessageRetriever;
95  delete myWarningRetriever;
96 }
97 
98 
99 FXint
101  GUINet* net = 0;
102  int simStartTime = 0;
103  int simEndTime = 0;
104  std::vector<std::string> guiSettingsFiles;
105  bool osgView = false;
107 
108  // register message callbacks
111  if (!OptionsCont::getOptions().getBool("no-warnings")) {
113  }
114 
115  // try to load the given configuration
116  if (!initOptions()) {
117  // the options are not valid but maybe we want to quit
118  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
119  submitEndAndCleanup(net, simStartTime, simEndTime);
120  return 0;
121  }
122  // within gui-based applications, nothing is reported to the console
126  // do this once again to get parsed options
128  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
129  GUIGlobals::gRunAfterLoad = oc.getBool("start");
130  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
131 
132  if (!MSFrame::checkOptions()) {
133  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
134  submitEndAndCleanup(net, simStartTime, simEndTime);
135  return 0;
136  }
137 
138  // initialise global settings
142  GUITexturesHelper::allowTextures(!oc.getBool("disable-textures"));
143  MSVehicleControl* vehControl = 0;
144 #ifdef HAVE_INTERNAL
145  GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
147  vehControl = new GUIMEVehicleControl();
148  } else
149 #endif
150  vehControl = new GUIVehicleControl();
151 
152  GUIEdgeControlBuilder* eb = 0;
153  try {
154  net = new GUINet(
155  vehControl,
156  new GUIEventControl(),
157  new GUIEventControl(),
158  new GUIEventControl());
159 #ifndef NO_TRACI
160  // need to init TraCI-Server before loading routes to catch VEHICLE_STATE_BUILT
161  std::map<int, TraCIServer::CmdExecutor> execs;
165 #endif
166 
167  eb = new GUIEdgeControlBuilder();
168  GUIDetectorBuilder db(*net);
169  NLJunctionControlBuilder jb(*net, db);
171  NLHandler handler("", *net, db, tb, *eb, jb);
172  tb.setHandler(&handler);
173  NLBuilder builder(oc, *net, *eb, jb, db, handler);
177  if (!builder.build()) {
178  throw ProcessError();
179  } else {
180  net->initGUIStructures();
181  simStartTime = string2time(oc.getString("begin"));
182  simEndTime = string2time(oc.getString("end"));
183  guiSettingsFiles = oc.getStringVector("gui-settings-file");
184 #ifdef HAVE_INTERNAL
185  osgView = oc.getBool("osg-view");
186 #endif
187  }
188  } catch (ProcessError& e) {
189  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
190  WRITE_ERROR(e.what());
191  }
192  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
193  delete net;
194  net = 0;
195 #ifndef _DEBUG
196  } catch (std::exception& e) {
197  WRITE_ERROR(e.what());
198  delete net;
199  net = 0;
200 #endif
201  }
202  if (net == 0) {
203  MSNet::clearAll();
204  }
205  delete eb;
206  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles, osgView);
207  return 0;
208 }
209 
210 
211 
212 void
214  const SUMOTime simStartTime,
215  const SUMOTime simEndTime,
216  const std::vector<std::string>& guiSettingsFiles,
217  const bool osgView) {
218  // remove message callbacks
222  // inform parent about the process
223  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile, guiSettingsFiles, osgView);
224  myEventQue.add(e);
226 }
227 
228 
229 bool
231  try {
233  oc.clear();
235  if (myFile != "") {
236  if (myLoadNet) {
237  oc.set("net-file", myFile);
238  } else {
239  oc.set("configuration-file", myFile);
240  }
241  OptionsIO::getOptions(true, 1, 0);
242  } else {
243  OptionsIO::getOptions(true);
244  }
245  return true;
246  } catch (ProcessError& e) {
247  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
248  WRITE_ERROR(e.what());
249  }
250  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
251  }
252  return false;
253 }
254 
255 
256 void
257 GUILoadThread::load(const std::string& file, bool isNet) {
258  myFile = file;
259  myLoadNet = isNet;
260  start();
261 }
262 
263 
264 void
265 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
266  GUIEvent* e = new GUIEvent_Message(type, msg);
267  myEventQue.add(e);
269 }
270 
271 
272 const std::string&
274  return myFile;
275 }
276 
277 
278 
279 /****************************************************************************/
280