#include "stdafx.h" #include "SpBase.h" #include "SpHelper.h" // 柜员上下文 0x30A #include "CounterContext_msg_g.h" using namespace CounterContext; #include "AssistantChannel_client_g.h" using namespace AssistantChannel; #include "chan_protocol.h" #include "EventCode.h" class CCounterContextEntity; class ChannelClient : public ChannelService_ClientBase { public: ChannelClient(CCounterContextEntity *pEntity); virtual void OnMessage(ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer pData); }; class CCounterContextEntity : public CEntityBase,public ILogListener, public ITimerListener { public: CCounterContextEntity() : m_pChannelClient(NULL), m_bConnectAssist(FALSE) {} virtual ~CCounterContextEntity() {} virtual const char *GetEntityName() const { return "CounterContext"; } virtual void OnPreStart(CAutoArray strArgs,CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnStart(Error_Succeed); pTransactionContext->SendAnswer(Error); } virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnClose(Error_Succeed); pTransactionContext->SendAnswer(Error); } virtual void OnLog(const CAutoArray &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel, const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID, const CAutoArray &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo) { if (LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS == dwUserCode) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS"); Sleep(900); if (m_pChannelClient != NULL){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Close AssistChannel Session "); m_pChannelClient->GetFunction()->CloseSession(); m_pChannelClient = NULL; m_bConnectAssist = FALSE; } if (Error_Succeed == ConnectAssistChannel()) { m_bConnectAssist = TRUE; } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start timer for reconnect to assist channel."); GetFunction()->SetTimer(1, this, 3150); } } } ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError) { CSmartPointer pFunc = GetFunction(); int i = 0; m_arrListener.Init(1); pFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS, NULL, false); if (preOperationError != Error_Succeed) return preOperationError; return Error_Succeed; } void OnStarted() { m_pChannelClient = new ChannelClient(this); if (Error_Succeed == ConnectAssistChannel()) { m_bConnectAssist = TRUE; } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start timer for reconnect to assist channel."); GetFunction()->SetTimer(1, this, 3150); } } ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError) { CSmartPointer spFunction = GetFunction(); for (int i = 0; i < m_arrListener.GetCount(); ++i) { spFunction->UnsubscribeLog(m_arrListener[i]); } m_pChannelClient->GetFunction()->CloseSession(); m_pChannelClient = NULL; return Error_Succeed; } void OnReceivePkt(int sub_type, const char *buffer, int size) { if (sub_type == ACM_AGENTCTX_INFO) { CounterBasicInfo evt; SpBuffer buf; buf.OpenRead(buffer, size); #if defined(RVC_OS_WIN) evt.Serialize(buf); #else CSimpleString16Bit name16; buf& name16; evt.name = CSimpleString16Bit2A(name16); CSimpleString16Bit workNumber16; buf& workNumber16; evt.workNumber = CSimpleString16Bit2A(workNumber16); CSimpleString16Bit callid16; buf& callid16; evt.callid = CSimpleString16Bit2A(callid16); CSimpleString16Bit skillCode16; buf& skillCode16; evt.skillCode = CSimpleString16Bit2A(skillCode16); CSimpleString16Bit skillDesc16; buf& skillDesc16; evt.skillDesc = CSimpleString16Bit2A(skillDesc16); CSimpleString16Bit level16; buf& level16; evt.level = CSimpleString16Bit2A(level16); #endif //RVC_OS_WIN SpSendBroadcast(GetFunction(), SP_MSG_OF(CounterBasicInfo), SP_MSG_SIG_OF(CounterBasicInfo), evt); } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown sub_type %d from agent!", sub_type); } } ErrorCodeEnum ConnectAssistChannel() { if (NULL == m_pChannelClient) { m_pChannelClient = new ChannelClient(this); } ErrorCodeEnum Error = m_pChannelClient->Connect(); if (Error_Succeed != Error) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect assistant channel failed!"); m_pChannelClient->SafeDelete(); m_pChannelClient = NULL; return Error; } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect assistant channel success!"); } ChannelService_BeginRecv_Sub Sub; Sub.type = ACM_TYPE_AGENTCTX; Error = (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->BeginRecv(Sub); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_AGENTCTX failed!"); m_pChannelClient->GetFunction()->CloseSession(); m_pChannelClient = NULL; return Error; } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_AGENTCTX success!"); } return Error; } void OnTimeout(DWORD dwTimerID) { if (1 == dwTimerID) { if (FALSE == m_bConnectAssist) { if (Error_Succeed == ConnectAssistChannel()) { m_bConnectAssist = TRUE; } } if (TRUE == m_bConnectAssist) { GetFunction()->KillTimer(1); } } } private: ChannelClient *m_pChannelClient; CAutoArray m_arrListener; BOOL m_bConnectAssist; }; ChannelClient::ChannelClient( CCounterContextEntity *pEntity ) : ChannelService_ClientBase(pEntity) { } void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer pData ) { if (Error == Error_Succeed) { CCounterContextEntity *pEntity = static_cast(m_pEntityBase); pEntity->OnReceivePkt(Msg.sub_type, (const char*)Msg.data.m_pData, Msg.data.m_iLength); } } SP_BEGIN_ENTITY_MAP() SP_ENTITY(CCounterContextEntity) SP_END_ENTITY_MAP()