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