// {{{ the iWear header
/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the Framework Core Library
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework 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 General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
// }}}

#ifndef __DAEMONBASE_H
#define __DAEMONBASE_H

#ifndef __THREAD_H
#include <iwear/thread.h>
#endif

#ifndef __SERVICE_H
#include <iwear/service.h>
#endif

#ifndef __OUTPUTMANAGER_H
#include <iwear_output/outputmanager.h>
#endif

#ifndef __INPUTMANAGER_H
#include <iwear_input/inputmanager.h>
#endif

#ifndef __SENSORMANAGER_H
#include <iwsens/sensormanager.h>
#endif

#ifndef __UTILITY_H
#include <iwear/utility.h>
#endif

#ifndef __DEBUGSTREAM_H
#include <iwear/debugstream.h>
#endif

using namespace std;
using namespace iwear;
using namespace iwear::output;
using namespace iwear::input;
using namespace iwear::sensor;

namespace iwear{

    // Forward Declarations (namespace iwear)
    class ServiceManager;
    class Configuration;

    namespace uiservices{

class DaemonBase : public Thread, public Service 
{

 public:

    /**
     * The standard constructor for daemons in the iWear
     * framework
     * @param config pointer to the global configuration
     * @param sm pointer to the service manager
     * @param om pointer to the output manager. Normal daemons
     *        need the handle in order to hand over output datas
     *        (widgets) tp the manager for display purposes
     * @param daemon_name the daemon's short name
     * @param daemon_description short descriptive text which
     *        holds information about what the daemon actually does
     */
    DaemonBase(Configuration* config,
	    EventDispatcher* ed, 
	       ServiceManager* sm,
	       OutputManager* om,
	       InputManager* im,
	       SensorManager* sens,
	       const string& daemon_name,
	       const string& daemon_description = "");

 
    /**
     * The destructor
     */
    virtual ~DaemonBase(void);

    /**
     * This method is called when the daemon thread is started
     */
    virtual void Run(void) = 0;

    /**
     * This method is called when the daemon thread is terminated
     */
    void Final(void);

    // --- inherited from Service class -----------------

    void Init(void);

    void Start( void );

    void Stop(void);

    void Reset(void);

    void Suspend(void);

    void Pause(void);

    void Resume(void);

    // --- END inherited from Service class END ----------

    /**
     * Return the service type which is service_ui_daemon
     * for all iWear daemons
     */
    inline virtual service_type get_type(void) {
	return service_ui_daemon;
    }

    /**
     * Return the name of the application
     */
    inline virtual const string get_name(void) {
	return this->daemon_name;
    }

    /**
     * Return a short description for the daemon
     */
    inline virtual const string& get_description(void) const {
	return this->daemon_description;
    }

    /**
     * Compares the uid's contained in the Daemons.
     */
    inline bool operator<(const DaemonBase& od) const {
        return this->ID < od.get_uid();
    }

    /**
     * Compares the uid's contained in the Daemons.
     */
    inline bool operator>(const DaemonBase& od) const {
        return this->ID > od.get_uid();
    }

    /**
     * Compares the uid's contained in the Daemons.
     */
    inline bool operator==(const DaemonBase& od) const{
        return this->ID == od.get_uid();
    }

    /**
     * Compares the uid's contained in the Daemons.
     */
    inline bool operator!=(const DaemonBase& od) const{
        return !this->operator==(od);
    }

protected:

    // --- interface for application developers -----------

    /**
     * Each deamon can overwrite this function in order
     * to set its own set of configuration value/key-pairs
     */
    virtual void set_default_configuration( void );

    virtual void Init_Daemon(void) = 0;

    virtual void Reset_Daemon(void){}

    virtual void Suspend_Daemon(void){}

    virtual void Pause_Daemon(void){}

    virtual void Resume_Daemon(void){}

    virtual void Final_Daemon(void) = 0;    

    // --- END interface for application developers END ---

    /**
     * This function handles the details of the configuration
     * load process
     */
    virtual void init_configuration( void );

    /**
     * The daemon name
     */
    string daemon_name;

    /**
     * A short description of the daemon
     */
    string daemon_description;
    
    /**
     * Handle for the input manager
     */
    InputManager* input_manager;

    /**
     * Handle for the output manager which is required by each
     * daemon in order to send OutputDatas to be presented to the
     * user
     */
    OutputManager* output_manager;

    /**
     * Handle for the SensorManager
     */
    SensorManager* sensor_manager;

    /**
     * Reference to the iWear configuration
     */
    Configuration* configuration;

};

    } // namespace uiservices
} // namespace iwear

#endif // __DAEMONBASE_H


