mod_countercontext.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "AssistantChannel_client_g.h"
  8. using namespace AssistantChannel;
  9. #include "chan_protocol.h"
  10. #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 = NULL;
  46. m_bConnectAssist = FALSE;
  47. }
  48. if (Error_Succeed == ConnectAssistChannel()) {
  49. m_bConnectAssist = TRUE;
  50. }
  51. else {
  52. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start timer for reconnect to assist channel.");
  53. GetFunction()->SetTimer(1, this, 3150);
  54. }
  55. }
  56. }
  57. ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  58. {
  59. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  60. int i = 0;
  61. m_arrListener.Init(1);
  62. pFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS, NULL, false);
  63. if (preOperationError != Error_Succeed)
  64. return preOperationError;
  65. return Error_Succeed;
  66. }
  67. void OnStarted()
  68. {
  69. m_pChannelClient = new ChannelClient(this);
  70. if (Error_Succeed == ConnectAssistChannel()) {
  71. m_bConnectAssist = TRUE;
  72. }
  73. else {
  74. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start timer for reconnect to assist channel.");
  75. GetFunction()->SetTimer(1, this, 3150);
  76. }
  77. }
  78. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  79. {
  80. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  81. for (int i = 0; i < m_arrListener.GetCount(); ++i)
  82. {
  83. spFunction->UnsubscribeLog(m_arrListener[i]);
  84. }
  85. m_pChannelClient->GetFunction()->CloseSession();
  86. m_pChannelClient = NULL;
  87. return Error_Succeed;
  88. }
  89. void OnReceivePkt(int sub_type, const char *buffer, int size)
  90. {
  91. if (sub_type == ACM_AGENTCTX_INFO) {
  92. CounterBasicInfo evt;
  93. SpBuffer buf;
  94. buf.OpenRead(buffer, size);
  95. #if defined(RVC_OS_WIN)
  96. evt.Serialize(buf);
  97. #else
  98. CSimpleString16Bit name16;
  99. buf& name16;
  100. evt.name = CSimpleString16Bit2A(name16);
  101. CSimpleString16Bit workNumber16;
  102. buf& workNumber16;
  103. evt.workNumber = CSimpleString16Bit2A(workNumber16);
  104. CSimpleString16Bit callid16;
  105. buf& callid16;
  106. evt.callid = CSimpleString16Bit2A(callid16);
  107. CSimpleString16Bit skillCode16;
  108. buf& skillCode16;
  109. evt.skillCode = CSimpleString16Bit2A(skillCode16);
  110. CSimpleString16Bit skillDesc16;
  111. buf& skillDesc16;
  112. evt.skillDesc = CSimpleString16Bit2A(skillDesc16);
  113. CSimpleString16Bit level16;
  114. buf& level16;
  115. evt.level = CSimpleString16Bit2A(level16);
  116. #endif //RVC_OS_WIN
  117. SpSendBroadcast(GetFunction(), SP_MSG_OF(CounterBasicInfo), SP_MSG_SIG_OF(CounterBasicInfo), evt);
  118. } else {
  119. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown sub_type %d from agent!", sub_type);
  120. }
  121. }
  122. ErrorCodeEnum ConnectAssistChannel()
  123. {
  124. if (NULL == m_pChannelClient) {
  125. m_pChannelClient = new ChannelClient(this);
  126. }
  127. ErrorCodeEnum Error = m_pChannelClient->Connect();
  128. if (Error_Succeed != Error) {
  129. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect assistant channel failed!");
  130. m_pChannelClient->SafeDelete();
  131. m_pChannelClient = NULL;
  132. return Error;
  133. }
  134. else {
  135. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect assistant channel success!");
  136. }
  137. ChannelService_BeginRecv_Sub Sub;
  138. Sub.type = ACM_TYPE_AGENTCTX;
  139. Error = (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->BeginRecv(Sub);
  140. if (Error != Error_Succeed) {
  141. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_AGENTCTX failed!");
  142. m_pChannelClient->GetFunction()->CloseSession();
  143. m_pChannelClient = NULL;
  144. return Error;
  145. }
  146. else {
  147. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_AGENTCTX success!");
  148. }
  149. return Error;
  150. }
  151. void OnTimeout(DWORD dwTimerID)
  152. {
  153. if (1 == dwTimerID) {
  154. if (FALSE == m_bConnectAssist) {
  155. if (Error_Succeed == ConnectAssistChannel()) {
  156. m_bConnectAssist = TRUE;
  157. }
  158. }
  159. if (TRUE == m_bConnectAssist) {
  160. GetFunction()->KillTimer(1);
  161. }
  162. }
  163. }
  164. private:
  165. ChannelClient *m_pChannelClient;
  166. CAutoArray<CUUID> m_arrListener;
  167. BOOL m_bConnectAssist;
  168. };
  169. ChannelClient::ChannelClient( CCounterContextEntity *pEntity ) : ChannelService_ClientBase(pEntity)
  170. {
  171. }
  172. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
  173. {
  174. if (Error == Error_Succeed) {
  175. CCounterContextEntity *pEntity = static_cast<CCounterContextEntity*>(m_pEntityBase);
  176. pEntity->OnReceivePkt(Msg.sub_type, (const char*)Msg.data.m_pData, Msg.data.m_iLength);
  177. }
  178. }
  179. SP_BEGIN_ENTITY_MAP()
  180. SP_ENTITY(CCounterContextEntity)
  181. SP_END_ENTITY_MAP()