mod_countercontext.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpHelper.h"
  4. // 柜员上下文 0x30A
  5. #include "CounterContext_msg_g.h"
  6. using namespace CounterContext;
  7. #include "mod_assistantchannel/AssistantChannel_client_g.h"
  8. using namespace AssistantChannel;
  9. #include "mod_assistantchannel/chan_protocol.h"
  10. #include "../include/EventCode.h"
  11. class CCounterContextEntity;
  12. class ChannelClient : public ChannelService_ClientBase
  13. {
  14. public:
  15. ChannelClient(CCounterContextEntity *pEntity);
  16. virtual void OnMessage(ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData);
  17. };
  18. class CCounterContextEntity : public CEntityBase,public ILogListener, public ITimerListener
  19. {
  20. public:
  21. CCounterContextEntity() : m_pChannelClient(NULL), m_bConnectAssist(FALSE) {}
  22. virtual ~CCounterContextEntity() {}
  23. virtual const char *GetEntityName() const { return "CounterContext"; }
  24. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  25. {
  26. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  27. pTransactionContext->SendAnswer(Error);
  28. }
  29. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  30. {
  31. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  32. pTransactionContext->SendAnswer(Error);
  33. }
  34. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  35. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  36. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo)
  37. {
  38. if (LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS == dwUserCode)
  39. {
  40. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS");
  41. Sleep(900);
  42. if (m_pChannelClient != NULL){
  43. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Close AssistChannel Session ");
  44. m_pChannelClient->GetFunction()->CloseSession();
  45. m_pChannelClient->SafeDelete();
  46. m_pChannelClient = NULL;
  47. m_bConnectAssist = FALSE;
  48. }
  49. if (Error_Succeed == ConnectAssistChannel()) {
  50. m_bConnectAssist = TRUE;
  51. }
  52. else {
  53. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start timer for reconnect to assist channel.");
  54. GetFunction()->SetTimer(1, this, 3150);
  55. }
  56. }
  57. }
  58. ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  59. {
  60. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  61. int i = 0;
  62. m_arrListener.Init(1);
  63. pFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS, NULL, false);
  64. if (preOperationError != Error_Succeed)
  65. return preOperationError;
  66. return Error_Succeed;
  67. }
  68. void OnStarted()
  69. {
  70. m_pChannelClient = new ChannelClient(this);
  71. if (Error_Succeed == ConnectAssistChannel()) {
  72. m_bConnectAssist = TRUE;
  73. }
  74. }
  75. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  76. {
  77. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  78. for (int i = 0; i < m_arrListener.GetCount(); ++i)
  79. {
  80. spFunction->UnsubscribeLog(m_arrListener[i]);
  81. }
  82. m_pChannelClient->GetFunction()->CloseSession();
  83. m_pChannelClient->SafeDelete();
  84. m_pChannelClient = NULL;
  85. return Error_Succeed;
  86. }
  87. void OnReceivePkt(int sub_type, const char *buffer, int size)
  88. {
  89. if (sub_type == ACM_AGENTCTX_INFO) {
  90. CounterBasicInfo evt;
  91. SpBuffer buf;
  92. buf.OpenRead(buffer, size);
  93. CSimpleString16Bit name16;
  94. buf& name16;
  95. evt.name = CSimpleString16Bit2A(name16);
  96. CSimpleString16Bit workNumber16;
  97. buf& workNumber16;
  98. evt.workNumber = CSimpleString16Bit2A(workNumber16);
  99. CSimpleString16Bit callid16;
  100. buf& callid16;
  101. evt.callid = CSimpleString16Bit2A(callid16);
  102. CSimpleString16Bit skillCode16;
  103. buf& skillCode16;
  104. evt.skillCode = CSimpleString16Bit2A(skillCode16);
  105. CSimpleString16Bit skillDesc16;
  106. buf& skillDesc16;
  107. evt.skillDesc = CSimpleString16Bit2A(skillDesc16);
  108. CSimpleString16Bit level16;
  109. buf& level16;
  110. evt.level = CSimpleString16Bit2A(level16);
  111. //evt.Serialize(buf);
  112. SpSendBroadcast(GetFunction(), SP_MSG_OF(CounterBasicInfo), SP_MSG_SIG_OF(CounterBasicInfo), evt);
  113. Dbg("SpSendBroadcast CounterBasicInfo and callid = %s, name = %s, workNumber = %s, level = %s, skillCode = %s, skillDesc = %s.", evt.callid.GetData(), evt.name.GetData(), evt.workNumber.GetData(), evt.level.GetData(), evt.skillCode.GetData(), evt.skillDesc.GetData());
  114. } else {
  115. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown sub_type %d from agent!", sub_type);
  116. }
  117. }
  118. ErrorCodeEnum ConnectAssistChannel()
  119. {
  120. if (NULL == m_pChannelClient) {
  121. m_pChannelClient = new ChannelClient(this);
  122. }
  123. ErrorCodeEnum Error = m_pChannelClient->Connect();
  124. if (Error_Succeed != Error) {
  125. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect assistant channel failed!");
  126. m_pChannelClient->SafeDelete();
  127. m_pChannelClient = NULL;
  128. return Error;
  129. }
  130. else {
  131. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect assistant channel success!");
  132. }
  133. ChannelService_BeginRecv_Sub Sub;
  134. Sub.type = ACM_TYPE_AGENTCTX;
  135. Error = m_pChannelClient->BeginRecv(Sub);
  136. if (Error != Error_Succeed) {
  137. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_AGENTCTX failed!");
  138. m_pChannelClient->GetFunction()->CloseSession();
  139. m_pChannelClient->SafeDelete();
  140. m_pChannelClient = NULL;
  141. return Error;
  142. }
  143. else {
  144. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_AGENTCTX success!");
  145. }
  146. return Error;
  147. }
  148. void OnTimeout(DWORD dwTimerID)
  149. {
  150. if (1 == dwTimerID) {
  151. if (Error_Succeed == ConnectAssistChannel()) {
  152. m_bConnectAssist = TRUE;
  153. }
  154. if (TRUE == m_bConnectAssist) {
  155. GetFunction()->KillTimer(1);
  156. }
  157. }
  158. }
  159. private:
  160. ChannelClient *m_pChannelClient;
  161. CAutoArray<CUUID> m_arrListener;
  162. BOOL m_bConnectAssist;
  163. };
  164. ChannelClient::ChannelClient( CCounterContextEntity *pEntity ) : ChannelService_ClientBase(pEntity)
  165. {
  166. }
  167. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
  168. {
  169. if (Error == Error_Succeed) {
  170. CCounterContextEntity *pEntity = static_cast<CCounterContextEntity*>(m_pEntityBase);
  171. pEntity->OnReceivePkt(Msg.sub_type, (const char*)Msg.data.m_pData, Msg.data.m_iLength);
  172. }
  173. }
  174. SP_BEGIN_ENTITY_MAP()
  175. SP_ENTITY(CCounterContextEntity)
  176. SP_END_ENTITY_MAP()