OpenWalnut
1.2.5
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifdef __linux__ 00026 #include <unistd.h> // used for getcwd (to get current directory) 00027 #endif 00028 00029 #if defined(__APPLE__) 00030 #include <mach-o/dyld.h> 00031 #endif 00032 00033 #include <iostream> 00034 #include <list> 00035 #include <string> 00036 #include <vector> 00037 00038 // Use filesystem version 2 for compatibility with newer boost versions. 00039 #ifndef BOOST_FILESYSTEM_VERSION 00040 #define BOOST_FILESYSTEM_VERSION 2 00041 #endif 00042 #include <boost/filesystem.hpp> 00043 #include <boost/thread/xtime.hpp> 00044 00045 #include "../common/WStringUtils.h" 00046 #include "../common/WThreadedRunner.h" 00047 #include "../dataHandler/WDataHandler.h" 00048 #include "../gui/WGUI.h" 00049 #include "WKernel.h" 00050 #include "WModule.h" 00051 #include "WModuleContainer.h" 00052 #include "WModuleFactory.h" 00053 #include "WROIManager.h" 00054 #include "WSelectionManager.h" 00055 00056 #include "WVersion.h" // NOTE: this file is auto-generated by CMAKE 00057 00058 /** 00059 * Used for program wide access to the kernel. 00060 */ 00061 WKernel* WKernel::m_kernel = NULL; 00062 00063 WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ): 00064 WThreadedRunner() 00065 { 00066 WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO ); 00067 wlog::debug( "Kernel" ) << "Version: " << W_VERSION; 00068 00069 // init the singleton 00070 m_kernel = this; 00071 00072 // initialize members 00073 m_gui = gui; 00074 m_graphicsEngine = ge; 00075 00076 // init 00077 init(); 00078 } 00079 00080 WKernel::~WKernel() 00081 { 00082 // cleanup 00083 WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO ); 00084 } 00085 00086 WKernel* WKernel::instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ) 00087 { 00088 if( m_kernel == NULL ) 00089 { 00090 new WKernel( ge, gui ); // m_kernel will be set in the constructor. 00091 } 00092 00093 return m_kernel; 00094 } 00095 00096 void WKernel::init() 00097 { 00098 // initialize 00099 m_roiManager = boost::shared_ptr< WROIManager >( new WROIManager() ); 00100 00101 m_selectionManager = boost::shared_ptr< WSelectionManager >( new WSelectionManager() ); 00102 00103 // get module factory 00104 m_moduleFactory = WModuleFactory::getModuleFactory(); 00105 00106 // init data handler 00107 WDataHandler::getDataHandler(); 00108 00109 // initialize module container 00110 m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer", 00111 "Root module container in Kernel." ) ); 00112 // this avoids the root container to be marked as "crashed" if a contained module crashes. 00113 m_moduleContainer->setCrashIfModuleCrashes( false ); 00114 00115 // load all modules 00116 m_moduleFactory->load(); 00117 } 00118 00119 WKernel* WKernel::getRunningKernel() 00120 { 00121 return m_kernel; 00122 } 00123 00124 boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const 00125 { 00126 return m_graphicsEngine; 00127 } 00128 00129 boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const 00130 { 00131 return m_moduleContainer; 00132 } 00133 00134 boost::shared_ptr< WGUI > WKernel::getGui() const 00135 { 00136 return m_gui; 00137 } 00138 00139 void WKernel::finalize() 00140 { 00141 WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO ); 00142 00143 // NOTE: stopping a container erases all modules inside. 00144 getRootContainer()->stop(); 00145 00146 WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO ); 00147 WDataHandler::getDataHandler()->clear(); 00148 } 00149 00150 void WKernel::threadMain() 00151 { 00152 WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO ); 00153 00154 // wait for GUI to be initialized properly 00155 m_gui->isInitialized().wait(); 00156 00157 // start GE 00158 m_graphicsEngine->run(); 00159 00160 // actually there is nothing more to do here 00161 waitForStop(); 00162 00163 WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO ); 00164 } 00165 00166 const WBoolFlag& WKernel::isFinishRequested() const 00167 { 00168 return m_shutdownFlag; 00169 } 00170 00171 void WKernel::loadDataSets( std::vector< std::string > fileNames ) 00172 { 00173 getRootContainer()->loadDataSets( fileNames ); 00174 } 00175 00176 void WKernel::loadDataSetsSynchronously( std::vector< std::string > fileNames ) 00177 { 00178 getRootContainer()->loadDataSetsSynchronously( fileNames ); 00179 } 00180 00181 boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype ) 00182 { 00183 return getRootContainer()->applyModule( applyOn, prototype ); 00184 } 00185 00186 boost::shared_ptr< WROIManager > WKernel::getRoiManager() 00187 { 00188 return m_roiManager; 00189 } 00190 00191 boost::shared_ptr< WSelectionManager>WKernel::getSelectionManager() 00192 { 00193 return m_selectionManager; 00194 }