#include <GUIRunThread.h>

Definition at line 62 of file GUIRunThread.h.
Public Types | |
| enum | { ID_THREAD_EVENT } |
Public Member Functions | |
| virtual void | begin () |
| virtual void | deleteSim () |
| GUINet & | getNet () const |
| GUIRunThread (MFXInterThreadEventClient *mw, FXRealSpinDial &simDelay, MFXEventQue &eq, FXEX::FXThreadEvent &ev) | |
| constructor | |
| virtual void | init (GUINet *net, SUMOTime start, SUMOTime end) |
| initialises the thread with the new simulation | |
| long | onThreadEvent (FXObject *, FXSelector, void *) |
| long | onThreadSignal (FXObject *, FXSelector, void *) |
| void | prepareDestruction () |
| void | resume () |
| void | retrieveMessage (const MsgHandler::MsgType type, const std::string &msg) |
| Retrieves messages from the loading module. | |
| virtual FXint | run () |
| starts the execution | |
| void | signal (FXuint seltype) |
| void | signal () |
| bool | simulationAvailable () const |
| virtual bool | simulationIsStartable () const |
| virtual bool | simulationIsStepable () const |
| virtual bool | simulationIsStopable () const |
| void | singleStep () |
| void | stop () |
| virtual | ~GUIRunThread () |
| destructor | |
Static Public Member Functions | |
| static void | sleep (long ms) |
Protected Member Functions | |
| void | makeStep () throw () |
Protected Attributes | |
| OutputDevice * | myErrorRetriever |
| The instances of message retriever encapsulations Needed to be deleted from the handler later on. | |
| MFXEventQue & | myEventQue |
| FXEX::FXThreadEvent & | myEventThrow |
| bool | myHalting |
| information whether the simulation is halting (is not being executed) | |
| OutputDevice * | myMessageRetriever |
| GUINet * | myNet |
| the loaded simulation network | |
| bool | myOk |
| bool | myQuit |
| FXRealSpinDial & | mySimDelay |
| SUMOTime | mySimEndTime |
| SUMOTime | mySimStartTime |
| the times the simulation starts and ends with | |
| bool | mySimulationInProgress |
| MFXMutex | mySimulationLock |
| bool | mySingle |
| OutputDevice * | myWarningRetriever |
anonymous enum [inherited] |
| GUIRunThread::GUIRunThread | ( | MFXInterThreadEventClient * | mw, | |
| FXRealSpinDial & | simDelay, | |||
| MFXEventQue & | eq, | |||
| FXEX::FXThreadEvent & | ev | |||
| ) |
constructor
Definition at line 71 of file GUIRunThread.cpp.
References MsgHandler::MT_ERROR, MsgHandler::MT_MESSAGE, MsgHandler::MT_WARNING, myErrorRetriever, myMessageRetriever, myWarningRetriever, and retrieveMessage().
00074 : FXSingleEventThread(gFXApp, parent), 00075 myNet(0), myQuit(false), mySimulationInProgress(false), myOk(true), 00076 mySimDelay(simDelay), myEventQue(eq), myEventThrow(ev) { 00077 myErrorRetriever = new MsgRetrievingFunction<GUIRunThread>(this, &GUIRunThread::retrieveMessage, MsgHandler::MT_ERROR); 00078 myMessageRetriever = new MsgRetrievingFunction<GUIRunThread>(this, &GUIRunThread::retrieveMessage, MsgHandler::MT_MESSAGE); 00079 myWarningRetriever = new MsgRetrievingFunction<GUIRunThread>(this, &GUIRunThread::retrieveMessage, MsgHandler::MT_WARNING); 00080 }
| GUIRunThread::~GUIRunThread | ( | ) | [virtual] |
destructor
Definition at line 83 of file GUIRunThread.cpp.
References deleteSim(), myErrorRetriever, myMessageRetriever, myNet, myQuit, mySimulationInProgress, and myWarningRetriever.
00083 { 00084 // the thread shall stop 00085 myQuit = true; 00086 deleteSim(); 00087 delete myErrorRetriever; 00088 delete myMessageRetriever; 00089 delete myWarningRetriever; 00090 // wait for the thread 00091 while (mySimulationInProgress||myNet!=0); 00092 }
| void GUIRunThread::begin | ( | ) | [virtual] |
starts the simulation (execution of one step after another)
Definition at line 242 of file GUIRunThread.cpp.
References myOk.
Referenced by GUIApplicationWindow::onCmdStart(), and GUIApplicationWindow::onCmdStep().
00242 { 00243 myOk = true; 00244 }
| void GUIRunThread::deleteSim | ( | ) | [virtual] |
deletes the existing simulation
Definition at line 261 of file GUIRunThread.cpp.
References MsgHandler::cleanupOnEnd(), OutputDevice::closeAll(), MSNet::closeSimulation(), MsgHandler::getErrorInstance(), MsgHandler::getMessageInstance(), MsgHandler::getWarningInstance(), MFXMutex::lock(), myErrorRetriever, myHalting, myMessageRetriever, myNet, mySimStartTime, mySimulationInProgress, mySimulationLock, myWarningRetriever, MsgHandler::removeRetriever(), and MFXMutex::unlock().
Referenced by GUIApplicationWindow::closeAllWindows(), run(), and ~GUIRunThread().
00261 { 00262 myHalting = true; 00263 // remove message callbacks 00264 MsgHandler::getErrorInstance()->removeRetriever(myErrorRetriever); 00265 MsgHandler::getWarningInstance()->removeRetriever(myWarningRetriever); 00266 MsgHandler::getMessageInstance()->removeRetriever(myMessageRetriever); 00267 // 00268 mySimulationLock.lock(); 00269 if (myNet!=0) { 00270 myNet->closeSimulation(mySimStartTime); 00271 } 00272 while (mySimulationInProgress); 00273 delete myNet; 00274 myNet = 0; 00275 OutputDevice::closeAll(); 00276 mySimulationLock.unlock(); 00277 MsgHandler::cleanupOnEnd(); 00278 }
| GUINet & GUIRunThread::getNet | ( | ) | const |
returns the loaded network
Definition at line 282 of file GUIRunThread.cpp.
References myNet.
Referenced by GUIApplicationWindow::getCurrentSimTime(), GUIApplicationWindow::handleEvent_SimulationStep(), GUIApplicationWindow::openNewView(), and run().
00282 { 00283 return *myNet; 00284 }
initialises the thread with the new simulation
Definition at line 96 of file GUIRunThread.cpp.
References MsgHandler::addRetriever(), MsgHandler::getErrorInstance(), MsgHandler::getMessageInstance(), MsgHandler::getWarningInstance(), myErrorRetriever, myMessageRetriever, myNet, mySimEndTime, mySimStartTime, and myWarningRetriever.
Referenced by GUIApplicationWindow::handleEvent_SimulationLoaded().
00096 { 00097 // assign new values 00098 myNet = net; 00099 mySimStartTime = start; 00100 mySimEndTime = end; 00101 // register message callbacks 00102 MsgHandler::getErrorInstance()->addRetriever(myErrorRetriever); 00103 MsgHandler::getMessageInstance()->addRetriever(myMessageRetriever); 00104 MsgHandler::getWarningInstance()->addRetriever(myWarningRetriever); 00105 }
| void GUIRunThread::makeStep | ( | ) | throw () [protected] |
Definition at line 154 of file GUIRunThread.cpp.
References MFXEventQue::add(), DELTA_T, MSNet::getCurrentTimeStep(), MsgHandler::getErrorInstance(), OptionsCont::getOptions(), GUINet::guiSimulationStep(), MsgHandler::inform(), MFXMutex::lock(), myEventQue, myEventThrow, myHalting, myNet, myOk, mySimEndTime, mySimulationInProgress, mySimulationLock, mySingle, FXEX::FXThreadEvent::signal(), MSNet::SIMSTATE_CONNECTION_CLOSED, MSNet::SIMSTATE_END_STEP_REACHED, MSNet::SIMSTATE_ERROR_IN_SIM, MSNet::SIMSTATE_NO_FURTHER_VEHICLES, MSNet::SIMSTATE_RUNNING, MSNet::SIMSTATE_TOO_MANY_VEHICLES, MSNet::simulationState(), MSNet::simulationStep(), MFXMutex::unlock(), and traci::TraCIServer::wasClosed().
Referenced by run().
00154 { 00155 GUIEvent *e = 0; 00156 // simulation is being perfomed 00157 mySimulationInProgress = true; 00158 // execute a single step 00159 try { 00160 mySimulationLock.lock(); 00161 myNet->simulationStep(); 00162 myNet->guiSimulationStep(); 00163 mySimulationLock.unlock(); 00164 00165 // inform parent that a step has been performed 00166 e = new GUIEvent_SimulationStep(); 00167 myEventQue.add(e); 00168 myEventThrow.signal(); 00169 00170 e = 0; 00171 MSNet::SimulationState state = myNet->simulationState(mySimEndTime); 00172 #ifndef NO_TRACI 00173 if (state!=MSNet::SIMSTATE_RUNNING) { 00174 if (OptionsCont::getOptions().getInt("remote-port")!=0&&!traci::TraCIServer::wasClosed()) { 00175 state = MSNet::SIMSTATE_RUNNING; 00176 } 00177 } 00178 #endif 00179 switch (state) { 00180 case MSNet::SIMSTATE_END_STEP_REACHED: 00181 case MSNet::SIMSTATE_NO_FURTHER_VEHICLES: 00182 case MSNet::SIMSTATE_CONNECTION_CLOSED: 00183 case MSNet::SIMSTATE_TOO_MANY_VEHICLES: 00184 e = new GUIEvent_SimulationEnded(state, myNet->getCurrentTimeStep()-DELTA_T); 00185 break; 00186 default: 00187 break; 00188 } 00189 if (e!=0) { 00190 myEventQue.add(e); 00191 myEventThrow.signal(); 00192 myHalting = true; 00193 } 00194 // stop the execution when only a single step should have 00195 // been performed 00196 if (mySingle) { 00197 myHalting = true; 00198 } 00199 // simulation step is over 00200 mySimulationInProgress = false; 00201 } catch (ProcessError &e2) { 00202 if (string(e2.what())!=string("Process Error") && std::string(e2.what())!=string("")) { 00203 MsgHandler::getErrorInstance()->inform(e2.what()); 00204 } 00205 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false); 00206 mySimulationLock.unlock(); 00207 mySimulationInProgress = false; 00208 e = new GUIEvent_SimulationEnded(MSNet::SIMSTATE_ERROR_IN_SIM, myNet->getCurrentTimeStep()); 00209 myEventQue.add(e); 00210 myEventThrow.signal(); 00211 myHalting = true; 00212 myOk = false; 00213 #ifndef _DEBUG 00214 } catch (...) { 00215 mySimulationLock.unlock(); 00216 mySimulationInProgress = false; 00217 e = new GUIEvent_SimulationEnded(MSNet::SIMSTATE_ERROR_IN_SIM, myNet->getCurrentTimeStep()); 00218 myEventQue.add(e); 00219 myEventThrow.signal(); 00220 myHalting = true; 00221 myOk = false; 00222 #endif 00223 } 00224 }
| long FXSingleEventThread::onThreadEvent | ( | FXObject * | , | |
| FXSelector | , | |||
| void * | ||||
| ) | [inherited] |
Definition at line 106 of file FXSingleEventThread.cpp.
References MFXInterThreadEventClient::eventOccured(), and FXSingleEventThread::myClient.
00106 { 00107 myClient->eventOccured(); 00108 /* 00109 FXuint seltype1 = FXSELTYPE(SEL_THREAD); 00110 if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) { 00111 } 00112 FXuint seltype = FXSELTYPE(sel); 00113 return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL); 00114 */ 00115 return 1; 00116 }
| long FXSingleEventThread::onThreadSignal | ( | FXObject * | , | |
| FXSelector | , | |||
| void * | ||||
| ) | [inherited] |
Definition at line 92 of file FXSingleEventThread.cpp.
References PIPE_READ, and FXEX::SEL_THREAD.
00092 { 00093 FXuint seltype=SEL_THREAD; 00094 #ifndef WIN32 00095 ::read(event[PIPE_READ],&seltype,sizeof(seltype)); 00096 #else 00097 //FIXME need win32 support 00098 #endif 00099 FXSelector sel = FXSEL(SEL_THREAD,0); 00100 handle(this,sel,0); 00101 return 0; 00102 }
| void GUIRunThread::prepareDestruction | ( | ) |
halts the thread before it shall be deleted
Definition at line 288 of file GUIRunThread.cpp.
References myHalting, and myQuit.
Referenced by GUIApplicationWindow::~GUIApplicationWindow().
| void GUIRunThread::resume | ( | ) |
called when the user presses the "resume"-button, this method resumes the execution after a break
Definition at line 228 of file GUIRunThread.cpp.
References myHalting, and mySingle.
Referenced by GUIApplicationWindow::onCmdStart().
| void GUIRunThread::retrieveMessage | ( | const MsgHandler::MsgType | type, | |
| const std::string & | msg | |||
| ) |
Retrieves messages from the loading module.
Definition at line 295 of file GUIRunThread.cpp.
References MFXEventQue::add(), myEventQue, myEventThrow, and FXEX::FXThreadEvent::signal().
Referenced by GUIRunThread().
00295 { 00296 GUIEvent *e = new GUIEvent_Message(type, msg); 00297 myEventQue.add(e); 00298 myEventThrow.signal(); 00299 }
| FXint GUIRunThread::run | ( | ) | [virtual] |
starts the execution
Reimplemented from FXSingleEventThread.
Definition at line 109 of file GUIRunThread.cpp.
References deleteSim(), gBreakpoints, SysUtils::getCurrentMillis(), MSNet::getCurrentTimeStep(), getNet(), FX::FXRealSpinDial::getValue(), makeStep(), myHalting, myNet, myOk, myQuit, mySimDelay, GUINet::setIdleDuration(), GUINet::setSimDuration(), FXSingleEventThread::sleep(), stop(), and SUMOReal.
00109 { 00110 long beg = 0; 00111 long end = 0; 00112 long end2 = -1; 00113 // perform an endless loop 00114 while (!myQuit) { 00115 // if the simulation shall be perfomed, do it 00116 if (!myHalting&&myNet!=0&&myOk) { 00117 if (getNet().logSimulationDuration()) { 00118 beg = SysUtils::getCurrentMillis(); 00119 if (end2!=-1) { 00120 getNet().setIdleDuration((int)(beg-end2)); 00121 } 00122 } 00123 // check whether we shall stop at this step 00124 bool haltAfter = 00125 find(gBreakpoints.begin(), gBreakpoints.end(), myNet->getCurrentTimeStep())!=gBreakpoints.end(); 00126 // do the step 00127 makeStep(); 00128 // stop if wished 00129 if (haltAfter) { 00130 stop(); 00131 } 00132 // wait if wanted 00133 SUMOReal val = (SUMOReal) mySimDelay.getValue(); 00134 if (getNet().logSimulationDuration()) { 00135 end = SysUtils::getCurrentMillis(); 00136 getNet().setSimDuration((int)(end-beg)); 00137 end2 = SysUtils::getCurrentMillis(); 00138 } 00139 if ((int) val!=0) { 00140 sleep((int) val); 00141 } 00142 } else { 00143 // sleep if the siulation is not running 00144 sleep(500); 00145 } 00146 } 00147 // delete a maybe existing simulation at the end 00148 deleteSim(); 00149 return 0; 00150 }
| void FXSingleEventThread::signal | ( | FXuint | seltype | ) | [inherited] |
Definition at line 82 of file FXSingleEventThread.cpp.
References FXSingleEventThread::event, and PIPE_WRITE.
00082 { 00083 #ifndef WIN32 00084 ::write(event[PIPE_WRITE],&seltype,sizeof(seltype)); 00085 #else 00086 ::SetEvent(event); 00087 #endif 00088 }
| void FXSingleEventThread::signal | ( | ) | [inherited] |
Definition at line 71 of file FXSingleEventThread.cpp.
References FXSingleEventThread::event, PIPE_WRITE, and FXEX::SEL_THREAD.
00071 { 00072 FXuint seltype=SEL_THREAD; 00073 #ifndef WIN32 00074 ::write(event[PIPE_WRITE],&seltype,sizeof(seltype)); 00075 #else 00076 ::SetEvent(event); 00077 #endif 00078 }
| bool GUIRunThread::simulationAvailable | ( | ) | const |
returns the information whether a simulation has been loaded
Definition at line 255 of file GUIRunThread.cpp.
References myNet.
Referenced by GUIApplicationWindow::onCmdStart(), GUIApplicationWindow::onCmdStep(), GUIApplicationWindow::onUpdAddMicro(), GUIApplicationWindow::onUpdEditBreakpoints(), GUIApplicationWindow::onUpdEditChosen(), and GUIApplicationWindow::openNewView().
00255 { 00256 return myNet!=0; 00257 }
| bool GUIRunThread::simulationIsStartable | ( | ) | const [virtual] |
Definition at line 303 of file GUIRunThread.cpp.
References myHalting, and myNet.
Referenced by GUIApplicationWindow::handleEvent_SimulationLoaded(), and GUIApplicationWindow::onUpdStart().
| bool GUIRunThread::simulationIsStepable | ( | ) | const [virtual] |
Definition at line 315 of file GUIRunThread.cpp.
References myHalting, and myNet.
Referenced by GUIApplicationWindow::onUpdStep().
| bool GUIRunThread::simulationIsStopable | ( | ) | const [virtual] |
Definition at line 309 of file GUIRunThread.cpp.
References myHalting, and myNet.
Referenced by GUIApplicationWindow::onUpdStop().
| void GUIRunThread::singleStep | ( | ) |
called when the user presses the "single step"-button, this method allows the thread to perform a single simulation step
Definition at line 235 of file GUIRunThread.cpp.
References myHalting, and mySingle.
Referenced by GUIApplicationWindow::onCmdStep().
| void FXSingleEventThread::sleep | ( | long | ms | ) | [static, inherited] |
Definition at line 120 of file FXSingleEventThread.cpp.
Referenced by run().
00120 { 00121 #ifdef WIN32 00122 Sleep(ms); 00123 #else 00124 long long us = ms * 1000; 00125 usleep(us); 00126 #endif 00127 }
| void GUIRunThread::stop | ( | ) |
halts the simulation execution
Definition at line 248 of file GUIRunThread.cpp.
References myHalting, and mySingle.
Referenced by GUIApplicationWindow::onCmdStop(), and run().
OutputDevice* GUIRunThread::myErrorRetriever [protected] |
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
Definition at line 139 of file GUIRunThread.h.
Referenced by deleteSim(), GUIRunThread(), init(), and ~GUIRunThread().
MFXEventQue& GUIRunThread::myEventQue [protected] |
FXEX::FXThreadEvent& GUIRunThread::myEventThrow [protected] |
bool GUIRunThread::myHalting [protected] |
information whether the simulation is halting (is not being executed)
Definition at line 121 of file GUIRunThread.h.
Referenced by deleteSim(), makeStep(), prepareDestruction(), resume(), run(), simulationIsStartable(), simulationIsStepable(), simulationIsStopable(), singleStep(), and stop().
OutputDevice * GUIRunThread::myMessageRetriever [protected] |
Definition at line 139 of file GUIRunThread.h.
Referenced by deleteSim(), GUIRunThread(), init(), and ~GUIRunThread().
GUINet* GUIRunThread::myNet [protected] |
the loaded simulation network
Definition at line 115 of file GUIRunThread.h.
Referenced by deleteSim(), getNet(), init(), makeStep(), run(), simulationAvailable(), simulationIsStartable(), simulationIsStepable(), simulationIsStopable(), and ~GUIRunThread().
bool GUIRunThread::myOk [protected] |
bool GUIRunThread::myQuit [protected] |
information whether the thread shall be stopped (if not, the thread stays in an endless loop)
Definition at line 125 of file GUIRunThread.h.
Referenced by prepareDestruction(), run(), and ~GUIRunThread().
FXRealSpinDial& GUIRunThread::mySimDelay [protected] |
SUMOTime GUIRunThread::mySimEndTime [protected] |
SUMOTime GUIRunThread::mySimStartTime [protected] |
the times the simulation starts and ends with
Definition at line 118 of file GUIRunThread.h.
Referenced by deleteSim(), and init().
bool GUIRunThread::mySimulationInProgress [protected] |
information whether a simulation step is being performed (otherwise the thread may be waiting or the simulation is maybe not performed at all)
Definition at line 130 of file GUIRunThread.h.
Referenced by deleteSim(), makeStep(), and ~GUIRunThread().
MFXMutex GUIRunThread::mySimulationLock [protected] |
bool GUIRunThread::mySingle [protected] |
information whether the thread is running in single step mode
Definition at line 135 of file GUIRunThread.h.
Referenced by makeStep(), resume(), singleStep(), and stop().
OutputDevice * GUIRunThread::myWarningRetriever [protected] |
Definition at line 139 of file GUIRunThread.h.
Referenced by deleteSim(), GUIRunThread(), init(), and ~GUIRunThread().
1.5.6