//---------------------------------------------------------------------------

#include <vcl.h>
#include "VMSCommLibF.h"
#pragma hdrstop

#include "JobThreadF.h"
#include "ClientSessionF.h"
#include "ClientSessionManagerF.h"
#include "CDSCtlrF.h"
#include "VmsProtocolF.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)


TJobThread *JobThread = NULL;
//---------------------------------------------------------------------------
TJobThread::TJobThread()
{
    FPnlState = NULL;
}
//---------------------------------------------------------------------------
TJobThread::~TJobThread()
{
}
//---------------------------------------------------------------------------

DWORD __fastcall TJobThread::Process(LPVOID AParam)
{
    TClientSession  *pSession;
    IPC_JOB_MESSAGE *pMsg;
    DWORD            dwJob;
    int              nRcvSize;
    char            *pRcvBuff;
    OverlappedIO     overlappedIO;

    TPanel *pState = FPnlState;

    while(!IsTerm() && !g_AppCfg.bThrExit)
    {
        try {
            if (pState) pState->Color = pState->Color == clLime ? clGreen : clRed;
        }catch(Exception &e) {}

        if (!g_jobQ.PopBlocking(dwJob))
        {
            continue;
        }
        if (dwJob == Q_CLOSE)
        {
            break;
        }
        else
        if (dwJob == Q_TICK)
        {
            if (pState) pState->Color = clLime;

            ClientSessionManager->SendFlushAllClient();
            ClientSessionManager->CheckCommandTimeoutSessions();
#if USE_POOL
#else
            ClientSessionManager->CollectGarbageSessions();
#endif
            continue;
        }
        else
        if (dwJob == eTcpCloseAll)
        {
            if (pState) pState->Color = clLime;

            ClientSessionManager->CloseAll();
            continue;
        }
        else
        if (dwJob == eVmsFormDownload)
        {
            // 정주기 시나리오 다운로드
            SendVmsFormDownload();
            continue;
        }

        if (pState) pState->Color = clLime;

        pMsg = (IPC_JOB_MESSAGE*)dwJob;

        try
        {
            pSession = (TClientSession*)pMsg->ObjPtr;
            if (pSession == NULL)
            {
                MERROR("JOBT Unknown Event Null session: %d", pMsg->Type);
            }
            else
            {
                switch(pMsg->Type)
                {
                case eTcpConnect:
                    if (pSession->OnConnect())
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eTcpErrorEvent:
                case eTcpErrorRecv:
                case eTcpErrorSend:
                case eTcpClose:
                    pSession->Disconnect();
                    break;
                case eTcpRecv:
                    nRcvSize = pSession->GetFreeSpaceSize();
                    pRcvBuff = pSession->GetBuffer();
                    if (nRcvSize >= pMsg->Len && pRcvBuff)
                    {
                        memcpy(pRcvBuff, pMsg->Buff, pMsg->Len);
                        overlappedIO.FSession = pSession;
                        RecvCompletion(0, pMsg->Len, (LPWSAOVERLAPPED)&overlappedIO, 0);
                    }
                    break;
                case eVmsScenarioDownload:
                    // 개별 명령에 의한 시나리오 다운로드
                    if (pSession->DownloadScenarioForm(false) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsParamReq:
                    if (pSession->ReqParameter() > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsLedPowerControl:
                    if (pSession->ReqLedPowerControl(pMsg->Buff[0]) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsFanPowerControl:
                    if (pSession->ReqFanPowerControl(pMsg->Buff[0]) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsTimeControl:
                    if (pSession->ReqTimeControl(pMsg->Buff) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsBrghSet:
                    if (pSession->ReqBrightSet(pMsg->Buff) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsConfigSet:
                    if (pSession->ReqConfigSet(pMsg->Buff) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsReset:
                    if (pSession->ReqReset((int)pMsg->Buff[0]) > 0)
                    {
                        pSession->SendFlush();
                    }
                    break;
                case eVmsLoginReq:
                    if (pSession->ReqLogin())
                    {
                        pSession->SendFlush();
                    }
                    break;
                }

                if (pSession->IsConnected)
                {
                    pSession->SendFlush();
                }
            }
        }
        __finally
        {
            //SAFE_DELETE(pMsg);
        }
    }

    Term();
    return 0;
}
//---------------------------------------------------------------------------

int __fastcall TJobThread::SendVmsFormDownload()
{
    CtlrItr it;
    for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
    {
        TCDSCtlr *pObj = (TCDSCtlr*)it->second;

        if (!pObj->Used) continue;
        if (!pObj->FProvide) continue;

        try
        {
            TClientSession *pSession = pObj->FSession;
            if (pSession)
            {
                if (pSession->DownloadScenarioForm(true) > 0)
                {
                    pSession->SendFlush();
                }
            }
        }
        catch(Exception &e)
        {
        }
    }
    return 0;
}
//---------------------------------------------------------------------------

