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 namespace ariba { 00043 namespace utility { 00044 00045 StartupWrapper::ConfigurationList StartupWrapper::configurations; 00046 #ifdef UNDERLAY_OMNET 00047 StartupWrapper::ModuleList StartupWrapper::modules; 00048 #endif 00049 00050 SystemEventType StartupWrapperEventStartup("StartupWrapperEventStartup"); 00051 00052 StartupWrapper::StartupWrapper(StartupInterface* _service) : service( _service ){ 00053 } 00054 00055 StartupWrapper::~StartupWrapper(){ 00056 } 00057 00058 #ifdef UNDERLAY_OMNET 00059 void StartupWrapper::insertCurrentModule(AribaOmnetModule* mod){ 00060 modules.push( mod ); 00061 } 00062 #endif 00063 00064 #ifdef UNDERLAY_OMNET 00065 AribaOmnetModule* StartupWrapper::getCurrentModule(){ 00066 assert( modules.size() > 0 ); 00067 00068 AribaOmnetModule* ret = modules.front(); 00069 modules.pop(); 00070 00071 return ret; 00072 } 00073 #endif 00074 00075 void StartupWrapper::startSystem(){ 00076 00077 // 00078 // having seeded the pseudo rng is always good 00079 // 00080 00081 srand( time(NULL) ); 00082 00083 // 00084 // init the system queue 00085 // 00086 00087 if( ! SystemQueue::instance().isRunning() ) 00088 SystemQueue::instance().run(); 00089 00090 // 00091 // init the logging system 00092 // 00093 // configure the logging 00094 // 00095 00096 log4cxx::BasicConfigurator::configure(); 00097 00098 // set the global log level to warning 00099 { 00100 log4cxx::LoggerPtr logger(log4cxx::Logger::getRootLogger()); 00101 logger->setLevel(log4cxx::Level::getInfo()); 00102 } 00103 00104 // set up again an individual level if you like 00105 #ifndef HAVE_MAEMO 00106 { 00107 //log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("MCPO")); 00108 //logger->setLevel(log4cxx::Level::getDebug()); 00109 } 00110 #endif //HAVE_MAEMO 00111 } 00112 00113 void StartupWrapper::stopSystem(){ 00114 SystemQueue::instance().cancel(); 00115 } 00116 00117 void StartupWrapper::initConfig(string filename){ 00118 configurations.push( filename ); 00119 Configuration::setConfigFilename( filename ); 00120 } 00121 00122 void StartupWrapper::handleSystemEvent(const SystemEvent& event){ 00123 00124 if( event.getType() == StartupWrapperEventStartup ){ 00125 00126 string config = configurations.front(); 00127 configurations.pop(); 00128 Configuration::setConfigFilename( config ); 00129 00130 // 00131 // start the actual application 00132 // 00133 00134 // TODO: im falle von omnet ist service = null, da von SpoVNetOmnetModule so übergeben 00135 // wie wird im Falle von omnet die anwendung erstellt? 00136 00137 service->startup(); 00138 00139 } 00140 00141 } 00142 00143 void StartupWrapper::startup(StartupInterface* service, bool block){ 00144 00145 StartupWrapper* startup = new StartupWrapper(service); 00146 service->wrapper = startup; 00147 00148 SystemQueue::instance().scheduleEvent( 00149 SystemEvent( startup, StartupWrapperEventStartup, NULL), 0 ); 00150 00151 #ifndef UNDERLAY_OMNET 00152 if( block ) getchar(); 00153 #endif 00154 } 00155 00156 void StartupWrapper::shutdown(StartupInterface* service, bool block){ 00157 00158 if( service == NULL || service->wrapper == NULL ) return; 00159 00160 #ifdef UNDERLAY_OMNET 00161 //TODO: service->shutdown(); 00162 #endif 00163 00164 if(block){ 00165 // call directly 00166 service->shutdown(); 00167 }else{ 00168 // call async, but not using systemqueue! // TODO: mem leak 00169 AsyncShutdown* async = new AsyncShutdown(service); 00170 async->runBlockingMethod(); 00171 } 00172 } 00173 00174 StartupWrapper::AsyncShutdown::AsyncShutdown(StartupInterface* _service) 00175 : service(_service){ 00176 } 00177 00178 void StartupWrapper::AsyncShutdown::blockingFunction(){ 00179 service->shutdown(); 00180 } 00181 00182 void StartupWrapper::AsyncShutdown::dispatchFunction(){ 00183 //unused 00184 } 00185 00186 }} // namespace ariba, utility