//
//  Copyright © 2004, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   WatchSentinel.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#if !defined(AFX_WATCHSENTINEL_H__FE62BE40_0628_4E5C_8132_36574CA6049D__INCLUDED_)
#define AFX_WATCHSENTINEL_H__FE62BE40_0628_4E5C_8132_36574CA6049D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BaseThread.h"

typedef void (*PFN_STATUSCHECK) ( void *pvParam, const int &riSensorIndex );

class EXPORT_DLL CWatchSentinel : public CBaseThread
{
public:

    ///////////////
    // Definitions.

    enum ETHREADMESSAGE
    {
        threadMessageCallbackInstalled  = threadMessageTerminate + 1,
        threadMessageForcePoll
    };

    ///////////////
    // Services.

                            CWatchSentinel              (   const unsigned long    &rulDefaultPollPeriod   = 10000 );       // 10s is the default poll period.
    virtual                ~CWatchSentinel              (   void );

    bool                    SetHealthCheckCallback      (   PFN_STATUSCHECK         pfnStatusCheck,
                                                            void                   *pvParam );

    bool                    ForceManualSensorPoll       ( void );

protected:

    ///////////////
    // Definitions.

    template <typename tCallback>
    class CStatusCheck
    {
    private:

        const static int    m_iSensorIndexUnspecified;

        tCallback           m_pfnCallback;
        void *              m_pvParam;

    public:

                            CStatusCheck                (   void );
        virtual            ~CStatusCheck                (   void );

        bool                Set                         (   tCallback               pCallback,
                                                            void *                  pvParam );

        void                operator ()                 (   const int              &riSensorIndex   = m_iSensorIndexUnspecified );

    };

    ///////////////
    // Attributes.

    const unsigned long             m_ulPollPeriodMilliseconds;
    const unsigned long             m_ulTerminatePeriod;
    CStatusCheck<PFN_STATUSCHECK>   m_StatusChecker;

    ///////////////
    // Services -- base class overrides et. al.

    void                    WatchCycle                  (   void );
    BOOL                    ProcessMessage              (   MSG                    *psMsg );
    void                    PollStatusCheck             (   void );

private:

    ///////////////
    // Services.

                            CWatchSentinel              (   const CWatchSentinel   &rRhs );                   // Not implemented thus private.
    CWatchSentinel &        operator =                  (   const CWatchSentinel   &rRhs );                   // Not implemented thus private.

};

#endif // !defined(AFX_WATCHSENTINEL_H__FE62BE40_0628_4E5C_8132_36574CA6049D__INCLUDED_)

