00001 // [License] 00002 // The Ariba-Underlay Copyright 00003 // 00004 // Copyright (c) 2008-2009, Institute of Telematics, Universität Karlsruhe (TH) 00005 // 00006 // Institute of Telematics 00007 // Universität Karlsruhe (TH) 00008 // Zirkel 2, 76128 Karlsruhe 00009 // Germany 00010 // 00011 // Redistribution and use in source and binary forms, with or without 00012 // modification, are permitted provided that the following conditions are 00013 // met: 00014 // 00015 // 1. Redistributions of source code must retain the above copyright 00016 // notice, this list of conditions and the following disclaimer. 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND 00022 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00024 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR 00025 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // The views and conclusions contained in the software and documentation 00034 // are those of the authors and should not be interpreted as representing 00035 // official policies, either expressed or implied, of the Institute of 00036 // Telematics. 00037 // [License] 00038 00039 #include "StartupWrapper.h" 00040 #include "ariba/config.h" 00041 00042 #ifdef HAVE_LOG4CXX_LOGGER_H 00043 #include <log4cxx/logger.h> 00044 #include <log4cxx/basicconfigurator.h> 00045 #endif // HAVE_LOG4CXX_LOGGER_H 00046 00047 namespace ariba { 00048 namespace utility { 00049 00050 StartupWrapper::ConfigurationList StartupWrapper::configurations; 00051 #ifdef UNDERLAY_OMNET 00052 StartupWrapper::ModuleList StartupWrapper::modules; 00053 #endif 00054 00055 SystemEventType StartupWrapperEventStartup("StartupWrapperEventStartup"); 00056 00057 StartupWrapper::StartupWrapper(StartupInterface* _service) : service( _service ){ 00058 } 00059 00060 StartupWrapper::~StartupWrapper(){ 00061 } 00062 00063 #ifdef UNDERLAY_OMNET 00064 void StartupWrapper::insertCurrentModule(AribaOmnetModule* mod){ 00065 modules.push( mod ); 00066 } 00067 #endif 00068 00069 #ifdef UNDERLAY_OMNET 00070 AribaOmnetModule* StartupWrapper::getCurrentModule(){ 00071 assert( modules.size() > 0 ); 00072 00073 AribaOmnetModule* ret = modules.front(); 00074 modules.pop(); 00075 00076 return ret; 00077 } 00078 #endif 00079 00080 void StartupWrapper::startSystem(){ 00081 00082 // 00083 // having seeded the pseudo rng is always good 00084 // 00085 00086 srand( time(NULL) ); 00087 00088 // 00089 // init the system queue 00090 // 00091 00092 if( ! SystemQueue::instance().isRunning() ) 00093 SystemQueue::instance().run(); 00094 00095 // 00096 // init the logging system 00097 // 00098 // configure the logging 00099 // 00100 00101 #ifdef HAVE_LOG4CXX_LOGGER_H 00102 log4cxx::BasicConfigurator::configure(); 00103 00104 // set the global log level to warning 00105 { 00106 log4cxx::LoggerPtr logger(log4cxx::Logger::getRootLogger()); 00107 logger->setLevel(log4cxx::Level::getInfo()); 00108 } 00109 00110 // set up again an individual level if you like 00111 #ifndef HAVE_MAEMO 00112 { 00113 //log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("MCPO")); 00114 //logger->setLevel(log4cxx::Level::getDebug()); 00115 } 00116 #endif //HAVE_MAEMO 00117 #endif //HAVE_LOG4CXX_LOGGER_H 00118 } 00119 00120 void StartupWrapper::stopSystem(){ 00121 SystemQueue::instance().cancel(); 00122 } 00123 00124 void StartupWrapper::initConfig(string filename){ 00125 configurations.push( filename ); 00126 Configuration::setConfigFilename( filename ); 00127 } 00128 00129 void StartupWrapper::handleSystemEvent(const SystemEvent& event){ 00130 00131 if( event.getType() == StartupWrapperEventStartup ){ 00132 00133 if(!configurations.empty()){ 00134 string config = configurations.front(); 00135 configurations.pop(); 00136 Configuration::setConfigFilename( config ); 00137 } 00138 00139 // 00140 // start the actual application 00141 // 00142 00143 // TODO: im falle von omnet ist service = null, da von SpoVNetOmnetModule so übergeben 00144 // wie wird im Falle von omnet die anwendung erstellt? 00145 00146 service->startup(); 00147 00148 } 00149 00150 } 00151 00152 void StartupWrapper::startup(StartupInterface* service, bool block){ 00153 00154 StartupWrapper* startup = new StartupWrapper(service); 00155 service->wrapper = startup; 00156 00157 SystemQueue::instance().scheduleEvent( 00158 SystemEvent( startup, StartupWrapperEventStartup, NULL), 0 ); 00159 00160 #ifndef UNDERLAY_OMNET 00161 if( block ) getchar(); 00162 #endif 00163 } 00164 00165 void StartupWrapper::shutdown(StartupInterface* service, bool block){ 00166 00167 if( service == NULL || service->wrapper == NULL ) return; 00168 00169 #ifdef UNDERLAY_OMNET 00170 //TODO: service->shutdown(); 00171 #endif 00172 00173 if(block){ 00174 // call directly 00175 service->shutdown(); 00176 }else{ 00177 // call async, but not using systemqueue! // TODO: mem leak 00178 AsyncShutdown* async = new AsyncShutdown(service); 00179 async->runBlockingMethod(); 00180 } 00181 } 00182 00183 StartupWrapper::AsyncShutdown::AsyncShutdown(StartupInterface* _service) 00184 : service(_service){ 00185 } 00186 00187 void StartupWrapper::AsyncShutdown::blockingFunction(){ 00188 service->shutdown(); 00189 } 00190 00191 void StartupWrapper::AsyncShutdown::dispatchFunction(){ 00192 //unused 00193 } 00194 00195 }} // namespace ariba, utility