GUILoadThread.cpp

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

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6