//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   Component.h
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    CComponent class definition file.
//
//  Notes:      According to MSDN (documentation on AfxFreeLibrary()):
//              "Both AfxFreeLibrary and AfxLoadLibrary maintain a reference count for 
//              each loaded library module. AfxFreeLibrary decrements the reference count 
//              of the loaded dynamic-link library (DLL) module. When the reference count 
//              reaches zero, the module is unmapped from the address space of the calling 
//              process and the handle is no longer valid. This reference count is 
//              incremented each time AfxLoadLibrary is called."
//
//              Therefore we don't need to explicitly handle reference counting ourselves
//              for multiple sensors that use the same DLL.
//

#if !defined(AFX_COMPONENT_H__078BF50A_DEA6_4E03_A482_93CE7DE3F220__INCLUDED_)
#define AFX_COMPONENT_H__078BF50A_DEA6_4E03_A482_93CE7DE3F220__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Include\Exports.h"

class CComponent
{
public:

    ///////////////
    // Services.

                CComponent                      (   void );
                CComponent                      (   const CString  &rLibraryName );
                                                
    virtual    ~CComponent                      (   void );

    bool        IsLoaded                        (   void ) const;

    bool        LoadDLL                         (   const CString  &rLibraryName );
    bool        UnloadDLL                       (   void );

    // DLL wrappers.

    int         Startup                         (   SENSORINFO     *pSensorInfo,
                                                    DWORD           dwThreadId,
                                                    UINT            uiDataReadyMessageId,
                                                    UINT            uiReplyReadyMessageId,
                                                    void           *vpfnLog,
                                                    void           *vpCriticalParameters );
                                                
    int         Shutdown                        (   int             iSensorIndex );
                                                
    int         Identify                        (   SENSORINFO     *pSensorInfo,
                                                    int             iSensorIndex );
                                                
    int         Load                            (   BYTE           *pbyData,
                                                    unsigned long  *pulSize,
                                                    unsigned long  *pulTimeStamp,
                                                    int             iSensorIndex );
                                                
    double      GetModuleVersion                (   void );
                                                
    int         SendCommand                     (   int             iSensorIndex,
                                                    int             iClientIndex,
                                                    char           *pszCommand );

    int         RetrieveStatus                  (   BYTE           *pbyData,
                                                    unsigned long  *pulSize,
                                                    unsigned long  *pulTimestamp,
                                                    int            *piClientIndex,
                                                    int             iSensorIndex );

    int         RouteMessage                    (   BYTE           *pbyMessage,
                                                    unsigned long   ulSize,
                                                    unsigned long   ulTimeStamp,
                                                    int             iSocketIndex,
                                                    int             iSensorIndex );

    int         ResetSensor                     (   int             iSensorIndex );

    int         IsSensorHealthy                 (   int             iSensorIndex );

private:

    ///////////////
    // Attributes.

    bool                    m_bLoaded;
    HINSTANCE               m_hInstLib;


    PFN_STARTUP             m_pfnStartup;
    PFN_SHUTDOWN            m_pfnShutdown;
    PFN_IDENTIFY            m_pfnIdentify;
    PFN_LOAD                m_pfnLoad;
    PFN_GETMODULEVERSION    m_pfnGetModuleVersion;
    PFN_SENDCOMMAND         m_pfnSendCommand;
    PFN_RETRIEVESTATUS      m_pfnRetrieveStatus;
    PFN_ROUTEMESSAGE        m_pfnRouteMessage;
    PFN_RESETSENSOR         m_pfnResetSensor;
    PFN_ISSENSORHEALTHY     m_pfnIsSensorHealthy;
};

#endif // !defined(AFX_COMPONENT_H__078BF50A_DEA6_4E03_A482_93CE7DE3F220__INCLUDED_)

