WKernel.cpp 3.79 KB
Newer Older
ebaum's avatar
[ADD]  
ebaum committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// 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
25 26 27
#include <list>

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

#include "WKernel.h"
ebaum's avatar
ebaum committed
30 31
#include "WModule.h"
#include "WTestModule.h"
ebaum's avatar
[ADD]  
ebaum committed
32 33
#include "../common/WException.h"

ebaum's avatar
ebaum committed
34 35
#include "../graphicsEngine/WGraphicsEngine.h"

36
/**
ebaum's avatar
ebaum committed
37 38 39 40 41 42
 * \par Description
 * Used for program wide access to the kernel.
 */
WKernel* kernel = NULL;

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

    kernel = this;

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

ebaum's avatar
ebaum committed
53 54 55
    // init GE, DataHandler, ...
    init();

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

WKernel::~WKernel()
{
    // cleanup
ebaum's avatar
ebaum committed
63
    std::cout << "Shutting down Kernel" << std::endl;
ebaum's avatar
[ADD]  
ebaum committed
64 65 66 67 68 69 70
}

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

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

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

81
int WKernel::getArgumentCount() const
ebaum's avatar
ebaum committed
82 83 84 85
{
    return m_ArgC;
}

86
char** WKernel::getArguments() const
ebaum's avatar
ebaum committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
    return m_ArgV;
}

int WKernel::run()
{
    std::cout << "Starting Kernel" << std::endl;

    // 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
    std::cout << "Starting modules:" << std::endl;
    for( std::list<WModule*>::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
109
            ++list_iter )
ebaum's avatar
ebaum committed
110 111 112 113 114 115 116 117 118 119 120
    {
        std::cout << "Starting Module: " << ( *list_iter )->getName() << std::endl;
        ( *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
121
            ++list_iter )
ebaum's avatar
ebaum committed
122 123 124 125 126 127 128 129 130 131 132 133
    {
        ( *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
134 135
void WKernel::loadModules()
{
ebaum's avatar
ebaum committed
136
    // TODO(ebaum): add dynamic loading here
ebaum's avatar
ebaum committed
137
    std::cout << "Loading modules:" << std::endl;
ebaum's avatar
ebaum committed
138 139
    m_modules.clear();

ebaum's avatar
ebaum committed
140 141 142 143
    WModule* m = new WTestModule();
    std::cout << "Loading Module: " << m->getName() << std::endl;

    m_modules.push_back( m );
ebaum's avatar
ebaum committed
144 145 146 147 148
}

void WKernel::init()
{
    // initialize
ebaum's avatar
ebaum committed
149 150 151 152 153 154 155 156 157 158

    // 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() );
}

159
bool WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
160 161
{
    return m_FinishRequested;
ebaum's avatar
[ADD]  
ebaum committed
162 163
}