Eclipse SUMO - Simulation of Urban MObility
GUIRunThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // The thread that runs the simulation
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cassert>
27 #include <string>
28 #include <iostream>
29 #include <algorithm>
30 
31 #include <guisim/GUINet.h>
35 #include "GUIApplicationWindow.h"
36 #include "GUIRunThread.h"
37 #include "GUIGlobals.h"
41 #include <utils/common/SysUtils.h>
47 #include <libsumo/Simulation.h>
48 
49 
50 // ===========================================================================
51 // member method definitions
52 // ===========================================================================
54  double& simDelay, FXSynchQue<GUIEvent*>& eq,
56  : FXSingleEventThread(app, parent),
57  myNet(nullptr), myHalting(true), myQuit(false), mySimulationInProgress(false), myOk(true), myHaveSignaledEnd(false),
58  mySimDelay(simDelay), myEventQue(eq), myEventThrow(ev) {
62 }
63 
64 
66  // the thread shall stop
67  myQuit = true;
68  deleteSim();
69  delete myErrorRetriever;
70  delete myMessageRetriever;
71  delete myWarningRetriever;
72  // wait for the thread
73  while (mySimulationInProgress || myNet != nullptr);
74 }
75 
76 
77 bool
79  assert(net != 0);
80  // assign new values
81  myOk = true;
82  myNet = net;
83  mySimStartTime = start;
84  mySimEndTime = end;
85  // register message callbacks
88  if (!OptionsCont::getOptions().getBool("no-warnings")) {
90  }
91  // preload the routes especially for TraCI
92  mySimulationLock.lock();
93  try {
94  net->setCurrentTimeStep(start);
95  net->loadRoutes();
96  } catch (ProcessError& e2) {
97  if (std::string(e2.what()) != std::string("Process Error") && std::string(e2.what()) != std::string("")) {
98  WRITE_ERROR(e2.what());
99  }
100  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
101  myHalting = true;
102  myOk = false;
103  mySimulationInProgress = false;
104 #ifndef _DEBUG
105  } catch (...) {
106  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
107  myHalting = true;
108  myOk = false;
109  mySimulationInProgress = false;
110 #endif
111  }
112  mySimulationLock.unlock();
113  return myOk;
114 }
115 
116 
117 FXint
119  long beg = 0;
120  long end = -1;
121  // perform an endless loop
122  while (!myQuit) {
123  // if the simulation shall be perfomed, do it
124  if (!myHalting && myNet != nullptr && myOk) {
126  if (end != -1) {
127  getNet().setIdleDuration((int)(beg - end));
128  }
129  // check whether we shall stop at this step
130  myBreakpointLock.lock();
131  const bool haltAfter = std::find(myBreakpoints.begin(), myBreakpoints.end(), myNet->getCurrentTimeStep()) != myBreakpoints.end();
132  myBreakpointLock.unlock();
133  // do the step
134  makeStep();
136  // stop if wished
137  if (haltAfter) {
138  stop();
139  }
140  // wait if wanted (delay is per simulated second)
141  long wait = (long)(mySimDelay * TS);
143  getNet().setSimDuration((int)(end - beg));
144  wait -= (end - beg);
145  if (wait > 0) {
146  sleep(wait);
147  }
148  } else {
149  // sleep if the simulation is not running
150  sleep(50);
151  }
152  }
153  // delete a maybe existing simulation at the end
154  deleteSim();
155  return 0;
156 }
157 
158 
159 void
161  GUIEvent* e = nullptr;
162  // simulation is being perfomed
163  mySimulationInProgress = true;
164  // execute a single step
165  try {
166  mySimulationLock.lock();
169  mySimulationLock.unlock();
170 
171  // inform parent that a step has been performed
172  e = new GUIEvent_SimulationStep();
175 
176  e = nullptr;
178  if (state == MSNet::SIMSTATE_LOADING) {
181  } else if (state != MSNet::SIMSTATE_RUNNING) {
182  if (TraCIServer::getInstance() != nullptr && !TraCIServer::wasClosed()) {
183  state = MSNet::SIMSTATE_RUNNING;
184  }
185  }
186  switch (state) {
193  WRITE_MESSAGE("Simulation ended at time: " + time2string(myNet->getCurrentTimeStep()));
194  WRITE_MESSAGE("Reason: " + MSNet::getStateMessage(state));
196  // ensure that files are closed (deleteSim is called a bit later by the gui thread)
197  // MSNet destructor may trigger MsgHandler (via routing device cleanup). Closing output devices here is not safe
198  // OutputDevice::closeAll();
199  myHaveSignaledEnd = true;
200  }
201  break;
202  default:
203  break;
204  }
205  if (e != nullptr) {
208  myHalting = true;
209  }
210  // stop the execution when only a single step should have
211  // been performed
212  if (mySingle) {
213  myHalting = true;
214  }
215  // simulation step is over
216  mySimulationInProgress = false;
217  } catch (ProcessError& e2) {
218  if (std::string(e2.what()) != std::string("Process Error") && std::string(e2.what()) != std::string("")) {
219  WRITE_ERROR(e2.what());
220  }
221  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
222  mySimulationLock.unlock();
223  mySimulationInProgress = false;
227  myHalting = true;
228  myOk = false;
229 #ifndef _DEBUG
230  } catch (...) {
231  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
232  mySimulationLock.unlock();
233  mySimulationInProgress = false;
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 != nullptr;
276 }
277 
278 
279 void
281  myHalting = true;
282  // remove message callbacks
286  //
287  mySimulationLock.lock();
288  if (myNet != nullptr) {
290  }
291  while (mySimulationInProgress);
292  delete myNet;
294  myNet = nullptr;
296  mySimulationLock.unlock();
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);
319 }
320 
321 
322 bool
324  return myNet != nullptr && myHalting;
325 }
326 
327 
328 bool
330  return myNet != nullptr && (!myHalting);
331 }
332 
333 
334 bool
336  return myNet != nullptr && myHalting;
337 }
338 
339 
340 void
343  if (mw != nullptr) {
344  for (GUIGlChildWindow* const window : mw->getViews()) {
345  window->getView()->waitForSnapshots(snapshotTime);
346  }
347  }
348 }
349 
350 
351 /****************************************************************************/
Event sent when the the simulation is over.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:72
The message is only something to show.
Definition: MsgHandler.h:53
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
long long int SUMOTime
Definition: SUMOTime.h:35
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
double & mySimDelay
Definition: GUIRunThread.h:153
GUINet & getNet() const
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
virtual bool simulationIsStopable() const
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
void setCurrentTimeStep(const SUMOTime step)
Sets the current simulation step (used by state loading)
Definition: MSNet.h:292
The final simulation step has been performed.
Definition: MSNet.h:103
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:166
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:249
#define TS
Definition: SUMOTime.h:44
void clear()
Clears this container.
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:97
virtual bool simulationIsStepable() const
void setIdleDuration(int val)
Sets the duration of the last step&#39;s idle part.
Definition: GUINet.cpp:405
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp: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:130
bool myHaveSignaledEnd
whether the simulation already ended
Definition: GUIRunThread.h:147
void push_back(T what)
Definition: FXSynchQue.h:91
The simulation does not contain further vehicles.
Definition: MSNet.h:105
An error occurred during the simulation step.
Definition: MSNet.h:109
std::vector< SUMOTime > myBreakpoints
List of breakpoints.
Definition: GUIRunThread.h:162
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:241
SimulationState simulationState(SUMOTime stopTime) const
Called after a simulation step, this method returns the current simulation state. ...
Definition: MSNet.cpp:578
static void sleep(long ms)
virtual ~GUIRunThread()
destructor
static GUIMainWindow * getInstance()
void closeSimulation(SUMOTime start)
Closes the simulation (all files, connections, etc.)
Definition: MSNet.cpp:436
SUMOTime mySimEndTime
Definition: GUIRunThread.h:127
FXSynchQue< GUIEvent * > & myEventQue
Definition: GUIRunThread.h:155
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
virtual bool simulationIsStartable() const
The connection to a client was closed by the client.
Definition: MSNet.h:107
The simulation is running.
Definition: MSNet.h:101
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
bool simulationAvailable() const
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:174
bool mySimulationInProgress
Definition: GUIRunThread.h:139
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59
OutputDevice * myWarningRetriever
Definition: GUIRunThread.h:151
GUINet * myNet
the loaded simulation network
Definition: GUIRunThread.h:124
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
OutputDevice * myMessageRetriever
Definition: GUIRunThread.h:151
The message is a warning.
Definition: MsgHandler.h:55
void setSimDuration(int val)
Sets the duration of the last step&#39;s simulation part.
Definition: GUINet.cpp:389
FXMutex mySimulationLock
Definition: GUIRunThread.h:159
Encapsulates an object&#39;s method for using it as a message retriever.
The simulation is loading.
Definition: MSNet.h:99
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on...
Definition: GUIRunThread.h:151
void prepareDestruction()
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:83
const std::vector< GUIGlChildWindow * > & getViews() const
Definition: GUIMainWindow.h:61
void waitForSnapshots(const SUMOTime snapshotTime)
static TraCIServer * getInstance()
Definition: TraCIServer.h:71
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
The simulation had too many teleports.
Definition: MSNet.h:113
FXMutex myBreakpointLock
Lock for modifying the list of breakpoints.
Definition: GUIRunThread.h:165
static void closeAll(bool keepErrorRetrievers=false)
static std::string getStateMessage(SimulationState state)
Returns the message to show if a certain state occurs.
Definition: MSNet.cpp:614
void simulationStep()
Performs a single simulation step (locking the simulation)
Definition: GUINet.cpp:224
virtual void begin()
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
void loadRoutes()
loads routes for the next few steps
Definition: MSNet.cpp:375
FXEX::FXThreadEvent & myEventThrow
Definition: GUIRunThread.h:157
SUMOTime mySimStartTime
the times the simulation starts and ends with
Definition: GUIRunThread.h:127
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
void guiSimulationStep()
Some further steps needed for gui processing.
Definition: GUINet.cpp:217
GUIRunThread(FXApp *app, MFXInterThreadEventClient *mw, double &simDelay, FXSynchQue< GUIEvent *> &eq, FXEX::FXThreadEvent &ev)
constructor
static bool wasClosed()
check whether close was requested
The message is an error.
Definition: MsgHandler.h:57