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_MESOSIM
64 #include <mesosim/MEVehicleControl.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;
99 
100  // within gui-based applications, nothing is reported to the console
104  // register message callbacks
108 
109  // try to load the given configuration
110  if (!initOptions()) {
111  // the options are not valid but maybe we want to quit
112  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
113  submitEndAndCleanup(net, simStartTime, simEndTime);
114  return 0;
115  }
116  // do this once again to get parsed options
118  GUIGlobals::gRunAfterLoad = oc.getBool("start");
119  GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
120  if (!MSFrame::checkOptions()) {
121  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
122  submitEndAndCleanup(net, simStartTime, simEndTime);
123  return 0;
124  }
125 
126  // initialise global settings
129  gAllowTextures = !oc.getBool("disable-textures");
130  MSVehicleControl* vehControl = 0;
131 #ifdef HAVE_MESOSIM
132  GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
134  vehControl = new MEVehicleControl();
135  } else
136 #endif
137  vehControl = new GUIVehicleControl();
138 
139  net = new GUINet(
140  vehControl,
141  new GUIEventControl(),
142  new GUIEventControl(),
143  new GUIEventControl());
145  GUIDetectorBuilder db(*net);
146  NLJunctionControlBuilder jb(*net, db);
148  NLHandler handler("", *net, db, tb, *eb, jb);
149  tb.setHandler(&handler);
150  NLBuilder builder(oc, *net, *eb, jb, db, handler);
151  try {
155  if (!builder.build()) {
156  throw ProcessError();
157  } else {
158  net->initGUIStructures();
159  simStartTime = string2time(oc.getString("begin"));
160  simEndTime = string2time(oc.getString("end"));
161  }
162  } catch (ProcessError& e) {
163  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
164  WRITE_ERROR(e.what());
165  }
166  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
167  delete net;
168  net = 0;
169 #ifndef _DEBUG
170  } catch (std::exception& e) {
171  WRITE_ERROR(e.what());
172  delete net;
173  net = 0;
174 #endif
175  }
176  if (net == 0) {
177  MSNet::clearAll();
178  }
179  delete eb;
180  submitEndAndCleanup(net, simStartTime, simEndTime);
181  return 0;
182 }
183 
184 
185 
186 void
188  SUMOTime simStartTime,
189  SUMOTime simEndTime) {
190  // remove message callbacks
194  // inform parent about the process
195  GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile,
196  OptionsCont::getOptions().getString("gui-settings-file"));
197  myEventQue.add(e);
199 }
200 
201 
202 bool
204  try {
206  oc.clear();
208  if (myFile != "") {
209  if (myLoadNet) {
210  oc.set("net-file", myFile);
211  } else {
212  oc.set("configuration-file", myFile);
213  }
214  OptionsIO::getOptions(true, 1, 0);
215  } else {
216  OptionsIO::getOptions(true);
217  }
218  return true;
219  } catch (ProcessError& e) {
220  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
221  WRITE_ERROR(e.what());
222  }
223  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
224  }
225  return false;
226 }
227 
228 
229 void
230 GUILoadThread::load(const std::string& file, bool isNet) {
231  myFile = file;
232  myLoadNet = isNet;
233  start();
234 }
235 
236 
237 void
238 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
239  GUIEvent* e = new GUIEvent_Message(type, msg);
240  myEventQue.add(e);
242 }
243 
244 
245 const std::string&
247  return myFile;
248 }
249 
250 
251 
252 /****************************************************************************/
253