//
//  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:   
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#if !defined(AFX_SUBSYSTEM_H__9956CB7D_0FC4_4D33_8BB7_B4D6013C1282__INCLUDED_)
#define AFX_SUBSYSTEM_H__9956CB7D_0FC4_4D33_8BB7_B4D6013C1282__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Sensor.h"
#include "MessageProcessor.h"
#include "GenericSocket.h"
#include "Parent.h"

class CSubsystem : public CSensor  
{
public:

                            CSubsystem          (   const int                  &riSensorIndex = -1 );
    virtual                ~CSubsystem          (   void );

    // Overrides from base class CSensor.

    bool                    Startup             (   const int                  &riSensorIndex,
                                                    const DWORD                &rdwThreadId,
                                                    const unsigned int         &ruiDataReadyMessageId,
                                                    const unsigned int         &ruiReplyReadyMessageId,
                                                    const int                  &riActivateOnStartup );

    bool                    Shutdown            (   void );

    bool                    Load                (   BYTE                       *pbyData,
                                                    unsigned long              *pulSize,
                                                    unsigned long              *pulTimeStamp );

    bool                    SendCommand         (   int                         iClientIndex,
                                                    char                       *pszCommand );

    bool                    RetrieveStatus      (   BYTE                       *pbyData,
                                                    unsigned long              *pulSize,
                                                    unsigned long              *pulTimeStamp,
                                                    int                        *piClientIndex );

    bool                    IsSensorHealthy     (   void );

    bool                    Reset               (   void );

    bool                    RouteMessage        (   const BYTE                 *pbyMessage,
                                                    const unsigned long        &rulSize,
                                                    const unsigned long        &rulTimeStamp );

    bool                    Identify            (   SENSORINFO                 *psSensorInfo );

protected:

    class CDataHandler : private CParent<CSubsystem>
    {
    public:

        //////////////
        // Services.

                            CDataHandler            (   CSubsystem const           *pParent );
        virtual            ~CDataHandler            (   void );

        bool                Create                  (   void );

        bool                Send                    (   BYTE * const                pbyStream,
                                                        const unsigned long        &rulNumBytes );

        bool                IsHealthy               (   void );

    private:

        //////////////
        // Attributes.

        bool                m_bReplyToHost;

        CMessageProcessor   m_MessageProcessor;
        CGenericSocket     *m_pGenericSocket;

        //////////////
        // Services.

        static bool         DataAvailableCallback   (   const void                 *pthis,
                                                        const BYTE                 *pbyData,
                                                        const unsigned long        &rulBytes,
                                                        const unsigned long        &rulTimestamp );

        bool                HandleReceivedData      (   const BYTE                 *pbyData,
                                                        const unsigned long        &rulBytes,
                                                        const unsigned long        &rulTimestamp );

                            CDataHandler            (   void );                                         // Not implemented thus private.
                            CDataHandler            (   const CDataHandler         &rRhs );             // Not implemented thus private.
        CDataHandler &      operator =              (   const CDataHandler         &rRhs );             // Not implemented thus private.
    };

    //////////////
    // Definitions.

    friend                  CDataHandler;                                                               // Grant CDataHandler access to its parent.

    //////////////
    // Attributes.

    CDataHandler           *m_pHandler;

    //////////////
    // Services.

    void                    MessageError            (   const unsigned long    &rulRecordId     = 0UL );

    void                    DestroyHandler          (   void );

    bool                    Send                    (   BYTE * const            pbyStream,
                                                        const unsigned long    &rulNumBytes );

};

#endif // !defined(AFX_SUBSYSTEM_H__9956CB7D_0FC4_4D33_8BB7_B4D6013C1282__INCLUDED_)

