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 /****************************************************************************/
GUINet::setIdleDuration
void setIdleDuration(int val)
Sets the duration of the last step's idle part.
Definition: GUINet.cpp:405
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:51
MsgHandler::MT_ERROR
The message is an error.
Definition: MsgHandler.h:57
GUIRunThread::mySimStartTime
SUMOTime mySimStartTime
the times the simulation starts and ends with
Definition: GUIRunThread.h:127
GUIRunThread::simulationIsStopable
virtual bool simulationIsStopable() const
Definition: GUIRunThread.cpp:329
MFXInterThreadEventClient
Definition: MFXInterThreadEventClient.h:28
GUIRunThread::singleStep
void singleStep()
Definition: GUIRunThread.cpp:252
FXSingleEventThread::sleep
static void sleep(long ms)
Definition: FXSingleEventThread.cpp:139
GUIRunThread::myBreakpointLock
FXMutex myBreakpointLock
Lock for modifying the list of breakpoints.
Definition: GUIRunThread.h:165
OutputDevice::closeAll
static void closeAll(bool keepErrorRetrievers=false)
Definition: OutputDevice.cpp:127
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
GUIRunThread::makeStep
void makeStep()
Definition: GUIRunThread.cpp:160
GUIRunThread::myHaveSignaledEnd
bool myHaveSignaledEnd
whether the simulation already ended
Definition: GUIRunThread.h:147
OptionsCont.h
MsgHandler::MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:53
GUIRunThread::begin
virtual void begin()
Definition: GUIRunThread.cpp:259
MSNet::simulationState
SimulationState simulationState(SUMOTime stopTime) const
Called after a simulation step, this method returns the current simulation state.
Definition: MSNet.cpp:578
MsgHandler.h
MsgRetrievingFunction
Encapsulates an object's method for using it as a message retriever.
Definition: MsgRetrievingFunction.h:42
MsgHandler::MT_WARNING
The message is a warning.
Definition: MsgHandler.h:55
GUIRunThread::mySingle
bool mySingle
Definition: GUIRunThread.h:144
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
GUIRunThread::myOk
bool myOk
Definition: GUIRunThread.h:141
GUIRunThread::~GUIRunThread
virtual ~GUIRunThread()
destructor
Definition: GUIRunThread.cpp:65
GUIRunThread::retrieveMessage
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
Definition: GUIRunThread.cpp:315
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
GUIRunThread::myHalting
bool myHalting
information whether the simulation is halting (is not being executed)
Definition: GUIRunThread.h:130
GUIRunThread::mySimEndTime
SUMOTime mySimEndTime
Definition: GUIRunThread.h:127
GUIEvent_Message.h
GUIEvent_SimulationEnded.h
GUIRunThread::simulationIsStartable
virtual bool simulationIsStartable() const
Definition: GUIRunThread.cpp:323
GUIRunThread.h
GUINet.h
MSNet::setCurrentTimeStep
void setCurrentTimeStep(const SUMOTime step)
Sets the current simulation step (used by state loading)
Definition: MSNet.h:292
MsgHandler::cleanupOnEnd
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:241
GUIEvent_SimulationStep.h
GUIEvent_SimulationEnded
Event sent when the the simulation is over.
Definition: GUIEvent_SimulationEnded.h:41
MSNet::closeSimulation
void closeSimulation(SUMOTime start)
Closes the simulation (all files, connections, etc.)
Definition: MSNet.cpp:436
SysUtils::getCurrentMillis
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
FXEX::FXThreadEvent::signal
void signal()
Definition: FXThreadEvent.cpp:100
GUIRunThread::mySimDelay
double & mySimDelay
Definition: GUIRunThread.h:153
MSNet::SIMSTATE_NO_FURTHER_VEHICLES
The simulation does not contain further vehicles.
Definition: MSNet.h:105
TraCIServer::getLoadArgs
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:249
MSNet::SimulationState
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:97
GUIRunThread::simulationAvailable
bool simulationAvailable() const
Definition: GUIRunThread.cpp:274
Simulation.h
GUIRunThread::mySimulationInProgress
bool mySimulationInProgress
Definition: GUIRunThread.h:139
GUIMainWindow::getInstance
static GUIMainWindow * getInstance()
Definition: GUIMainWindow.cpp:183
MSNet::SIMSTATE_END_STEP_REACHED
The final simulation step has been performed.
Definition: MSNet.h:103
GUIRunThread::mySimulationLock
FXMutex mySimulationLock
Definition: GUIRunThread.h:159
GUIRunThread::getNet
GUINet & getNet() const
Definition: GUIRunThread.cpp:302
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:72
TS
#define TS
Definition: SUMOTime.h:44
SysUtils.h
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
GUIRunThread::myWarningRetriever
OutputDevice * myWarningRetriever
Definition: GUIRunThread.h:151
GUIRunThread::myNet
GUINet * myNet
the loaded simulation network
Definition: GUIRunThread.h:124
GUIGlobals.h
MSNet::SIMSTATE_ERROR_IN_SIM
An error occurred during the simulation step.
Definition: MSNet.h:109
GUIRunThread::simulationIsStepable
virtual bool simulationIsStepable() const
Definition: GUIRunThread.cpp:335
OutputDevice.h
GUIRunThread::resume
void resume()
Definition: GUIRunThread.cpp:245
ProcessError
Definition: UtilExceptions.h:40
GUIApplicationWindow.h
GUIRunThread::myMessageRetriever
OutputDevice * myMessageRetriever
Definition: GUIRunThread.h:151
GUIRunThread::myQuit
bool myQuit
Definition: GUIRunThread.h:134
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
MSNet::SIMSTATE_LOADING
The simulation is loading.
Definition: MSNet.h:99
UtilExceptions.h
GUIRunThread::stop
void stop()
Definition: GUIRunThread.cpp:267
MSNet::loadRoutes
void loadRoutes()
loads routes for the next few steps
Definition: MSNet.cpp:375
GUIRunThread::myErrorRetriever
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
Definition: GUIRunThread.h:151
GUIMainWindow::getViews
const std::vector< GUIGlChildWindow * > & getViews() const
Definition: GUIMainWindow.h:61
GUIRunThread::myBreakpoints
std::vector< SUMOTime > myBreakpoints
List of breakpoints.
Definition: GUIRunThread.h:162
GUIRunThread::waitForSnapshots
void waitForSnapshots(const SUMOTime snapshotTime)
Definition: GUIRunThread.cpp:341
MSNet::SIMSTATE_CONNECTION_CLOSED
The connection to a client was closed by the client.
Definition: MSNet.h:107
FXSynchQue< GUIEvent * >
GUIRunThread::myEventQue
FXSynchQue< GUIEvent * > & myEventQue
Definition: GUIRunThread.h:155
GUIMainWindow
Definition: GUIMainWindow.h:47
MSNet::SIMSTATE_RUNNING
The simulation is running.
Definition: MSNet.h:101
GUIEvent
Definition: GUIEvent.h:77
GUIRunThread::myEventThrow
FXEX::FXThreadEvent & myEventThrow
Definition: GUIRunThread.h:157
GUIGlObjectStorage::gIDStorage
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
Definition: GUIGlObjectStorage.h:141
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:166
GUIEvent_SimulationStep
Definition: GUIEvent_SimulationStep.h:37
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
TraCIServer.h
MsgRetrievingFunction.h
FXSynchQue::push_back
void push_back(T what)
Definition: FXSynchQue.h:91
config.h
TraCIServer::getInstance
static TraCIServer * getInstance()
Definition: TraCIServer.h:71
FXSingleEventThread
Definition: FXSingleEventThread.h:35
TraCIServer::wasClosed
static bool wasClosed()
check whether close was requested
Definition: TraCIServer.cpp:313
FXEX::FXThreadEvent
Definition: FXThreadEvent.h:106
GUIGlChildWindow
Definition: GUIGlChildWindow.h:41
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
MSNet::getStateMessage
static std::string getStateMessage(SimulationState state)
Returns the message to show if a certain state occurs.
Definition: MSNet.cpp:614
GUINet
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:83
GUINet::simulationStep
void simulationStep()
Performs a single simulation step (locking the simulation)
Definition: GUINet.cpp:224
GUINet::setSimDuration
void setSimDuration(int val)
Sets the duration of the last step's simulation part.
Definition: GUINet.cpp:389
MSVehicleControl.h
MSNet::SIMSTATE_TOO_MANY_TELEPORTS
The simulation had too many teleports.
Definition: MSNet.h:113
GUIRunThread::run
virtual FXint run()
starts the execution
Definition: GUIRunThread.cpp:118
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
GUINet::guiSimulationStep
void guiSimulationStep()
Some further steps needed for gui processing.
Definition: GUINet.cpp:217
GUIEvent_Message
Definition: GUIEvent_Message.h:39
GUIRunThread::deleteSim
virtual void deleteSim()
Definition: GUIRunThread.cpp:280
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
GUIRunThread::init
virtual bool init(GUINet *net, SUMOTime start, SUMOTime end)
initialises the thread with the new simulation
Definition: GUIRunThread.cpp:78
OptionsIO.h
GUIRunThread::prepareDestruction
void prepareDestruction()
Definition: GUIRunThread.cpp:308
GUIGlObjectStorage::clear
void clear()
Clears this container.
Definition: GUIGlObjectStorage.cpp:112
GUIRunThread::GUIRunThread
GUIRunThread(FXApp *app, MFXInterThreadEventClient *mw, double &simDelay, FXSynchQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
Definition: GUIRunThread.cpp:53
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:174
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59