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.sourceforge.net/
12 // Copyright (C) 2001-2012 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 <guisim/GUINet.h>
35 #include <guisim/GUIEventControl.h>
36 #include <netload/NLBuilder.h>
37 #include <netload/NLHandler.h>
46 #include <utils/options/Option.h>
50 #include <microsim/MSFrame.h>
52 #include "GUIApplicationWindow.h"
53 #include "GUILoadThread.h"
54 #include "GUIGlobals.h"
61 #include <ctime>
62 
63 #ifdef HAVE_INTERNAL
64 #include <mesogui/GUIMEVehicleControl.h>
65 #endif
66 
67 #ifdef CHECK_MEMORY_LEAKS
68 #include <foreign/nvwa/debug_new.h>
69 #endif // CHECK_MEMORY_LEAKS
70 
71 
72 // ===========================================================================
73 // member method definitions
74 // ===========================================================================
77  : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
78  myEventThrow(ev) {
83 }
84 
85 
87  delete myErrorRetriever;
88  delete myMessageRetriever;
89  delete myWarningRetriever;
90 }
91 
92 
93 FXint
95  GUINet* net = 0;
96  int simStartTime = 0;
97  int simEndTime = 0;
98  std::vector<std::string> guiSettingsFiles;
100 
101  // within gui-based applications, nothing is reported to the console
105  // register message callbacks
109 
110  // try to load the given configuration
111  if (!initOptions()) {
112  // the options are not valid but maybe we want to quit
113  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
114  submitEndAndCleanup(net, simStartTime, simEndTime);
115  return 0;
116  }
117  // do this once again to get parsed options
119  GUIGlobals::gRunAfterLoad = oc.getBool("start");
120  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
121 
122  if (!MSFrame::checkOptions()) {
123  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
124  submitEndAndCleanup(net, simStartTime, simEndTime);
125  return 0;
126  }
127 
128  // initialise global settings
132  gAllowTextures = !oc.getBool("disable-textures");
133  MSVehicleControl* vehControl = 0;
134 #ifdef HAVE_INTERNAL
135  GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
137  vehControl = new GUIMEVehicleControl();
138  } else
139 #endif
140  vehControl = new GUIVehicleControl();
141 
142  net = new GUINet(
143  vehControl,
144  new GUIEventControl(),
145  new GUIEventControl(),
146  new GUIEventControl());
148  GUIDetectorBuilder db(*net);
149  NLJunctionControlBuilder jb(*net, db);
151  NLHandler handler("", *net, db, tb, *eb, jb);
152  tb.setHandler(&handler);
153  NLBuilder builder(oc, *net, *eb, jb, db, handler);
154  try {
158  if (!builder.build()) {
159  throw ProcessError();
160  } else {
161  net->initGUIStructures();
162  simStartTime = string2time(oc.getString("begin"));
163  simEndTime = string2time(oc.getString("end"));
164  guiSettingsFiles = oc.getStringVector("gui-settings-file");
165  }
166  } catch (ProcessError& e) {
167  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
168  WRITE_ERROR(e.what());
169  }
170  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
171  delete net;
172  net = 0;
173 #ifndef _DEBUG
174  } catch (std::exception& e) {
175  WRITE_ERROR(e.what());
176  delete net;
177  net = 0;
178 #endif
179  }
180  if (net == 0) {
181  MSNet::clearAll();
182  }
183  delete eb;
184  submitEndAndCleanup(net, simStartTime, simEndTime, guiSettingsFiles);
185  return 0;
186 }
187 
188 
189 
190 void
192  SUMOTime simStartTime,
193  SUMOTime simEndTime,
194  const std::vector<std::string>& guiSettingsFiles) {
195  // remove message callbacks
199  // inform parent about the process
200  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile, guiSettingsFiles);
201  myEventQue.add(e);
203 }
204 
205 
206 bool
208  try {
210  oc.clear();
212  if (myFile != "") {
213  if (myLoadNet) {
214  oc.set("net-file", myFile);
215  } else {
216  oc.set("configuration-file", myFile);
217  }
218  OptionsIO::getOptions(true, 1, 0);
219  } else {
220  OptionsIO::getOptions(true);
221  }
222  return true;
223  } catch (ProcessError& e) {
224  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
225  WRITE_ERROR(e.what());
226  }
227  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
228  }
229  return false;
230 }
231 
232 
233 void
234 GUILoadThread::load(const std::string& file, bool isNet) {
235  myFile = file;
236  myLoadNet = isNet;
237  start();
238 }
239 
240 
241 void
242 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
243  GUIEvent* e = new GUIEvent_Message(type, msg);
244  myEventQue.add(e);
246 }
247 
248 
249 const std::string&
251  return myFile;
252 }
253 
254 
255 
256 /****************************************************************************/
257