SUMO - Simulation of Urban MObility
GUILoadThread.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Class describing the thread that performs the loading of a simulation
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <iostream>
00034 #include <guisim/GUINet.h>
00035 #include <guisim/GUIEventControl.h>
00036 #include <netload/NLBuilder.h>
00037 #include <netload/NLHandler.h>
00038 #include <netload/NLJunctionControlBuilder.h>
00039 #include <guinetload/GUIEdgeControlBuilder.h>
00040 #include <guinetload/GUIDetectorBuilder.h>
00041 #include <guinetload/GUITriggerBuilder.h>
00042 #include <guisim/GUIVehicleControl.h>
00043 #include <microsim/output/MSDetectorControl.h>
00044 #include <utils/common/UtilExceptions.h>
00045 #include <utils/options/OptionsCont.h>
00046 #include <utils/options/Option.h>
00047 #include <utils/options/OptionsIO.h>
00048 #include <utils/common/MsgHandler.h>
00049 #include <utils/foxtools/MFXEventQue.h>
00050 #include <microsim/MSFrame.h>
00051 #include <utils/common/MsgRetrievingFunction.h>
00052 #include "GUIApplicationWindow.h"
00053 #include "GUILoadThread.h"
00054 #include "GUIGlobals.h"
00055 #include "GUIEvent_SimulationLoaded.h"
00056 #include <utils/gui/events/GUIEvent_Message.h>
00057 #include <utils/gui/windows/GUIAppEnum.h>
00058 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00059 #include <utils/gui/images/GUITexturesHelper.h>
00060 #include <utils/common/RandHelper.h>
00061 #include <ctime>
00062 
00063 #ifdef HAVE_MESOSIM
00064 #include <mesosim/MEVehicleControl.h>
00065 #endif
00066 
00067 #ifdef CHECK_MEMORY_LEAKS
00068 #include <foreign/nvwa/debug_new.h>
00069 #endif // CHECK_MEMORY_LEAKS
00070 
00071 
00072 // ===========================================================================
00073 // member method definitions
00074 // ===========================================================================
00075 GUILoadThread::GUILoadThread(FXApp* app, MFXInterThreadEventClient* mw,
00076                              MFXEventQue& eq, FXEX::FXThreadEvent& ev)
00077     : FXSingleEventThread(app, mw), myParent(mw), myEventQue(eq),
00078       myEventThrow(ev) {
00079     myErrorRetriever = new MsgRetrievingFunction<GUILoadThread>(this, &GUILoadThread::retrieveMessage, MsgHandler::MT_ERROR);
00080     myMessageRetriever = new MsgRetrievingFunction<GUILoadThread>(this, &GUILoadThread::retrieveMessage, MsgHandler::MT_MESSAGE);
00081     myWarningRetriever = new MsgRetrievingFunction<GUILoadThread>(this, &GUILoadThread::retrieveMessage, MsgHandler::MT_WARNING);
00082     MsgHandler::getErrorInstance()->addRetriever(myErrorRetriever);
00083 }
00084 
00085 
00086 GUILoadThread::~GUILoadThread() {
00087     delete myErrorRetriever;
00088     delete myMessageRetriever;
00089     delete myWarningRetriever;
00090 }
00091 
00092 
00093 FXint
00094 GUILoadThread::run() {
00095     GUINet* net = 0;
00096     int simStartTime = 0;
00097     int simEndTime = 0;
00098     OptionsCont& oc = OptionsCont::getOptions();
00099 
00100     // within gui-based applications, nothing is reported to the console
00101     MsgHandler::getMessageInstance()->removeRetriever(&OutputDevice::getDevice("stdout"));
00102     MsgHandler::getWarningInstance()->removeRetriever(&OutputDevice::getDevice("stderr"));
00103     MsgHandler::getErrorInstance()->removeRetriever(&OutputDevice::getDevice("stderr"));
00104     // register message callbacks
00105     MsgHandler::getMessageInstance()->addRetriever(myMessageRetriever);
00106     MsgHandler::getErrorInstance()->addRetriever(myErrorRetriever);
00107     MsgHandler::getWarningInstance()->addRetriever(myWarningRetriever);
00108 
00109     // try to load the given configuration
00110     if (!initOptions()) {
00111         // the options are not valid but maybe we want to quit
00112         GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
00113         submitEndAndCleanup(net, simStartTime, simEndTime);
00114         return 0;
00115     }
00116     // do this once again to get parsed options
00117     MsgHandler::initOutputOptions();
00118     GUIGlobals::gRunAfterLoad = oc.getBool("start");
00119     GUIGlobals::gQuitOnEnd = oc.getBool("quit-on-end");
00120     if (!MSFrame::checkOptions()) {
00121         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00122         submitEndAndCleanup(net, simStartTime, simEndTime);
00123         return 0;
00124     }
00125 
00126     // initialise global settings
00127     RandHelper::initRandGlobal();
00128     MSFrame::setMSGlobals(oc);
00129     gAllowTextures = !oc.getBool("disable-textures");
00130     MSVehicleControl* vehControl = 0;
00131 #ifdef HAVE_MESOSIM
00132     GUIVisualizationSettings::UseMesoSim = MSGlobals::gUseMesoSim;
00133     if (MSGlobals::gUseMesoSim) {
00134         vehControl = new MEVehicleControl();
00135     } else 
00136 #endif
00137         vehControl = new GUIVehicleControl();
00138     
00139     net = new GUINet(
00140             vehControl,
00141             new GUIEventControl(),
00142             new GUIEventControl(), 
00143             new GUIEventControl());
00144     GUIEdgeControlBuilder* eb = new GUIEdgeControlBuilder();
00145     GUIDetectorBuilder db(*net);
00146     NLJunctionControlBuilder jb(*net, db);
00147     GUITriggerBuilder tb;
00148     NLHandler handler("", *net, db, tb, *eb, jb);
00149     tb.setHandler(&handler);
00150     NLBuilder builder(oc, *net, *eb, jb, db, handler);
00151     try {
00152         MsgHandler::getErrorInstance()->clear();
00153         MsgHandler::getWarningInstance()->clear();
00154         MsgHandler::getMessageInstance()->clear();
00155         if (!builder.build()) {
00156             throw ProcessError();
00157         } else {
00158             net->initGUIStructures();
00159             simStartTime = string2time(oc.getString("begin"));
00160             simEndTime = string2time(oc.getString("end"));
00161         }
00162     } catch (ProcessError& e) {
00163         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
00164             WRITE_ERROR(e.what());
00165         }
00166         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00167         delete net;
00168         net = 0;
00169 #ifndef _DEBUG
00170     } catch (std::exception& e) {
00171         WRITE_ERROR(e.what());
00172         delete net;
00173         net = 0;
00174 #endif
00175     }
00176     if (net == 0) {
00177         MSNet::clearAll();
00178     }
00179     delete eb;
00180     submitEndAndCleanup(net, simStartTime, simEndTime);
00181     return 0;
00182 }
00183 
00184 
00185 
00186 void
00187 GUILoadThread::submitEndAndCleanup(GUINet* net,
00188                                    SUMOTime simStartTime,
00189                                    SUMOTime simEndTime) {
00190     // remove message callbacks
00191     MsgHandler::getErrorInstance()->removeRetriever(myErrorRetriever);
00192     MsgHandler::getWarningInstance()->removeRetriever(myWarningRetriever);
00193     MsgHandler::getMessageInstance()->removeRetriever(myMessageRetriever);
00194     // inform parent about the process
00195     GUIEvent* e = new GUIEvent_SimulationLoaded(net, simStartTime, simEndTime, myFile,
00196             OptionsCont::getOptions().getString("gui-settings-file"));
00197     myEventQue.add(e);
00198     myEventThrow.signal();
00199 }
00200 
00201 
00202 bool
00203 GUILoadThread::initOptions() {
00204     try {
00205         OptionsCont& oc = OptionsCont::getOptions();
00206         oc.clear();
00207         MSFrame::fillOptions();
00208         if (myFile != "") {
00209             if (myLoadNet) {
00210                 oc.set("net-file", myFile);
00211             } else {
00212                 oc.set("configuration-file", myFile);
00213             }
00214             OptionsIO::getOptions(true, 1, 0);
00215         } else {
00216             OptionsIO::getOptions(true);
00217         }
00218         return true;
00219     } catch (ProcessError& e) {
00220         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
00221             WRITE_ERROR(e.what());
00222         }
00223         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00224     }
00225     return false;
00226 }
00227 
00228 
00229 void
00230 GUILoadThread::load(const std::string& file, bool isNet) {
00231     myFile = file;
00232     myLoadNet = isNet;
00233     start();
00234 }
00235 
00236 
00237 void
00238 GUILoadThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
00239     GUIEvent* e = new GUIEvent_Message(type, msg);
00240     myEventQue.add(e);
00241     myEventThrow.signal();
00242 }
00243 
00244 
00245 const std::string&
00246 GUILoadThread::getFileName() const {
00247     return myFile;
00248 }
00249 
00250 
00251 
00252 /****************************************************************************/
00253 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines