OpenWalnut  1.3.1
WKernel.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifdef __linux__
26  #include <unistd.h> // used for getcwd (to get current directory)
27 #endif
28 
29 #if defined(__APPLE__)
30  #include <mach-o/dyld.h>
31 #endif
32 
33 #include <string>
34 #include <vector>
35 
36 #include "../common/WThreadedRunner.h"
37 #include "../common/WTimer.h"
38 #include "../common/WRealtimeTimer.h"
39 #include "../dataHandler/WDataHandler.h"
40 #include "../gui/WGUI.h"
41 #include "WKernel.h"
42 #include "WModuleContainer.h"
43 #include "WModuleFactory.h"
44 #include "WROIManager.h"
45 #include "WSelectionManager.h"
46 
47 #include "core/WVersion.h" // NOTE: this file is auto-generated by CMAKE
48 
49 /**
50  * Used for program wide access to the kernel.
51  */
53 
54 WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
56  m_timer( WTimer::SPtr( new WRealtimeTimer() ) )
57 {
58  WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
59  wlog::debug( "Kernel" ) << "Version: " << W_VERSION;
60 
61  setThreadName( "Kernel" );
62 
63  // init the singleton
64  m_kernel = this;
65 
66  // initialize members
67  m_gui = gui;
68  m_graphicsEngine = ge;
69 
70  // init
71  init();
72 }
73 
75 {
76  // cleanup
77  WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
78 }
79 
80 WKernel* WKernel::instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui )
81 {
82  if( m_kernel == NULL )
83  {
84  new WKernel( ge, gui ); // m_kernel will be set in the constructor.
85  }
86 
87  return m_kernel;
88 }
89 
91 {
92  // initialize
93  m_roiManager = boost::shared_ptr< WROIManager >( new WROIManager() );
94 
95  m_selectionManager = boost::shared_ptr< WSelectionManager >( new WSelectionManager() );
96 
97  // get module factory
99 
100  // init data handler
102 
103  // initialize module container
104  m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
105  "Root module container in Kernel." ) );
106  // this avoids the root container to be marked as "crashed" if a contained module crashes.
107  m_moduleContainer->setCrashIfModuleCrashes( false );
108 
109  // load all modules
110  m_moduleFactory->load();
111 }
112 
114 {
115  return m_kernel;
116 }
117 
118 boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
119 {
120  return m_graphicsEngine;
121 }
122 
123 boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
124 {
125  return m_moduleContainer;
126 }
127 
128 boost::shared_ptr< WGUI > WKernel::getGui() const
129 {
130  return m_gui;
131 }
132 
134 {
135  WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
136 
137  // NOTE: stopping a container erases all modules inside.
138  getRootContainer()->stop();
139 
140  WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
141  WDataHandler::getDataHandler()->clear();
142 }
143 
145 {
146  WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
147 
148  // wait for GUI to be initialized properly
149  m_gui->isInitialized().wait();
150 
151  // start GE
152  m_graphicsEngine->run();
153 
154  // actually there is nothing more to do here
155  waitForStop();
156 
157  WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
158 }
159 
161 {
162  return m_shutdownFlag;
163 }
164 
165 WBatchLoader::SPtr WKernel::loadDataSets( std::vector< std::string > filenames, bool suppressColormaps )
166 {
167  return getRootContainer()->loadDataSets( filenames, suppressColormaps );
168 }
169 
170 WBatchLoader::SPtr WKernel::loadDataSetsSynchronously( std::vector< std::string > filenames, bool suppressColormaps )
171 {
172  return getRootContainer()->loadDataSetsSynchronously( filenames, suppressColormaps );
173 }
174 
175 boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
176 {
177  return getRootContainer()->applyModule( applyOn, prototype );
178 }
179 
180 boost::shared_ptr< WROIManager > WKernel::getRoiManager()
181 {
182  return m_roiManager;
183 }
184 
185 boost::shared_ptr< WSelectionManager>WKernel::getSelectionManager()
186 {
187  return m_selectionManager;
188 }
189 
191 {
192  return m_timer;
193 }
194