HolderContextFSM.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. #include "stdafx.h"
  2. #include "windows.h"
  3. #include "HolderContextFSM.h"
  4. #include "..\EventCode.h"
  5. #define OP_START_ENTITY 1
  6. #define OP_STOP_ENTITY 0
  7. struct callback_entry : public IReleasable
  8. {
  9. virtual ~callback_entry() {}
  10. CSimpleStringA EntityName;
  11. union {
  12. void *pRawData;
  13. int state;
  14. };
  15. int op;
  16. ErrorCodeEnum ErrorResult;
  17. };
  18. namespace StateTask{
  19. //add code by @wb
  20. //定时上报持有者状态
  21. struct ReportStateTask : public ITaskSp
  22. {
  23. HolderContextFSM* fsm;
  24. explicit ReportStateTask(HolderContextFSM* f) : fsm(f) {}
  25. void Process()
  26. {
  27. Dbg("@wb TimerUploadHolderState init");
  28. while(1)
  29. {
  30. Dbg("@wb m_bLogon%d",fsm->m_bLogon);
  31. //持有者从签入到签出时间之内,定期上报一次状态
  32. if (fsm->m_bLogon)
  33. {
  34. Dbg("@wb TimerUploadHolderState m_bLogon");
  35. if (!fsm->UploadHolderState())
  36. {
  37. Dbg("TimerUploadHolderState failed!");
  38. //将业务层状态置为N,避免分行断开服务时,无效呼入
  39. /*
  40. Sleep(FIX_SETSTATE_TIMER);
  41. EnterCriticalSection(&fsm->cs);
  42. Dbg("Set HolderState to N state!");
  43. fsm->SetHolderState("N");
  44. LeaveCriticalSection(&fsm->cs);
  45. Sleep(FIX_SETSTATE_TIMER);
  46. */
  47. Sleep(FIX_SETSTATE_TIMER_1);
  48. EnterCriticalSection(&fsm->cs);
  49. Dbg("Set HolderState to N State!");
  50. fsm->SetHolderState("N");
  51. LeaveCriticalSection(&fsm->cs);
  52. Sleep(FIX_SETSTATE_TIMER_1);
  53. }
  54. }
  55. Dbg("@wb TimerUploadHolderState Sleep");
  56. //定时时间
  57. Sleep(FIX_UPLOAD_TIMER);
  58. }
  59. }
  60. };
  61. struct HandlePackageTask : public ITaskSp
  62. {
  63. HolderContextFSM* fsm;
  64. explicit HandlePackageTask(HolderContextFSM* f) : fsm(f) {}
  65. //依据reqStateVec中未确认包个数和时戳来实现重传与确认机制
  66. void Process()
  67. {
  68. Dbg("@wb enter before Thread for retransmit");
  69. while(1)
  70. {
  71. if (fsm->m_bLogon)
  72. {
  73. // 记录当前时间
  74. long indexNow;
  75. //记录标识
  76. int countID = 1;
  77. // 统计个数判断是否需要断开连接后重连
  78. EnterCriticalSection(&fsm->cs);
  79. Dbg("@wb indentified values for retransmit");
  80. //针对分行
  81. int count = fsm->reqStateVec.size();
  82. Dbg("count %d", count);
  83. //针对总行
  84. int countN = fsm->hdStateReqVec.size();
  85. Dbg("countN %d", countN);
  86. //针对recvState
  87. int countRecSta = fsm->recvState.size();
  88. //超出 最大个数进行清空list,关闭与分行连接,重新连接的操作
  89. if(count > PACKAGE_MAX_NUM)
  90. {
  91. Dbg("@wb Disconnect for retransmit");
  92. fsm->Disconnect();
  93. //执行清空操作
  94. fsm->reqStateVec.clear();
  95. //将业务层状态置为N,避免分行断开服务时,无效呼入
  96. Dbg("Set HolderState to N state!");
  97. EnterCriticalSection(&fsm->cs);
  98. fsm->SetHolderState("N");
  99. LeaveCriticalSection(&fsm->cs);
  100. Dbg("@wb Reconnect for retransmit");
  101. fsm->Reconnect();
  102. LeaveCriticalSection(&fsm->cs);
  103. }
  104. else
  105. {
  106. //遍历进行超时重传
  107. int m_size = fsm->reqStateVec.size();
  108. Dbg("m_size %d", m_size);
  109. for (int i = 0; i < m_size; i ++)
  110. {
  111. Dbg("@wb GetTickCount for retransmit");
  112. indexNow = GetTickCount(); //获取当前时间
  113. Dbg("indexNow %d", indexNow);
  114. long timeRecord = 0; //存储状态上报包中的时间
  115. Dbg("fsm->reqStateVec[i].dwNow %s",fsm->reqStateVec[i].dwNow);
  116. sscanf(fsm->reqStateVec[i].dwNow, "%d", &timeRecord );
  117. Dbg("timeRecord %d", timeRecord);
  118. //出现超时进行重传,生成新的SerialID和当前时间
  119. if((indexNow - timeRecord) > FIX_DWTICK_TIMER)
  120. {
  121. countID ++;
  122. Dbg("countID%d", countID);
  123. sprintf(fsm->reqStateVec[i].SerialID, "%d", countID);
  124. char time[16] = {0};
  125. ltoa(indexNow, time, 10);
  126. memcpy(fsm->reqStateVec[i].dwNow, time, strlen(time));
  127. Dbg("@wb retransmit req success!");
  128. if(!fsm->ReTransmissReq(fsm->reqStateVec[i]))
  129. {
  130. Dbg("retransmit req not success!");
  131. }
  132. else
  133. Dbg("retransmit req success!");
  134. }
  135. }
  136. }
  137. if(countN >= PACKAGE_MAX_NUM)
  138. {
  139. //将业务层状态置为N,避免总行断开服务时,无效呼入
  140. Dbg("Set HolderState to N state!");
  141. EnterCriticalSection(&fsm->cs);
  142. fsm->SetHolderState("N");
  143. LeaveCriticalSection(&fsm->cs);
  144. Dbg("Clear all the hdStateReqVec element!");
  145. fsm->hdStateReqVec.clear();
  146. }
  147. if(countRecSta >= RECV_MAX_NUM)
  148. {
  149. fsm->recvState.clear();
  150. }
  151. LeaveCriticalSection(&fsm->cs);
  152. }
  153. Dbg("@wb Sleep retransmit!");
  154. Sleep(FIX_CHECK_LIST_TIMER);
  155. }
  156. }
  157. };
  158. }
  159. using namespace StateTask;
  160. class HolderContextFSM;
  161. HolderContextFSM::HolderContextFSM():m_pConnection(NULL)
  162. {
  163. m_HolderState.State = "N";//默认持有者状态,N
  164. m_pConnectService = NULL;
  165. m_bLogon = FALSE;
  166. m_nErrcode = Error_Succeed;
  167. m_strErrmsg = "";
  168. m_bHolderBusy = FALSE;
  169. m_bMenu = TRUE;
  170. m_CurCallState = "O";
  171. m_bIncomeHangup = FALSE;
  172. memset(m_cTaskData, 0, 1024);
  173. memset(m_cHolderSkill, 0, 128);
  174. }
  175. HolderContextFSM::~HolderContextFSM()
  176. {
  177. }
  178. void HolderContextFSM::OnStateTrans( int iSrcState, int iDstState )
  179. {
  180. Dbg("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  181. }
  182. void HolderContextFSM::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  183. {
  184. }
  185. ErrorCodeEnum HolderContextFSM::OnInit()
  186. {
  187. ErrorCodeEnum Error;
  188. /*Error = LoadServerConfig();
  189. if (Error != Error_Succeed)
  190. {
  191. LOG_TRACE("load HolderContext config failed!");
  192. return Error;
  193. }*/
  194. Error = LoadRootConfig();
  195. if (Error != Error_Succeed)
  196. {
  197. LOG_TRACE("load root.ini config failed!");
  198. return Error;
  199. }
  200. Dbg("LoadRootConfig ok, m_strMachineType[%s],m_strSite[%s]",m_strMachineType.GetData(),m_strSite.GetData());
  201. if (Error == Error_Succeed)
  202. {
  203. AddStateHooker(this);
  204. }
  205. //add广播界面退出通知消息eMsg
  206. Connecting e;
  207. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_exitRecordUI , eMsgSig_exitRecordUI , e);
  208. Dbg("SpSendBroadcast eMsgexitRecordUI");
  209. InitializeCriticalSection(&cs);
  210. //获取终端号
  211. CSystemStaticInfo si;
  212. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  213. m_strTerminalNo = si.strTerminalID;
  214. Dbg("Get TerminalId[%s] from root.ini",m_strTerminalNo);
  215. m_HolderState.TerminalNo = m_strTerminalNo;
  216. //初始化分行、总行状态发送记录vector
  217. reqStateVec.reserve(1000);
  218. hdStateReqVec.reserve(1000);
  219. StateAnsNum.reserve(10);
  220. recvState.reserve(1000);
  221. recvState.push_back('S');
  222. //add code by @wb
  223. //创建定时上报状态线程
  224. ReportStateTask* pReportTask = new ReportStateTask(this);
  225. GetEntityBase()->GetFunction()->PostThreadPoolTask(pReportTask);
  226. Dbg("task1 created");
  227. //add code by @wb
  228. //创建处理状态上报确认线程
  229. HandlePackageTask* pHandleTask = new HandlePackageTask(this);
  230. GetEntityBase()->GetFunction()->PostThreadPoolTask(pHandleTask);
  231. Dbg("task2 created");
  232. //......
  233. return Error;
  234. }
  235. ErrorCodeEnum HolderContextFSM::OnExit()
  236. {
  237. DeleteCriticalSection(&cs);
  238. return Error_Succeed;
  239. }
  240. /*
  241. 状态上报规则
  242. 1.None、Identified、NotHeld为客户不在现场,用户不在现场的状态无需更新用户状态
  243. 2.
  244. */
  245. void HolderContextFSM::s0_on_entry()
  246. {
  247. Dbg("enter s0[None]!");
  248. m_HolderState.State = "N";
  249. //Sleep(15000);
  250. //非Pad,不需要上报状态
  251. if (0 != strcmp(m_strMachineType.GetData(), MACHINETYPE_PAD))
  252. {
  253. Dbg("非Pad,无需状态上报!");
  254. return;
  255. }
  256. ////开始获取用户状态
  257. //BOOL bRet = GetHolderState();
  258. //if (!bRet)
  259. //{
  260. //}
  261. //Dbg("when s0_on_entry, start get hodler state!");
  262. //Test
  263. //if (1)
  264. //{
  265. // m_HolderState.HolderID = "074168";
  266. // //m_HolderState.State = "S";
  267. // //UploadHolderState();
  268. // //Test,进入Standby状态
  269. // //PostEventLIFO(new FSMEvent(USER_EVT_JMP_STANDBY));
  270. //}
  271. //Test
  272. /*m_HolderState.HolderID = "00210706";
  273. QueryHolderSkill();*/
  274. ScheduleTimer(1, 1000);
  275. }
  276. void HolderContextFSM::s0_on_exit()
  277. {
  278. CancelTimer(1);
  279. }
  280. unsigned int HolderContextFSM::s0_on_event( FSMEvent* event )
  281. {
  282. Connecting e;
  283. switch (event->iEvt)
  284. {
  285. case EVT_TIMER:
  286. ScheduleTimer(1, 1000); //没有收到状态循环等待
  287. break;
  288. case USER_EVT_JMP_IDENTIFIED:
  289. //进入S1:Identified状态
  290. Dbg("s0_on_event, recieve USER_EVT_JMP_IDENTIFIED, go to S1!");
  291. break;
  292. case USER_EVT_JMP_STANDBY:
  293. //进入S3:Standby状态
  294. Dbg("s0_on_event, recieve USER_EVT_JMP_STANDBY, go to S3!");
  295. //add广播 eMsg_OnlineStandBy为S的消息
  296. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_OnlineStandBy, eMsgSig_OnlineStandBy, e);
  297. Dbg("SpSendBroadcast eMsg_OnlineStandBy");
  298. break;
  299. case USER_EVT_JMP_BUSY:
  300. //进入S5:Busy状态
  301. Dbg("s0_on_event, recieve USER_EVT_JMP_BUSY, go to S5!");
  302. //add广播Busy消息
  303. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_OnlineBusy, eMsgSig_OnlineBusy, e);
  304. Dbg("SpSendBroadcast eMsg_OnlineBusy");
  305. break;
  306. default:
  307. Dbg("s0_on_event, recieve no defined event,[%d], continue wait!",event->iEvt);
  308. ScheduleTimer(1, 1000);
  309. break;
  310. }
  311. return 0;
  312. }
  313. //open console
  314. void HolderContextFSM::s1_on_entry()
  315. {
  316. //Identified,暂时不需要此状态
  317. m_HolderState.State = "I";
  318. Dbg("enter s1[Identified]");
  319. ScheduleTimer(2, 1000);
  320. }
  321. void HolderContextFSM::s1_on_exit()
  322. {
  323. CancelTimer(2);
  324. }
  325. unsigned int HolderContextFSM::s1_on_event(FSMEvent* event)
  326. {
  327. if (event->iEvt == EVT_TIMER)
  328. {
  329. Dbg("s1_on_event, recieve timer, goto s0");
  330. }
  331. return 0;
  332. }
  333. void HolderContextFSM::s2_on_entry()
  334. {
  335. //Unservice状态,暂时不需要此状态
  336. Dbg("enter s2[Unservice]");
  337. m_HolderState.State = "U";
  338. if (!UploadHolderState())
  339. {
  340. Dbg("UploadHolderState s2 failed!");
  341. }
  342. ScheduleTimer(3, 1000);
  343. }
  344. void HolderContextFSM::s2_on_exit()
  345. {
  346. CancelTimer(3);
  347. }
  348. unsigned int HolderContextFSM::s2_on_event(FSMEvent* event)
  349. {
  350. if (event->iEvt == EVT_TIMER)
  351. {
  352. Dbg("s2_on_event, recieve timer, goto s0");
  353. }
  354. return 0;
  355. }
  356. void HolderContextFSM::s3_on_entry()
  357. {
  358. Dbg("enter s3[Standby]");
  359. //每次登录都重新下载任务, TODO:下载后怎么发起通话请求?
  360. //连线任务无需主动下载,其它任务待定
  361. /*BOOL bRet = GetConnectTaskFromInstrationServer();
  362. if (!bRet)
  363. {
  364. Dbg("从分行获取任务失败");
  365. }*/
  366. m_HolderState.State = "S";
  367. if (!UploadHolderState())
  368. {
  369. Dbg("UploadHolderState s3 failed!");
  370. }
  371. }
  372. void HolderContextFSM::s3_on_exit()
  373. {
  374. CancelTimer(4);
  375. }
  376. unsigned int HolderContextFSM::s3_on_event(FSMEvent* event)
  377. {
  378. Connecting e;
  379. switch (event->iEvt)
  380. {
  381. case EVT_TIMER:
  382. //TODO:什么场景触发?又业务界面通知?
  383. Dbg("s3_on_event, recieve timer, go to S0!");
  384. break;
  385. case USER_EVT_JMP_NOTHELD:
  386. //进入S4:NotHeld状态
  387. Dbg("s3_on_event, recieve USER_EVT_JMP_IDENTIFIED, go to S4!");
  388. break;
  389. case USER_EVT_JMP_BUSY:
  390. //进入S5:Busy状态
  391. Dbg("s3_on_event, recieve USER_EVT_JMP_BUSY, go to S5!");
  392. //add广播Busy消息
  393. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_OnlineBusy, eMsgSig_OnlineBusy, e);
  394. Dbg("SpSendBroadcast eMsg_OnlineBusy");
  395. break;
  396. case USER_EVT_JMP_LOCAL:
  397. //进入S6:Local状态
  398. Dbg("s3_on_event, recieve USER_EVT_JMP_LOCAL, go to S6!");
  399. break;
  400. case USER_EVT_JMP_RINGING:
  401. //进入S8:Ringing状态
  402. Dbg("s3_on_event, recieve USER_EVT_JMP_RINGING, go to S8!");
  403. break;
  404. case USER_EVT_JMP_DISCONNECT:
  405. //进入S12:Disconnect状态
  406. Dbg("s3_on_event, recieve USER_EVT_JMP_DISCONNECT, go to S12!");
  407. break;
  408. case USER_EVT_JMP_NONE:
  409. //进入0:None状态
  410. Dbg("s3_on_event, recieve USER_EVT_JMP_NONE, go to S0!");
  411. //add广播 eMsg_Offline消息
  412. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_Offline, eMsgSig_Offline, e);
  413. Dbg("SpSendBroadcast eMsg_Offline");
  414. break;
  415. default:
  416. Dbg("s3_on_event, recieve no defined event,[%d], continue wait!",event->iEvt);
  417. //ScheduleTimer(1, 1000);
  418. break;
  419. }
  420. return 0;
  421. }
  422. void HolderContextFSM::s4_on_entry()
  423. {
  424. Dbg("enter s4[NotHeld]");
  425. //m_HolderState.State = "B";//对应什么状态值?
  426. if (!UploadHolderState())
  427. {
  428. Dbg("UploadHolderState s4 failed!");
  429. }
  430. }
  431. void HolderContextFSM::s4_on_exit()
  432. {
  433. CancelTimer(5);
  434. }
  435. unsigned int HolderContextFSM::s4_on_event(FSMEvent* event)
  436. {
  437. if (event->iEvt == EVT_TIMER)
  438. {
  439. Dbg("Verify cert err");
  440. }
  441. return 0;
  442. }
  443. void HolderContextFSM::s5_on_entry()
  444. {
  445. Dbg("enter s5[Busy]");
  446. m_HolderState.State = "B";
  447. if (!UploadHolderState())
  448. {
  449. Dbg("UploadHolderState s5 failed!");
  450. }
  451. }
  452. void HolderContextFSM::s5_on_exit()
  453. {
  454. }
  455. unsigned int HolderContextFSM::s5_on_event(FSMEvent* event)
  456. {
  457. Connecting e;
  458. switch (event->iEvt)
  459. {
  460. case EVT_TIMER:
  461. //TODO:什么场景触发?又业务界面通知?
  462. Dbg("s5_on_event, recieve timer, go to S0!");
  463. break;
  464. //add by zl 2017.12.08
  465. case USER_EVT_JMP_NONE:
  466. Dbg("s5_on_event, recieve USER_EVT_JMP_NONE, go to S0!");
  467. //add广播 eMsg_Offline消息
  468. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_Offline, eMsgSig_Offline, e);
  469. Dbg("SpSendBroadcast eMsg_Offline");
  470. break;
  471. case USER_EVT_JMP_STANDBY:
  472. //进入S3:Standby状态
  473. Dbg("s5_on_event, recieve USER_EVT_JMP_STANDBY, go to S3!");
  474. //add广播 eMsg_OnlineStandBy为S的消息
  475. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_OnlineStandBy, eMsgSig_OnlineStandBy, e);
  476. Dbg("SpSendBroadcast eMsg_OnlineStandBy");
  477. break;
  478. default:
  479. Dbg("s5_on_event, recieve no defined event,[%d], continue wait!",event->iEvt);
  480. //ScheduleTimer(1, 1000);
  481. break;
  482. }
  483. return 0;
  484. }
  485. void HolderContextFSM::s6_on_entry()
  486. {
  487. Dbg("enter s6[Local]");
  488. m_HolderState.State = "L";
  489. if (!UploadHolderState())
  490. {
  491. Dbg("UploadHolderState s6 failed!");
  492. }
  493. }
  494. void HolderContextFSM::s6_on_exit()
  495. {
  496. //CancelTimer(1);
  497. }
  498. unsigned int HolderContextFSM::s6_on_event(FSMEvent* event)
  499. {
  500. Dbg("s6_on_event");
  501. return 0;
  502. }
  503. void HolderContextFSM::s7_on_entry()
  504. {
  505. Dbg("enter s7[Working]");
  506. m_HolderState.State = "W";
  507. if (!UploadHolderState())
  508. {
  509. Dbg("UploadHolderState s7 failed!");
  510. }
  511. }
  512. void HolderContextFSM::s7_on_exit()
  513. {
  514. //CancelTimer(1);
  515. }
  516. unsigned int HolderContextFSM::s7_on_event(FSMEvent* event)
  517. {
  518. Dbg("s7_on_event");
  519. return 0;
  520. }
  521. void HolderContextFSM::s8_on_entry()
  522. {
  523. Dbg("enter s8[Ringing]");
  524. m_HolderState.State = "R";
  525. if (!UploadHolderState())
  526. {
  527. Dbg("UploadHolderState s8 failed!");
  528. }
  529. //给业务层广播振铃消息
  530. Connecting e;
  531. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_Ringing, eMsgSig_Ringing, e);
  532. Dbg("SpSendBroadcast eMsg_Ringing");
  533. }
  534. void HolderContextFSM::s8_on_exit()
  535. {
  536. //CancelTimer(1);
  537. }
  538. unsigned int HolderContextFSM::s8_on_event(FSMEvent* event)
  539. {
  540. switch (event->iEvt)
  541. {
  542. case EVT_TIMER:
  543. //TODO:什么场景触发?又业务界面通知?
  544. Dbg("s8_on_event, recieve timer, go to S0!");
  545. break;
  546. case USER_EVT_JMP_STANDBY:
  547. //进入S3:Standby状态
  548. Dbg("s8_on_event, recieve USER_EVT_JMP_STANDBY, go to S3!");
  549. break;
  550. case USER_EVT_JMP_BUSY:
  551. //进入S5:Busy状态
  552. Dbg("s8_on_event, recieve USER_EVT_JMP_BUSY, go to S5!");
  553. break;
  554. case USER_EVT_JMP_ONLINE:
  555. //进入S9:Online状态
  556. Dbg("s8_on_event, recieve USER_EVT_JMP_ONLINE, go to S9!");
  557. break;
  558. case USER_EVT_JMP_DEALING:
  559. //进入S10:Dealing状态
  560. Dbg("s8_on_event, recieve USER_EVT_JMP_DEALING, go to S10!");
  561. break;
  562. default:
  563. Dbg("s8_on_event, recieve no defined event, continue wait!");
  564. //ScheduleTimer(1, 1000);
  565. break;
  566. }
  567. return 0;
  568. }
  569. void HolderContextFSM::s9_on_entry()
  570. {
  571. Dbg("enter s9[Online]");
  572. //m_HolderState.State = "B";//对应什么状态值?
  573. if (!UploadHolderState())
  574. {
  575. Dbg("UploadHolderState s9 failed!");
  576. }
  577. }
  578. void HolderContextFSM::s9_on_exit()
  579. {
  580. //CancelTimer(1);
  581. }
  582. unsigned int HolderContextFSM::s9_on_event(FSMEvent* event)
  583. {
  584. switch (event->iEvt)
  585. {
  586. case EVT_TIMER:
  587. //TODO:什么场景触发?又业务界面通知?
  588. Dbg("s9_on_event, recieve timer, go to S0!");
  589. break;
  590. case USER_EVT_JMP_DEALING:
  591. //进入S10:Dealing状态
  592. Dbg("s9_on_event, recieve USER_EVT_JMP_DEALING, go to S10!");
  593. break;
  594. case USER_EVT_JMP_EDIT:
  595. //进入S11:Edit状态
  596. Dbg("s9_on_event, recieve USER_EVT_JMP_EDIT, go to S11!");
  597. break;
  598. default:
  599. Dbg("s9_on_event, recieve no defined event, continue wait!");
  600. //ScheduleTimer(1, 1000);
  601. break;
  602. }
  603. return 0;
  604. }
  605. void HolderContextFSM::s10_on_entry()
  606. {
  607. Dbg("enter s10[Dealing]");
  608. m_HolderState.State = "D";
  609. if (!UploadHolderState())
  610. {
  611. Dbg("UploadHolderState s10 failed!");
  612. }
  613. }
  614. void HolderContextFSM::s10_on_exit()
  615. {
  616. //CancelTimer(1);
  617. }
  618. unsigned int HolderContextFSM::s10_on_event(FSMEvent* event)
  619. {
  620. switch (event->iEvt)
  621. {
  622. case EVT_TIMER:
  623. //TODO:什么场景触发?又业务界面通知?
  624. Dbg("s10_on_event, recieve timer, go to S0!");
  625. break;
  626. case USER_EVT_JMP_EDIT:
  627. //进入S11:Edit状态
  628. Dbg("s10_on_event, recieve USER_EVT_JMP_EDIT, go to S11!");
  629. break;
  630. default:
  631. Dbg("s10_on_event, recieve no defined event, continue wait!");
  632. //ScheduleTimer(1, 1000);
  633. break;
  634. }
  635. return 0;
  636. }
  637. void HolderContextFSM::s11_on_entry()
  638. {
  639. Dbg("enter s11[Edit]");
  640. m_HolderState.State = "E";
  641. if (!UploadHolderState())
  642. {
  643. Dbg("UploadHolderState s11 failed!");
  644. }
  645. }
  646. void HolderContextFSM::s11_on_exit()
  647. {
  648. //CancelTimer(1);
  649. }
  650. unsigned int HolderContextFSM::s11_on_event(FSMEvent* event)
  651. {
  652. switch (event->iEvt)
  653. {
  654. case EVT_TIMER:
  655. //TODO:什么场景触发?又业务界面通知?
  656. Dbg("s11_on_event, recieve timer, go to S0!");
  657. break;
  658. case USER_EVT_JMP_EDITEND:
  659. //进入S11:Edit状态
  660. Dbg("s11_on_event, recieve USER_EVT_JMP_EDITEND, go to S11!");
  661. break;
  662. default:
  663. Dbg("s11_on_event, recieve no defined event, continue wait!");
  664. //ScheduleTimer(1, 1000);
  665. break;
  666. }
  667. return 0;
  668. }
  669. void HolderContextFSM::s12_on_entry()
  670. {
  671. Dbg("enter s12[Disconnect]");
  672. m_HolderState.State = "D";
  673. if (!UploadHolderState())
  674. {
  675. Dbg("UploadHolderState s12 failed!");
  676. }
  677. }
  678. void HolderContextFSM::s12_on_exit()
  679. {
  680. //CancelTimer(1);
  681. }
  682. unsigned int HolderContextFSM::s12_on_event(FSMEvent* event)
  683. {
  684. switch (event->iEvt)
  685. {
  686. case EVT_TIMER:
  687. //TODO:什么场景触发?又业务界面通知?
  688. Dbg("s12_on_event, recieve timer, go to S0!");
  689. break;
  690. case USER_EVT_JMP_EDITEND:
  691. //进入S3:Standby状态
  692. Dbg("s12_on_event, recieve USER_EVT_JMP_STANDBY, go to S11!");
  693. break;
  694. default:
  695. Dbg("s12_on_event, recieve no defined event, continue wait!");
  696. //ScheduleTimer(1, 1000);
  697. break;
  698. }
  699. return 0;
  700. }
  701. ErrorCodeEnum HolderContextFSM::LoadServerConfig()
  702. {
  703. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  704. CSmartPointer<IConfigInfo> spConfig;
  705. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Software, spConfig);
  706. if (Error == Error_Succeed)
  707. {
  708. Error = spConfig->ReadConfigValue("VerifyKey", "Key", m_StringVerifyKey);
  709. if(Error == Error_Succeed)
  710. Error = spConfig->ReadConfigValue("GuiConsole", "Name", m_StringConsoleName);
  711. }
  712. return Error;
  713. }
  714. ErrorCodeEnum HolderContextFSM::LoadCenterConfig()
  715. {
  716. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  717. CSmartPointer<IConfigInfo> spConfig;
  718. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  719. if (Error_Succeed == Error)
  720. {
  721. /*Error = spConfig->ReadConfigValueInt("MaintainWatcher", "CheckTerMinalNo", m_nCheckTerminalNo);
  722. if (Error_Succeed == Error)
  723. {
  724. Dbg("get CheckTerMinalNo=%d from CenterSetting.ini", m_nCheckTerminalNo);
  725. }
  726. else
  727. {
  728. Dbg("get CheckTerMinalNo from CenterSetting.ini failed");
  729. }
  730. Error = spConfig->ReadConfigValueInt("MaintainWatcher", "CheckMaintainTask", m_nCheckMaintainTask);
  731. if (Error_Succeed == Error)
  732. {
  733. Dbg("get CheckMaintainTask=%d from CenterSetting.ini", m_nCheckMaintainTask);
  734. }
  735. else
  736. {
  737. Dbg("get CheckMaintainTask from CenterSetting.ini failed");
  738. }
  739. Error = spConfig->ReadConfigValueInt("MaintainWatcher", "Verify_BlackList", m_nVerifyBlackList);
  740. if (Error_Succeed == Error)
  741. {
  742. Dbg("get Verify_BlackList=%d from CenterSetting.ini", m_nVerifyBlackList);
  743. }
  744. else
  745. {
  746. Dbg("get Verify_BlackList from CenterSetting.ini failed");
  747. }*/
  748. }
  749. return Error;
  750. }
  751. ErrorCodeEnum HolderContextFSM::LoadRootConfig()
  752. {
  753. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  754. CSmartPointer<IConfigInfo> spConfig;
  755. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spConfig);
  756. if (Error == Error_Succeed)
  757. {
  758. Error = spConfig->ReadConfigValue("Terminal", "MachineType", m_strMachineType);
  759. if(Error == Error_Succeed)
  760. {
  761. Error = spConfig->ReadConfigValue("Terminal", "Site", m_strSite);
  762. if (Error != Error_Succeed)
  763. {
  764. Dbg("ReadConfigValue, get Site value failed");
  765. }
  766. }
  767. else
  768. {
  769. Dbg("ReadConfigValue, get MachineType value failed");
  770. }
  771. }
  772. return Error;
  773. }
  774. //add code by @wb
  775. //上报持有者状态
  776. BOOL HolderContextFSM::UploadHolderState()
  777. {
  778. EnterCriticalSection(&cs);
  779. if (!m_pConnection)
  780. {
  781. m_pConnection = new instructionsServerConnection(m_pEntity, this);
  782. if (m_pConnection)
  783. {
  784. if (!m_pConnection->ConnectFromCentralSetting())
  785. {
  786. Dbg("connect instructions server fail,config or Servers Error!");
  787. m_pConnection->handleDisconnect();
  788. m_pConnection = NULL;
  789. LeaveCriticalSection(&cs);
  790. return FALSE;
  791. }
  792. }
  793. else
  794. {
  795. Dbg("UploadHolderState new instructionsServerConnection failed!");
  796. LeaveCriticalSection(&cs);
  797. return FALSE;
  798. }
  799. }
  800. if (m_pConnection->IsConnectionOK())
  801. {
  802. m_pConnection->SendHolderStateReq();
  803. }
  804. else
  805. {
  806. //连接失败,关闭链接重连
  807. if (NULL != m_pConnection)
  808. {
  809. m_pConnection->handleDisconnect();
  810. m_pConnection = NULL;
  811. Dbg("Close m_pConnection");
  812. }
  813. Dbg("connect to instructions server failed, can not update holder state!");
  814. LeaveCriticalSection(&cs);
  815. return FALSE;
  816. }
  817. LeaveCriticalSection(&cs);
  818. return TRUE;
  819. }
  820. //add code by @wb
  821. //重传持有者状态确认包
  822. BOOL HolderContextFSM::ReTransmissReq(HolderStateReqEx req)
  823. {
  824. EnterCriticalSection(&cs);
  825. if (!m_pConnection)
  826. {
  827. m_pConnection = new instructionsServerConnection(m_pEntity, this);
  828. if (m_pConnection)
  829. {
  830. if (!m_pConnection->ConnectFromCentralSetting())
  831. {
  832. Dbg("connect instructions server fail,config or Servers Error!");
  833. m_pConnection->handleDisconnect();
  834. m_pConnection = NULL;
  835. LeaveCriticalSection(&cs);
  836. return FALSE;
  837. }
  838. }
  839. else
  840. {
  841. Dbg("UploadHolderState new instructionsServerConnection failed!");
  842. LeaveCriticalSection(&cs);
  843. return FALSE;
  844. }
  845. }
  846. if (m_pConnection->IsConnectionOK())
  847. {
  848. m_pConnection->reSendHdStateReq(req);
  849. }
  850. else
  851. {
  852. //连接失败,关闭链接重连
  853. if (NULL != m_pConnection)
  854. {
  855. m_pConnection->handleDisconnect();
  856. m_pConnection = NULL;
  857. Dbg("Close m_pConnection");
  858. }
  859. Dbg("connect to instructions server failed, can not reSend holder state!");
  860. LeaveCriticalSection(&cs);
  861. return FALSE;
  862. }
  863. LeaveCriticalSection(&cs);
  864. return TRUE;
  865. }
  866. //add code by @wb
  867. //断开与分行服务连接
  868. void HolderContextFSM::Disconnect()
  869. {
  870. EnterCriticalSection(&cs);
  871. if (m_pConnection != NULL)
  872. {
  873. m_pConnection->handleDisconnect();
  874. m_pConnection = NULL;
  875. Dbg("Close m_pConnection");
  876. }
  877. LeaveCriticalSection(&cs);
  878. }
  879. //add code by @wb
  880. //重新与分行服务进行连接
  881. void HolderContextFSM::Reconnect()
  882. {
  883. EnterCriticalSection(&cs);
  884. if (!m_pConnection)
  885. {
  886. m_pConnection = new instructionsServerConnection(m_pEntity, this);
  887. if (m_pConnection)
  888. {
  889. if (!m_pConnection->ConnectFromCentralSetting())
  890. {
  891. Dbg("connect instructions server fail,config or Servers Error!");
  892. m_pConnection->handleDisconnect();
  893. m_pConnection = NULL;
  894. LeaveCriticalSection(&cs);
  895. }
  896. else
  897. {
  898. Dbg("connect instructions server success!");
  899. }
  900. }
  901. else
  902. {
  903. Dbg("UploadHolderState new instructionsServerConnection failed!");
  904. LeaveCriticalSection(&cs);
  905. }
  906. }
  907. if(m_pConnection->IsConnectionOK())
  908. Dbg("Reconnect is success!");
  909. else
  910. {
  911. //连接失败,关闭链接重连
  912. if (NULL != m_pConnection)
  913. {
  914. m_pConnection->handleDisconnect();
  915. m_pConnection = NULL;
  916. Dbg("Close m_pConnection");
  917. }
  918. LeaveCriticalSection(&cs);
  919. }
  920. LeaveCriticalSection(&cs);
  921. }
  922. //发送连线请求
  923. BOOL HolderContextFSM::SendConnectTask()
  924. {
  925. //调用陈晃提供的实体接口发起请求
  926. ErrorCodeEnum eErr = Error_Succeed;
  927. ConnectService_StartCallExternal_Ans ans;
  928. ConnectService_StartCallExternal_Req req;
  929. req.CommandParam.Alloc(1024);
  930. memcpy(req.CommandParam.m_pData,m_cTaskData,1024);
  931. if (m_pConnectService == NULL)
  932. {
  933. m_pConnectService = new ConnectService_ClientBase(this->GetEntityBase());
  934. eErr = m_pConnectService->Connect();
  935. if (Error_Succeed != eErr)
  936. {
  937. Dbg("counter connector connected failed.");
  938. m_pConnectService = NULL;
  939. return FALSE;
  940. }
  941. else
  942. {
  943. Dbg("counter connector connected succeed.");
  944. }
  945. }
  946. //TODO:根据错误原因是否来决定是否重试,失败原因要及时通知总行
  947. eErr = m_pConnectService->StartCallExternal(req, ans,10000);
  948. if (Error_Succeed != eErr)
  949. {
  950. Dbg("StartCallExternal failed.");
  951. m_pConnectService = NULL;
  952. return FALSE;
  953. }
  954. else
  955. {
  956. Dbg("StartCallExternal succeed");
  957. }
  958. req.CommandParam.Clear();
  959. return TRUE;
  960. }
  961. BOOL HolderContextFSM::HangupTask()
  962. {
  963. //TODO:调用陈晃提供的实体接口发起请求
  964. ErrorCodeEnum eErr = Error_Succeed;
  965. ConnectService_StopCall_Ans ans;
  966. ConnectService_StopCall_Req req;
  967. req.SessionParam.Alloc(1024);
  968. memcpy(req.SessionParam.m_pData,m_cTaskData,1024);
  969. //TODO:根据错误原因是否来决定是否重试,失败原因要及时通知总行
  970. if (m_pConnectService == NULL)
  971. {
  972. m_pConnectService = new ConnectService_ClientBase(this->GetEntityBase());
  973. eErr = m_pConnectService->Connect();
  974. if (Error_Succeed != eErr) {
  975. Dbg("counter connector connected failed.");
  976. m_pConnectService = NULL;
  977. return FALSE;
  978. }
  979. else{
  980. Dbg("counter connector connected succeed!");
  981. }
  982. }
  983. eErr = m_pConnectService->StopCall(req, ans,10000);
  984. if (Error_Succeed != eErr)
  985. {
  986. Dbg("StopCall failed.");
  987. m_pConnectService = NULL;
  988. return FALSE;
  989. }
  990. else
  991. {
  992. Dbg("StopCall succeed!");
  993. }
  994. req.SessionParam.Clear();
  995. return TRUE;
  996. }
  997. //查询持有人技能
  998. DWORD HolderContextFSM::QueryHolderSkill()
  999. {
  1000. EnterCriticalSection(&cs);
  1001. if (!m_pConnection)
  1002. {
  1003. m_pConnection = new instructionsServerConnection(m_pEntity, this);
  1004. if (!m_pConnection->ConnectFromCentralSetting())
  1005. {
  1006. Dbg("connect instruction server fail,config or Servers Error!");
  1007. m_pConnection->handleDisconnect();
  1008. m_pConnection = NULL;
  1009. LeaveCriticalSection(&cs);
  1010. SetErrMsg(ERR_CONNECTSERVICE_FROM_CENTER,"根据集中配置连接指令服务失败");
  1011. return ERR_CONNECTSERVICE_FROM_CENTER;
  1012. }
  1013. }
  1014. if (m_pConnection->IsConnectionOK())
  1015. {
  1016. DWORD dwRet = m_pConnection->SendHolderSkillReq();
  1017. if (Error_Succeed == dwRet)
  1018. {
  1019. Dbg("send SendHolderSkillReq success");
  1020. }
  1021. else
  1022. {
  1023. Dbg("send SendHolderSkillReq failed");
  1024. LeaveCriticalSection(&cs);
  1025. return dwRet;
  1026. }
  1027. }
  1028. else
  1029. {
  1030. //连接失败,关闭链接重连
  1031. if (NULL != m_pConnection)
  1032. {
  1033. m_pConnection->handleDisconnect();
  1034. m_pConnection = NULL;
  1035. Dbg("Close m_pConnection");
  1036. }
  1037. Dbg("connect to instructions server failed, can not get holder skill!");
  1038. LeaveCriticalSection(&cs);
  1039. SetErrMsg(ERR_CONNECT,"网络异常,无法连接指令服务");
  1040. return ERR_CONNECT;
  1041. }
  1042. LeaveCriticalSection(&cs);
  1043. return Error_Succeed;
  1044. }
  1045. //TODO:监控IEbrowser状态,如果为非Idle状态,持有者状态设置为“N”,但“N”状态不上报,怎么处理?(新加一个错误状态)
  1046. //获取通话状态
  1047. BOOL HolderContextFSM::GetCallState()
  1048. {
  1049. //判断终端是否在业务界面首页,不在首页不弹出客户经理登录框
  1050. ErrorCodeEnum Error = GetEntityBase()->GetFunction()->GetSysVar("CallState",m_strCallstate);
  1051. if (Error_NotExist == Error)
  1052. {
  1053. Dbg("CallState is not exist, errcode=%d!", Error);
  1054. return FALSE;
  1055. }
  1056. else if (Error_Succeed != Error)
  1057. {
  1058. Dbg("Get CallState failed, errcode=%d!", Error);
  1059. return FALSE;
  1060. }
  1061. else
  1062. {
  1063. /* "O", Offline
  1064. "C", Connecting
  1065. "H", HandFree
  1066. "P", Pickup
  1067. "B", Broken
  1068. "F", Fail
  1069. "R", Releasing
  1070. */
  1071. ////Dbg("Get UIState success, m_strUIstate=%s!", m_strUIstate);
  1072. //if ((-1 != FindSubStr(m_strUIstate, "M"))
  1073. // || (-1 != FindSubStr(m_strUIstate, "A"))
  1074. // || (-1 != FindSubStr(m_strUIstate, "T")))
  1075. //{
  1076. // //界面状态UIState。定义当前交互层所处的交互模式。有8种状态:目录浏览M、随机广告A、目标广告T、业务填单F、交易确认C、业务提交S、请求回报R、暂停响应Z。初始状态为M。
  1077. // //M、A、T三种状态都认为在首页
  1078. // //Dbg("At Menu status!");
  1079. // m_bMenu = TRUE;
  1080. //}
  1081. //else
  1082. //{
  1083. // //不在首页
  1084. // //Dbg("Not at Menu status!");
  1085. // m_bMenu = FALSE;
  1086. //}
  1087. return TRUE;
  1088. }
  1089. }
  1090. //获取持有者状态,TODO:增加系统变量HolderState
  1091. BOOL HolderContextFSM::GetHolderState()
  1092. {
  1093. ErrorCodeEnum Error = GetEntityBase()->GetFunction()->GetSysVar("HolderState",(CSimpleStringA)m_HolderState.State);
  1094. if (Error_NotExist == Error)
  1095. {
  1096. Dbg("HolderState is not exist, errcode=%d!", Error);
  1097. return FALSE;
  1098. }
  1099. else if (Error_Succeed != Error)
  1100. {
  1101. Dbg("Get HolderState failed, errcode=%d!", Error);
  1102. return FALSE;
  1103. }
  1104. else
  1105. {
  1106. /* "O", Offline
  1107. "C", Connecting
  1108. "H", HandFree
  1109. "P", Pickup
  1110. "B", Broken
  1111. "F", Fail
  1112. "R", Releasing
  1113. */
  1114. ////Dbg("Get UIState success, m_strUIstate=%s!", m_strUIstate);
  1115. //if ((-1 != FindSubStr(m_strUIstate, "M"))
  1116. // || (-1 != FindSubStr(m_strUIstate, "A"))
  1117. // || (-1 != FindSubStr(m_strUIstate, "T")))
  1118. //{
  1119. // //界面状态UIState。定义当前交互层所处的交互模式。有8种状态:目录浏览M、随机广告A、目标广告T、业务填单F、交易确认C、业务提交S、请求回报R、暂停响应Z。初始状态为M。
  1120. // //M、A、T三种状态都认为在首页
  1121. // //Dbg("At Menu status!");
  1122. // m_bMenu = TRUE;
  1123. //}
  1124. //else
  1125. //{
  1126. // //不在首页
  1127. // //Dbg("Not at Menu status!");
  1128. // m_bMenu = FALSE;
  1129. //}
  1130. return TRUE;
  1131. }
  1132. }
  1133. //由业务层设置持有者状态
  1134. BOOL HolderContextFSM::SetHolderState(CSimpleStringA strState)
  1135. {
  1136. Dbg("SetHolderState[%s]", strState);
  1137. string State = strState;
  1138. //根据的状态值发送事件跳转到相应的状态机
  1139. if ("S" == State)
  1140. {
  1141. //就绪(S)
  1142. PostEventLIFO(new FSMEvent(USER_EVT_JMP_STANDBY));
  1143. }
  1144. else if ("B" == State)
  1145. {
  1146. //示忙(B)
  1147. PostEventLIFO(new FSMEvent(USER_EVT_JMP_BUSY));
  1148. }
  1149. else if ("R" == State)
  1150. {
  1151. //振铃(R)
  1152. PostEventLIFO(new FSMEvent(USER_EVT_JMP_RINGING));
  1153. }
  1154. else if ("E" == State)
  1155. {
  1156. //会话整理(E)
  1157. PostEventLIFO(new FSMEvent(USER_EVT_JMP_EDIT));
  1158. }
  1159. else if ("N" == State)
  1160. {
  1161. //未绑定(N),签出时处于此状态
  1162. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NONE));
  1163. }
  1164. else
  1165. {
  1166. Dbg("SetHolderState failed, unkonwn state value");
  1167. return FALSE;
  1168. }
  1169. return TRUE;
  1170. }
  1171. //校验持有者技能
  1172. DWORD HolderContextFSM::CheckHolderRights()
  1173. {
  1174. //add by zl 20170811 暂时放开远程服务资质校验
  1175. return Error_Succeed;
  1176. //获取权限
  1177. /*DWORD dwRet = QueryHolderSkill();
  1178. if (Error_Succeed != dwRet)
  1179. {
  1180. return dwRet;
  1181. }
  1182. //校验权限
  1183. CSimpleStringA strSkillList = m_cHolderSkill;
  1184. Dbg("[%s]技能列表[%s]",m_HolderState.HolderID.GetData(),strSkillList.GetData());
  1185. if (-1 == FindSubStr(strSkillList, "ConnectService"))
  1186. {
  1187. SetErrMsg(ERR_NO_CONNECTRIGHT,"用户没有远程服务资质");
  1188. return ERR_NO_CONNECTRIGHT;
  1189. }
  1190. return Error_Succeed;*/
  1191. }
  1192. //获取持有者信息
  1193. BOOL GetSAPFromUserInfo(const char *strHolderInfo, char *cSAP)
  1194. {
  1195. if (NULL == strHolderInfo || NULL == cSAP)
  1196. {
  1197. return FALSE;
  1198. }
  1199. string strUserInfo = strHolderInfo;
  1200. int len = strUserInfo.length();
  1201. int nPos = strUserInfo.find("SAP=");
  1202. if (nPos < 0)
  1203. {
  1204. return FALSE;
  1205. }
  1206. string strRecord = strUserInfo.substr(nPos, len-1);
  1207. nPos = strRecord.find_first_of(";");
  1208. if (nPos < 0)
  1209. {
  1210. return FALSE;
  1211. }
  1212. string strUserIDRecord = strRecord.substr(0, nPos);
  1213. if (strUserIDRecord.length() <= 4)
  1214. {
  1215. return FALSE;
  1216. }
  1217. string userID = strUserIDRecord.substr(4,nPos -4);
  1218. memcpy(cSAP, userID.c_str(), 8);
  1219. return TRUE;
  1220. }
  1221. BOOL GetAgentIDFromUserInfo(const char *strHolderInfo, char *cAgentID)
  1222. {
  1223. if (NULL == strHolderInfo || NULL == cAgentID)
  1224. {
  1225. return FALSE;
  1226. }
  1227. string strUserInfo = strHolderInfo;
  1228. int len = strUserInfo.length();
  1229. int nPos = strUserInfo.find("UserID=");
  1230. if (nPos < 0)
  1231. {
  1232. return FALSE;
  1233. }
  1234. string strRecord = strUserInfo.substr(nPos, len-1);
  1235. nPos = strRecord.find_first_of(";");
  1236. if (nPos < 0)
  1237. {
  1238. return FALSE;
  1239. }
  1240. string strUserIDRecord = strRecord.substr(0, nPos);
  1241. if (strUserIDRecord.length() <= 7)
  1242. {
  1243. return FALSE;
  1244. }
  1245. string userID = strUserIDRecord.substr(7,nPos -7);
  1246. memcpy(cAgentID, userID.c_str(), 10);
  1247. return TRUE;
  1248. }
  1249. BOOL HolderContextFSM::GetHolderMsg(CSimpleStringA strHolderInfo)
  1250. {
  1251. Dbg("GetHolderMsg success!");
  1252. m_strHolderInfo = strHolderInfo;
  1253. //解析持有者信息
  1254. char cSAP[9] = {0};
  1255. if (!GetSAPFromUserInfo(m_strHolderInfo.GetData(),cSAP))
  1256. {
  1257. Dbg("GetSAPFromUserInfo failed!");
  1258. return FALSE;
  1259. }
  1260. m_HolderState.HolderID = cSAP;
  1261. Dbg("SAP=%s", m_HolderState.HolderID);
  1262. //判断HolderID是否为空 add by zl 20171129
  1263. if (m_HolderState.HolderID.IsNullOrEmpty())
  1264. {
  1265. Dbg("SAP is not exist!");
  1266. SetErrMsg(ERR_NO_CONNECTRIGHT,"当前用户没有SAP号");
  1267. return FALSE;
  1268. }
  1269. else
  1270. {
  1271. //PostEventLIFO(new FSMEvent(USER_EVT_JMP_STANDBY)); D:\BranchServer\RvcBranchServer1.9.5\RvcFramework.exe
  1272. }
  1273. return TRUE;
  1274. }
  1275. //查找子串
  1276. DWORD HolderContextFSM::FindSubStr(const char* source, const char* target)
  1277. {
  1278. unsigned int i,j;
  1279. if (strlen(source) < strlen(target))
  1280. {
  1281. return -1;
  1282. }
  1283. if (1 == strlen(source)
  1284. && 1== strlen(target)
  1285. && source[0] == target[0])
  1286. {
  1287. return 0;
  1288. }
  1289. for (i = 0;i<=strlen(source)-strlen(target);i++)
  1290. {
  1291. if (source[i]==target[0])
  1292. {
  1293. for (j = 1;j<=strlen(target);j++)
  1294. {
  1295. if (source[i+j]!=target[j])
  1296. break;
  1297. }
  1298. if ((j == strlen(target))
  1299. || (j == strlen(target)+1))
  1300. {
  1301. return i;
  1302. }
  1303. j = 0;
  1304. }
  1305. }
  1306. return -1;
  1307. }
  1308. void HolderContextFSM::SetErrMsg(DWORD dwErrcode, CSimpleStringA strErrmsg)
  1309. {
  1310. m_nErrcode = dwErrcode;
  1311. m_strErrmsg = strErrmsg;
  1312. }
  1313. //获取指定实体状态
  1314. BOOL HolderContextFSM::GetEntityState(CSimpleStringA strEntityName, int &nState)
  1315. {
  1316. ErrorCodeEnum eErr;
  1317. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  1318. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  1319. CEntityRunInfo acInfo;
  1320. eErr = pFunc->GetEntityRunInfo(strEntityName,acInfo);
  1321. Dbg("[%s] state return %d",strEntityName, eErr);
  1322. if (eErr == Error_Succeed)
  1323. {
  1324. nState = acInfo.eState;
  1325. Dbg("[%s] state is [%d]", strEntityName, acInfo.eState);
  1326. }
  1327. else
  1328. {
  1329. Dbg("GetEntityRunInfo failed, errcode[%d]",eErr);
  1330. }
  1331. return TRUE;
  1332. }
  1333. ErrorCodeEnum HolderContextFSM::AsyncStartEntity(const char *entity_name, const char *cmdline, void *pData)
  1334. {
  1335. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  1336. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  1337. if (pFuncPrivilege != NULL) {
  1338. CSmartPointer<IAsynWaitSp> spWait;
  1339. ErrorCodeEnum Error = pFuncPrivilege->StartEntity(entity_name, cmdline, spWait);
  1340. //if (Error == Error_Succeed) {
  1341. // callback_entry *entry = new callback_entry();
  1342. // entry->pRawData = pData;
  1343. // entry->EntityName = entity_name;
  1344. // entry->ErrorResult = Error_Unexpect;
  1345. // entry->op = OP_START_ENTITY;
  1346. // //spWait->SetCallback(this, entry);
  1347. //}z
  1348. return Error;
  1349. } else {
  1350. return Error_NoPrivilege;
  1351. }
  1352. }
  1353. ErrorCodeEnum HolderContextFSM::AsyncStopEntity(const char *entity_name, void *pData)
  1354. {
  1355. CSmartPointer<IEntityFunction> pFunc = m_pEntity->GetFunction();
  1356. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  1357. if (pFuncPrivilege != NULL) {
  1358. CSmartPointer<IAsynWaitSp> spWait;
  1359. ErrorCodeEnum Error = pFuncPrivilege->StopEntity(entity_name, spWait);
  1360. //if (Error == Error_Succeed) {
  1361. // callback_entry *entry = new callback_entry();
  1362. // entry->pRawData = pData;
  1363. // entry->EntityName = entity_name;
  1364. // entry->ErrorResult = Error_Unexpect;
  1365. // entry->op = OP_STOP_ENTITY;
  1366. // //spWait->SetCallback(this, entry);
  1367. //}
  1368. return Error;
  1369. }
  1370. else
  1371. {
  1372. return Error_NoPrivilege;
  1373. }
  1374. }