//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   ClientConnections.h
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:

#if !defined(AFX_CLIENTCONNECTIONS_H__A48B8044_0187_46A5_8730_B0D01F1D610C__INCLUDED_)
#define AFX_CLIENTCONNECTIONS_H__A48B8044_0187_46A5_8730_B0D01F1D610C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "SocketServer.h"
#include "ConnectedClient.h"
#include "Logger.h"

class CClientConnections
{
public:

	            CClientConnections                  (   const CLogger                   *pLogger,
                                                        const unsigned long             &rulMaxMessageLength,
                                                        const unsigned int              &ruiListenPort );

    virtual    ~CClientConnections                  (   void );

    bool        IsInitialized                       (   void ) const;

    bool        Send                                (   const int                       &riSocketIndex,
                                                        const BYTE                      *pbyRecord,
                                                        const unsigned long             &rulBytes );

    bool        SendToAllClients                    (   const BYTE                      *pbyData,
                                                        const unsigned long             &rulSize );

    bool        SendToSubscribingClients            (   const int                       &riSensorIndex,
                                                        const unsigned long             &rulRecordType,
                                                        const BYTE                      *pbyData,
                                                        const unsigned long             &rulSize );

    bool        SubscribingClients                  (   int *                           &rpiSocketIndexes,
                                                        int                             &riNumberOfClients,
                                                        const int                       &riSensorIndex,
                                                        const unsigned long             &rulRecordType );

    bool        Verbose                             (   const int                       &riSocketIndex ) const;

    bool        Verbose                             (   const int                       &riSocketIndex,
                                                        const bool                      &rbVerbose );

    bool        Subscribe                           (   const int                       &riSocketIndex,
                                                        const int                       &riSensorIndex,
                                                        const unsigned long             &rulRecordNumber,
                                                        const bool                      &rbEnable );

    bool        Disconnect                          (   const int                       &riSocketIndex );

    bool        SendAlarm                           (   const EALARMID                  &reAlarmId,
                                                        const BYTE                      *pbyData,
                                                        const unsigned long             &rulSize );

    bool        AlarmMode                           (   const int                       &riSocketIndex,
                                                        const EALARMID                  &reAlarmId,
                                                        const int                       &riAlarmAction );

    bool        ReportActiveAlarms                  (   const int                       &riSocketIndex,
                                                        CAlarm                          &rAlarm );

private:

    ///////////////
    // Definitions.

    typedef std::list<CConnectedClient>     ConnectedClientList_t;
    typedef ConnectedClientList_t::iterator ConnectedClientListIterator_t;

    ///////////////
    // Attributes.

    const CLogger          *m_pLogger;
    const unsigned long     m_ulMaxMessageLength;
    const unsigned int      m_uiListenPort;

    bool                    m_bInitialized;

    CSocketServer          *m_pSocketServer;
    ConnectedClientList_t   m_ClientList;
    mutable CCritical       m_ClientAccessGuard;

    ///////////////
    // Services.

    bool                    Dispatch                (   const BYTE                      *pbyData,
                                                        const unsigned long             &rulNumBytes,
                                                        const unsigned long             &rulTimeStamp,
                                                        const int                       &riSocketIndex );

    bool                    Connect                 (   const int                       &riSocketIndex );

    bool                    PostDisconnect          (   const int                       &riSocketIndex );

    bool                    ClientFromSocketIndex   (   const int                       &riSocketIndex,
                                                        ConnectedClientListIterator_t   &rpClientItem );

    bool                    DecodeAndQueueCommand   (   const BYTE                      *pbyData,
                                                        const unsigned long             &rulNumBytes,
                                                        const unsigned long             &rulTimeStamp,
                                                        const int                       &riSocketIndex );

    bool                    Decode7kCommand         (   const BYTE                      *pbyData,
                                                        const unsigned long             &rulNumBytes, 
                                                        unsigned long                   &rulCommand,
                                                        int                             &riSensorIndex,
                                                        unsigned long                   &rulAction,
                                                        CString                         &rParameters );

    static
    void                    SocketRxHandler         (   BYTE                            *pbyData,
                                                        PVOID                           lpvParam,
                                                        ULONG                           ulNumBytes,
                                                        INT                             iSocketIndex,
                                                        ULONG                           ulTimeStamp,
                                                        INT                             iReasonForCallback );

    // Following services are not implemented.

	                    CClientConnections      (   void );                                             // Not implemented.
                        CClientConnections      (   const CClientConnections        &rRhs );            // Not implemented.
    CClientConnections & operator =             (   const CClientConnections        &rRhs );            // Not implemented.

};

#endif // !defined(AFX_CLIENTCONNECTIONS_H__A48B8044_0187_46A5_8730_B0D01F1D610C__INCLUDED_)
