FlowControlFSM.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "stdafx.h"
  2. #include "FlowControlFSM.h"
  3. #define LOG_EVT_EXIT_ACM_FLOW 0x30500002 //退出坐席控制流程
  4. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData )
  5. {
  6. LOG_FUNCTION();
  7. if (Error == Error_Succeed) {
  8. if (Msg.state == eChannelState_Connected) {
  9. m_pFSM->PostEventFIFO(new FSMEvent(USER_EVT_CHAN_ON));
  10. } else if (Msg.state == eChannelState_Idle) {
  11. m_pFSM->PostEventFIFO(new FSMEvent(USER_EVT_CHAN_OFF));
  12. }
  13. }
  14. }
  15. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
  16. {
  17. LOG_FUNCTION();
  18. if (Error == Error_Succeed) {
  19. m_pFSM->ProcessPacket(Msg.sub_type, Msg.data);
  20. }
  21. }
  22. CFlowControlFSM::CFlowControlFSM(): m_bIspad(FALSE)
  23. {
  24. }
  25. CFlowControlFSM::~CFlowControlFSM()
  26. {
  27. }
  28. void CFlowControlFSM::OnStateTrans( int iSrcState, int iDstState )
  29. {
  30. LOG_TRACE("Trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  31. switch (iDstState) {
  32. case s0:
  33. SetState("O", "Offline"); // offline
  34. break;
  35. case s1:
  36. SetState("F", "FrontFlow"); // frontflow
  37. break;
  38. case s2:
  39. SetState("B", "BackFlow"); // backflow
  40. break;
  41. case s3:
  42. SetState("C", "FrontCall"); // front call
  43. break;
  44. case s5:
  45. SetState("T", "Transferring"); // Transferring
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. ErrorCodeEnum CFlowControlFSM::OnInit()
  52. {
  53. ErrorCodeEnum Error;
  54. m_pClient = new ChannelClient(m_pEntity, this);
  55. Error = m_pClient->Connect();
  56. if (Error == Error_Succeed) {
  57. ChannelService_BeginState_Sub StateSub;
  58. Error = m_pClient->BeginState(StateSub);
  59. if (Error == Error_Succeed) {
  60. ChannelService_BeginRecv_Sub Sub;
  61. Sub.type = ACM_TYPE_FLW;
  62. Error = m_pClient->BeginRecv(Sub);
  63. }
  64. if (Error != Error_Succeed) {
  65. m_pClient->GetFunction()->CloseSession();
  66. m_pClient = NULL;
  67. }
  68. } else {
  69. m_pClient->SafeDelete();
  70. m_pClient = NULL;
  71. }
  72. if (Error == Error_Succeed) {
  73. AddStateHooker(this);
  74. }
  75. return Error;
  76. }
  77. ErrorCodeEnum CFlowControlFSM::OnExit()
  78. {
  79. if (m_pClient != NULL) {
  80. m_pClient->GetFunction()->CloseSession();
  81. m_pClient = NULL;
  82. }
  83. RemoveStateHooker(this);
  84. void FinishClose(CEntityBase *pEntity, ErrorCodeEnum Error = Error_Succeed);
  85. FinishClose(m_pEntity);
  86. return Error_Succeed;
  87. }
  88. BOOL CFlowControlFSM::ReConnectionAssistchan()
  89. {
  90. if (m_pClient != NULL)
  91. {
  92. m_pClient->GetFunction()->CloseSession();
  93. m_pClient = NULL;
  94. Dbg("Close AssistChannel Session ");
  95. }
  96. if (m_pClient == NULL)
  97. {
  98. Dbg("ReConnection AssistChannel Session");
  99. ErrorCodeEnum Error;
  100. m_pClient = new ChannelClient(m_pEntity, this);
  101. Error = m_pClient->Connect();
  102. if (Error != Error_Succeed)
  103. {
  104. m_pClient->SafeDelete();
  105. Dbg("m_channelClient connect fail!");
  106. return FALSE;
  107. }
  108. if(Error == Error_Succeed)
  109. {
  110. ChannelService_BeginState_Sub ChannelSub;
  111. Error = m_pClient->BeginState(ChannelSub);
  112. if (Error != Error_Succeed)
  113. {
  114. LOG_TRACE("BeginState biz channel failed!");
  115. m_pClient->GetFunction()->CloseSession();
  116. m_pClient = NULL;
  117. return FALSE;
  118. }
  119. }
  120. if(Error == Error_Succeed)
  121. {
  122. ChannelService_BeginRecv_Sub Sub;
  123. Sub.type = ACM_TYPE_FLW;
  124. Error = m_pClient->BeginRecv(Sub);
  125. if (Error != Error_Succeed)
  126. {
  127. LOG_TRACE("BeginState biz channel failed!");
  128. m_pClient->GetFunction()->CloseSession();
  129. m_pClient = NULL;
  130. return FALSE;
  131. }
  132. }
  133. }
  134. return TRUE;
  135. }
  136. unsigned int CFlowControlFSM::s0_on_event(FSMEvent* event)
  137. {
  138. if (event->iEvt == USER_EVT_ANSACMFLOW) {
  139. AgentFlowResult evt;
  140. evt.error = Error_NetBroken;
  141. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  142. } else if (event->iEvt == USER_EVT_REQAGENTFLOW) {
  143. AgentFlowResult evt;
  144. evt.error = Error_NetBroken;
  145. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  146. }
  147. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  148. {
  149. ReConnectionAssistchan();
  150. }
  151. return 0;
  152. }
  153. unsigned int CFlowControlFSM::s1_on_event(FSMEvent* event)
  154. {
  155. if (event->iEvt == USER_EVT_REQAGENTFLOW) {
  156. ReqAgentFlowEvent *rafe = static_cast<ReqAgentFlowEvent*>(event);
  157. ErrorCodeEnum Error;
  158. if (m_pClient) {
  159. ChannelService_Send_Info Info;
  160. Info.compress = true;
  161. Info.encrypt = true;
  162. Info.sub_type = ACM_SUBTYPE_REQ_FLOW;
  163. Info.type = ACM_TYPE_FLW;
  164. SpBuffer buf;
  165. buf.OpenWrite();
  166. CSimpleString16Bit reqContext = CSimpleStringW216Bit(rafe->req_context);
  167. buf & reqContext;
  168. Dbg("%s After convert: %s", __FUNCTION__, CSimpleString16Bit2A(reqContext).GetData());
  169. Info.data = buf.ToBlob();
  170. Error = m_pClient->Send(Info);
  171. } else
  172. {
  173. Error = Error_NetBroken;
  174. }
  175. return Error == Error_Succeed ? 1 : 0;
  176. } else if (event->iEvt == USER_EVT_NTFENTFLOW) {
  177. LOG_TRACE("spbroadcast NotifyEnterFlowEvent");
  178. NotifyEnterFlowEvent *nef = static_cast<NotifyEnterFlowEvent*>(event);
  179. NotifyEnterFlow evt;
  180. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(NotifyEnterFlow), SP_MSG_SIG_OF(NotifyEnterFlow), evt);
  181. }
  182. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  183. {
  184. ReConnectionAssistchan();
  185. }
  186. return 0;
  187. }
  188. unsigned int CFlowControlFSM::s2_on_event(FSMEvent* event)
  189. {
  190. if (event->iEvt == USER_EVT_CHAN_OFF) {
  191. // notify UI to unlock the screen
  192. AgentFlowResult evt;
  193. evt.error = Error_NetBroken;
  194. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  195. } else if (event->iEvt == USER_EVT_ANSAGENTFLOW) {
  196. AnsAgentFlowEvent *afe = static_cast<AnsAgentFlowEvent*>(event);
  197. AgentFlowResult evt;
  198. CSimpleStringW ans_context = CSimpleString16Bit2W(afe->ans_context);
  199. evt.ans_context = ans_context;
  200. evt.error = afe->err ? Error_Unexpect : Error_Succeed;
  201. Dbg("AgentFlowResult!!!!!");
  202. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  203. } else if (event->iEvt == USER_EVT_REQACMFLOW) {
  204. ReqACMFlowEvent *afe = static_cast<ReqACMFlowEvent *>(event);
  205. ACMFlowInvoke evt;
  206. CSimpleStringW req_context = CSimpleString16Bit2W(afe->req_context);
  207. evt.req_context = req_context;
  208. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(ACMFlowInvoke), SP_MSG_SIG_OF(ACMFlowInvoke), evt);
  209. } else if (event->iEvt == USER_EVT_NTFENTFLOW) {
  210. LOG_TRACE("notify enter flow, disallow control!");
  211. DisallowControl();
  212. }
  213. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  214. {
  215. ReConnectionAssistchan();
  216. }
  217. return 0;
  218. }
  219. void CFlowControlFSM::s2_on_exit()
  220. {
  221. //only pad vervison use
  222. if (m_bIspad)
  223. {
  224. LogEvent(Severity_Middle,LOG_EVT_EXIT_ACM_FLOW,"exit acm flow");
  225. }
  226. }
  227. void CFlowControlFSM::s3_on_entry()
  228. {
  229. }
  230. void CFlowControlFSM::s3_on_exit() {}
  231. unsigned int CFlowControlFSM::s3_on_event(FSMEvent* event)
  232. {
  233. if (event->iEvt == USER_EVT_ANSACMFLOW) {
  234. AnsACMFlowEvent *afe = static_cast<AnsACMFlowEvent*>(event);
  235. ErrorCodeEnum Error;
  236. if (m_pClient) {
  237. ChannelService_Send_Info Info;
  238. Info.compress = true;
  239. Info.encrypt = true;
  240. Info.sub_type = ACM_SUBTYPE_ANS_FLOW;
  241. Info.type = ACM_TYPE_FLW;
  242. SpBuffer buf;
  243. buf.OpenWrite();
  244. CSimpleString16Bit ansContext = CSimpleStringW216Bit(afe->ans_context);
  245. buf& ansContext;
  246. Dbg("%s After convert: %s", __FUNCTION__, CSimpleString16Bit2A(ansContext).GetData());
  247. Info.data = buf.ToBlob();
  248. Error = m_pClient->Send(Info);
  249. } else {
  250. Error = Error_NetBroken;
  251. }
  252. return Error == Error_Succeed ? 1 : 0;
  253. } else if (event->iEvt == USER_EVT_CHAN_OFF) {
  254. AgentFlowResult evt;
  255. evt.error = Error_NetBroken;
  256. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  257. }
  258. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  259. {
  260. ReConnectionAssistchan();
  261. }
  262. return 0;
  263. }
  264. void CFlowControlFSM::s5_on_entry()
  265. {
  266. }
  267. void CFlowControlFSM::s5_on_exit() { }
  268. unsigned int CFlowControlFSM::s5_on_event(FSMEvent* event)
  269. {
  270. if (event->iEvt == USER_EVT_DISCTRL) {
  271. DisallowControl();
  272. } else if (event->iEvt == USER_EVT_REQAGENTFLOW) {
  273. ReqAgentFlowEvent *rafe = static_cast<ReqAgentFlowEvent*>(event);
  274. ErrorCodeEnum Error;
  275. if (m_pClient) {
  276. ChannelService_Send_Info Info;
  277. Info.compress = true;
  278. Info.encrypt = true;
  279. Info.sub_type = ACM_SUBTYPE_REQ_FLOW;
  280. Info.type = ACM_TYPE_FLW;
  281. SpBuffer buf;
  282. buf.OpenWrite();
  283. CSimpleString16Bit reqContext = CSimpleStringW216Bit(rafe->req_context);
  284. buf& reqContext;
  285. Dbg("%s After convert: %s", __FUNCTION__, CSimpleString16Bit2A(reqContext).GetData());
  286. Info.data = buf.ToBlob();
  287. Error = m_pClient->Send(Info);
  288. } else {
  289. Error = Error_NetBroken;
  290. }
  291. return Error == Error_Succeed ? 1 : 0;
  292. }
  293. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  294. {
  295. ReConnectionAssistchan();
  296. }
  297. return 0;
  298. }
  299. unsigned int CFlowControlFSM::s7_on_event(FSMEvent* event)
  300. {
  301. if (event->iEvt == USER_EVT_ASSIS_IDEL)
  302. {
  303. ReConnectionAssistchan();
  304. }
  305. return 0;
  306. }
  307. void CFlowControlFSM::ProcessPacket( int sub_type, CBlob &blob )
  308. {
  309. if (sub_type == ACM_SUBTYPE_ANS_FLOW) {
  310. AnsAgentFlowEvent *e = new AnsAgentFlowEvent();
  311. SpBuffer buf;
  312. buf.OpenRead((const char*)blob.m_pData, blob.m_iLength);
  313. buf.SetLength(blob.m_iLength);
  314. buf & e->err & e->ans_context;
  315. PostEventFIFO(e);
  316. } else if (sub_type == ACM_SUBTYPE_REQ_FLOW) {
  317. ReqACMFlowEvent *e = new ReqACMFlowEvent();
  318. SpBuffer buf;
  319. buf.OpenRead((const char*)blob.m_pData, blob.m_iLength);
  320. buf.SetLength(blob.m_iLength);
  321. buf & e->req_context;
  322. PostEventFIFO(e);
  323. } else if (sub_type == ACM_SUBTYPE_NTF_ENTFLOW) {
  324. NotifyEnterFlowEvent *e = new NotifyEnterFlowEvent();
  325. e->m_pFSM = this;
  326. SpBuffer buf;
  327. buf.OpenRead((const char*)blob.m_pData, blob.m_iLength);
  328. buf.SetLength(blob.m_iLength);
  329. buf & e->context;
  330. PostEventFIFO(e);
  331. } else {
  332. LOG_TRACE("unknown sub_type from agent!");
  333. }
  334. }
  335. ErrorCodeEnum CFlowControlFSM::SetState(const char *s, const char *sdesc)
  336. {
  337. LOG_TRACE("set BackInitiative to %s, %s", s, sdesc);
  338. return m_pEntity->GetFunction()->SetSysVar("BackInitiative", s);
  339. }
  340. ErrorCodeEnum CFlowControlFSM::DisallowControl()
  341. {
  342. LOG_FUNCTION();
  343. if (m_pClient) {
  344. ChannelService_Send_Info Info;
  345. Info.compress = false;
  346. Info.encrypt = true;
  347. Info.id = 0;
  348. Info.sub_type = ACM_SUBTYPE_DIS_CONTROL;
  349. Info.type = ACM_TYPE_FLW;
  350. return m_pClient->Send(Info);
  351. } else {
  352. return Error_NetBroken;
  353. }
  354. }
  355. void NotifyEnterFlowEvent::OnUnhandled()
  356. {
  357. LOG_FUNCTION();
  358. m_pFSM->DisallowControl();
  359. }