ConnectorFSM.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. #include "stdafx.h"
  2. #include "ConnectorFSM.h"
  3. #include "../include/ModuleMix.h"
  4. #define RELEASEING_TIMER_INTERVAL 1200
  5. #define RELEASEING_SIP_TIMER 20000
  6. #define SIP_CALL_TIMER 70000 //sip呼叫超时时间,20170314,增加超时时间解决坐席忽闪问题
  7. #define RELEASEING_SIP_TIMEOUT 30000
  8. #define SIP_CONNECT_FAIL_TIMES 10
  9. #define LIVEDETECT_CONNECT_INTERVAL 5000 //live detection实体检测间隔5s
  10. #define LIVEDETECTION_TIMEOUT 60000 //活体检测超时时间60s
  11. class SyncServiceClient:public SyncService_ClientBase
  12. {
  13. public:
  14. SyncServiceClient(CEntityBase *pEntity, ACMCallFSM* fsm) : SyncService_ClientBase(pEntity), m_fsm(fsm) {}
  15. ACMCallFSM *m_fsm;
  16. };
  17. class MyPhoneClient : public PhoneService_ClientBase
  18. {
  19. public:
  20. MyPhoneClient(CEntityBase *pEntity, ACMCallFSM* fsm) : PhoneService_ClientBase(pEntity), m_fsm(fsm) {}
  21. virtual void OnMessage(ErrorCodeEnum Error, PhoneService_PhoneState_Info &Msg, CSmartPointer<IReleasable> pData)
  22. {
  23. Dbg("rx sip phone state = %d, status = %s", Msg.state, (LPCSTR)Msg.status);
  24. //LOG_TRACE("rx sip phone state = %d", Msg.state);
  25. if (Error == Error_Succeed) {
  26. switch (Msg.state) {
  27. case ePhone_Init:
  28. case ePhone_Terminated:
  29. {
  30. FSMEvent *e = new FSMEvent(USER_EVT_SIP_STATE_IDLE);
  31. m_fsm->PostEventFIFO(e);
  32. }
  33. break;
  34. case ePhone_Ready:
  35. {
  36. Dbg("sip channel connected");
  37. FSMEvent *e = new FSMEvent(USER_EVT_SIP_STATE_CONNECTED);
  38. m_fsm->PostEventFIFO(e);
  39. }
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. }
  46. ACMCallFSM *m_fsm;
  47. };
  48. class MyChannelClient : public ChannelService_ClientBase
  49. {
  50. public:
  51. MyChannelClient(CEntityBase *pEntity, ACMCallFSM* fsm) : ChannelService_ClientBase(pEntity), m_fsm(fsm) {}
  52. virtual void OnMessage(ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData)
  53. {
  54. if (Error == Error_Succeed) {
  55. switch (Msg.state) {
  56. case eChannelState_Idle:
  57. {
  58. FSMEvent *e = new FSMEvent(USER_EVT_CHAN_STATE_IDLE);
  59. m_fsm->PostEventFIFO(e);
  60. }
  61. break;
  62. case eChannelState_Connected:
  63. {
  64. Dbg("CHAN_STATE_CONNECTED.param = %s", (LPCSTR)Msg.param);
  65. FSMEvent *e = new ChanStateConnectedEvent(Msg.param);
  66. m_fsm->PostEventFIFO(e);
  67. }
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. }
  74. ACMCallFSM *m_fsm;
  75. };
  76. ACMCallFSM::ACMCallFSM( )
  77. : m_pPhoneClient(NULL), m_pChannelClient(NULL), m_bRing(FALSE),m_nCurSipServer(MAIN_SERVER),m_nCurChanServer(MAIN_SERVER)
  78. {
  79. }
  80. ACMCallFSM::~ACMCallFSM()
  81. {
  82. }
  83. ErrorCodeEnum ACMCallFSM::OnInit()
  84. {
  85. memset(m_strSIPProxyIP,0,sizeof(m_strSIPProxyIP));
  86. memset(m_strSIPCallNum,0,sizeof(m_strSIPCallNum));
  87. memset(m_iSIPProxyPort,0,sizeof(m_iSIPProxyPort));
  88. memset(m_strChanCallNum,0,sizeof(m_strChanCallNum));
  89. memset(m_strChanProxyIP,0,sizeof(m_strChanProxyIP));
  90. memset(m_iSIPProxyPort,0,sizeof(m_iSIPProxyPort));
  91. ErrorCodeEnum Error = LoadConfig();
  92. if (Error != Error_Succeed)
  93. goto on_error;
  94. Dbg("LoadConfig success");
  95. GetEntityBase()->GetFunction()->SetSysVar("CallState", "O"); // set to offline state
  96. Error = LoadTerminalId();
  97. if (Error != Error_Succeed)
  98. goto on_error;
  99. Dbg("sip proxy ip1:%s,num1:%s,port1:%d;ip2:%s,num2:%s,port2:%d", (LPCSTR)m_strSIPProxyIP[0],(LPCSTR)m_strSIPCallNum[0], m_iSIPProxyPort[0],(LPCSTR)m_strSIPProxyIP[1],(LPCSTR)m_strSIPCallNum[1], m_iSIPProxyPort[1]);
  100. Dbg("chan proxy ip1:%s,num1:%s,port1:%d;ip2:%s,num2:%s,port2:%d", (LPCSTR)m_strChanProxyIP[0],(LPCSTR)m_strChanCallNum[0], m_iChanProxyPort[0],(LPCSTR)m_strChanProxyIP[1],(LPCSTR)m_strChanCallNum[1], m_iChanProxyPort[1]);
  101. Dbg("TerminalId: %s", (LPCSTR)m_strTerminalId);
  102. m_bHangup = FALSE;
  103. m_bHandFree = TRUE;
  104. m_bAgentHandFree = TRUE;
  105. m_bIsAgentControl = FALSE;
  106. m_nSipErrorNum = 0;
  107. m_nLiveDetctionTime = 0;
  108. memset(&m_CallingParam,0,sizeof(m_CallingParam));
  109. m_pPhoneClient = new MyPhoneClient(m_pEntity, this);
  110. Error = m_pPhoneClient->Connect();
  111. if (Error != Error_Succeed)
  112. goto on_error;
  113. PhoneService_BeginState_Sub PhoneSub;
  114. Error = m_pPhoneClient->BeginState(PhoneSub);
  115. if (Error != Error_Succeed)
  116. goto on_error;
  117. m_pSyncServiceClient = new SyncServiceClient(m_pEntity,this);
  118. Error = m_pSyncServiceClient->Connect();
  119. if (Error != Error_Succeed)
  120. {
  121. Dbg("Connect SyncService Fail!");
  122. m_pSyncServiceClient->SafeDelete();
  123. m_pSyncServiceClient = NULL;
  124. //goto on_error;
  125. }
  126. m_nSysCallType = 0;
  127. m_pChannelClient = new MyChannelClient(m_pEntity, this);
  128. Error = m_pChannelClient->Connect();
  129. if (Error != Error_Succeed)
  130. goto on_error;
  131. ChannelService_BeginState_Sub ChannelSub;
  132. Error = m_pChannelClient->BeginState(ChannelSub);
  133. if (Error != Error_Succeed)
  134. goto on_error;
  135. Dbg("fsm init ok!");
  136. AddStateHooker(this);
  137. return Error;
  138. on_error:
  139. Dbg("on_error");
  140. FSMEvent *e = new FSMEvent(USER_EVT_ERROR);
  141. PostEventFIFO(e); // jump to error state
  142. return Error;
  143. }
  144. BOOL ACMCallFSM::ReConnectionAssistchan()
  145. {
  146. if (m_pChannelClient != NULL)
  147. {
  148. m_pChannelClient->GetFunction()->CloseSession();
  149. m_pChannelClient->SafeDelete();
  150. m_pChannelClient = NULL;
  151. Dbg("Close AssistChannel Session ");
  152. }
  153. if (m_pChannelClient == NULL)
  154. {
  155. Dbg("ReConnection AssistChannel Session");
  156. ErrorCodeEnum Error;
  157. m_pChannelClient = new MyChannelClient(m_pEntity, this);
  158. Error = m_pChannelClient->Connect();
  159. if (Error != Error_Succeed)
  160. {
  161. m_pChannelClient->SafeDelete();
  162. Dbg("m_channelClient connect fail!");
  163. return FALSE;
  164. }
  165. if (Error == Error_Succeed)
  166. {
  167. ChannelService_BeginState_Sub ChannelSub;
  168. Error = m_pChannelClient->BeginState(ChannelSub);
  169. if (Error != Error_Succeed)
  170. {
  171. Dbg("BeginState biz channel failed!");
  172. m_pChannelClient->GetFunction()->CloseSession();
  173. m_pChannelClient->SafeDelete();
  174. m_pChannelClient = NULL;
  175. return FALSE;
  176. }
  177. }
  178. }
  179. return TRUE;
  180. }
  181. BOOL ACMCallFSM::ReConnectionSipphone()
  182. {
  183. if (m_pPhoneClient != NULL)
  184. {
  185. m_pPhoneClient->GetFunction()->CloseSession();
  186. m_pPhoneClient->SafeDelete();
  187. m_pPhoneClient = NULL;
  188. Dbg("Close sip Session ");
  189. }
  190. if (m_pPhoneClient == NULL)
  191. {
  192. Dbg("ReConnection sip Session");
  193. ErrorCodeEnum Error;
  194. m_pPhoneClient = new MyPhoneClient(m_pEntity, this);
  195. Error = m_pPhoneClient->Connect();
  196. if (Error != Error_Succeed)
  197. {
  198. m_pPhoneClient->SafeDelete();
  199. Dbg("m_phoneClient connect fail!");
  200. return FALSE;
  201. }
  202. if (Error == Error_Succeed)
  203. {
  204. PhoneService_BeginState_Sub PhoneSub;
  205. Error = m_pPhoneClient->BeginState(PhoneSub);
  206. if (Error != Error_Succeed)
  207. {
  208. Dbg("BeginState sip failed!");
  209. m_pPhoneClient->GetFunction()->CloseSession();
  210. m_pPhoneClient->SafeDelete();
  211. m_pPhoneClient = NULL;
  212. return FALSE;
  213. }
  214. }
  215. }
  216. return TRUE;
  217. }
  218. int ACMCallFSM::GetDelayTime()
  219. {
  220. ErrorCodeEnum error= Error_Unexpect;
  221. SyncService_GetMachineData_Req req;
  222. req.key = "DelayTime";
  223. SyncService_GetMachineData_Ans ans;
  224. DWORD Timeout = 500;
  225. if (m_pSyncServiceClient)
  226. {
  227. error = m_pSyncServiceClient->GetMachineData(req,ans,Timeout);
  228. }
  229. if (error == Error_Succeed)
  230. {
  231. int delaytime = atoi(ans.value);
  232. return delaytime;
  233. }
  234. else
  235. {
  236. return -1;
  237. }
  238. }
  239. void ACMCallFSM::SetDelayTime()
  240. {
  241. if (m_pSyncServiceClient)
  242. {
  243. SyncService_SetMachineData_Info info;
  244. info.key = "DelayTime";
  245. info.data = "0";
  246. m_pSyncServiceClient->SetMachineData(info);
  247. Dbg("set delay time to 0");
  248. }
  249. }
  250. ErrorCodeEnum ACMCallFSM::OnExit()
  251. {
  252. return Error_Succeed;
  253. }
  254. void ACMCallFSM::OnStateTrans(int iSrcState, int iDstState)
  255. {
  256. Dbg("FSM state from state %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  257. if (CheckBeginRing(iSrcState, iDstState)) {
  258. StartRing();
  259. } else {
  260. if (CheckEndRing(iSrcState, iDstState))
  261. StopRing();
  262. }
  263. if (iSrcState != FSM_STATE_INIT && iDstState != FSM_STATE_EXIT) {
  264. int st1 = TranslateState(iSrcState);
  265. int st2 = TranslateState(iDstState);
  266. if (st1 != st2) {
  267. PhoneState evt;
  268. evt.state = st2;
  269. evt.status = CSimpleStringA::Format("OnStateTrans from state %d to %d", st1, st2);
  270. //LOG_TRACE(evt.status);
  271. if (!((st2 == eState_Fail)&&(m_nCurChanServer!=Error_Server)&&(m_nCurSipServer!=Error_Server))||m_bHangup)
  272. {
  273. Dbg("Broadcast state from %d to %d", st1, st2);
  274. SpSendBroadcast(GetEntityBase()->GetFunction(), SP_MSG_OF(PhoneState), SP_MSG_SIG_OF(PhoneState), evt);
  275. }
  276. SetCallState(st2);
  277. GetEntityBase()->GetFunction()->SetUserDefineState(st2+USER_SIP_OFFLINE);
  278. }
  279. }
  280. }
  281. ErrorCodeEnum ACMCallFSM::SetCallState(int state)
  282. {
  283. char *sts[] = {
  284. "O", // Offline
  285. "C", // Connecting
  286. "H", // HandFree
  287. "P", // Pickup
  288. "B", // Broken
  289. "F", // Fail
  290. "R", // Releasing
  291. "L" // LiveDetect
  292. };
  293. char* strCallState = "O";
  294. if (0 <= state && state < sizeof(sts)/sizeof(char*)){
  295. strCallState = sts[state];
  296. }
  297. Dbg("set call state to [%s].", strCallState);
  298. return GetEntityBase()->GetFunction()->SetSysVar("CallState", strCallState);
  299. }
  300. void ACMCallFSM::s0_on_entry()
  301. {
  302. m_strHintCallNum.Clear();
  303. m_nCurChanServer = MAIN_SERVER;
  304. m_nCurSipServer = MAIN_SERVER;
  305. memset(&m_CallingParam,0,sizeof(m_CallingParam));
  306. GetEntityBase()->GetFunction()->SetSysVar("CallType", "N"); //进入初始状态时设置呼叫模式为常规呼叫
  307. m_nSysCallType = 0;
  308. }
  309. void ACMCallFSM::s0_on_exit()
  310. {
  311. m_bHangup = FALSE;
  312. }
  313. unsigned int ACMCallFSM::s0_on_event(FSMEvent* event)
  314. {
  315. if (event->iEvt == USER_EVT_PICKUP_CALL)
  316. {
  317. memset(&m_CallingParam,0,sizeof(m_CallingParam)); //正常呼叫
  318. GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE, CALLTYPE_NORMAL); // 设置呼叫模式为常规呼叫
  319. m_nSysCallType = 0;
  320. }
  321. else if (event->iEvt == USER_EVT_HANDFREE_CALL)
  322. {
  323. GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE,CALLTYPE_NORMAL); // 设置呼叫模式为常规呼叫
  324. m_nSysCallType = 0;
  325. memset(&m_CallingParam,0,sizeof(m_CallingParam)); // 正常呼叫
  326. }
  327. else if (event->iEvt == USER_EVT_COMMAND_CALL)
  328. {
  329. //由指令模块触发呼叫
  330. GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE, CALLTYPE_MOBILE); // 设置呼叫模式为手机呼叫
  331. m_nSysCallType = 1;
  332. Dbg("start command call!");
  333. }
  334. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  335. {
  336. ReConnectionAssistchan();
  337. }
  338. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  339. {
  340. ReConnectionSipphone();
  341. }
  342. else if (event->iEvt == USER_EVT_STARTVIDEODISPLAY)
  343. {
  344. StartVideoDisplayEvent *e = static_cast<StartVideoDisplayEvent *>(event);
  345. StartVideo((LPCSTR)e->m_param);
  346. }
  347. else if (event->iEvt == USER_EVT_SHOWLOACALVIDEO)
  348. {
  349. ShowLocalVideoEvent *e = static_cast<ShowLocalVideoEvent *>(event);
  350. SetCallingType(NORMAL_CALLTYPE);
  351. StartVideo((LPCSTR)e->m_param);
  352. }
  353. else if (event->iEvt == USER_EVT_SHOWLOACALREMOTEVIDEO)
  354. {
  355. ShowLocalAndRemoteVideoEvent *e = static_cast<ShowLocalAndRemoteVideoEvent *>(event);
  356. SetCallingType(NORMAL_CALLTYPE);
  357. StartVideo((LPCSTR)e->m_param);
  358. }
  359. else if (event->iEvt == USER_EVT_DOUBLE_RECORD_CALL)
  360. {
  361. m_CallingParam.nCallType = DOUBLERECORD_CALLTYPE;
  362. //GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE,CALLTYPE_RECORD); // 设置呼叫模式为双录呼叫
  363. m_nSysCallType = 0;
  364. Dbg("start double record call,call type is %d.", m_CallingParam.nCallType);
  365. }
  366. else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
  367. {
  368. Dbg("stop show local and remote video.");
  369. StopVideo();
  370. SetCallingType(NORMAL_CALLTYPE);
  371. }
  372. return 0;
  373. }
  374. void ACMCallFSM::s7_on_entry()
  375. {
  376. }
  377. void ACMCallFSM::s7_on_exit()
  378. {
  379. }
  380. unsigned int ACMCallFSM::s7_on_event(FSMEvent* event)
  381. {
  382. if (event->iEvt == USER_EVT_STOPLOCALVIDEO)
  383. {
  384. Dbg("stop show local video");
  385. StopVideo();
  386. }
  387. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  388. {
  389. ReConnectionSipphone();
  390. }
  391. return 0;
  392. }
  393. void ACMCallFSM::s9_on_entry()
  394. {
  395. Dbg("start liveness detection.");
  396. m_nLiveDetctionTime = 0;
  397. ScheduleTimer(9, 2*LIVEDETECT_CONNECT_INTERVAL);
  398. }
  399. void ACMCallFSM::s9_on_exit()
  400. {
  401. CancelTimer(9);
  402. m_nLiveDetctionTime = 0;
  403. }
  404. unsigned int ACMCallFSM::s9_on_event(FSMEvent* event)
  405. {
  406. if (event->iEvt == USER_EVT_STOPVIDEODISPLAY)
  407. {
  408. //ScheduleTimer(9,3000);
  409. Dbg("stop live detection display");
  410. StopVideo();
  411. LogEvent(Severity_Middle,LOG_EVT_RELEASELIVEDETECTION,"release live detection");
  412. }
  413. else if (event->iEvt == EVT_TIMER)
  414. {
  415. m_nLiveDetctionTime += LIVEDETECT_CONNECT_INTERVAL;
  416. if (Error_InvalidState == GetLivenessDetectionStatus()){
  417. StopLivenessDetection();
  418. }
  419. else{
  420. if (m_nLiveDetctionTime > LIVEDETECTION_TIMEOUT){
  421. Dbg("liveness detection timeout.");
  422. StopLivenessDetection();
  423. }
  424. else{
  425. ScheduleTimer(9, LIVEDETECT_CONNECT_INTERVAL);
  426. }
  427. }
  428. }
  429. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  430. {
  431. ReConnectionSipphone();
  432. }
  433. return 0;
  434. }
  435. void ACMCallFSM::s8_on_entry()
  436. {
  437. int time = GetDelayTime();
  438. Dbg("get Delay time = %d",time);
  439. if (time > 0)
  440. {
  441. ScheduleTimer(8,time*1000);
  442. }
  443. else
  444. {
  445. PostEventFIFO(new FSMEvent(EVT_TIMER));
  446. }
  447. }
  448. void ACMCallFSM::s8_on_exit()
  449. {
  450. CancelTimer(8);
  451. }
  452. unsigned int ACMCallFSM::s8_on_event(FSMEvent* event)
  453. {
  454. return 0;
  455. }
  456. void ACMCallFSM::s10_on_entry()
  457. {
  458. //LOG_FUNCTION();
  459. ErrorCodeEnum Error = Error_Succeed;
  460. if (m_nCurSipServer == Error_Server)
  461. {
  462. Error = Error_NetBroken;
  463. }
  464. if (Error != Error_Succeed)
  465. {
  466. PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
  467. }
  468. }
  469. void ACMCallFSM::s10_on_exit() {}
  470. unsigned int ACMCallFSM::s10_on_event(FSMEvent* event)
  471. {
  472. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  473. {
  474. ReConnectionAssistchan();
  475. }
  476. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  477. {
  478. ReConnectionSipphone();
  479. }
  480. else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
  481. {
  482. Dbg("hang up call and stop show local and remote video.");
  483. PostEventFIFO(new FSMEvent(USER_EVT_HANGUP));
  484. m_bHangup=TRUE;
  485. StopVideo();
  486. }
  487. return 0;
  488. }
  489. void ACMCallFSM::s11_on_entry()
  490. {
  491. ErrorCodeEnum Error = Error_Succeed;
  492. if (m_nCurSipServer == Error_Server)
  493. {
  494. Error = Error_NetBroken;
  495. }
  496. if (Error == Error_Succeed)
  497. {
  498. if (m_CallingParam.nCallType != NORMAL_CALLTYPE && m_CallingParam.nCallType != DOUBLERECORD_CALLTYPE)
  499. {
  500. Dbg("Begin Make Distribute Call!");
  501. Error = MakeCall(m_nCurSipServer,m_CallingParam);
  502. if (Error!=Error_Succeed)
  503. {
  504. m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
  505. }
  506. Dbg("make call result:0x%08x", Error);
  507. }
  508. else
  509. {
  510. if (NORMAL_CALLTYPE == m_CallingParam.nCallType){
  511. Dbg("Begin Make Normal Call!");
  512. }
  513. else{
  514. Dbg("Begin Make Record Call!");
  515. }
  516. Error = MakeCall(m_strHintCallNum.GetLength() > 0 ? (LPCSTR)m_strHintCallNum : (LPCSTR)m_strSIPCallNum[m_nCurSipServer],m_nCurSipServer);
  517. if (Error!=Error_Succeed)
  518. {
  519. m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
  520. }
  521. Dbg("make call result:0x%08x", Error);
  522. }
  523. }
  524. if (Error != Error_Succeed)
  525. {
  526. PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
  527. }
  528. else
  529. {
  530. ScheduleTimer(11,SIP_CALL_TIMER);
  531. }
  532. }
  533. void ACMCallFSM::s11_on_exit()
  534. {
  535. CancelTimer(11);
  536. }
  537. unsigned int ACMCallFSM::s11_on_event(FSMEvent* event)
  538. {
  539. if (event->iEvt == USER_EVT_HANGUP)
  540. {
  541. m_bHangup = TRUE;
  542. }
  543. else if (event->iEvt == USER_EVT_SIP_STATE_IDLE)
  544. {
  545. m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
  546. m_nSipErrorNum++;
  547. //StopChannel();
  548. }
  549. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  550. {
  551. ReConnectionAssistchan();
  552. }
  553. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  554. {
  555. ReConnectionSipphone();
  556. }
  557. else if (event->iEvt == EVT_TIMER)
  558. {
  559. Dbg("sip call timeout,release call");
  560. m_nSipErrorNum++;
  561. }
  562. else if (event->iEvt == USER_EVT_JMP_FAIL)
  563. {
  564. m_nSipErrorNum++;
  565. }
  566. else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
  567. {
  568. Dbg("hang up call and stop show local and remote video.");
  569. PostEventFIFO(new FSMEvent(USER_EVT_HANGUP));
  570. m_bHangup=TRUE;
  571. StopVideo();
  572. }
  573. return 0;
  574. }
  575. void ACMCallFSM::s12_on_entry()
  576. {
  577. ErrorCodeEnum Error = Error_Succeed;
  578. if (m_nCurChanServer == Error_Server)
  579. {
  580. Error = Error_NetBroken;
  581. }
  582. m_nSipErrorNum = 0;
  583. Sleep(200);
  584. if (Error == Error_Succeed)
  585. {
  586. Dbg("begin start channel,m_nCurChanServer=%d",m_nCurChanServer);
  587. if (m_CallingParam.nCallType == NORMAL_CALLTYPE && m_CallingParam.nCallType != DOUBLERECORD_CALLTYPE)
  588. {
  589. Error = StartChannel(m_nCurChanServer);
  590. }
  591. else
  592. {
  593. Error = StartChannel(m_nCurChanServer,m_CallingParam);
  594. if (DOUBLERECORD_CALLTYPE == m_CallingParam.nCallType){
  595. if (FALSE == m_bHandFree){
  596. StopSpeakerAudioCapture();
  597. Dbg("double record call type, current is pick up mode, stop speaker audio capture.");
  598. }
  599. }
  600. }
  601. Dbg("start channel result:0x%08x", Error);
  602. if (Error != Error_Succeed)
  603. {
  604. Dbg("start channel failed:0x%08x,start hangup", Error);
  605. HangupCall();
  606. m_nCurChanServer?(m_nCurChanServer=Error_Server):(m_nCurChanServer=BACK_SERVER);
  607. Dbg("hangup call result:0x%08x", Error);
  608. }
  609. }
  610. if (Error != Error_Succeed)
  611. {
  612. PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
  613. }
  614. }
  615. void ACMCallFSM::s12_on_exit() {}
  616. unsigned int ACMCallFSM::s12_on_event(FSMEvent* event)
  617. {
  618. if (event->iEvt == USER_EVT_HANGUP)
  619. {
  620. m_bHangup = TRUE;
  621. }
  622. else if (event->iEvt == USER_EVT_SIP_STATE_IDLE)
  623. {
  624. m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
  625. StopChannel();
  626. }
  627. else if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
  628. {
  629. if (!m_bHangup)
  630. {
  631. m_nCurChanServer?(m_nCurChanServer=Error_Server):(m_nCurChanServer=BACK_SERVER);
  632. }
  633. HangupCall();
  634. //StopVideo();
  635. }
  636. else if (event->iEvt == USER_EVT_CHAN_STATE_CONNECTED)
  637. {
  638. ChanStateConnectedEvent *e = static_cast<ChanStateConnectedEvent *>(event);
  639. StartVideo((LPCSTR)e->m_param);
  640. }
  641. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  642. {
  643. ReConnectionAssistchan();
  644. }
  645. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  646. {
  647. ReConnectionSipphone();
  648. }
  649. else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
  650. {
  651. Dbg("hang up call and stop show local and remote video.");
  652. PostEventFIFO(new FSMEvent(USER_EVT_HANGUP));
  653. m_bHangup=TRUE;
  654. StopVideo();
  655. }
  656. return 0;
  657. }
  658. void ACMCallFSM::s13_on_entry()
  659. {
  660. if (m_bHandFree)
  661. {
  662. PostEventLIFO(new FSMEvent(USER_EVT_JMP_HANDFREE));
  663. }
  664. else
  665. {
  666. PostEventLIFO(new FSMEvent(USER_EVT_JMP_PICKUP));
  667. }
  668. }
  669. void ACMCallFSM::s13_on_exit() {}
  670. unsigned int ACMCallFSM::s13_on_event(FSMEvent* event)
  671. {
  672. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  673. {
  674. ReConnectionAssistchan();
  675. }
  676. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  677. {
  678. ReConnectionSipphone();
  679. }
  680. return 0;
  681. }
  682. void ACMCallFSM::s14_on_entry()
  683. {
  684. }
  685. void ACMCallFSM::s14_on_exit()
  686. {
  687. }
  688. unsigned int ACMCallFSM::s14_on_event(FSMEvent* event)
  689. {
  690. if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
  691. {
  692. Dbg("stop show local and remote video.");
  693. StopVideo();
  694. }
  695. else if (event->iEvt == USER_EVT_STOP_RECORD_BROADCAST)
  696. {
  697. Dbg("stop double record broadcast.");
  698. }
  699. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  700. {
  701. ReConnectionSipphone();
  702. }
  703. return 0;
  704. }
  705. void ACMCallFSM::s20_on_entry()
  706. {
  707. StopChannel();
  708. }
  709. void ACMCallFSM::s20_on_exit() {}
  710. unsigned int ACMCallFSM::s20_on_event(FSMEvent* event)
  711. {
  712. if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
  713. {
  714. StopVideo();
  715. }
  716. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  717. {
  718. ReConnectionAssistchan();
  719. }
  720. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  721. {
  722. ReConnectionSipphone();
  723. }
  724. return 0;
  725. }
  726. void ACMCallFSM::s21_on_entry()
  727. {
  728. ScheduleTimer(21,RELEASEING_SIP_TIMER);
  729. HangupCall();
  730. }
  731. void ACMCallFSM::s21_on_exit()
  732. {
  733. CancelTimer(21);
  734. CancelTimer(210);
  735. }
  736. unsigned int ACMCallFSM::s21_on_event(FSMEvent* event)
  737. {
  738. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  739. {
  740. ReConnectionAssistchan();
  741. }
  742. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  743. {
  744. ReConnectionSipphone();
  745. }
  746. else if (event->iEvt == EVT_TIMER)
  747. {
  748. if(event->param1 == 21)
  749. {
  750. Dbg("hangupcall timeout,release call");
  751. ScheduleTimer(210,RELEASEING_SIP_TIMEOUT);
  752. ReleaseCall(0);
  753. }
  754. else if (event->param1 == 210)
  755. {
  756. Dbg("release call timeout,restart sipphone");
  757. PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
  758. //重启mod_sipphone
  759. //RealSipErrorCheck();
  760. LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
  761. }
  762. }
  763. return 0;
  764. }
  765. void ACMCallFSM::s22_on_entry()
  766. {
  767. StopChannel();
  768. }
  769. void ACMCallFSM::s22_on_exit() {}
  770. unsigned int ACMCallFSM::s22_on_event(FSMEvent* event)
  771. {
  772. if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
  773. {
  774. StopVideo();
  775. }
  776. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  777. {
  778. ReConnectionAssistchan();
  779. }
  780. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  781. {
  782. ReConnectionSipphone();
  783. }
  784. return 0;
  785. }
  786. void ACMCallFSM::s23_on_entry()
  787. {
  788. if((!m_bHangup)&&(((m_nCurSipServer==BACK_SERVER)&&(m_nCurChanServer!=Error_Server))||((m_nCurChanServer==BACK_SERVER)&&(m_nCurSipServer!=Error_Server))))
  789. {
  790. PostEventLIFO(new FSMEvent(USER_EVT_RECONNECT));
  791. }
  792. else
  793. {
  794. ScheduleTimer(23, RELEASEING_TIMER_INTERVAL);
  795. }
  796. }
  797. void ACMCallFSM::s23_on_exit()
  798. {
  799. CancelTimer(23);
  800. }
  801. unsigned int ACMCallFSM::s23_on_event(FSMEvent* event)
  802. {
  803. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  804. {
  805. ReConnectionAssistchan();
  806. }
  807. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  808. {
  809. ReConnectionSipphone();
  810. }
  811. else if (event->iEvt == EVT_TIMER)
  812. {
  813. if (m_nSipErrorNum >= SIP_CONNECT_FAIL_TIMES)
  814. {
  815. m_nSipErrorNum = 0;
  816. //RealSipErrorCheck();
  817. LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
  818. }
  819. }
  820. return 0;
  821. }
  822. void ACMCallFSM::s24_on_entry()
  823. {
  824. ScheduleTimer(24, RELEASEING_TIMER_INTERVAL);
  825. }
  826. void ACMCallFSM::s24_on_exit()
  827. {
  828. CancelTimer(24);
  829. }
  830. unsigned int ACMCallFSM::s24_on_event(FSMEvent* event)
  831. {
  832. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  833. {
  834. ReConnectionAssistchan();
  835. }
  836. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  837. {
  838. ReConnectionSipphone();
  839. }
  840. return 0;
  841. }
  842. void ACMCallFSM::s3_on_entry() {}
  843. void ACMCallFSM::s3_on_exit() {}
  844. unsigned int ACMCallFSM::s3_on_event(FSMEvent* event)
  845. {
  846. //LOG_TRACE("ACMCallFSM::s3_on_event, id = %d", event->iEvt);
  847. if (event->iEvt == USER_EVT_TO_HANDFREE)
  848. {
  849. //m_bHandFree = TRUE;
  850. }
  851. else if (event->iEvt == USER_EVT_TO_PICKUP)
  852. {
  853. //m_bHandFree = FALSE;
  854. }
  855. else if (event->iEvt == USER_EVT_SIP_STATE_IDLE)
  856. {
  857. StopChannel();
  858. }
  859. else if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
  860. {
  861. HangupCall();
  862. StopVideo();
  863. }
  864. else if (event->iEvt == USER_EVT_AGENT_WRITABLE)
  865. {
  866. AllowAgentWrite();
  867. }
  868. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  869. {
  870. ReConnectionAssistchan();
  871. }
  872. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  873. {
  874. ReConnectionSipphone();
  875. }
  876. return 0;
  877. }
  878. void ACMCallFSM::s4_on_entry() {}
  879. void ACMCallFSM::s4_on_exit() {}
  880. unsigned int ACMCallFSM::s4_on_event(FSMEvent* event)
  881. {
  882. if (event->iEvt == USER_EVT_TO_HANDFREE)
  883. {
  884. //m_bHandFree = TRUE;
  885. }
  886. else if (event->iEvt == USER_EVT_TO_PICKUP)
  887. {
  888. //m_bHandFree = FALSE;
  889. }
  890. else if (event->iEvt == USER_EVT_SIP_STATE_IDLE) {
  891. StopChannel();
  892. } else if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
  893. HangupCall();
  894. StopVideo();
  895. } else if (event->iEvt == USER_EVT_AGENT_WRITABLE) {
  896. AllowAgentWrite();
  897. }
  898. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  899. {
  900. ReConnectionAssistchan();
  901. }
  902. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  903. {
  904. ReConnectionSipphone();
  905. }
  906. return 0;
  907. }
  908. void ACMCallFSM::s50_on_entry() {}
  909. void ACMCallFSM::s50_on_exit() {}
  910. unsigned int ACMCallFSM::s50_on_event(FSMEvent* event)
  911. {
  912. if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
  913. StopVideo();
  914. }
  915. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  916. {
  917. ReConnectionAssistchan();
  918. }
  919. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  920. {
  921. ReConnectionSipphone();
  922. }
  923. return 0;
  924. }
  925. void ACMCallFSM::s51_on_entry() {}
  926. void ACMCallFSM::s51_on_exit() {}
  927. unsigned int ACMCallFSM::s51_on_event(FSMEvent* event)
  928. {
  929. if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
  930. StopVideo();
  931. }
  932. return 0;
  933. }
  934. void ACMCallFSM::s52_on_entry()
  935. {
  936. ScheduleTimer(52,RELEASEING_SIP_TIMER);
  937. }
  938. void ACMCallFSM::s52_on_exit()
  939. {
  940. CancelTimer(52);
  941. CancelTimer(520);
  942. }
  943. unsigned int ACMCallFSM::s52_on_event(FSMEvent* event)
  944. {
  945. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  946. {
  947. ReConnectionAssistchan();
  948. }
  949. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  950. {
  951. ReConnectionSipphone();
  952. }
  953. else if (event->iEvt == EVT_TIMER)
  954. {
  955. if (event->param1 == 52)
  956. {
  957. ScheduleTimer(520,RELEASEING_SIP_TIMEOUT);
  958. Dbg("sip finished timeout,release call");
  959. ReleaseCall(0);
  960. }
  961. else if (event->param1 == 520)
  962. {
  963. Dbg("release call timeout,restart sipphone");
  964. PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
  965. //重启mod_sipphone
  966. //RealSipErrorCheck();
  967. LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
  968. }
  969. }
  970. return 0;
  971. }
  972. void ACMCallFSM::s53_on_entry()
  973. {
  974. ScheduleTimer(53, RELEASEING_TIMER_INTERVAL);
  975. }
  976. void ACMCallFSM::s53_on_exit()
  977. {
  978. CancelTimer(53);
  979. }
  980. unsigned int ACMCallFSM::s53_on_event(FSMEvent* event)
  981. {
  982. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  983. {
  984. ReConnectionAssistchan();
  985. }
  986. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  987. {
  988. ReConnectionSipphone();
  989. }
  990. else if (event->iEvt == EVT_TIMER)
  991. {
  992. if (m_nSipErrorNum >= SIP_CONNECT_FAIL_TIMES)
  993. {
  994. m_nSipErrorNum = 0;
  995. //RealSipErrorCheck();
  996. LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
  997. }
  998. }
  999. return 0;
  1000. }
  1001. void ACMCallFSM::s60_on_entry()
  1002. {
  1003. StopChannel();
  1004. HangupCall();
  1005. }
  1006. void ACMCallFSM::s60_on_exit() {}
  1007. unsigned int ACMCallFSM::s60_on_event(FSMEvent* event)
  1008. {
  1009. if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
  1010. {
  1011. StopVideo();
  1012. }
  1013. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  1014. {
  1015. ReConnectionAssistchan();
  1016. }
  1017. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  1018. {
  1019. ReConnectionSipphone();
  1020. }
  1021. return 0;
  1022. }
  1023. void ACMCallFSM::s61_on_entry() {}
  1024. void ACMCallFSM::s61_on_exit() {}
  1025. unsigned int ACMCallFSM::s61_on_event(FSMEvent* event)
  1026. {
  1027. if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
  1028. StopVideo();
  1029. }
  1030. else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  1031. {
  1032. ReConnectionAssistchan();
  1033. }
  1034. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  1035. {
  1036. ReConnectionSipphone();
  1037. }
  1038. return 0;
  1039. }
  1040. void ACMCallFSM::s62_on_entry()
  1041. {
  1042. ScheduleTimer(62,RELEASEING_SIP_TIMER);
  1043. }
  1044. void ACMCallFSM::s62_on_exit()
  1045. {
  1046. CancelTimer(62);
  1047. CancelTimer(620);
  1048. }
  1049. unsigned int ACMCallFSM::s62_on_event(FSMEvent* event)
  1050. {
  1051. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  1052. {
  1053. ReConnectionAssistchan();
  1054. }
  1055. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  1056. {
  1057. ReConnectionSipphone();
  1058. }
  1059. else if (event->iEvt == EVT_TIMER)
  1060. {
  1061. if (event->param1 == 62)
  1062. {
  1063. ScheduleTimer(620,RELEASEING_SIP_TIMEOUT);
  1064. Dbg("sip hangup timeout,release call");
  1065. ReleaseCall(0);
  1066. }
  1067. else if (event->param1 == 620)
  1068. {
  1069. Dbg("release call timeout,restart sipphone");
  1070. PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
  1071. //重启mod_sipphone
  1072. //RealSipErrorCheck();
  1073. LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
  1074. }
  1075. }
  1076. return 0;
  1077. }
  1078. void ACMCallFSM::s63_on_entry()
  1079. {
  1080. ScheduleTimer(63, RELEASEING_TIMER_INTERVAL);
  1081. }
  1082. void ACMCallFSM::s63_on_exit()
  1083. {
  1084. CancelTimer(63);
  1085. }
  1086. unsigned int ACMCallFSM::s63_on_event(FSMEvent* event)
  1087. {
  1088. if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
  1089. {
  1090. ReConnectionAssistchan();
  1091. }
  1092. else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
  1093. {
  1094. ReConnectionSipphone();
  1095. }
  1096. else if (event->iEvt == EVT_TIMER)
  1097. {
  1098. if (m_nSipErrorNum >= SIP_CONNECT_FAIL_TIMES)
  1099. {
  1100. m_nSipErrorNum = 0;
  1101. //RealSipErrorCheck();
  1102. LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
  1103. }
  1104. }
  1105. return 0;
  1106. }
  1107. int ACMCallFSM::TranslateState( int innerState )
  1108. {
  1109. switch (innerState) {
  1110. case s0:
  1111. return eState_Offline;
  1112. case s8:
  1113. case s10:
  1114. case s11:
  1115. case s12:
  1116. case s13:
  1117. return eState_Connecting;
  1118. case s20:
  1119. case s21:
  1120. case s22:
  1121. case s23:
  1122. case s24:
  1123. return eState_Fail;
  1124. case s3:
  1125. return eState_HandFree;
  1126. case s4:
  1127. return eState_Pickup;
  1128. case s50:
  1129. case s51:
  1130. case s52:
  1131. case s53:
  1132. return eState_Broken;
  1133. case s7:
  1134. case s14:
  1135. case s60:
  1136. case s61:
  1137. case s62:
  1138. case s63:
  1139. return eState_Releasing;
  1140. case s9:
  1141. return eState_LiveDetect;
  1142. default:
  1143. assert(0);
  1144. return 0;
  1145. }
  1146. }