mod_countercontext.cpp 5.6 KB

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