123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- #pragma once
- #include "SpFSM.h"
- #include "BranchDevice_server_g.h"
- #include "BranchDevice_def_g.h"
- #include "BranchDeviceClass.h"
- #include "TWProcHelper.h"
- #include <tchar.h>
- #include <strsafe.h>
- #include "../../Tool/spbranch/branch_def.h"
- using namespace SpBranch;
- #include <map>
- using namespace BranchDevice;
- typedef ErrorCodeEnum ( *LpCreateDevCom)(DeviceBaseClass *&baseObj);
- typedef ErrorCodeEnum ( *LpReleaseDevCom)(DeviceBaseClass *&pBaseObj);
- // Completion keys for the completion port
- #define COMPKEY_TERMINATE ((UINT_PTR) 0)
- #define COMPKEY_STATUS ((UINT_PTR) 1)
- #define COMPKEY_JOBOBJECT ((UINT_PTR) 2)
- #define CMDLINE_SIZE 520
- #define DEFAULT_MSG_SIZE 256
- #define MAX_MSG_SIZE 512
- #define DEFAULT_INFO_SIZE 630
- enum EvtType
- {
- USER_EVT_QUIT = (EVT_USER + 1),
- USER_EVT_OPEN,
- USER_EVT_CMD,
- USER_EVT_GETINFO,
- USER_EVT_OPENFINISHED,
- USER_EVT_CREATEPROCESS,
- USER_EVT_CREATEPROCESSFINISHED,
- USER_EVT_JOBNOTIFYSTART,
- USER_EVT_JOBNOTIFYFINISH,
- };
- struct BranchDevHandle
- {
- char szDevName[64];
- char szDevDllPath[MAX_PATH];
- //-1 dll_path error or get dev name error;
- //-2 process closed;
- //-3 invalid DevSN(temporary);
- //-4 get dev_params failed;
- //-5 invalid pipe status or create process failed;
- //-6 open device failed,
- //-7 load dll failed
- //=0 Normal
- //>0 returned from QueryDevice.
- int nStatus;
- int nReserved;
- char szInfo[DEFAULT_INFO_SIZE];
- };
- struct RWStruct {
- RWStruct()
- :hParentWrite(NULL),
- hParentRead(NULL),
- hChildWrite(NULL),
- hChildRead(NULL),
- dwProcessID(0),
- hFinishedFlag(NULL)
- {
- //empty.
- InitializeCriticalSection(&csExe);
- }
- ~RWStruct()
- {
- Cleanup();
- DeleteCriticalSection(&csExe);
- }
- BOOL IsValidRW()
- {
- if( ( hParentWrite == NULL || hParentWrite == INVALID_HANDLE_VALUE ) ||
- ( hParentRead == NULL || hParentRead == INVALID_HANDLE_VALUE ) ||
- ( hChildWrite == NULL || hChildWrite == INVALID_HANDLE_VALUE ) ||
- ( hChildRead == NULL || hChildRead == INVALID_HANDLE_VALUE ) ||
- dwProcessID == 0
- )
- {
- return FALSE;
- }
- return TRUE;
- }
- void Cleanup()
- {
- if(!( hParentWrite == NULL || hParentWrite == INVALID_HANDLE_VALUE ))
- {
- CloseHandle(hParentWrite);
- hParentWrite = NULL;
- }
- if(!( hParentRead == NULL || hParentRead == INVALID_HANDLE_VALUE ))
- {
- CloseHandle(hParentRead);
- hParentRead = NULL;
- }
- if(!( hChildWrite == NULL || hChildWrite == INVALID_HANDLE_VALUE ))
- {
- CloseHandle(hChildWrite);
- hChildWrite = NULL;
- }
- if(!( hChildRead == NULL || hChildRead == INVALID_HANDLE_VALUE ))
- {
- CloseHandle(hChildRead);
- hChildRead = NULL;
- }
- if(!( hFinishedFlag == NULL || hFinishedFlag == INVALID_HANDLE_VALUE ))
- {
- CloseHandle(hFinishedFlag);
- hFinishedFlag = NULL;
- }
- dwProcessID = 0;
- }
- HANDLE hParentWrite;
- HANDLE hParentRead;
- HANDLE hChildWrite;
- HANDLE hChildRead;
- DWORD dwProcessID;
- HANDLE hFinishedFlag;
- CRITICAL_SECTION csExe;
- };
- typedef RWStruct *PRWStruct;
- class CBranchDeviceFSM : public FSMImpl<CBranchDeviceFSM>
- {
- public:
- CBranchDeviceFSM():
- m_nDevNum(0),
- m_pDevHandles(NULL),
- m_pProcHelper(NULL),
- m_bNeedJob(FALSE),
- m_hNotifyFinishedEvent(NULL),
- m_pRWStr(NULL)
- {
- m_eCurState = Error_NotInit;
- m_pProcHelper = new CTWProcHelper();
- InitializeCriticalSection(&s_cs);
- }
- ~CBranchDeviceFSM()
- {
- Cleanup();
- DeleteCriticalSection(&s_cs);
- if(m_pProcHelper)
- {
- delete m_pProcHelper;
- m_pProcHelper = NULL;
- }
- }
- virtual ErrorCodeEnum OnInit();
- virtual ErrorCodeEnum OnExit();
- void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
- enum {s0, s1, s2, s3};
- BEGIN_FSM_STATE(CBranchDeviceFSM)
- FSM_STATE_ENTRY(s0, "Creating", s0_on_entry, s0_on_exit, s0_on_event)
- FSM_STATE_ENTRY(s1, "Opening", s1_on_entry, s1_on_exit, s1_on_event)
- FSM_STATE_ENTRY(s2, "Idle", s2_on_entry, s2_on_exit, s2_on_event)
- FSM_STATE_ENTRY(s3, "Failed", s3_on_entry, s3_on_exit, s3_on_event)
- END_FSM_STATE()
- BEGIN_FSM_RULE(CBranchDeviceFSM, s0)
- FSM_RULE_ENTRY(s0, s1, USER_EVT_CREATEPROCESSFINISHED, 0)
- FSM_RULE_ENTRY(s1, s2, USER_EVT_OPENFINISHED, 0)
- END_FSM_RULE()
- virtual void s0_on_entry();
- virtual void s0_on_exit();
- virtual unsigned int s0_on_event(FSMEvent* e);
- virtual void s1_on_entry();
- virtual void s1_on_exit();
- virtual unsigned int s1_on_event(FSMEvent* e);
- virtual void s2_on_entry();
- virtual void s2_on_exit();
- virtual unsigned int s2_on_event(FSMEvent* e);
- virtual void s3_on_entry();
- virtual void s3_on_exit();
- virtual unsigned int s3_on_event(FSMEvent* e);
- ErrorCodeEnum OpenDevs();
- ErrorCodeEnum OpenSpecifiedDev(int devSN);
- ErrorCodeEnum CreateAdapterLoadedProcesses();
- ErrorCodeEnum ReleaseDevs(bool bCloseDev = true);
- ErrorCodeEnum ReleaseSpcfDev(int devSN, bool bCloseDev);
- int GetDevCount() { return m_nDevNum; }
- void HandleCMD(SpReqAnsContext<BranchDeviceService_OpCmd_Req,
- BranchDeviceService_OpCmd_Ans>::Pointer ctx);
- void HandleGetInfo(SpReqAnsContext<BranchDeviceService_GetDevInfo_Req,
- BranchDeviceService_GetDevInfo_Ans>::Pointer ctx);
- static CRITICAL_SECTION s_cs;
- static UINT WINAPI DispatchThreadFunc( void* pArguments );
- SpReqAnsContext<BranchDeviceService_OpCmd_Req, BranchDeviceService_OpCmd_Ans>::Pointer m_ctx;
- BOOL JobInitialize();
- void JobNotify();
- void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize)
- {
- int aimIdx = -1;
- for(int idx=0; idx<m_nDevNum; idx++)
- {
- if(m_pRWStr[idx] != NULL && m_pRWStr[idx]->dwProcessID == PID)
- {
- aimIdx = idx;
- break;
- }
- }
- if(aimIdx != -1)
- {
- _tcscpy_s(szProcessName, cchSize, m_pDevHandles[aimIdx].szDevName);
- return;
- }
- if(!m_bNeedJob)
- {
- _tcscpy_s(szProcessName, cchSize, TEXT("???"));
- return;
- }
- return m_pProcHelper->GetProcessName(PID, szProcessName, cchSize);
- }
- //AnonyPipe
- BOOL CreateSpecifiedProcess(int);
- //default 30sec returned back.
- ErrorCodeEnum ProcessExchange(int nIdentifier,
- SpBranch_Command_Req& req, SpBranch_Command_Ans& ans,
- DWORD dwMillSec = 30000);
- BOOL RestartChildProcess(int idx);
- private:
- void Cleanup()
- {
- LOG_FUNCTION();
- EnterCriticalSection(&s_cs);
- if(m_bNeedJob)
- {
- m_bNeedJob = FALSE;
- UINT uExitCode = 0;
- m_pProcHelper->Terminate(uExitCode);
- // Post a special key that tells the completion port thread to terminate
- PostQueuedCompletionStatus(m_hIOCP, 0, COMPKEY_TERMINATE, NULL);
- //WaitForSingleObject(m_hNotifyFinishedEvent, 30000);
- Sleep(500);
- CloseHandle(m_hIOCP);
- CloseHandle(m_hNotifyFinishedEvent);
- for(int idx=0; idx<m_nDevNum; idx++)
- {
- if(m_pDevHandles != NULL && m_pDevHandles[idx].nReserved == 1)
- {
- if(m_pRWStr != NULL && m_pRWStr[idx] != NULL)
- {
- if(m_pProcHelper != NULL)
- m_pProcHelper->DestoryProcess(m_pRWStr[idx]->dwProcessID);
- }
- }
- }
- }
- if(m_nDevNum > 0)
- {
- Dbg("to destory malloced object...");
- //ReleaseDevs();
- free(m_pDevHandles);
- m_pDevHandles = NULL;
- for(int idx=0; idx<m_nDevNum; idx++)
- {
- m_pRWStr[idx]->Cleanup();
- delete m_pRWStr[idx];
- m_pRWStr[idx] = NULL;
- }
- free(m_pRWStr);
- m_pRWStr = NULL;
- m_nDevNum = 0;
- Dbg("Finish destorying.");
- }
- LeaveCriticalSection(&s_cs);
- }
- private:
- int m_nDevNum;
- BranchDevHandle* m_pDevHandles;
- CTWProcHelper* m_pProcHelper;
- BOOL m_bNeedJob;
- // Completion port that receives Job notifications
- HANDLE m_hIOCP;
- HANDLE m_hNotifyFinishedEvent;
- //AnonyPipe
- PRWStruct* m_pRWStr;
- ErrorCodeEnum m_eCurState;
- //For DualActive adapte -Josephus@201775 16:57:11
- CSimpleStringA m_strSpBranchPath;
- };
- class OperateCMDEvent : public FSMEvent
- {
- public:
- OperateCMDEvent() : FSMEvent(USER_EVT_CMD){}
- ~OperateCMDEvent(){}
- SpReqAnsContext<BranchDeviceService_OpCmd_Req,
- BranchDeviceService_OpCmd_Ans>::Pointer m_ctx;
- virtual void OnUnhandled()
- {
- if(m_ctx != NULL)
- {
- m_ctx->Answer(Error_InvalidState);
- }
- }
- };
- class GetInfoEvent : public FSMEvent
- {
- public:
- GetInfoEvent() : FSMEvent(USER_EVT_GETINFO){}
- ~GetInfoEvent(){}
- SpReqAnsContext<BranchDeviceService_GetDevInfo_Req,
- BranchDeviceService_GetDevInfo_Ans>::Pointer m_ctx;
- virtual void OnUnhandled()
- {
- if(m_ctx != NULL)
- {
- m_ctx->Answer(Error_InvalidState);
- }
- }
- };
- struct GetInfoTask : public ITaskSp
- {
- CBranchDeviceFSM* m_fsm;
- SpReqAnsContext<BranchDeviceService_GetDevInfo_Req,
- BranchDeviceService_GetDevInfo_Ans>::Pointer m_ctx;
- GetInfoTask(CBranchDeviceFSM* fsm): m_fsm(fsm){}
- void Process()
- {
- m_fsm->HandleGetInfo(m_ctx);
- return;
- }
- void SetContext(
- SpReqAnsContext<BranchDeviceService_GetDevInfo_Req,
- BranchDeviceService_GetDevInfo_Ans>::Pointer ctx)
- {
- m_ctx = ctx;
- return;
- }
- };
- struct OperateCMDTask : public ITaskSp
- {
- CBranchDeviceFSM* m_fsm;
- SpReqAnsContext<BranchDeviceService_OpCmd_Req,
- BranchDeviceService_OpCmd_Ans>::Pointer m_ctx;
- OperateCMDTask(CBranchDeviceFSM* fsm): m_fsm(fsm){}
- void Process()
- {
- //CTWProcHelper::SetThreadPrivilege(TRUE);
- m_fsm->HandleCMD(m_ctx);
- return;
- }
- void SetContext(
- SpReqAnsContext<BranchDeviceService_OpCmd_Req,
- BranchDeviceService_OpCmd_Ans>::Pointer ctx)
- {
- m_ctx = ctx;
- return;
- }
- };
- struct JobNotifyTask : public ITaskSp
- {
- CBranchDeviceFSM* m_fsm;
- JobNotifyTask(CBranchDeviceFSM* fsm): m_fsm(fsm){}
- void Process()
- {
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_JOBNOTIFYSTART));
- m_fsm->JobNotify();
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_JOBNOTIFYFINISH));
- }
- };
- struct OpenDeviceTask : public ITaskSp
- {
- CBranchDeviceFSM* m_fsm;
- OpenDeviceTask(CBranchDeviceFSM* fsm): m_fsm(fsm){}
- void Process()
- {
- //Open branchDevice sequentially. [Josephus in 10:19:43 2016/6/23]
- Sleep(800);
- m_fsm->OpenDevs();
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_OPENFINISHED));
- }
- };
- struct CreateProcessTask : public ITaskSp
- {
- CBranchDeviceFSM* m_fsm;
- CreateProcessTask(CBranchDeviceFSM* fsm): m_fsm(fsm){}
- void Process()
- {
- m_fsm->CreateAdapterLoadedProcesses();
- m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_CREATEPROCESSFINISHED));
- }
- };
|