OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <iostream>
34 #include <list>
35 #include <string>
36 #include <vector>
37 
38 // Use filesystem version 2 for compatibility with newer boost versions.
39 #ifndef BOOST_FILESYSTEM_VERSION
40  #define BOOST_FILESYSTEM_VERSION 2
41 #endif
42 #include <boost/filesystem.hpp>
43 #include <boost/thread/xtime.hpp>
44 
45 #include "../common/WStringUtils.h"
46 #include "../common/WThreadedRunner.h"
47 #include "../dataHandler/WDataHandler.h"
48 #include "../gui/WGUI.h"
49 #include "WKernel.h"
50 #include "WModule.h"
51 #include "WModuleContainer.h"
52 #include "WModuleFactory.h"
53 #include "WROIManager.h"
54 #include "WSelectionManager.h"
55 
56 #include "WVersion.h" // NOTE: this file is auto-generated by CMAKE
57 
58 /**
59  * Used for program wide access to the kernel.
60  */
62 
63 WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
65 {
66  WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
67  wlog::debug( "Kernel" ) << "Version: " << W_VERSION;
68 
69  // init the singleton
70  m_kernel = this;
71 
72  // initialize members
73  m_gui = gui;
74  m_graphicsEngine = ge;
75 
76  // init
77  init();
78 }
79 
81 {
82  // cleanup
83  WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
84 }
85 
86 WKernel* WKernel::instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui )
87 {
88  if( m_kernel == NULL )
89  {
90  new WKernel( ge, gui ); // m_kernel will be set in the constructor.
91  }
92 
93  return m_kernel;
94 }
95 
97 {
98  // initialize
99  m_roiManager = boost::shared_ptr< WROIManager >( new WROIManager() );
100 
101  m_selectionManager = boost::shared_ptr< WSelectionManager >( new WSelectionManager() );
102 
103  // get module factory
105 
106  // init data handler
108 
109  // initialize module container
110  m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
111  "Root module container in Kernel." ) );
112  // this avoids the root container to be marked as "crashed" if a contained module crashes.
113  m_moduleContainer->setCrashIfModuleCrashes( false );
114 
115  // load all modules
116  m_moduleFactory->load();
117 }
118 
120 {
121  return m_kernel;
122 }
123 
124 boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
125 {
126  return m_graphicsEngine;
127 }
128 
129 boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
130 {
131  return m_moduleContainer;
132 }
133 
134 boost::shared_ptr< WGUI > WKernel::getGui() const
135 {
136  return m_gui;
137 }
138 
140 {
141  WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
142 
143  // NOTE: stopping a container erases all modules inside.
144  getRootContainer()->stop();
145 
146  WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
147  WDataHandler::getDataHandler()->clear();
148 }
149 
151 {
152  WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
153 
154  // wait for GUI to be initialized properly
155  m_gui->isInitialized().wait();
156 
157  // start GE
158  m_graphicsEngine->run();
159 
160  // actually there is nothing more to do here
161  waitForStop();
162 
163  WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
164 }
165 
167 {
168  return m_shutdownFlag;
169 }
170 
171 void WKernel::loadDataSets( std::vector< std::string > fileNames )
172 {
173  getRootContainer()->loadDataSets( fileNames );
174 }
175 
176 void WKernel::loadDataSetsSynchronously( std::vector< std::string > fileNames )
177 {
178  getRootContainer()->loadDataSetsSynchronously( fileNames );
179 }
180 
181 boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
182 {
183  return getRootContainer()->applyModule( applyOn, prototype );
184 }
185 
186 boost::shared_ptr< WROIManager > WKernel::getRoiManager()
187 {
188  return m_roiManager;
189 }
190 
191 boost::shared_ptr< WSelectionManager>WKernel::getSelectionManager()
192 {
193  return m_selectionManager;
194 }