OpenWalnut 1.3.1
WKernel.cpp
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 <string>
00034 #include <vector>
00035 
00036 #include "../common/WThreadedRunner.h"
00037 #include "../common/WTimer.h"
00038 #include "../common/WRealtimeTimer.h"
00039 #include "../dataHandler/WDataHandler.h"
00040 #include "../gui/WGUI.h"
00041 #include "WKernel.h"
00042 #include "WModuleContainer.h"
00043 #include "WModuleFactory.h"
00044 #include "WROIManager.h"
00045 #include "WSelectionManager.h"
00046 
00047 #include "core/WVersion.h"   // NOTE: this file is auto-generated by CMAKE
00048 
00049 /**
00050  * Used for program wide access to the kernel.
00051  */
00052 WKernel* WKernel::m_kernel = NULL;
00053 
00054 WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
00055     WThreadedRunner(),
00056     m_timer( WTimer::SPtr( new WRealtimeTimer() ) )
00057 {
00058     WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
00059     wlog::debug( "Kernel" ) << "Version: " << W_VERSION;
00060 
00061     setThreadName( "Kernel" );
00062 
00063     // init the singleton
00064     m_kernel = this;
00065 
00066     // initialize members
00067     m_gui = gui;
00068     m_graphicsEngine = ge;
00069 
00070     // init
00071     init();
00072 }
00073 
00074 WKernel::~WKernel()
00075 {
00076     // cleanup
00077     WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
00078 }
00079 
00080 WKernel* WKernel::instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui )
00081 {
00082     if( m_kernel == NULL )
00083     {
00084         new WKernel( ge, gui ); // m_kernel will be set in the constructor.
00085     }
00086 
00087     return m_kernel;
00088 }
00089 
00090 void WKernel::init()
00091 {
00092     // initialize
00093     m_roiManager = boost::shared_ptr< WROIManager >( new WROIManager() );
00094 
00095     m_selectionManager = boost::shared_ptr< WSelectionManager >( new WSelectionManager() );
00096 
00097     // get module factory
00098     m_moduleFactory = WModuleFactory::getModuleFactory();
00099 
00100     // init data handler
00101     WDataHandler::getDataHandler();
00102 
00103     // initialize module container
00104     m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
00105                 "Root module container in Kernel." ) );
00106     // this avoids the root container to be marked as "crashed" if a contained module crashes.
00107     m_moduleContainer->setCrashIfModuleCrashes( false );
00108 
00109     // load all modules
00110     m_moduleFactory->load();
00111 }
00112 
00113 WKernel* WKernel::getRunningKernel()
00114 {
00115     return m_kernel;
00116 }
00117 
00118 boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
00119 {
00120     return m_graphicsEngine;
00121 }
00122 
00123 boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
00124 {
00125     return m_moduleContainer;
00126 }
00127 
00128 boost::shared_ptr< WGUI > WKernel::getGui() const
00129 {
00130     return m_gui;
00131 }
00132 
00133 void WKernel::finalize()
00134 {
00135     WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
00136 
00137     // NOTE: stopping a container erases all modules inside.
00138     getRootContainer()->stop();
00139 
00140     WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
00141     WDataHandler::getDataHandler()->clear();
00142 }
00143 
00144 void WKernel::threadMain()
00145 {
00146     WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
00147 
00148     // wait for GUI to be initialized properly
00149     m_gui->isInitialized().wait();
00150 
00151     // start GE
00152     m_graphicsEngine->run();
00153 
00154     // actually there is nothing more to do here
00155     waitForStop();
00156 
00157     WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
00158 }
00159 
00160 const WBoolFlag& WKernel::isFinishRequested() const
00161 {
00162     return m_shutdownFlag;
00163 }
00164 
00165 WBatchLoader::SPtr WKernel::loadDataSets( std::vector< std::string > filenames, bool suppressColormaps )
00166 {
00167     return getRootContainer()->loadDataSets( filenames, suppressColormaps );
00168 }
00169 
00170 WBatchLoader::SPtr WKernel::loadDataSetsSynchronously( std::vector< std::string > filenames, bool suppressColormaps )
00171 {
00172     return getRootContainer()->loadDataSetsSynchronously( filenames, suppressColormaps );
00173 }
00174 
00175 boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
00176 {
00177     return getRootContainer()->applyModule( applyOn, prototype );
00178 }
00179 
00180 boost::shared_ptr< WROIManager > WKernel::getRoiManager()
00181 {
00182     return m_roiManager;
00183 }
00184 
00185 boost::shared_ptr< WSelectionManager>WKernel::getSelectionManager()
00186 {
00187     return m_selectionManager;
00188 }
00189 
00190 WTimer::ConstSPtr WKernel::getTimer() const
00191 {
00192     return m_timer;
00193 }
00194