mod_initiativetransfer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "FlowControlFSM.h"
  4. #include "Event.h"
  5. #include "InitiativeTransfer_server_g.h"
  6. #include "InitiativeTransfer_msg_g.h"
  7. #include "EventCode.h"
  8. #include "../mod_interactivecontrol/Event.h"
  9. using namespace InitiativeTransfer;
  10. #ifndef RVC_CONNECT_ASSIST_ID
  11. #define RVC_CONNECT_ASSIST_ID 1
  12. #endif
  13. class CFlowControlEntity;
  14. class FlowControlServiceSession : public FlowService_ServerSessionBase
  15. {
  16. public:
  17. FlowControlServiceSession(CFlowControlEntity *pEntity) : m_pEntity(pEntity) {}
  18. virtual void Handle_SwitchToAgentFlow(SpOnewayCallContext<FlowService_SwitchToAgentFlow_Info>::Pointer ctx);
  19. virtual void Handle_DisallowControl(SpOnewayCallContext<FlowService_DisallowControl_Info>::Pointer ctx);
  20. virtual void Handle_ReturnAgent(SpOnewayCallContext<FlowService_ReturnAgent_Info>::Pointer ctx);
  21. virtual void Handle_SwitchToFrontFlow(SpOnewayCallContext<FlowService_SwitchToFrontFlow_Info>::Pointer ctx);
  22. private:
  23. CFlowControlEntity *m_pEntity;
  24. };
  25. class CFlowControlEntity : public CEntityBase, public ILogListener, public ITimerListener
  26. {
  27. public:
  28. CFlowControlEntity() { }
  29. virtual ~CFlowControlEntity() {}
  30. virtual const char *GetEntityName() const { return "InitiativeTransfer"; }
  31. virtual bool IsService()const{return true;}
  32. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  33. {
  34. //MessageBoxA(0, 0, 0, 0);
  35. m_fsm = new CFlowControlFSM();
  36. ErrorCodeEnum Error = m_fsm->Init(this);
  37. int i = 0;
  38. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  39. m_arrListener.Init(3);
  40. spFunction->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS,NULL,false);
  41. spFunction->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU,NULL,false);
  42. spFunction->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_EXIT_BACKRUN,NULL,false);
  43. pTransactionContext->SendAnswer(Error);
  44. }
  45. void OnStarted()
  46. {
  47. if (Error_Succeed == m_fsm->ConnectToAssistchan(false)){
  48. m_fsm->m_bConnectAssit = true;
  49. }
  50. else{
  51. GetFunction()->SetTimer(RVC_CONNECT_ASSIST_ID, this, 5000);
  52. }
  53. }
  54. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  55. {
  56. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_EXIT));
  57. m_pTransactionContext = pTransactionContext; // save for later use
  58. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  59. for (int i = 0; i < m_arrListener.GetCount(); ++i)
  60. {
  61. spFunction->UnsubscribeLog(m_arrListener[i]);
  62. }
  63. m_arrListener.Clear();
  64. }
  65. CSmartPointer<ITransactionContext> m_pTransactionContext;
  66. void FinishClose(ErrorCodeEnum Error)
  67. {
  68. if (m_pTransactionContext != NULL)
  69. {
  70. m_pTransactionContext->SendAnswer(Error);
  71. }
  72. }
  73. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  74. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  75. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo)
  76. {
  77. if (dwUserCode == LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS)
  78. {
  79. GetFunction()->SetTimer(RVC_CONNECT_ASSIST_ID, this, 5000);
  80. m_fsm->m_bConnectAssit = false;
  81. Sleep(1000);
  82. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_ASSIS_IDEL));
  83. }
  84. else if (dwUserCode == LOG_EVT_UI_RETURNMENU)
  85. {
  86. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_MENU_RETURN));
  87. }
  88. else if (dwUserCode == LOG_EVT_UI_EXIT_BACKRUN)
  89. {
  90. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_EXIT_BACKRUN));
  91. }
  92. }
  93. virtual CServerSessionBase *OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszClass*/)
  94. {
  95. return new FlowControlServiceSession(this);
  96. }
  97. virtual void OnTimeout(DWORD dwTimerID)
  98. {
  99. if (RVC_CONNECT_ASSIST_ID == dwTimerID) {
  100. if (false == m_fsm->m_bConnectAssit) {
  101. if (Error_Succeed == m_fsm->ConnectToAssistchan(false)) {
  102. m_fsm->m_bConnectAssit = true;
  103. }
  104. }
  105. if (m_fsm->m_bConnectAssit) {
  106. GetFunction()->KillTimer(RVC_CONNECT_ASSIST_ID);
  107. }
  108. }
  109. }
  110. void SwitchToAgentFlow(CSimpleStringW req_context)
  111. {
  112. ReqAgentFlowEvent *e = new ReqAgentFlowEvent();
  113. e->req_context = req_context;
  114. e->m_pEntityBase = this;
  115. m_fsm->PostEventFIFO(e);
  116. }
  117. void DisallowControl()
  118. {
  119. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_DISCTRL));
  120. }
  121. void SwitchToFrontFlow()
  122. {
  123. m_fsm->PostEventFIFO(new FSMEvent(USER_EVT_EXIT_BACKRUN));
  124. }
  125. void ReturnAgent(CSimpleStringW ans_ctx)
  126. {
  127. AnsACMFlowEvent *e = new AnsACMFlowEvent();
  128. e->ans_context = ans_ctx;
  129. m_fsm->PostEventFIFO(e);
  130. }
  131. private:
  132. CFlowControlFSM *m_fsm;
  133. CAutoArray<CUUID> m_arrListener;
  134. };
  135. void FlowControlServiceSession::Handle_SwitchToAgentFlow( SpOnewayCallContext<FlowService_SwitchToAgentFlow_Info>::Pointer ctx )
  136. {
  137. DbgToBeidou(ctx->link, __FUNCTION__)();
  138. m_pEntity->SwitchToAgentFlow(ctx->Info.req_context);
  139. }
  140. void FlowControlServiceSession::Handle_DisallowControl( SpOnewayCallContext<FlowService_DisallowControl_Info>::Pointer ctx )
  141. {
  142. DbgToBeidou(ctx->link, __FUNCTION__)();
  143. m_pEntity->DisallowControl();
  144. }
  145. void FlowControlServiceSession::Handle_ReturnAgent( SpOnewayCallContext<FlowService_ReturnAgent_Info>::Pointer ctx )
  146. {
  147. DbgToBeidou(ctx->link, __FUNCTION__)();
  148. m_pEntity->ReturnAgent(ctx->Info.ans_context);
  149. }
  150. void FlowControlServiceSession::Handle_SwitchToFrontFlow(SpOnewayCallContext<FlowService_SwitchToFrontFlow_Info>::Pointer ctx)
  151. {
  152. DbgToBeidou(ctx->link, __FUNCTION__)();
  153. m_pEntity->SwitchToFrontFlow();
  154. }
  155. void FinishClose(CEntityBase *pEntity, ErrorCodeEnum Error = Error_Succeed)
  156. {
  157. if (pEntity) {
  158. CFlowControlEntity *pFlowControlEntity = static_cast<CFlowControlEntity *>(pEntity);
  159. pFlowControlEntity->FinishClose(Error);
  160. }
  161. }
  162. SP_BEGIN_ENTITY_MAP()
  163. SP_ENTITY(CFlowControlEntity)
  164. SP_END_ENTITY_MAP()