FlowControlFSM.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include "stdafx.h"
  2. #include "FlowControlFSM.h"
  3. #define LOG_EVT_EXIT_ACM_FLOW 0x30500002 //退出坐席控制流程
  4. //错误码
  5. #define LOG_ERR_S0_ANSACMFLOW 0x30500080
  6. #define LOG_ERR_S0_REQAGENTFLOW 0x30500081
  7. #define LOG_ERR_S1_REQAGENTFLOW_FAILED 0x30500082
  8. #define LOG_ERR_S2_ANSAGENTFLOW_FAILED 0x30500083
  9. #define LOG_ERR_S2_NTFENTFLOW_FAILED 0x30500084
  10. #define LOG_ERR_S3_ANSACMFLOW_FAILED 0x30500085
  11. #define LOG_ERR_S5_DISCTRL_FAILED 0x30500086
  12. #define LOG_ERR_S5_REQAGENTFLOW_FAILED 0x30500087
  13. #define LOG_ERR_S7_ENTRY 0x30500088
  14. #define LOG_ERR_S1_ASSIS_IDEL 0x30500089
  15. #define LOG_ERR_S2_ASSIS_IDEL 0x3050008A
  16. #define LOG_ERR_S3_ASSIS_IDEL 0x3050008B
  17. #define LOG_ERR_S5_ASSIS_IDEL 0x3050008C
  18. #define LOG_EVT_CONNEC_ASSISTCHAN_FAILED 0x3050008D
  19. #define LOG_ERR_S2_CHAN_OFF 0x3050008E
  20. #define LOG_ERR_S3_CHAN_OFF 0x3050008F
  21. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData )
  22. {
  23. if (Error == Error_Succeed) {
  24. if (Msg.state == eChannelState_Connected) {
  25. m_pFSM->PostEventFIFO(new FSMEvent(USER_EVT_CHAN_ON));
  26. }
  27. else if (Msg.state == eChannelState_Idle) {
  28. m_pFSM->PostEventFIFO(new FSMEvent(USER_EVT_CHAN_OFF));
  29. }
  30. }
  31. }
  32. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
  33. {
  34. if (Error == Error_Succeed) {
  35. m_pFSM->ProcessPacket(Msg.sub_type, Msg.data);
  36. }
  37. }
  38. CFlowControlFSM::CFlowControlFSM(): m_pClient(NULL)
  39. {
  40. }
  41. CFlowControlFSM::~CFlowControlFSM()
  42. {
  43. }
  44. void CFlowControlFSM::OnStateTrans( int iSrcState, int iDstState )
  45. {
  46. ChanState evt;
  47. evt.state = iDstState;
  48. evt.status = CSimpleStringA::Format("OnStateTrans from state %d to %d", iSrcState, iDstState);
  49. SpSendBroadcast(GetEntityBase()->GetFunction(), SP_MSG_OF(ChanState), SP_MSG_SIG_OF(ChanState), evt);
  50. switch (iDstState) {
  51. case s0:
  52. SetState("O", "Offline"); // offline
  53. break;
  54. case s1:
  55. SetState("F", "FrontFlow"); // frontflow
  56. break;
  57. case s2:
  58. SetState("B", "BackFlow"); // backflow
  59. break;
  60. case s3:
  61. SetState("C", "FrontCall"); // front call
  62. break;
  63. case s5:
  64. SetState("T", "Transferring"); // Transferring
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. ErrorCodeEnum CFlowControlFSM::OnInit()
  71. {
  72. m_bConnectAssit = false;
  73. AddStateHooker(this);
  74. return Error_Succeed;
  75. }
  76. ErrorCodeEnum CFlowControlFSM::OnExit()
  77. {
  78. if (m_pClient != NULL) {
  79. m_pClient->GetFunction()->CloseSession();
  80. m_pClient = NULL;
  81. }
  82. RemoveStateHooker(this);
  83. void FinishClose(CEntityBase *pEntity, ErrorCodeEnum Error = Error_Succeed);
  84. FinishClose(m_pEntity);
  85. return Error_Succeed;
  86. }
  87. bool CFlowControlFSM::IsAssistchanEntityAvailable()
  88. {
  89. if (Error_Succeed == ConnectToAssistchan(false)) {
  90. return true;
  91. }
  92. else {
  93. return false;
  94. }
  95. }
  96. ErrorCodeEnum CFlowControlFSM::ConnectToAssistchan(bool blogevt)
  97. {
  98. if (!IsAssistchanConnectSessionOK()) {
  99. FreeAssistchanClient();
  100. m_pClient = new ChannelClient(m_pEntity, this);
  101. ErrorCodeEnum errorCode = m_pClient->Connect();
  102. if (Error_Succeed != errorCode) {
  103. m_pClient->SafeDelete();
  104. m_pClient = NULL;
  105. if (blogevt) {
  106. LogWarn(Severity_Middle, Error_InvalidState, LOG_EVT_CONNEC_ASSISTCHAN_FAILED,
  107. CSimpleStringA::Format("Connect to assistchan entity failed:0x%08x.", errorCode));
  108. }
  109. return Error_InvalidState;
  110. }
  111. else {
  112. if (blogevt) {
  113. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Connect to assistchan Client Succeed!");
  114. }
  115. ChannelService_BeginState_Sub ChannelSub;
  116. errorCode = m_pClient->BeginState(ChannelSub);
  117. if (Error_Succeed != errorCode)
  118. {
  119. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("BeginState biz channel failed!");
  120. m_pClient->GetFunction()->CloseSession();
  121. m_pClient = NULL;
  122. return errorCode;
  123. }
  124. ChannelService_BeginRecv_Sub Sub;
  125. Sub.type = ACM_TYPE_FLW;
  126. errorCode = m_pClient->BeginRecv(Sub);
  127. if (Error_Succeed != errorCode)
  128. {
  129. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Register ACM_TYPE_FLW failed!");
  130. m_pClient->GetFunction()->CloseSession();
  131. m_pClient = NULL;
  132. return errorCode;
  133. }
  134. }
  135. }
  136. return Error_Succeed;
  137. }
  138. bool CFlowControlFSM::IsAssistchanConnectSessionOK()
  139. {
  140. return (m_pClient != NULL && !m_pClient->QuerySessionClosed());
  141. }
  142. void CFlowControlFSM::FreeAssistchanClient()
  143. {
  144. if (m_pClient) {
  145. m_pClient->GetFunction()->CloseSession();
  146. m_pClient = NULL;
  147. }
  148. }
  149. unsigned int CFlowControlFSM::s0_on_event(FSMEvent* event)
  150. {
  151. if (event->iEvt == USER_EVT_ANSACMFLOW) {
  152. AgentFlowResult evt;
  153. evt.error = Error_NetBroken;
  154. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  155. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S0_ANSACMFLOW, "s0 receive ANSACMFLOW");
  156. } else if (event->iEvt == USER_EVT_REQAGENTFLOW) {
  157. AgentFlowResult evt;
  158. evt.error = Error_NetBroken;
  159. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  160. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S0_REQAGENTFLOW, "s0 receive REQAGENTFLOW");
  161. }
  162. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  163. {
  164. if (Error_Succeed == ConnectToAssistchan(false)) {
  165. m_bConnectAssit = true;
  166. }
  167. }
  168. return 0;
  169. }
  170. unsigned int CFlowControlFSM::s1_on_event(FSMEvent* event)
  171. {
  172. if (event->iEvt == USER_EVT_REQAGENTFLOW) {
  173. ReqAgentFlowEvent *rafe = static_cast<ReqAgentFlowEvent*>(event);
  174. ErrorCodeEnum Error;
  175. if (m_pClient) {
  176. ChannelService_Send_Info Info;
  177. Info.compress = true;
  178. Info.encrypt = true;
  179. Info.sub_type = ACM_SUBTYPE_REQ_FLOW;
  180. Info.type = ACM_TYPE_FLW;
  181. SpBuffer buf;
  182. buf.OpenWrite();
  183. #if defined(RVC_OS_WIN)
  184. buf& rafe->req_context;
  185. #else
  186. CSimpleString16Bit reqContext = CSimpleStringW216Bit(rafe->req_context);
  187. buf& reqContext;
  188. #endif //RVC_OS_WIN
  189. Info.data = buf.ToBlob();
  190. Error = m_pClient->Send(Info);
  191. }
  192. else {
  193. Error = Error_NetBroken;
  194. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S1_REQAGENTFLOW_FAILED, "s1 receive REQAGENTFLOW but chan is off.");
  195. }
  196. return Error == Error_Succeed ? 1 : 0;
  197. }
  198. else if (event->iEvt == USER_EVT_NTFENTFLOW) {
  199. NotifyEnterFlowEvent *nef = static_cast<NotifyEnterFlowEvent*>(event);
  200. NotifyEnterFlow evt;
  201. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(NotifyEnterFlow), SP_MSG_SIG_OF(NotifyEnterFlow), evt);
  202. }
  203. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  204. {
  205. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S1_ASSIS_IDEL, "s1 ASSIS_IDEL.");
  206. if (Error_Succeed == ConnectToAssistchan(false)) {
  207. m_bConnectAssit = true;
  208. }
  209. }
  210. return 0;
  211. }
  212. unsigned int CFlowControlFSM::s2_on_event(FSMEvent* event)
  213. {
  214. if (event->iEvt == USER_EVT_CHAN_OFF) {
  215. // notify UI to unlock the screen
  216. AgentFlowResult evt;
  217. evt.error = Error_NetBroken;
  218. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  219. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S2_CHAN_OFF, "s2 chan off.");
  220. }
  221. else if (event->iEvt == USER_EVT_ANSAGENTFLOW) {
  222. AnsAgentFlowEvent *afe = static_cast<AnsAgentFlowEvent*>(event);
  223. AgentFlowResult evt;
  224. #if defined(RVC_OS_WIN)
  225. evt.ans_context = afe->ans_context;
  226. #else
  227. CSimpleStringW ans_context = CSimpleString16Bit2W(afe->ans_context);
  228. evt.ans_context = ans_context;
  229. #endif //RVC_OS_WIN
  230. evt.error = afe->err ? Error_Unexpect : Error_Succeed;
  231. if (evt.error != Error_Succeed){
  232. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S2_ANSAGENTFLOW_FAILED, "s2 receive ANSAGENTFLOW error.");
  233. }
  234. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  235. }
  236. else if (event->iEvt == USER_EVT_REQACMFLOW) {
  237. ReqACMFlowEvent *afe = static_cast<ReqACMFlowEvent *>(event);
  238. ACMFlowInvoke evt;
  239. #if defined(RVC_OS_WIN)
  240. evt.req_context = afe->req_context;
  241. #else
  242. CSimpleStringW req_context = CSimpleString16Bit2W(afe->req_context);
  243. evt.req_context = req_context;
  244. #endif //RVC_OS_WIN
  245. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(ACMFlowInvoke), SP_MSG_SIG_OF(ACMFlowInvoke), evt);
  246. }
  247. else if (event->iEvt == USER_EVT_NTFENTFLOW) {
  248. ErrorCodeEnum result = DisallowControl();
  249. if (result != Error_Succeed){
  250. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S2_NTFENTFLOW_FAILED, "s2 send NTFENTFLOW error.");
  251. }
  252. }
  253. else if (event->iEvt == USER_EVT_ASSIS_IDEL){
  254. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S2_ASSIS_IDEL, "s2 ASSIS_IDEL.");
  255. if (Error_Succeed == ConnectToAssistchan(false)) {
  256. m_bConnectAssit = true;
  257. }
  258. }
  259. return 0;
  260. }
  261. void CFlowControlFSM::s2_on_exit()
  262. {
  263. }
  264. void CFlowControlFSM::s3_on_entry()
  265. {
  266. }
  267. void CFlowControlFSM::s3_on_exit() {}
  268. unsigned int CFlowControlFSM::s3_on_event(FSMEvent* event)
  269. {
  270. if (event->iEvt == USER_EVT_ANSACMFLOW) {
  271. AnsACMFlowEvent *afe = static_cast<AnsACMFlowEvent*>(event);
  272. ErrorCodeEnum Error;
  273. if (m_pClient) {
  274. ChannelService_Send_Info Info;
  275. Info.compress = true;
  276. Info.encrypt = true;
  277. Info.sub_type = ACM_SUBTYPE_ANS_FLOW;
  278. Info.type = ACM_TYPE_FLW;
  279. SpBuffer buf;
  280. buf.OpenWrite();
  281. #if defined(RVC_OS_WIN)
  282. buf& afe->ans_context;
  283. #else
  284. CSimpleString16Bit ansContext = CSimpleStringW216Bit(afe->ans_context);
  285. buf& ansContext;
  286. #endif //RVC_OS_WIN
  287. Info.data = buf.ToBlob();
  288. Error = m_pClient->Send(Info);
  289. }
  290. else {
  291. Error = Error_NetBroken;
  292. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S3_ANSACMFLOW_FAILED, "s3 send ANSACMFLOW error.");
  293. }
  294. return Error == Error_Succeed ? 1 : 0;
  295. }
  296. else if (event->iEvt == USER_EVT_CHAN_OFF) {
  297. AgentFlowResult evt;
  298. evt.error = Error_NetBroken;
  299. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(AgentFlowResult), SP_MSG_SIG_OF(AgentFlowResult), evt);
  300. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S3_CHAN_OFF, "s3 chan off.");
  301. }
  302. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  303. {
  304. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S3_ASSIS_IDEL, "s3 ASSIS_IDEL.");
  305. if (Error_Succeed == ConnectToAssistchan(false)) {
  306. m_bConnectAssit = true;
  307. }
  308. }
  309. return 0;
  310. }
  311. void CFlowControlFSM::s5_on_entry()
  312. {
  313. }
  314. void CFlowControlFSM::s5_on_exit() { }
  315. unsigned int CFlowControlFSM::s5_on_event(FSMEvent* event)
  316. {
  317. if (event->iEvt == USER_EVT_DISCTRL) {
  318. ErrorCodeEnum result = DisallowControl();
  319. if (result != Error_Succeed) {
  320. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S5_DISCTRL_FAILED, "s5 send DISCTRL error.");
  321. }
  322. }
  323. else if (event->iEvt == USER_EVT_REQAGENTFLOW) {
  324. ReqAgentFlowEvent* rafe = static_cast<ReqAgentFlowEvent*>(event);
  325. ErrorCodeEnum Error;
  326. if (m_pClient) {
  327. ChannelService_Send_Info Info;
  328. Info.compress = true;
  329. Info.encrypt = true;
  330. Info.sub_type = ACM_SUBTYPE_REQ_FLOW;
  331. Info.type = ACM_TYPE_FLW;
  332. SpBuffer buf;
  333. buf.OpenWrite();
  334. #if defined(RVC_OS_WIN)
  335. buf& rafe->req_context;
  336. #else
  337. CSimpleString16Bit reqContext = CSimpleStringW216Bit(rafe->req_context);
  338. buf& reqContext;
  339. #endif //RVC_OS_WIN
  340. Info.data = buf.ToBlob();
  341. Error = m_pClient->Send(Info);
  342. }
  343. else {
  344. Error = Error_NetBroken;
  345. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S5_REQAGENTFLOW_FAILED, "s5 send REQAGENTFLOW error.");
  346. }
  347. return Error == Error_Succeed ? 1 : 0;
  348. }
  349. else if (event->iEvt == USER_EVT_ASSIS_IDEL)
  350. {
  351. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S5_ASSIS_IDEL, "s5 ASSIS_IDEL.");
  352. if (Error_Succeed == ConnectToAssistchan(false)) {
  353. m_bConnectAssit = true;
  354. }
  355. }
  356. return 0;
  357. }
  358. void CFlowControlFSM::s7_on_entry()
  359. {
  360. LogWarn(Severity_Low, Error_Debug, LOG_ERR_S7_ENTRY, "s7 ENTRY.");
  361. }
  362. unsigned int CFlowControlFSM::s7_on_event(FSMEvent* event)
  363. {
  364. if (event->iEvt == USER_EVT_ASSIS_IDEL){
  365. if (Error_Succeed == ConnectToAssistchan(false)) {
  366. m_bConnectAssit = true;
  367. }
  368. }
  369. return 0;
  370. }
  371. void CFlowControlFSM::ProcessPacket( int sub_type, CBlob &blob )
  372. {
  373. if (sub_type == ACM_SUBTYPE_ANS_FLOW) {
  374. AnsAgentFlowEvent *e = new AnsAgentFlowEvent();
  375. SpBuffer buf;
  376. buf.OpenRead((const char*)blob.m_pData, blob.m_iLength);
  377. buf.SetLength(blob.m_iLength);
  378. buf & e->err & e->ans_context;
  379. PostEventFIFO(e);
  380. } else if (sub_type == ACM_SUBTYPE_REQ_FLOW) {
  381. ReqACMFlowEvent *e = new ReqACMFlowEvent();
  382. SpBuffer buf;
  383. buf.OpenRead((const char*)blob.m_pData, blob.m_iLength);
  384. buf.SetLength(blob.m_iLength);
  385. buf & e->req_context;
  386. PostEventFIFO(e);
  387. } else if (sub_type == ACM_SUBTYPE_NTF_ENTFLOW) {
  388. NotifyEnterFlowEvent *e = new NotifyEnterFlowEvent();
  389. e->m_pFSM = this;
  390. SpBuffer buf;
  391. buf.OpenRead((const char*)blob.m_pData, blob.m_iLength);
  392. buf.SetLength(blob.m_iLength);
  393. buf & e->context;
  394. PostEventFIFO(e);
  395. } else {
  396. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown sub_type from agent!");
  397. }
  398. }
  399. ErrorCodeEnum CFlowControlFSM::SetState(const char *s, const char *sdesc)
  400. {
  401. return m_pEntity->GetFunction()->SetSysVar("BackInitiative", s);
  402. }
  403. ErrorCodeEnum CFlowControlFSM::DisallowControl()
  404. {
  405. if (m_pClient) {
  406. ChannelService_Send_Info Info;
  407. Info.compress = false;
  408. Info.encrypt = true;
  409. Info.id = 0;
  410. Info.sub_type = ACM_SUBTYPE_DIS_CONTROL;
  411. Info.type = ACM_TYPE_FLW;
  412. return m_pClient->Send(Info);
  413. } else {
  414. return Error_NetBroken;
  415. }
  416. }
  417. void NotifyEnterFlowEvent::OnUnhandled()
  418. {
  419. m_pFSM->DisallowControl();
  420. }