SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUIRunThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The thread that runs the 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 <cassert>
34 #include <string>
35 #include <iostream>
36 #include <algorithm>
37 
38 #include <guisim/GUINet.h>
42 #include "GUIApplicationWindow.h"
43 #include "GUIRunThread.h"
44 #include "GUIGlobals.h"
47 #include <utils/common/SysUtils.h>
52 
53 #ifndef NO_TRACI
55 #endif
56 
57 #ifdef CHECK_MEMORY_LEAKS
58 #include <foreign/nvwa/debug_new.h>
59 #endif // CHECK_MEMORY_LEAKS
60 
61 
62 // ===========================================================================
63 // member method definitions
64 // ===========================================================================
66  FXRealSpinDial& simDelay, MFXEventQue& eq,
68  : FXSingleEventThread(app, parent),
69  myNet(0), myHalting(true), myQuit(false), mySimulationInProgress(false), myOk(true),
70  mySimDelay(simDelay), myEventQue(eq), myEventThrow(ev) {
74 }
75 
76 
78  // the thread shall stop
79  myQuit = true;
80  deleteSim();
81  delete myErrorRetriever;
82  delete myMessageRetriever;
83  delete myWarningRetriever;
84  // wait for the thread
85  while (mySimulationInProgress || myNet != 0);
86 }
87 
88 
89 bool
91  assert(net != 0);
92  // assign new values
93  myNet = net;
94  mySimStartTime = start;
95  mySimEndTime = end;
96  // register message callbacks
100  // preload the routes especially for TraCI
102  try {
103  net->setCurrentTimeStep(start);
104  net->loadRoutes();
105  } catch (ProcessError& e2) {
106  if (std::string(e2.what()) != std::string("Process Error") && std::string(e2.what()) != std::string("")) {
107  WRITE_ERROR(e2.what());
108  }
109  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
110  myHalting = true;
111  myOk = false;
112  mySimulationInProgress = false;
113 #ifndef _DEBUG
114  } catch (...) {
115  myHalting = true;
116  myOk = false;
117  mySimulationInProgress = false;
118 #endif
119  }
121  return myOk;
122 }
123 
124 
125 FXint
127  long beg = 0;
128  long end = -1;
129  // perform an endless loop
130  while (!myQuit) {
131  // if the simulation shall be perfomed, do it
132  if (!myHalting && myNet != 0 && myOk) {
133  if (getNet().logSimulationDuration()) {
135  if (end != -1) {
136  getNet().setIdleDuration((int)(beg - end));
137  }
138  }
139  // check whether we shall stop at this step
141  // do the step
142  makeStep();
143  // stop if wished
144  if (haltAfter) {
145  stop();
146  }
147  // wait if wanted
148  long wait = (long) mySimDelay.getValue();
149  if (getNet().logSimulationDuration()) {
151  getNet().setSimDuration((int)(end - beg));
152  wait -= (end - beg);
153  }
154  if (wait > 0) {
155  sleep(wait);
156  }
157  } else {
158  // sleep if the simulation is not running
159  sleep(500);
160  }
161  }
162  // delete a maybe existing simulation at the end
163  deleteSim();
164  return 0;
165 }
166 
167 
168 void
170  GUIEvent* e = 0;
171  // simulation is being perfomed
172  mySimulationInProgress = true;
173  // execute a single step
174  try {
179 
180  // inform parent that a step has been performed
181  e = new GUIEvent_SimulationStep();
182  myEventQue.add(e);
184 
185  e = 0;
187 #ifndef NO_TRACI
188  if (state != MSNet::SIMSTATE_RUNNING) {
189  if (OptionsCont::getOptions().getInt("remote-port") != 0 && !TraCIServer::wasClosed()) {
190  state = MSNet::SIMSTATE_RUNNING;
191  }
192  }
193 #endif
194  switch (state) {
199  WRITE_MESSAGE("Simulation ended at time: " + time2string(myNet->getCurrentTimeStep()));
200  WRITE_MESSAGE("Reason: " + MSNet::getStateMessage(state));
202  break;
203  default:
204  break;
205  }
206  if (e != 0) {
207  myEventQue.add(e);
209  myHalting = true;
210  }
211  // stop the execution when only a single step should have
212  // been performed
213  if (mySingle) {
214  myHalting = true;
215  }
216  // simulation step is over
217  mySimulationInProgress = false;
218  } catch (ProcessError& e2) {
219  if (std::string(e2.what()) != std::string("Process Error") && std::string(e2.what()) != std::string("")) {
220  WRITE_ERROR(e2.what());
221  }
222  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
224  mySimulationInProgress = false;
226  myEventQue.add(e);
228  myHalting = true;
229  myOk = false;
230 #ifndef _DEBUG
231  } catch (...) {
233  mySimulationInProgress = false;
235  myEventQue.add(e);
237  myHalting = true;
238  myOk = false;
239 #endif
240  }
241 }
242 
243 
244 void
246  mySingle = false;
247  myHalting = false;
248 }
249 
250 
251 void
253  mySingle = true;
254  myHalting = false;
255 }
256 
257 
258 void
260  // report the begin when wished
261  WRITE_MESSAGE("Simulation started with time: " + time2string(mySimStartTime));
262  myOk = true;
263 }
264 
265 
266 void
268  mySingle = false;
269  myHalting = true;
270 }
271 
272 
273 bool
275  return myNet != 0;
276 }
277 
278 
279 void
281  myHalting = true;
282  // remove message callbacks
286  //
288  if (myNet != 0) {
290  }
291  while (mySimulationInProgress);
292  delete myNet;
294  myNet = 0;
298 }
299 
300 
301 GUINet&
303  return *myNet;
304 }
305 
306 
307 void
309  myHalting = true;
310  myQuit = true;
311 }
312 
313 
314 void
315 GUIRunThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
316  GUIEvent* e = new GUIEvent_Message(type, msg);
317  myEventQue.add(e);
319 }
320 
321 
322 bool
324  return myNet != 0 && myHalting;
325 }
326 
327 
328 bool
330  return myNet != 0 && (!myHalting);
331 }
332 
333 
334 bool
336  return myNet != 0 && myHalting;
337 }
338 
339 
340 
341 /****************************************************************************/
342 
Event sent when the the simulation is over.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
The message is only something to show.
Definition: MsgHandler.h:61
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual bool simulationIsStepable() const
virtual FXint run()
starts the execution
virtual void deleteSim()
virtual bool init(GUINet *net, SUMOTime start, SUMOTime end)
initialises the thread with the new simulation
The simulation contains too many vehicles (.
Definition: MSNet.h:106
virtual bool simulationIsStartable() const
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
void setCurrentTimeStep(const SUMOTime step)
Sets the current simulation step (used by state loading)
Definition: MSNet.h:218
The final simulation step has been performed.
Definition: MSNet.h:98
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:161
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:210
void clear()
Clears this container.
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:94
void setIdleDuration(int val)
Sets the duration of the last step's idle part.
Definition: GUINet.cpp:398
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
void add(void *what)
Definition: MFXEventQue.cpp:57
static std::vector< SUMOTime > gBreakpoints
List of breakpoints.
Definition: GUIGlobals.h:58
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
bool myHalting
information whether the simulation is halting (is not being executed)
Definition: GUIRunThread.h:121
The simulation does not contain further vehicles.
Definition: MSNet.h:100
An error occured during the simulation step.
Definition: MSNet.h:104
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:238
MFXMutex mySimulationLock
Definition: GUIRunThread.h:147
FXdouble getValue() const
Return current value.
static void sleep(long ms)
virtual ~GUIRunThread()
destructor
static void closeAll()
void closeSimulation(SUMOTime start)
Closes the simulation (all files, connections, etc.)
Definition: MSNet.cpp:314
SUMOTime mySimEndTime
Definition: GUIRunThread.h:118
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
The connection to a client was closed by the client.
Definition: MSNet.h:102
The simulation is running.
Definition: MSNet.h:96
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the.
Definition: MsgHandler.cpp:177
bool mySimulationInProgress
Definition: GUIRunThread.h:130
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
virtual bool simulationIsStopable() const
GUIRunThread(FXApp *app, MFXInterThreadEventClient *mw, FXRealSpinDial &simDelay, MFXEventQue &eq, FXEX::FXThreadEvent &ev)
constructor
OutputDevice * myWarningRetriever
Definition: GUIRunThread.h:139
GUINet * myNet
the loaded simulation network
Definition: GUIRunThread.h:115
GUINet & getNet() const
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
void unlock()
release mutex lock
Definition: MFXMutex.cpp:94
OutputDevice * myMessageRetriever
Definition: GUIRunThread.h:139
The message is a warning.
Definition: MsgHandler.h:63
void setSimDuration(int val)
Sets the duration of the last step's simulation part.
Definition: GUINet.cpp:382
Encapsulates an object's method for using it as a message retriever.
MFXEventQue & myEventQue
Definition: GUIRunThread.h:143
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on...
Definition: GUIRunThread.h:139
SimulationState simulationState(SUMOTime stopTime) const
Called after a simulation step, this method returns the current simulation state. ...
Definition: MSNet.cpp:452
void prepareDestruction()
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:90
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
void lock()
lock mutex
Definition: MFXMutex.cpp:84
bool simulationAvailable() const
static std::string getStateMessage(SimulationState state)
Returns the message to show if a certain state occurs.
Definition: MSNet.cpp:483
void simulationStep()
Performs a single simulation step (locking the simulation)
Definition: GUINet.cpp:222
#define DELTA_T
Definition: SUMOTime.h:50
virtual void begin()
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:48
void loadRoutes()
loads routes for the next few steps
Definition: MSNet.cpp:308
FXEX::FXThreadEvent & myEventThrow
Definition: GUIRunThread.h:145
SUMOTime mySimStartTime
the times the simulation starts and ends with
Definition: GUIRunThread.h:118
Spinner control.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:197
void guiSimulationStep()
Some further steps needed for gui processing.
Definition: GUINet.cpp:215
static bool wasClosed()
check whether close was requested
FXRealSpinDial & mySimDelay
Definition: GUIRunThread.h:141
The message is an error.
Definition: MsgHandler.h:65