OpenWalnut  1.5.0dev
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 <memory>
34 #include <string>
35 #include <vector>
36 
37 #include "../common/WLogger.h"
38 #include "../common/WRealtimeTimer.h"
39 #include "../common/WThreadedRunner.h"
40 #include "../common/WTimer.h"
41 #include "../dataHandler/WDataHandler.h"
42 #include "../ui/WUI.h"
43 #include "WKernel.h"
44 #include "WModuleContainer.h"
45 #include "WModuleFactory.h"
46 #include "WROIManager.h"
47 #include "WSelectionManager.h"
48 #include "core/WVersion.h" // NOTE: this file is auto-generated by CMAKE
49 #include "exceptions/WKernelException.h"
50 
51 /**
52  * Used for program wide access to the kernel.
53  */
55 
56 WKernel::WKernel( std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui ):
58  m_timer( WTimer::SPtr( new WRealtimeTimer() ) )
59 {
60  WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
61  wlog::debug( "Kernel" ) << "Version: " << W_VERSION;
62 
63  setThreadName( "Kernel" );
64 
65  // init the singleton
66  m_kernel = this;
67 
68  // initialize members
69  m_ui = ui;
70  m_graphicsEngine = ge;
71 
72  // init
73  init();
74 }
75 
77 {
78  // cleanup
79  WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
80 }
81 
82 WKernel* WKernel::instance( std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui )
83 {
84  if( m_kernel == NULL )
85  {
86  new WKernel( ge, ui ); // m_kernel will be set in the constructor.
87  }
88 
89  return m_kernel;
90 }
91 
93 {
94  // initialize
95  m_roiManager = std::shared_ptr< WROIManager >( new WROIManager() );
96 
97  m_selectionManager = std::shared_ptr< WSelectionManager >( new WSelectionManager() );
98 
99  // get module factory
101 
102  // init data handler
104 
105  // initialize module container
106  m_moduleContainer = std::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
107  "Root module container in Kernel." ) );
108  // this avoids the root container to be marked as "crashed" if a contained module crashes.
109  m_moduleContainer->setCrashIfModuleCrashes( false );
110 
111  // load all modules
112  m_moduleFactory->load();
113 
114  m_scriptEngine = std::shared_ptr< WScriptEngine >( new WScriptEngine( m_moduleContainer ) );
115 }
116 
118 {
119  return m_kernel;
120 }
121 
122 std::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
123 {
124  return m_graphicsEngine;
125 }
126 
127 std::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
128 {
129  return m_moduleContainer;
130 }
131 
132 std::shared_ptr< WUI > WKernel::getUI() const
133 {
134  return m_ui;
135 }
136 
138 {
139  WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
140 
141  // NOTE: stopping a container erases all modules inside.
142  getRootContainer()->stop();
143 
144  WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
145  WDataHandler::getDataHandler()->clear();
146 }
147 
149 {
150  WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
151 
152  // wait for UI to be initialized properly
153  if( m_ui )
154  {
155  m_ui->isInitialized().wait();
156  }
157  else
158  {
159  wlog::warn( "Kernel" ) << "Expected UI instance but none was initialized.";
160  }
161 
162  // start GE
163  if( m_graphicsEngine )
164  {
165  m_graphicsEngine->run();
166 
167  // wait for it to be ready
168  m_graphicsEngine->waitForFinalize();
169  }
170  else
171  {
172  wlog::warn( "Kernel" ) << "Expected GE instance but none was initialized.";
173  }
174 
175  // do extension loading
176  wlog::info( "Kernel" ) << "Initializing extensions.";
177  WModuleFactory::getModuleLoader()->initializeExtensions();
178 
179  // done. Notify anyone waiting
180  wlog::info( "Kernel" ) << "Initialization completed.";
182 
183  // actually there is nothing more to do here
184  waitForStop();
185 
186  WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
187 }
188 
190 {
191  return m_shutdownFlag;
192 }
193 
194 WBatchLoader::SPtr WKernel::loadDataSets( std::vector< std::string > filenames, bool suppressColormaps )
195 {
196  return getRootContainer()->loadDataSets( filenames, suppressColormaps );
197 }
198 
199 WBatchLoader::SPtr WKernel::loadDataSetsSynchronously( std::vector< std::string > filenames, bool suppressColormaps )
200 {
201  return getRootContainer()->loadDataSetsSynchronously( filenames, suppressColormaps );
202 }
203 
204 std::shared_ptr< WModule > WKernel::applyModule( std::shared_ptr< WModule > applyOn, std::shared_ptr< WModule > prototype )
205 {
206  return getRootContainer()->applyModule( applyOn, prototype );
207 }
208 
209 std::shared_ptr< WROIManager > WKernel::getRoiManager()
210 {
211  return m_roiManager;
212 }
213 
214 std::shared_ptr< WSelectionManager>WKernel::getSelectionManager()
215 {
216  return m_selectionManager;
217 }
218 
219 std::shared_ptr<WScriptEngine> WKernel::getScriptEngine()
220 {
221  return m_scriptEngine;
222 }
223 
225 {
226  return m_timer;
227 }
228 
230 {
231  switch( signal )
232  {
233  case KERNEL_STARTUPCOMPLETE:
234  return m_startupCompleted.subscribeSignal( notifier );
235  default:
236  std::ostringstream s;
237  s << "Could not subscribe to unknown signal.";
238  throw WKernelException( s.str() );
239  break;
240  }
241 }
std::shared_ptr< WBatchLoader > SPtr
Shared ptr abbreviation.
Definition: WBatchLoader.h:48
virtual void notify()
Notifies all waiting threads.
boost::signals2::connection subscribeSignal(t_ConditionNotifierType notifier) const
Subscribes a specified function to be notified on condition change.
Definition: WCondition.cpp:50
static std::shared_ptr< WDataHandler > getDataHandler()
As WDataHandler is a singleton -> return instance.
General purpose exception and therefore base class for all kernel related exceptions.
OpenWalnut kernel, managing modules and interaction between UI, GE and DataHandler.
Definition: WKernel.h:61
WConditionOneShot m_startupCompleted
Notified when the startup, including GE and UI has been completed.
Definition: WKernel.h:292
virtual boost::signals2::connection subscribeSignal(THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier)
Connects a specified notify function with a signal this thread instance is offering.
std::shared_ptr< WGraphicsEngine > m_graphicsEngine
Pointer to an initialized graphics engine.
Definition: WKernel.h:241
std::shared_ptr< WScriptEngine > m_scriptEngine
The script engine to use.
Definition: WKernel.h:266
std::shared_ptr< WSelectionManager > getSelectionManager()
get for selection manager
Definition: WKernel.cpp:214
std::shared_ptr< WModuleFactory > m_moduleFactory
The module factory to use.
Definition: WKernel.h:256
static WKernel * getRunningKernel()
Returns pointer to the currently running kernel.
Definition: WKernel.cpp:117
virtual void threadMain()
Function that has to be overwritten for execution.
Definition: WKernel.cpp:148
WKernel(std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui)
Constructor is protected because this class is a singleton.
Definition: WKernel.cpp:56
static WKernel * m_kernel
Pointer to the unique instance of this singleton class.
Definition: WKernel.h:282
static WKernel * instance(std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui)
Returns pointer to the running kernel or a new if no kernel was there.
Definition: WKernel.cpp:82
WTimer::SPtr m_timer
The ow system timer.
Definition: WKernel.h:287
const WBoolFlag & isFinishRequested() const
Determines whether all threads should finish.
Definition: WKernel.cpp:189
std::shared_ptr< WModuleContainer > m_moduleContainer
The container containing the modules.
Definition: WKernel.h:261
std::shared_ptr< WModule > applyModule(std::shared_ptr< WModule > applyOn, std::shared_ptr< WModule > prototype)
Function combines to modules.
Definition: WKernel.cpp:204
WBatchLoader::SPtr loadDataSetsSynchronously(std::vector< std::string > filenames, bool suppressColormaps=false)
Loads the specified files synchronously.
Definition: WKernel.cpp:199
std::shared_ptr< WUI > getUI() const
Getter for the associated UI.
Definition: WKernel.cpp:132
std::shared_ptr< WSelectionManager > m_selectionManager
pointer to a selection manager
Definition: WKernel.h:251
std::shared_ptr< WROIManager > m_roiManager
Pointer to a roi manager.
Definition: WKernel.h:246
std::shared_ptr< WModuleContainer > getRootContainer() const
Returns the root module container.
Definition: WKernel.cpp:127
void finalize()
Stops execution of the modules in the root container.
Definition: WKernel.cpp:137
std::shared_ptr< WScriptEngine > getScriptEngine()
Get the script engine of this kernel.
Definition: WKernel.cpp:219
void init()
Initializes the graphics engine, data handler and so on.
Definition: WKernel.cpp:92
KERNEL_SIGNAL
Enum of all possible signals WKernel instances can emit.
Definition: WKernel.h:78
WTimer::ConstSPtr getTimer() const
Returns the system timer.
Definition: WKernel.cpp:224
boost::function< void(void) > t_KernelGenericSignalHandlerType
Signal for generic events.
Definition: WKernel.h:67
std::shared_ptr< WROIManager > getRoiManager()
get for roi manager
Definition: WKernel.cpp:209
virtual ~WKernel()
Destructor.
Definition: WKernel.cpp:76
WBatchLoader::SPtr loadDataSets(std::vector< std::string > filenames, bool suppressColormaps=false)
Load specified datasets.
Definition: WKernel.cpp:194
std::shared_ptr< WGraphicsEngine > getGraphicsEngine() const
Returns pointer to currently running instance of graphics engine.
Definition: WKernel.cpp:122
std::shared_ptr< WUI > m_ui
The UI.
Definition: WKernel.h:236
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
Class able to contain other modules.
static SPtr getModuleFactory()
Returns instance of the module factory to use to create modules.
static std::shared_ptr< WModuleLoader > getModuleLoader()
Returns instance of the module loader.
Class to store and manage different ROI's for fiber selection.
Definition: WROIManager.h:39
Realtime timing.
The script engine.
Definition: WScriptEngine.h:41
manages the several selection tools
Base class for all classes needing to be executed in a separate thread.
std::shared_ptr< WThreadedRunner > SPtr
Abbreviation to a shared_ptr to this type.
void setThreadName(std::string name)
Set the name of the thread.
void waitForStop()
Let the thread sleep until a stop request was given.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base class for timing.
Definition: WTimer.h:37
std::shared_ptr< const WTimer > ConstSPtr
Convenience typedef for a const shared_ptr.
Definition: WTimer.h:47
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331
WStreamedLogger warn(const std::string &source)
Logging a warning message.
Definition: WLogger.h:309
WStreamedLogger info(const std::string &source)
Logging an information message.
Definition: WLogger.h:320