WKernel.cpp 5.68 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 25
//
// 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/>.
//
//---------------------------------------------------------------------------

#include <iostream>
ebaum's avatar
ebaum committed
26
#include <list>
27 28
#include <string>
#include <vector>
ebaum's avatar
ebaum committed
29 30

#include <boost/thread/xtime.hpp>
ebaum's avatar
[ADD]  
ebaum committed
31 32

#include "WKernel.h"
ebaum's avatar
ebaum committed
33
#include "WModule.h"
34
#include "../modules/navigationSlices/WNavigationSliceModule.h"
ebaum's avatar
[ADD]  
ebaum committed
35 36
#include "../common/WException.h"

ebaum's avatar
ebaum committed
37 38
#include "../graphicsEngine/WGraphicsEngine.h"

39
/**
ebaum's avatar
ebaum committed
40 41 42 43 44
 * Used for program wide access to the kernel.
 */
WKernel* kernel = NULL;

WKernel::WKernel( int argc, char* argv[] )
ebaum's avatar
[ADD]  
ebaum committed
45
{
ebaum's avatar
ebaum committed
46 47 48 49
    std::cout << "Initializing Kernel" << std::endl;

    kernel = this;

ebaum's avatar
[ADD]  
ebaum committed
50
    // initialize members
ebaum's avatar
ebaum committed
51 52 53
    m_ArgC = argc;
    m_ArgV = argv;
    m_FinishRequested = false;
ebaum's avatar
[ADD]  
ebaum committed
54

ebaum's avatar
ebaum committed
55 56 57
    // init GE, DataHandler, ...
    init();

ebaum's avatar
[ADD]  
ebaum committed
58 59 60 61 62 63 64
    // load modules
    loadModules();
}

WKernel::~WKernel()
{
    // cleanup
65
    m_Logger->addLogMessage( "Shutting down Kernel", "Kernel", LL_DEBUG );
ebaum's avatar
[ADD]  
ebaum committed
66 67 68 69 70 71 72
}

WKernel::WKernel( const WKernel& other )
{
    *this = other;
}

ebaum's avatar
ebaum committed
73 74 75 76 77
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

78
boost::shared_ptr<WGraphicsEngine> WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
79 80 81 82
{
    return m_GraphicsEngine;
}

83 84 85 86 87 88
boost::shared_ptr<WDataHandler> WKernel::getDataHandler() const
{
    return m_DataHandler;
}


89
int WKernel::getArgumentCount() const
ebaum's avatar
ebaum committed
90 91 92 93
{
    return m_ArgC;
}

94
char** WKernel::getArguments() const
ebaum's avatar
ebaum committed
95 96 97 98 99 100
{
    return m_ArgV;
}

int WKernel::run()
{
101
    m_Logger->addLogMessage( "Starting Kernel", "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
102 103 104 105 106 107 108 109 110 111 112 113 114

    // TODO(ebaum): add separate graphics thread here
    m_GraphicsEngine->run();

    // run Gui
    // TODO(all): clean up this option handler mess
    m_Gui->run();

    // run? data handler stuff?

    // run module execution threads
    // TODO(ebaum): after having modules loaded they should be started here.
    // currently this is just the test module
115
    m_Logger->addLogMessage( "*** Starting modules:", "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
116
    for( std::list<WModule*>::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
117
            ++list_iter )
ebaum's avatar
ebaum committed
118
    {
119
        m_Logger->addLogMessage( "Starting module: " + ( *list_iter )->getName(), "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
120 121 122 123 124 125 126 127 128
        ( *list_iter )->run();
    }

    // wait
    m_Gui->wait( false );
    m_FinishRequested = true;

    // wait for modules to finish
    for( std::list<WModule*>::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
129
            ++list_iter )
ebaum's avatar
ebaum committed
130 131 132 133 134 135 136 137 138 139 140
    {
        ( *list_iter )->wait( true );
    }

    // finally GE
    m_GraphicsEngine->wait( true );

    // how to get QT return code from its thread?
    return 0;
}

ebaum's avatar
[ADD]  
ebaum committed
141 142
void WKernel::loadModules()
{
ebaum's avatar
ebaum committed
143
    // TODO(ebaum): add dynamic loading here
144
    m_Logger->addLogMessage( "*** Loading Modules: ", "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
145 146
    m_modules.clear();

147
    WModule* m = new WNavigationSliceModule();
148 149
    m_Logger->addLogMessage( "Loading module: " + m->getName(), "Kernel", LL_DEBUG );

ebaum's avatar
ebaum committed
150 151

    m_modules.push_back( m );
ebaum's avatar
ebaum committed
152 153 154 155 156
}

void WKernel::init()
{
    // initialize
157 158 159 160
    findAppPath();

    // initalize Logger
    m_Logger = boost::shared_ptr<WLogger>( new WLogger() );
ebaum's avatar
ebaum committed
161 162 163 164 165 166 167 168

    // initialize graphics engine
    // this also includes initialization of WGEScene and OpenSceneGraph
    m_GraphicsEngine = boost::shared_ptr<WGraphicsEngine>( new WGraphicsEngine );

    // initialize GUI
    // TODO(all): clean up this option handler mess
    m_Gui = boost::shared_ptr<WMainApplication>( new WMainApplication() );
169 170 171

    // initialize Datahandler
    m_DataHandler = boost::shared_ptr<WDataHandler>( new WDataHandler() );
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

bool WKernel::findAppPath()
{
    // FIXME (schurade)
    // this should work on linux, have to implement it for windows and mac later

    int length;
    char appPath[255];

    length = readlink( "/proc/self/exe", appPath, sizeof( appPath ) );

    // Catch some errors
    if ( length < 0 )
    {
        fprintf( stderr, "Error resolving symlink /proc/self/exe.\n" );
        return false;
    }
    if ( length >= 255 )
    {
        fprintf( stderr, "Path too long. Truncated.\n" );
        return false;
    }

    // the string this readlink() function returns is appended with a '@'.
    appPath[length] = '\0';

    // strip off the executable name
    while ( appPath[length] != '/' )
    {
        appPath[length] = '\0';
        --length;
    }

    m_AppPath = appPath;

    std::string shaderPath( appPath );
    m_ShaderPath = shaderPath + "shaders/";

    return true;
ebaum's avatar
ebaum committed
212 213
}

214
bool WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
215 216
{
    return m_FinishRequested;
ebaum's avatar
[ADD]  
ebaum committed
217 218
}

219 220 221 222 223
void WKernel::doLoadDataSets( std::vector< std::string > fileNames )
{
    m_DataHandler->loadDataSets( fileNames );
}

224 225 226 227 228
boost::shared_ptr<WDataHandler> WKernel::getDataHandler()
{
    return m_DataHandler;
}

229 230 231 232 233 234 235 236 237
std::string WKernel::getAppPath()
{
    return m_AppPath;
}

std::string WKernel::getShaderPath()
{
    return m_ShaderPath;
}
238 239 240 241 242

boost::shared_ptr<WLogger> WKernel::getLogger()
{
    return m_Logger;
}