WKernel.cpp 5.54 KB
Newer Older
ebaum's avatar
[ADD]  
ebaum committed
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
ebaum's avatar
[ADD]  
ebaum committed
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
ebaum's avatar
[ADD]  
ebaum committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25
#ifdef __linux__
26
    #include <unistd.h> // used for getcwd (to get current directory)
27 28
#endif

29
#if defined(__APPLE__)
30
    #include <mach-o/dyld.h>
31 32
#endif

33 34
#include <string>
#include <vector>
ebaum's avatar
ebaum committed
35

36
#include "../common/WLogger.h"
37
#include "../common/WThreadedRunner.h"
38 39
#include "../common/WTimer.h"
#include "../common/WRealtimeTimer.h"
Mathias Goldau's avatar
Mathias Goldau committed
40
#include "../dataHandler/WDataHandler.h"
41
#include "../gui/WGUI.h"
42
#include "WKernel.h"
43
#include "WModuleContainer.h"
44
#include "WModuleFactory.h"
45 46
#include "WROIManager.h"
#include "WSelectionManager.h"
ebaum's avatar
[ADD]  
ebaum committed
47

48
#include "core/WVersion.h"   // NOTE: this file is auto-generated by CMAKE
49

50
/**
ebaum's avatar
ebaum committed
51 52
 * Used for program wide access to the kernel.
 */
53
WKernel* WKernel::m_kernel = NULL;
ebaum's avatar
ebaum committed
54

55
WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
56 57
    WThreadedRunner(),
    m_timer( WTimer::SPtr( new WRealtimeTimer() ) )
ebaum's avatar
[ADD]  
ebaum committed
58
{
59
    WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
60
    wlog::debug( "Kernel" ) << "Version: " << W_VERSION;
ebaum's avatar
ebaum committed
61

62 63
    setThreadName( "Kernel" );

64
    // init the singleton
65
    m_kernel = this;
ebaum's avatar
ebaum committed
66

ebaum's avatar
[ADD]  
ebaum committed
67
    // initialize members
68 69
    m_gui = gui;
    m_graphicsEngine = ge;
70

71
    // init
ebaum's avatar
ebaum committed
72
    init();
ebaum's avatar
[ADD]  
ebaum committed
73 74 75 76 77
}

WKernel::~WKernel()
{
    // cleanup
78
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
ebaum's avatar
[ADD]  
ebaum committed
79 80
}

81 82 83 84 85 86 87 88 89 90
WKernel* WKernel::instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui )
{
    if( m_kernel == NULL )
    {
        new WKernel( ge, gui ); // m_kernel will be set in the constructor.
    }

    return m_kernel;
}

91
void WKernel::init()
ebaum's avatar
[ADD]  
ebaum committed
92
{
93
    // initialize
94
    m_roiManager = boost::shared_ptr< WROIManager >( new WROIManager() );
95

schurade's avatar
schurade committed
96 97
    m_selectionManager = boost::shared_ptr< WSelectionManager >( new WSelectionManager() );

98 99 100
    // get module factory
    m_moduleFactory = WModuleFactory::getModuleFactory();

101 102 103
    // init data handler
    WDataHandler::getDataHandler();

104 105 106
    // initialize module container
    m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
                "Root module container in Kernel." ) );
107 108
    // this avoids the root container to be marked as "crashed" if a contained module crashes.
    m_moduleContainer->setCrashIfModuleCrashes( false );
109 110 111

    // load all modules
    m_moduleFactory->load();
112 113

    m_scriptEngine = boost::shared_ptr< WScriptEngine >( new WScriptEngine( m_moduleContainer ) );
ebaum's avatar
[ADD]  
ebaum committed
114 115
}

ebaum's avatar
ebaum committed
116 117
WKernel* WKernel::getRunningKernel()
{
118
    return m_kernel;
ebaum's avatar
ebaum committed
119 120
}

121
boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
122
{
123
    return m_graphicsEngine;
ebaum's avatar
ebaum committed
124 125
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
126
boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
127
{
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
128
    return m_moduleContainer;
129 130
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
131
boost::shared_ptr< WGUI > WKernel::getGui() const
132
{
133
    return m_gui;
134 135
}

136
void WKernel::finalize()
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
137
{
138
    WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
139 140

    // NOTE: stopping a container erases all modules inside.
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
141
    getRootContainer()->stop();
142 143 144

    WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
    WDataHandler::getDataHandler()->clear();
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
145 146
}

147
void WKernel::threadMain()
ebaum's avatar
ebaum committed
148
{
149
    WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
ebaum's avatar
ebaum committed
150

151
    // wait for GUI to be initialized properly
152 153 154 155
    if( m_gui )
    {
        m_gui->isInitialized().wait();
    }
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
156

157
    // start GE
158 159 160 161
    if( m_graphicsEngine )
    {
        m_graphicsEngine->run();
    }
162

163 164
    // actually there is nothing more to do here
    waitForStop();
165

166
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
167 168
}

169
const WBoolFlag& WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
170
{
171
    return m_shutdownFlag;
ebaum's avatar
[ADD]  
ebaum committed
172 173
}

174
WBatchLoader::SPtr WKernel::loadDataSets( std::vector< std::string > filenames, bool suppressColormaps )
175
{
176
    return getRootContainer()->loadDataSets( filenames, suppressColormaps );
177 178
}

179
WBatchLoader::SPtr WKernel::loadDataSetsSynchronously( std::vector< std::string > filenames, bool suppressColormaps )
180
{
181
    return getRootContainer()->loadDataSetsSynchronously( filenames, suppressColormaps );
182 183
}

184
boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
185
{
186
    return getRootContainer()->applyModule( applyOn, prototype );
187 188
}

189
boost::shared_ptr< WROIManager > WKernel::getRoiManager()
190 191 192
{
    return m_roiManager;
}
193

schurade's avatar
schurade committed
194 195 196 197
boost::shared_ptr< WSelectionManager>WKernel::getSelectionManager()
{
    return m_selectionManager;
}
198

199 200 201 202 203
boost::shared_ptr<WScriptEngine> WKernel::getScriptEngine()
{
    return m_scriptEngine;
}

204 205 206 207 208
WTimer::ConstSPtr WKernel::getTimer() const
{
    return m_timer;
}