DiosSMachine.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. // DiosSMachine.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "DiosSMachine.h"
  5. #include "common_api.h"
  6. #include <sstream>
  7. using namespace std;
  8. Log4CPP::Logger* mLog::gLogger = nullptr;
  9. //-----------------------------
  10. DiosStMEvt::DiosStMEvt()
  11. {
  12. m_pEvt = new ResDataObject();
  13. m_pInfo = new ResDataObject();
  14. }
  15. DiosStMEvt::DiosStMEvt(const DiosStMEvt &tValue)
  16. {
  17. m_pEvt = new ResDataObject();
  18. m_pInfo = new ResDataObject();
  19. (*m_pEvt) = (*tValue.m_pEvt);
  20. (*m_pInfo) = (*tValue.m_pInfo);
  21. }
  22. DiosStMEvt::~DiosStMEvt()
  23. {
  24. delete m_pEvt;
  25. m_pEvt = NULL;
  26. delete m_pInfo;
  27. m_pInfo = NULL;
  28. }
  29. bool DiosStMEvt::SetEvt(const char* pKey, INT Val, const char *pInfo)
  30. {
  31. m_pEvt->clear();
  32. m_pInfo->clear();
  33. if (pKey)
  34. {
  35. m_pEvt->add(pKey, Val);
  36. m_pInfo->add(pKey, Val);
  37. }
  38. else
  39. {
  40. //m_pEvt->add("", Val);
  41. //m_pInfo->add("", Val);
  42. return true;
  43. }
  44. if (pInfo)
  45. {
  46. m_pInfo->add(DiosStmEvtInfo, pInfo);
  47. }
  48. else
  49. {
  50. m_pInfo->add(DiosStmEvtInfo, "");
  51. }
  52. return true;
  53. }
  54. bool DiosStMEvt::IsEmpty()
  55. {
  56. return (m_pEvt->size() == 0);
  57. }
  58. bool DiosStMEvt::SetEvt(const char* pKey, const char *pVal, const char *pInfo)
  59. {
  60. bool ret = true;
  61. m_pEvt->clear();
  62. m_pInfo->clear();
  63. if (pKey == NULL)
  64. {
  65. return true;
  66. }
  67. if (pVal)
  68. {
  69. ret = m_pEvt->add(pKey, pVal);
  70. m_pInfo->add(pKey, pVal);
  71. }
  72. else
  73. {
  74. ret = m_pEvt->add(pKey, "");
  75. m_pInfo->add(pKey, "");
  76. }
  77. if (pInfo)
  78. {
  79. m_pInfo->add(DiosStmEvtInfo, pInfo);
  80. }
  81. else
  82. {
  83. m_pInfo->add(DiosStmEvtInfo, "");
  84. }
  85. return ret;
  86. }
  87. ResDataObject& DiosStMEvt::GetEvtContext()
  88. {
  89. return (*m_pEvt);
  90. }
  91. DiosStMEvt& DiosStMEvt::operator = (const DiosStMEvt &tValue)
  92. {
  93. if (this != &tValue)
  94. {
  95. (*m_pEvt) = (*tValue.m_pEvt);
  96. (*m_pInfo) = (*tValue.m_pInfo);
  97. }
  98. return (*this);
  99. }
  100. bool DiosStMEvt::operator == (const DiosStMEvt &Obj)
  101. {
  102. return (*m_pEvt == (*(Obj.m_pEvt)));
  103. }
  104. const char *DiosStMEvt::encode()
  105. {
  106. return m_pInfo->encode();
  107. }
  108. bool DiosStMEvt::decode(const char *pdata)
  109. {
  110. ResDataObject Obj;
  111. bool ret = m_pInfo->decode(pdata);
  112. if (ret)
  113. {
  114. size_t size = m_pInfo->size();
  115. if ((size > 0) && (size <= 2))
  116. {
  117. ret = false;
  118. string strStmEvtInfo = DiosStmEvtInfo;
  119. for (size_t i = 0; i < size; i++)
  120. {
  121. const char *pKey = (*m_pInfo).GetKey(i);
  122. if (strStmEvtInfo == pKey)
  123. {
  124. continue;
  125. }
  126. m_pEvt->clear();
  127. m_pEvt->add(pKey, (*m_pInfo)[i]);
  128. ret = true;
  129. }
  130. }
  131. else
  132. {
  133. ret = false;
  134. }
  135. }
  136. if (ret == false)
  137. {
  138. m_pEvt->clear();
  139. m_pInfo->clear();
  140. }
  141. return ret;
  142. }
  143. //-----------------------------
  144. DiosStMRouteLine::DiosStMRouteLine()
  145. {
  146. m_ActiveState = false;
  147. m_InRoute = true;
  148. m_pEvt = new DiosStMEvt();
  149. m_pSrcRoutePos = new string();
  150. m_pGuard = new string();
  151. m_pDesRoutePos = new string();
  152. m_Timeout = TIMEOUT_TEMP;
  153. }
  154. DiosStMRouteLine::DiosStMRouteLine(const DiosStMRouteLine &tValue)
  155. {
  156. m_ActiveState = false;
  157. m_InRoute = true;
  158. m_pEvt = new DiosStMEvt();
  159. m_pSrcRoutePos = new string();
  160. m_pGuard = new string();
  161. m_pDesRoutePos = new string();
  162. m_Timeout = TIMEOUT_TEMP;
  163. if (tValue.m_InRoute)
  164. {
  165. SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  166. }
  167. else
  168. {
  169. SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  170. }
  171. }
  172. DiosStMRouteLine::~DiosStMRouteLine()
  173. {
  174. delete m_pEvt;
  175. delete m_pSrcRoutePos;
  176. delete m_pDesRoutePos;
  177. delete m_pGuard;
  178. }
  179. void DiosStMRouteLine::SetRoute(DiosStMEvt &evt, const char* pGuard, const char* pDes)
  180. {
  181. m_InRoute = true;
  182. (*m_pSrcRoutePos) = "";
  183. (*m_pEvt) = evt;
  184. if (pGuard)
  185. {
  186. (*m_pGuard) = pGuard;
  187. }
  188. else
  189. {
  190. (*m_pGuard) = "";
  191. }
  192. if (pDes)
  193. {
  194. (*m_pDesRoutePos) = pDes;
  195. }
  196. else
  197. {
  198. (*m_pDesRoutePos) = "";
  199. }
  200. }
  201. void DiosStMRouteLine::SetRoute(DiosStMEvt &evt, const char* pSrc, const char* pGuard, const char* pDes)
  202. {
  203. m_InRoute = false;
  204. (*m_pEvt) = evt;
  205. if (pSrc)
  206. {
  207. (*m_pSrcRoutePos) = pSrc;
  208. }
  209. else
  210. {
  211. (*m_pSrcRoutePos) = "";
  212. }
  213. if (pGuard)
  214. {
  215. (*m_pGuard) = pGuard;
  216. }
  217. else
  218. {
  219. (*m_pGuard) = "";
  220. }
  221. if (pDes)
  222. {
  223. (*m_pDesRoutePos) = pDes;
  224. }
  225. else
  226. {
  227. (*m_pDesRoutePos) = "";
  228. }
  229. }
  230. const char* DiosStMRouteLine::GetDesName()
  231. {
  232. return m_pDesRoutePos->c_str();
  233. }
  234. const char* DiosStMRouteLine::GetSrcName()
  235. {
  236. if (m_InRoute == false)
  237. {
  238. return m_pSrcRoutePos->c_str();
  239. }
  240. return NULL;
  241. }
  242. const char* DiosStMRouteLine::GetGuardName()
  243. {
  244. return m_pGuard->c_str();
  245. }
  246. //true:in,false:out
  247. bool DiosStMRouteLine::GetRouteType()
  248. {
  249. return m_InRoute;
  250. }
  251. DiosStMRouteLine::operator DiosStMEvt *()
  252. {
  253. return m_pEvt;
  254. }
  255. DiosStMRouteLine& DiosStMRouteLine::operator = (const DiosStMRouteLine &tValue)
  256. {
  257. if (&tValue != this)
  258. {
  259. if (tValue.m_InRoute)
  260. {
  261. SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  262. }
  263. else
  264. {
  265. SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
  266. }
  267. }
  268. return (*this);
  269. }
  270. void DiosStMRouteLine::Active(bool state, DWORD timeout)
  271. {
  272. m_ActiveState = state;
  273. if (m_ActiveState == false)
  274. {
  275. m_Timeout = timeout;
  276. }
  277. else
  278. {
  279. //get maximum timeperiod
  280. if (timeout > m_Timeout)
  281. {
  282. m_Timeout = timeout;
  283. }
  284. }
  285. }
  286. bool DiosStMRouteLine::GetActiveState()
  287. {
  288. return m_ActiveState;
  289. }
  290. DWORD DiosStMRouteLine::GetTimeout()
  291. {
  292. return m_Timeout;
  293. }
  294. //================================
  295. DiosStMRoutePos::DiosStMRoutePos()
  296. {
  297. m_ActiveState = false;
  298. m_pRoutePosName = new string();
  299. m_pOutRouteLineVec = new vector<DiosStMRouteLine *>();
  300. m_Timeout = TIMEOUT_TEMP;
  301. }
  302. DiosStMRoutePos::DiosStMRoutePos(const char *pName)
  303. {
  304. m_ActiveState = false;
  305. m_pRoutePosName = new string();
  306. (*m_pRoutePosName) = pName;
  307. m_pOutRouteLineVec = new vector<DiosStMRouteLine *>();
  308. m_Timeout = TIMEOUT_TEMP;
  309. }
  310. DiosStMRoutePos::~DiosStMRoutePos()
  311. {
  312. delete m_pRoutePosName;
  313. m_pRoutePosName = NULL;
  314. if (m_pOutRouteLineVec != NULL)
  315. {
  316. if (m_pOutRouteLineVec->size() > 0)
  317. {
  318. for (size_t i = 0; i < m_pOutRouteLineVec->size(); i++)
  319. {
  320. DiosStMRouteLine *p = (*m_pOutRouteLineVec)[i];
  321. delete p;
  322. }
  323. }
  324. delete m_pOutRouteLineVec;
  325. m_pOutRouteLineVec = NULL;
  326. }
  327. }
  328. void DiosStMRoutePos::Active(bool state,DWORD timeout)
  329. {
  330. m_ActiveState = state;
  331. if (m_ActiveState == false)
  332. {
  333. m_Timeout = timeout;
  334. }
  335. else
  336. {
  337. //get maximum timeperiod
  338. if (timeout > m_Timeout)
  339. {
  340. m_Timeout = timeout;
  341. }
  342. }
  343. }
  344. bool DiosStMRoutePos::GetActiveState()
  345. {
  346. return m_ActiveState;
  347. }
  348. bool DiosStMRoutePos::IsSMachine()
  349. {
  350. return false;
  351. }
  352. bool DiosStMRoutePos::PosAddOutRouteLine(DiosStMEvt &Evt, const char *pSrcPosName, const char *pGuardName, const char *pDesPosName)
  353. {
  354. DiosStMRouteLine *pLine = new DiosStMRouteLine();
  355. pLine->SetRoute(Evt, pSrcPosName, pGuardName, pDesPosName);
  356. m_pOutRouteLineVec->push_back(pLine);
  357. return true;
  358. }
  359. void DiosStMRoutePos::SetName(const char *pName)
  360. {
  361. (*m_pRoutePosName) = pName;
  362. }
  363. const char* DiosStMRoutePos::GetName()
  364. {
  365. return m_pRoutePosName->c_str();
  366. }
  367. DWORD DiosStMRoutePos::GetRouteLineCount()
  368. {
  369. if (m_pOutRouteLineVec)
  370. {
  371. return (DWORD)m_pOutRouteLineVec->size();
  372. }
  373. return 0;
  374. }
  375. DiosStMRouteLine **DiosStMRoutePos::GetOutRouteLineVec()
  376. {
  377. if (m_pOutRouteLineVec)
  378. {
  379. if (m_pOutRouteLineVec->size() > 0)
  380. {
  381. return &((*m_pOutRouteLineVec)[0]);
  382. }
  383. }
  384. return NULL;
  385. }
  386. DiosStMRouteLine *DiosStMRoutePos::operator [](DiosStMEvt &Evt)
  387. {
  388. if (m_pOutRouteLineVec)
  389. {
  390. if (m_pOutRouteLineVec->size() > 0)
  391. {
  392. for (size_t i = 0; i < m_pOutRouteLineVec->size(); i++)
  393. {
  394. if ((*(DiosStMEvt *)(*(*m_pOutRouteLineVec)[i])) == Evt)
  395. {
  396. return (*m_pOutRouteLineVec)[i];
  397. }
  398. }
  399. }
  400. }
  401. return NULL;
  402. }
  403. DWORD DiosStMRoutePos::GetTimeout()
  404. {
  405. return m_Timeout;
  406. }
  407. //================================
  408. DiosSMachineIF::DiosSMachineIF()
  409. {
  410. m_pStateMachineName = new string();
  411. m_pParent = 0;
  412. m_pCurrentRoutePos = NULL;
  413. //evt list
  414. m_pArrivedEvts = new MsgCircle<DiosStMEvt>();
  415. //routepos
  416. m_pRoutePosMap = new map<string, DiosStMRoutePos*>();
  417. DiosStMRoutePos *p = NULL;
  418. p = new DiosStMRoutePos(DiosStmEntryPosName);
  419. p->Active(true, TIMEOUT_TEMP);
  420. (*m_pRoutePosMap)[DiosStmEntryPosName] = p;
  421. //printf(" == this[% 08X] Add Route Pos [%s] \n", this, DiosStmEntryPosName);
  422. p = new DiosStMRoutePos(DiosStmExitPosName);
  423. p->Active(true, TIMEOUT_TEMP);
  424. (*m_pRoutePosMap)[DiosStmExitPosName] = p;
  425. //inline
  426. m_pRouteInLineMap = new vector<DiosStMRouteLine*>();
  427. //external
  428. m_RouteExternalEvtCount = 0;
  429. m_pRouteExternalMap = NULL;
  430. //running state
  431. m_RunningState = CreateEvent(0, 1, 0, 0);
  432. m_StateCangeEvt = CreateEvent(0, 0, 0, 0);
  433. }
  434. DiosSMachineIF::~DiosSMachineIF()
  435. {
  436. delete m_pStateMachineName;
  437. m_pStateMachineName = 0;
  438. //cp
  439. m_pCurrentRoutePos = NULL;
  440. //external
  441. m_RouteExternalEvtCount = 0;
  442. m_pRouteExternalMap = NULL;
  443. //clear inline path
  444. for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
  445. {
  446. delete (*m_pRouteInLineMap)[i];
  447. }
  448. delete m_pRouteInLineMap;
  449. m_pRouteInLineMap = NULL;
  450. //clear routepos
  451. map<string, DiosStMRoutePos*>::iterator iter = m_pRoutePosMap->begin();
  452. while (iter != m_pRoutePosMap->end())
  453. {
  454. if (iter->second->IsSMachine())
  455. {
  456. DiosSubSMachine* pSub = (DiosSubSMachine*)iter->second;
  457. delete pSub;
  458. }
  459. else
  460. {
  461. delete iter->second;
  462. }
  463. ++iter;
  464. }
  465. delete m_pRoutePosMap;
  466. m_pRoutePosMap = NULL;
  467. //evt list
  468. delete m_pArrivedEvts;
  469. m_pArrivedEvts = NULL;
  470. CloseHandle(m_RunningState);
  471. CloseHandle(m_StateCangeEvt);
  472. return;
  473. }
  474. void DiosSMachineIF::SetStateMachineName(const char *pszName)
  475. {
  476. (*m_pStateMachineName) = pszName;
  477. }
  478. const char *DiosSMachineIF::GetStateMachineName()
  479. {
  480. return m_pStateMachineName->c_str();
  481. }
  482. void DiosSMachineIF::SetParentSMachine(DiosSMachineIF *pParent)
  483. {
  484. m_pParent = pParent;
  485. }
  486. void DiosSMachineIF::PushStateChange(ResDataObject &ChangedPos)
  487. {
  488. if (m_pParent)
  489. {
  490. m_pParent->PushStateChange(ChangedPos);
  491. }
  492. }
  493. bool DiosSMachineIF::IsSMachine()
  494. {
  495. return true;
  496. }
  497. HANDLE DiosSMachineIF::GetStateChangeEvtHandle()
  498. {
  499. return m_StateCangeEvt;
  500. }
  501. void DiosSMachineIF::SetRunningState(bool Running)
  502. {
  503. Thread_Lock();
  504. if (Running)
  505. {
  506. SetEvent(m_RunningState);
  507. }
  508. else
  509. {
  510. ResetEvent(m_RunningState);
  511. }
  512. Thread_UnLock();
  513. }
  514. bool DiosSMachineIF::GetRunningState(DWORD waittime)
  515. {
  516. bool ret = (WaitForSingleObject(m_RunningState, waittime) == WAIT_OBJECT_0);
  517. if (ret == false)
  518. {
  519. //printf("GetRunningState Failed.WTF??\n");
  520. }
  521. return ret;
  522. }
  523. //init state machine
  524. //第一个节点的接入比较特殊,所以做了个函数
  525. bool DiosSMachineIF::AddEntryRoutePos(DiosStMRoutePos *pPos)
  526. {
  527. if (strlen(pPos->GetName()) == 0)
  528. {
  529. return false;
  530. }
  531. if ((*m_pRoutePosMap)[DiosStmEntryPosName]->GetRouteLineCount() > 0)
  532. {
  533. return false;
  534. }
  535. //add pos
  536. (*m_pRoutePosMap)[pPos->GetName()] = pPos;
  537. DiosStMEvt evt;
  538. //make a line
  539. (*m_pRoutePosMap)[DiosStmEntryPosName]->PosAddOutRouteLine(evt, DiosStmEntryPosName, NULL, pPos->GetName());
  540. return true;
  541. }
  542. bool DiosSMachineIF::AddRoutePos(DiosStMRoutePos *pPos)
  543. {
  544. if (strlen(pPos->GetName()) == 0)
  545. {
  546. return false;
  547. }
  548. //add pos
  549. //printf("=== this [%08X] Add RoutePos [%s] \n",this, pPos->GetName());
  550. if (string(pPos->GetName()) == "FrameStart")
  551. {
  552. //printf("stop");
  553. }
  554. (*m_pRoutePosMap)[pPos->GetName()] = pPos;
  555. return true;
  556. }
  557. bool DiosSMachineIF::AddInRouteLine(DiosStMEvt &Evt, const char *pGuardName, const char *pDesPosName)
  558. {
  559. DiosStMRouteLine *pLine = new DiosStMRouteLine();
  560. pLine->SetRoute(Evt, pGuardName, pDesPosName);
  561. pLine->Active(true, 0);//timeout没必要,因为是全局事件
  562. m_pRouteInLineMap->push_back(pLine);
  563. return true;
  564. }
  565. bool DiosSMachineIF::AddOutRouteLine(DiosStMEvt &Evt, const char *pSrcPosName, const char *pGuardName, const char *pDesPosName)
  566. {
  567. bool ret = true;
  568. try{
  569. auto route = (*m_pRoutePosMap)[pSrcPosName];
  570. auto itf = m_pRoutePosMap->find(pSrcPosName);
  571. if (itf != m_pRoutePosMap->end())
  572. {
  573. //printf("\n Got it %s \n", pSrcPosName);
  574. }
  575. else
  576. {
  577. //printf("\n Route Pos %s has not set....\n", pSrcPosName);
  578. }
  579. ret = route->PosAddOutRouteLine(Evt, pSrcPosName, pGuardName, pDesPosName);
  580. }
  581. catch (...)
  582. {
  583. ret = false;
  584. }
  585. return ret;
  586. }
  587. //active state machine
  588. bool DiosSMachineIF::ActiveRoutePos(const char *pPosName, DWORD timeout)
  589. {
  590. bool ret = true;
  591. try{
  592. map<string, DiosStMRoutePos*>::iterator iterOut = m_pRoutePosMap->find(pPosName);
  593. if (iterOut != m_pRoutePosMap->end())
  594. {
  595. (*m_pRoutePosMap)[pPosName]->Active(true, timeout);
  596. }
  597. //find sub route machine
  598. iterOut = (*m_pRoutePosMap).begin();
  599. while (iterOut != (*m_pRoutePosMap).end())
  600. {
  601. if (iterOut->second->IsSMachine() == true)
  602. {
  603. DiosSubSMachine *pSub = (DiosSubSMachine *)(iterOut->second);
  604. pSub->ActiveRoutePos(pPosName, timeout);
  605. }
  606. ++iterOut;
  607. }
  608. }
  609. catch (...)
  610. {
  611. ret = false;
  612. }
  613. return ret;
  614. }
  615. //bool DiosSMachineIF::ActiveInRouteLine(DiosStMEvt &Evt, DWORD timeout)
  616. //{
  617. // bool ret = false;
  618. // try{
  619. // //find In Route Line
  620. // for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
  621. // {
  622. // if ((*(DiosStMEvt*)(*(*m_pRouteInLineMap)[i])) == Evt)
  623. // {
  624. // (*m_pRouteInLineMap)[i]->Active(true,timeout);
  625. //
  626. // ret = true;
  627. //
  628. // break;
  629. // }
  630. // }
  631. //
  632. // }
  633. // catch (...)
  634. // {
  635. // ret = false;
  636. // }
  637. //
  638. // return ret;
  639. //
  640. //}
  641. bool DiosSMachineIF::ActiveRouteLine(DiosStMEvt &Evt, DWORD timeout)
  642. {
  643. bool ret = true;
  644. try{
  645. //out path
  646. map<string, DiosStMRoutePos*>::iterator iterOut = (*m_pRoutePosMap).begin();
  647. while (iterOut != (*m_pRoutePosMap).end())
  648. {
  649. if (iterOut->second->IsSMachine() == false)
  650. {
  651. DWORD RouteCount = iterOut->second->GetRouteLineCount();
  652. DiosStMRouteLine **RouteLines = iterOut->second->GetOutRouteLineVec();
  653. for (DWORD i = 0; i < RouteCount; i++)
  654. {
  655. if ((*(DiosStMEvt *)(*(RouteLines[i]))) == Evt)
  656. {
  657. RouteLines[i]->Active(true, timeout);
  658. }
  659. }
  660. }
  661. else
  662. {
  663. //子对象自行激活自己
  664. //DiosSubSMachine *pSub = (DiosSubSMachine *)(iterOut->second);
  665. //pSub->ActiveRouteLine(Evt, timeout);
  666. }
  667. ++iterOut;
  668. }
  669. //in path
  670. for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
  671. {
  672. if ((*(DiosStMEvt *)(*((*m_pRouteInLineMap)[i]))) == Evt)
  673. {
  674. (*m_pRouteInLineMap)[i]->Active(true, timeout);
  675. }
  676. }
  677. //DiosStMRouteLine* pLine = (*(*m_pRoutePosMap)[pPosName])[Evt];
  678. //if (pLine)
  679. //{
  680. // pLine->Active(true, timeout);
  681. // ret = true;
  682. //}
  683. }
  684. catch (...)
  685. {
  686. ret = false;
  687. }
  688. return ret;
  689. }
  690. bool DiosSMachineIF::DeActiveAll()
  691. {
  692. bool ret = true;
  693. try{
  694. //out path
  695. map<string, DiosStMRoutePos*>::iterator iterOut = (*m_pRoutePosMap).begin();
  696. while (iterOut != (*m_pRoutePosMap).end())
  697. {
  698. if (iterOut->second->IsSMachine() == false)
  699. {
  700. //deactive pos
  701. iterOut->second->Active(false, 0);
  702. //deactive line
  703. DWORD RouteCount = iterOut->second->GetRouteLineCount();
  704. DiosStMRouteLine **RouteLines = iterOut->second->GetOutRouteLineVec();
  705. for (DWORD i = 0; i < RouteCount; i++)
  706. {
  707. RouteLines[i]->Active(false, 0);
  708. }
  709. }
  710. else
  711. {
  712. DiosSubSMachine *pSub = (DiosSubSMachine *)(iterOut->second);
  713. pSub->DeActiveAll();
  714. }
  715. ++iterOut;
  716. }
  717. //in path
  718. for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
  719. {
  720. (*m_pRouteInLineMap)[i]->Active(false, 0);
  721. }
  722. }
  723. catch (...)
  724. {
  725. ret = false;
  726. }
  727. return ret;
  728. }
  729. void DiosSMachineIF::ClearState(bool bClearEvent)
  730. {
  731. Thread_Lock();
  732. SetRunningState(false);
  733. map<string, DiosStMRoutePos*>::iterator iterOut = (*m_pRoutePosMap).begin();
  734. while (iterOut != (*m_pRoutePosMap).end())
  735. {
  736. if (iterOut->second->IsSMachine())
  737. {
  738. DiosSubSMachine *pSub = (DiosSubSMachine *)(iterOut->second);
  739. pSub->ClearState(bClearEvent);
  740. }
  741. ++iterOut;
  742. }
  743. m_pCurrentRoutePos = NULL;
  744. if(bClearEvent)
  745. m_pArrivedEvts->Clear();
  746. m_RouteExternalEvtCount = 0;
  747. m_pRouteExternalMap = NULL;
  748. Thread_UnLock();
  749. }
  750. HANDLE DiosSMachineIF::GetEvtNotifyHandle()
  751. {
  752. return (*m_pArrivedEvts).GetNotifyHandle();
  753. }
  754. bool DiosSMachineIF::PeekEvent(DiosStMEvt &Evt)
  755. {
  756. //check it's existance
  757. return (*m_pArrivedEvts).Peek(Evt);
  758. }
  759. //evt
  760. bool DiosSMachineIF::PopEvent(DiosStMEvt &Evt)
  761. {
  762. return (*m_pArrivedEvts).DeQueue(Evt);
  763. }
  764. bool DiosSMachineIF::PushEvent(DiosStMEvt &Evt)
  765. {
  766. bool ret = false;
  767. Thread_Lock();
  768. ResDataObject& EvtContext = Evt.GetEvtContext();
  769. mLog::FINFO("Push Key[{$}]:Val[{$}]", EvtContext.GetKey(0), (const char*)EvtContext[0]);
  770. if (m_pCurrentRoutePos)
  771. {
  772. if (m_pCurrentRoutePos->IsSMachine())
  773. {
  774. DiosSubSMachine* pMachine = (DiosSubSMachine*)m_pCurrentRoutePos;
  775. pMachine->Thread_Lock();
  776. if (pMachine->GetRunningState() == true)
  777. {
  778. mLog::FINFO("Enter SubStateMachine ");
  779. ret = pMachine->PushEvent(Evt);
  780. pMachine->Thread_UnLock();
  781. Thread_UnLock();
  782. return ret;
  783. }
  784. pMachine->Thread_UnLock();
  785. }
  786. }
  787. Thread_UnLock();
  788. //check it's existance
  789. (*m_pArrivedEvts).Lock();
  790. DWORD size = (*m_pArrivedEvts).size();
  791. for (DWORD i = 0; i < size; i++)
  792. {
  793. if ((*m_pArrivedEvts)[i] == Evt)
  794. {
  795. (*m_pArrivedEvts).UnLock();
  796. return ret;
  797. }
  798. }
  799. ret = true;
  800. mLog::FINFO("m_pArrivedEvts InQueue This Event");
  801. (*m_pArrivedEvts).InQueue(Evt);
  802. (*m_pArrivedEvts).UnLock();
  803. return true;
  804. }
  805. //for state machine thread
  806. DIOSSTMRET DiosSMachineIF::StateMachineEntry(DWORD timeout)
  807. {
  808. return DIOSSMRET_OK;
  809. }
  810. DIOSSTMRET DiosSMachineIF::StateMachineExit(DWORD timeout)
  811. {
  812. return DIOSSMRET_OK;
  813. }
  814. DIOSSTMRET DiosSMachineIF::StateMachineAction(const char *pAction, DWORD timeout)
  815. {
  816. return DIOSSMRET_OK;
  817. }
  818. DIOSSTMRET DiosSMachineIF::StateMachineGuard(const char *pGuard, DWORD timeout)
  819. {
  820. return DIOSSMRET_OK;
  821. }
  822. /// <summary>
  823. /// 实际的状态机等待事件
  824. /// </summary>
  825. /// <param name="pLocalEvts"></param>
  826. /// <param name="CountOfLocal"></param>
  827. /// <param name="pExternalEvts"></param>
  828. /// <param name="CountOfExternal"></param>
  829. /// <param name="pOutpathEvts"></param>
  830. /// <param name="CountOfOutpath"></param>
  831. /// <param name="timeout"></param>
  832. /// <param name="Evt"></param>
  833. /// <returns>
  834. /// 返回值为负:退出状态机;返回值为等到的 事件的下标
  835. /// </returns>
  836. int DiosSMachineIF::StateMachineWaitForEvents(
  837. DiosStMRouteLine *pLocalEvts[], DWORD CountOfLocal,
  838. DiosStMRouteLine *pExternalEvts[], DWORD CountOfExternal,
  839. DiosStMRouteLine *pOutpathEvts[], DWORD CountOfOutpath,
  840. DWORD timeout, DiosStMEvt& Evt
  841. )
  842. {
  843. //执行最基础的处理
  844. return -1;
  845. }
  846. DiosStMRoutePos *DiosSMachineIF::GetCurrentRoutePos()
  847. {
  848. return m_pCurrentRoutePos;
  849. }
  850. DIOSSTMRET DiosSMachineIF::TransToPos(const char *pPosName)
  851. {
  852. DIOSSTMRET ret = DIOSSMRET_OK;
  853. //des check
  854. try{
  855. Thread_Lock();
  856. m_pCurrentRoutePos = (*m_pRoutePosMap)[string(pPosName)];
  857. Thread_UnLock();
  858. SetEvent(m_StateCangeEvt);
  859. //printf("Trans To %s \n", pPosName);
  860. }
  861. catch (...)
  862. {
  863. assert(0);//never gonna reach
  864. ret = DIOSSMRET_NG;
  865. }
  866. return ret;
  867. }
  868. void DiosSMachineIF::PostError(const char *pErrorVal, const char *pErrorInfo)
  869. {
  870. DiosStMEvt evt;
  871. if (pErrorVal)
  872. {
  873. //printf("--PostError:%s--\n", pErrorVal);
  874. }
  875. else
  876. {
  877. //printf("--PostError:Empty--\n");
  878. }
  879. evt.SetEvt(DiosErrorType, pErrorVal,pErrorInfo);
  880. PushEvent(evt);
  881. }
  882. DiosStMRouteLine *DiosSMachineIF::GetEntryRouteLine()
  883. {
  884. //routepos
  885. DiosStMRoutePos *pFirst = (*m_pRoutePosMap)[string(DiosStmEntryPosName)];
  886. if (pFirst)
  887. {
  888. if (pFirst->GetRouteLineCount() == 1)
  889. {
  890. return ((m_pCurrentRoutePos->GetOutRouteLineVec())[0]);
  891. }
  892. }
  893. return NULL;
  894. }
  895. void DiosSMachineIF::CopyEvtTo(DiosSMachineIF *pDes)
  896. {
  897. //排序上先ORG的消息,然后des的消息
  898. bool ret = true;
  899. DiosStMEvt Evt;
  900. mLog::FINFO("Begin from {$} To {$}", GetStateMachineName(), pDes->GetStateMachineName());
  901. //copy des to org
  902. while (ret)
  903. {
  904. ret = pDes->PopEvent(Evt);
  905. if (ret)
  906. {
  907. PushEvent(Evt);
  908. }
  909. }
  910. //org -> des
  911. ret = true;
  912. while (ret)
  913. {
  914. ret = PopEvent(Evt);
  915. if (ret)
  916. {
  917. pDes->PushEvent(Evt);
  918. }
  919. }
  920. mLog::FINFO("End with {$}", GetStateMachineName());
  921. }
  922. //检查:路径,状态点,以及它们的ActiveState
  923. bool DiosSMachineIF::PrePareStateMachine()
  924. {
  925. //状态点的检查
  926. map<string, DiosStMRoutePos*>::iterator iter = m_pRoutePosMap->begin();
  927. while (iter != m_pRoutePosMap->end())
  928. {
  929. if (iter->second->IsSMachine())
  930. {
  931. //sub machine
  932. DiosSubSMachine* pSub = static_cast<DiosSubSMachine*>(iter->second);
  933. if (pSub->PrePareStateMachine() == false)
  934. {
  935. ostringstream buf;
  936. buf << "Sub StateMachine Error.Name:" << (iter->second)->GetName();
  937. PostError(DiosFrameError,buf.str().c_str());
  938. mLog::FERROR("Post FrameError.{$}",buf.str().c_str());
  939. return false;//submachine err
  940. }
  941. }
  942. else
  943. {
  944. //route pos
  945. bool RouteLineActived = false;
  946. //OutRouteLine的检查
  947. DWORD VecCount = iter->second->GetRouteLineCount();
  948. DiosStMRouteLine **pVec = iter->second->GetOutRouteLineVec();
  949. for (DWORD i = 0; i < VecCount; i++)
  950. {
  951. //Activestate
  952. RouteLineActived |= pVec[i]->GetActiveState();
  953. //Des的有效性
  954. const char *pDes = pVec[i]->GetDesName();
  955. map<string, DiosStMRoutePos*>::iterator desok = m_pRoutePosMap->find(string(pDes));
  956. if (desok == m_pRoutePosMap->end())
  957. {
  958. //no des
  959. //ostringstream buf;
  960. string buf;
  961. buf = "No Destination of RouteLine.RouteName:" + string(iter->second->GetName()) + "DesName:" + string(pDes);
  962. PostError(DiosFrameError, buf.c_str());
  963. mLog::FERROR("Post FrameError.{$}", buf.c_str());
  964. return false;
  965. }
  966. }
  967. //多个路径情况下,是否有一个Active
  968. if (VecCount > 1)
  969. {
  970. if (RouteLineActived == false)
  971. {
  972. string buf;
  973. buf = "No Specific Route Actived in Multiple OutRouteLines.RouteName:" + string(iter->second->GetName());
  974. PostError(DiosFrameError, buf.c_str());
  975. mLog::FERROR("Post FrameError.{$}", buf.c_str());
  976. return false;
  977. }
  978. }
  979. }
  980. ++iter;
  981. }
  982. //InRouteLine的检查
  983. for (DWORD i = 0; i < m_pRouteInLineMap->size(); i++)
  984. {
  985. //Des的有效性
  986. const char *pDes = (*m_pRouteInLineMap)[i]->GetDesName();
  987. map<string, DiosStMRoutePos*>::iterator desok = m_pRoutePosMap->find(string(pDes));
  988. if (desok == m_pRoutePosMap->end())
  989. {
  990. //no des
  991. string buf;
  992. buf = "No Destination of InRouteLine.DesName:" + string(pDes);
  993. PostError(DiosFrameError, buf.c_str());
  994. mLog::FERROR("Post FrameError.{$}", buf.c_str());
  995. return false;
  996. }
  997. }
  998. //第一个RouteLine
  999. // 允许有多个
  1000. //DWORD firstRTLCount = (*m_pRoutePosMap)[DiosStmEntryPosName]->GetRouteLineCount();
  1001. //if (firstRTLCount == 0 || firstRTLCount > 1)
  1002. //{
  1003. // string buf;
  1004. // buf = "No First RouteLine or Multiple Routeline";
  1005. // PostError(DiosFrameError, buf.c_str());
  1006. // mLog::FERROR("Post FrameError.{$}", buf.c_str());
  1007. // return false;
  1008. //}
  1009. //无法检查全局ERROR事件的状态点
  1010. return true;
  1011. }
  1012. DWORD CalcMaximumTimeout(DiosStMRouteLine **pVec,DWORD Count)
  1013. {
  1014. DWORD timeout = 0;
  1015. if (pVec != NULL && Count > 0)
  1016. {
  1017. for (DWORD i = 0; i < Count; i++)
  1018. {
  1019. if (pVec[i]->GetActiveState())
  1020. {
  1021. if (pVec[i]->GetTimeout() > timeout)
  1022. {
  1023. timeout = pVec[i]->GetTimeout();
  1024. }
  1025. }
  1026. }
  1027. }
  1028. return timeout;
  1029. }
  1030. /// <summary>
  1031. /// 状态机顶层等待事件
  1032. /// </summary>
  1033. /// <param name="ExtEvtIndex"></param>
  1034. /// <param name="Evt"></param>
  1035. /// <returns>实际的状态机 跳转到的状态</returns>
  1036. DiosStMRouteLine* DiosSMachineIF::StateMachineWaitForEvents(int &ExtEvtIndex, DiosStMEvt& Evt)
  1037. {
  1038. ExtEvtIndex = -1;
  1039. mLog::FINFO("Begin with {$}", GetStateMachineName());
  1040. //after state action,do notify hit a state pos.
  1041. DiosStMRoutePos *pCurPos = GetCurrentRoutePos();
  1042. if (pCurPos)
  1043. {
  1044. ResDataObject StatePos;
  1045. StatePos.add(GetStateMachineName(), pCurPos->GetName());
  1046. PushStateChange(StatePos);
  1047. mLog::FINFO("PushStateChange {$} with {$}", pCurPos->GetName(), GetStateMachineName());
  1048. }
  1049. DiosStMRouteLine **pLocal = NULL;
  1050. DWORD LocalCount = (DWORD)m_pRouteInLineMap->size();
  1051. if (LocalCount > 0)
  1052. {
  1053. pLocal = &((*m_pRouteInLineMap)[0]);
  1054. }
  1055. DWORD OutLineCount = m_pCurrentRoutePos->GetRouteLineCount();
  1056. DiosStMRouteLine **pOutline = m_pCurrentRoutePos->GetOutRouteLineVec();
  1057. DWORD MaxTimeout = CalcMaximumTimeout(pLocal, LocalCount);//local
  1058. DWORD timeout = CalcMaximumTimeout(pOutline, OutLineCount);//outline
  1059. if (timeout > MaxTimeout)
  1060. {
  1061. MaxTimeout = timeout;
  1062. }
  1063. timeout = CalcMaximumTimeout(m_pRouteExternalMap, (DWORD)m_RouteExternalEvtCount);//external
  1064. if (timeout > MaxTimeout)
  1065. {
  1066. MaxTimeout = timeout;
  1067. }
  1068. mLog::FINFO("Wait for Events LocalCount={$} OutLineCount={$} MaxTimeout={$} m_RouteExternalEvtCount={$}",
  1069. LocalCount, OutLineCount, MaxTimeout, m_RouteExternalEvtCount);
  1070. for (int i = 0; i < 2; i++)
  1071. {
  1072. int ret = 0;
  1073. ret = StateMachineWaitForEvents(
  1074. pLocal, LocalCount,
  1075. m_pRouteExternalMap, (DWORD)m_RouteExternalEvtCount,
  1076. pOutline, OutLineCount,
  1077. MaxTimeout, Evt);
  1078. mLog::FINFO("StateMachine {$} WaitForEvents Return={$}", GetStateMachineName(), ret);
  1079. if (ret >= 0)
  1080. {
  1081. if ((DWORD)ret < LocalCount)
  1082. {
  1083. //local
  1084. return pLocal[ret];
  1085. }
  1086. else if ((DWORD)ret < LocalCount + m_RouteExternalEvtCount)
  1087. {
  1088. //external
  1089. ExtEvtIndex = ret - LocalCount;
  1090. return NULL;
  1091. }
  1092. //outline
  1093. ret -= (LocalCount + (DWORD)m_RouteExternalEvtCount);
  1094. return pOutline[ret];
  1095. }
  1096. else if (ret == -1)
  1097. {
  1098. PostError(DiosFrameError);
  1099. mLog::FERROR("Timeout .Post FrameError.");
  1100. }
  1101. else if (ret == -2)
  1102. {
  1103. PostError(DiosFrameError);
  1104. mLog::FERROR("Device Error .Post FrameError.");
  1105. }
  1106. else if (ret == -3)
  1107. {
  1108. ExtEvtIndex = -2;//thread exit
  1109. return NULL;
  1110. }
  1111. }
  1112. assert(0);//必须要有异常处理的方案,没有情况直接蹦.
  1113. mLog::FERROR("No Exption Method.");
  1114. return NULL;
  1115. }
  1116. INT DiosSMachineIF::EnterSubStateMachine(HANDLE ThreadExitEvt, DiosStMEvt& Evt)
  1117. {
  1118. DiosSubSMachine* pSub = (DiosSubSMachine*)m_pCurrentRoutePos;
  1119. size_t WaitCount = m_RouteExternalEvtCount + m_pRouteInLineMap->size();
  1120. vector<DiosStMRouteLine *> ExternalWaitEvts;
  1121. //merge externalevts + internalevts
  1122. for (DWORD i = 0; i < m_RouteExternalEvtCount; i++)
  1123. {
  1124. ExternalWaitEvts.push_back(m_pRouteExternalMap[i]);
  1125. }
  1126. for (DWORD i = 0; i < m_pRouteInLineMap->size(); i++)
  1127. {
  1128. ExternalWaitEvts.push_back((*m_pRouteInLineMap)[i]);
  1129. }
  1130. pSub->PushEvent(Evt);
  1131. return pSub->ExecStateMachine(ThreadExitEvt,&(ExternalWaitEvts[0]), WaitCount, false);
  1132. }
  1133. /// <summary>
  1134. /// 状态机主体执行函数,也可以是子状态机
  1135. /// </summary>
  1136. /// <param name="ThreadExitEvt"></param>
  1137. /// <param name="pExternalWaitEvts"></param>
  1138. /// <param name="WaitCount"></param>
  1139. /// <param name="bClear"></param>
  1140. /// <returns></returns>
  1141. INT DiosSMachineIF::ExecStateMachine(HANDLE ThreadExitEvt, DiosStMRouteLine *pExternalWaitEvts[], size_t WaitCount, bool bClear)
  1142. {
  1143. DIOSSTMRET ret = DIOSSMRET_OK;
  1144. ClearState(bClear);
  1145. SetRunningState(true);
  1146. if (pExternalWaitEvts != NULL && WaitCount > 0)
  1147. {
  1148. m_RouteExternalEvtCount = WaitCount;
  1149. m_pRouteExternalMap = &pExternalWaitEvts[0];
  1150. }
  1151. //m_pCurrentRoutePos = (*m_pRoutePosMap)[string(DiosStmEntryPosName)];
  1152. TransToPos(DiosStmEntryPosName);
  1153. //入口Action
  1154. ret = StateMachineEntry(m_pCurrentRoutePos->GetTimeout());
  1155. if (ret != DIOSSMRET_OK)
  1156. {
  1157. PostError(DiosFrameError);
  1158. mLog::FERROR("StateMachineEntry Failed.");
  1159. }
  1160. do
  1161. {
  1162. //wait events
  1163. INT RouteWay = -1;
  1164. DiosStMEvt Evt;
  1165. Evt.SetEvt("TestNoFind", "");
  1166. DiosStMRouteLine *pRouteLine = StateMachineWaitForEvents(RouteWay, Evt);
  1167. RouteLine_Process:
  1168. if (RouteWay >= 0)
  1169. {
  1170. //External way
  1171. //goto exitpos
  1172. mLog::FINFO("StateMachineWaitForEvents got external evt.exit sub state machine [{$}].", Evt.GetEvtContext().encode());
  1173. TransToPos(DiosStmExitPosName);
  1174. //run action without results
  1175. ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
  1176. //一旦在Exit出错,会再次进入WaitEvt,再执行Exit,.........dead loop
  1177. //if (ret != DIOSSMRET_OK)
  1178. //{
  1179. // continue;
  1180. //}
  1181. SetRunningState(false);
  1182. return RouteWay;
  1183. }
  1184. else if (RouteWay == -1)
  1185. {
  1186. //this machine
  1187. if (pRouteLine->GetActiveState())
  1188. {
  1189. //Guard
  1190. const char *pGuard = pRouteLine->GetGuardName();
  1191. if (pGuard != NULL && strlen(pGuard) > 0)
  1192. {
  1193. ret = StateMachineGuard(pGuard, TIMEOUT_TEMP);
  1194. if (ret != DIOSSMRET_OK)
  1195. {
  1196. continue;
  1197. }
  1198. }
  1199. }
  1200. //TransToPos
  1201. TransToPos(pRouteLine->GetDesName());
  1202. mLog::FINFO("TransToPos:{$} from evnt {$}", pRouteLine->GetDesName(), Evt.GetEvtContext().encode());
  1203. if (string(DiosStmExitPosName) == m_pCurrentRoutePos->GetName())
  1204. {
  1205. //exit
  1206. //run action without results
  1207. ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
  1208. //check ret
  1209. if (ret != DIOSSMRET_OK)
  1210. {
  1211. PostError(DiosFrameError);
  1212. mLog::FERROR("PostError DiosFrameError .curPos:{$}", m_pCurrentRoutePos->GetName());
  1213. continue;
  1214. }
  1215. //退出的时候,把事件带出去
  1216. PushEvent(Evt);
  1217. //exit here
  1218. SetRunningState(false);
  1219. return -1;
  1220. }
  1221. //statepos or statemachine
  1222. if (m_pCurrentRoutePos->GetActiveState())
  1223. {
  1224. if (m_pCurrentRoutePos->IsSMachine())
  1225. {
  1226. //子状态机嵌套
  1227. //copy evt in
  1228. CopyEvtTo((DiosSubSMachine*)m_pCurrentRoutePos);
  1229. mLog::FINFO("Enter Sub StateMachine to {$} from Evt[{$}]", m_pCurrentRoutePos->GetName(), Evt.GetEvtContext().GetKey(0));
  1230. //run sub state machine
  1231. INT ExitWay = EnterSubStateMachine(ThreadExitEvt, Evt);
  1232. mLog::FINFO("exit from Sub StateMachine.{$},ret:{$}", m_pCurrentRoutePos->GetName(),ExitWay);
  1233. //copy evt out
  1234. ((DiosSubSMachine*)m_pCurrentRoutePos)->CopyEvtTo(this);
  1235. //trans to position
  1236. if (ExitWay >= 0)
  1237. {
  1238. if ((size_t)ExitWay < m_RouteExternalEvtCount)
  1239. {
  1240. mLog::FINFO("exit from statemachine.{$} from event {$}", m_pCurrentRoutePos->GetName(), Evt.GetEvtContext().encode());
  1241. //external evt
  1242. TransToPos(DiosStmExitPosName);
  1243. //run action without results
  1244. ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
  1245. //一旦在Exit出错,会再次进入WaitEvt,再执行Exit,.........dead loop
  1246. //if (ret != DIOSSMRET_OK)
  1247. //{
  1248. // continue;
  1249. //}
  1250. SetRunningState(false);
  1251. return ExitWay;
  1252. }
  1253. else
  1254. {
  1255. //local evt
  1256. //trans to pos
  1257. RouteWay = -1;
  1258. pRouteLine = (*m_pRouteInLineMap)[ExitWay - m_RouteExternalEvtCount];
  1259. goto RouteLine_Process;
  1260. }
  1261. }
  1262. else
  1263. {
  1264. //outline .normal exit from statemachine
  1265. mLog::FINFO("exit from sub statemachine");
  1266. }
  1267. }
  1268. else
  1269. {
  1270. //just route pos
  1271. //check exitposition
  1272. //if (string(DiosStmExitPosName) == m_pCurrentRoutePos->GetName())
  1273. //{
  1274. // //exit
  1275. // //run action without results
  1276. // ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
  1277. // //check ret
  1278. // if (ret != DIOSSMRET_OK)
  1279. // {
  1280. // PostError(DiosFrameError);
  1281. // continue;
  1282. // }
  1283. // //exit here
  1284. // SetRunningState(false);
  1285. // return -1;
  1286. //}
  1287. //run action
  1288. ret = StateMachineAction(m_pCurrentRoutePos->GetName(), m_pCurrentRoutePos->GetTimeout());
  1289. mLog::FINFO("SmAction:{$}.RET:{$}", m_pCurrentRoutePos->GetName(),(int)ret);
  1290. //check ret
  1291. if (ret != DIOSSMRET_OK)
  1292. {
  1293. if (ret == DIOSSMRET_EXIT)
  1294. {
  1295. //exit thread
  1296. //goto exitpos
  1297. TransToPos(DiosStmExitPosName);
  1298. //run action without results
  1299. ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
  1300. SetRunningState(false);
  1301. return -1;
  1302. }
  1303. PostError(DiosFrameError);
  1304. mLog::FERROR("StateMachineAction Error Post DiosFrameError .curPos:{$}", m_pCurrentRoutePos->GetName());
  1305. }
  1306. }
  1307. }
  1308. }
  1309. else
  1310. {
  1311. //exit thread
  1312. //goto exitpos
  1313. TransToPos(DiosStmExitPosName);
  1314. //run action without results
  1315. ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
  1316. SetRunningState(false);
  1317. return -1;
  1318. }
  1319. } while (1);
  1320. SetRunningState(false);
  1321. return -1;
  1322. }
  1323. //-------------------------------------
  1324. DiosSMachine::DiosSMachine()
  1325. {
  1326. m_pStatePosList = new MsgQueue<ResDataObject>();
  1327. m_StateQuedEvent = CreateEvent(0, 1, 0, 0);
  1328. if (mLog::gLogger == nullptr)
  1329. {
  1330. string strLogPath = GetProcessDirectory() + R"(\Conf\Log4CPP.Config.xml)";
  1331. //LogHost = ((string)getRootpath()).c_str();
  1332. //if (LogHost.length() <= 1)
  1333. //{
  1334. // char szName[256];
  1335. // sprintf(szName, "/LogicDevice_%08d", GetCurrentProcessId());
  1336. // LogHost = szName;
  1337. //}
  1338. Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "StateMachine");
  1339. //Log4CPP::GlobalContext::Map::Set("LogHost", LogHost.c_str());
  1340. Log4CPP::ThreadContext::Map::Set(ECOM::Utility::Hash("LogHost"), "StateMachine");
  1341. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  1342. mLog::gLogger = Log4CPP::LogManager::GetLogger("StateMachine");
  1343. mLog::FINFO("Code Build datetime [{$} {$}]", __DATE__, __TIME__);
  1344. }
  1345. }
  1346. DiosSMachine::~DiosSMachine()
  1347. {
  1348. delete m_pStatePosList;
  1349. CloseHandle(m_StateQuedEvent);
  1350. //if (GetLogger() != 0)
  1351. //{
  1352. // ReleseLogger((Logger*)GetLogger());
  1353. // SetLogger(0);
  1354. //}
  1355. }
  1356. void DiosSMachine::SetStateMachineLog(const char *pszLogTitle)
  1357. {
  1358. //if (GetLogger() == 0)
  1359. //{
  1360. // string logfile = GetProcessDirectory() + "\\logs\\";
  1361. // logfile = FormatstdString("%s%s.log", logfile.c_str(), pszLogTitle);
  1362. // Logger *pLog = CreateLogger();
  1363. // pLog->SetLogFilepath(logfile.c_str());
  1364. // SetLogger(pLog);
  1365. //}
  1366. }
  1367. bool DiosSMachine::OnStartThread()
  1368. {
  1369. return PrePareStateMachine();
  1370. }
  1371. bool DiosSMachine::Exec()
  1372. {
  1373. ExecStateMachine(GetExitEvt(),NULL, 0);
  1374. return false;
  1375. }
  1376. void DiosSMachine::PushStateChange(ResDataObject &ChangedPos)
  1377. {
  1378. m_pStatePosList->InQueue(ChangedPos);
  1379. SetEvent(m_StateQuedEvent);
  1380. }
  1381. bool DiosSMachine::PopStateChange(ResDataObject &LastPos)
  1382. {
  1383. bool ret = false;
  1384. if (m_pStatePosList->size() > 0)
  1385. {
  1386. m_pStatePosList->DeQueue(LastPos);
  1387. m_LastHitStatePos = LastPos;
  1388. ret = true;
  1389. }
  1390. if (m_pStatePosList->size() == 0)
  1391. {
  1392. ResetEvent(m_StateQuedEvent);
  1393. }
  1394. else
  1395. {
  1396. SetEvent(m_StateQuedEvent);
  1397. }
  1398. if (ret == false)
  1399. {
  1400. if (m_LastHitStatePos.size() > 0)
  1401. {
  1402. LastPos = m_LastHitStatePos;
  1403. ret = true;
  1404. }
  1405. }
  1406. return ret;
  1407. }
  1408. HANDLE DiosSMachine::GetStateQuedEvent()
  1409. {
  1410. return m_StateQuedEvent;
  1411. }
  1412. bool DiosSMachine::StartStateMachine(DiosStMRouteLine *pExternalWaitEvts[], DWORD WaitCount)
  1413. {
  1414. m_LastHitStatePos.clear();
  1415. if (WaitTheThreadEnd(0) == false)
  1416. {
  1417. //it's in running state
  1418. //printf("StartStateMachine Failed.it's in running state\n");
  1419. return false;
  1420. }
  1421. if (pExternalWaitEvts != NULL && WaitCount > 0)
  1422. {
  1423. m_RouteExternalEvtCount = WaitCount;
  1424. m_pRouteExternalMap = &pExternalWaitEvts[0];
  1425. }
  1426. bool ret = StartThread();
  1427. if (ret)
  1428. {
  1429. return GetRunningState(TIMEOUT_TEMP);
  1430. }
  1431. return ret;
  1432. }
  1433. void DiosSMachine::StopStateMachine(DWORD timeout)
  1434. {
  1435. //直接StopThread好像不是个办法,得让状态机退出前进行OnExit操作.
  1436. StopThread(timeout);
  1437. ClearState();
  1438. }
  1439. DiosSubSMachine::DiosSubSMachine(void)
  1440. {
  1441. }
  1442. DiosSubSMachine::DiosSubSMachine(const char *pName) : DiosStMRoutePos(pName)
  1443. {
  1444. SetStateMachineName(pName);
  1445. }
  1446. DiosSubSMachine::~DiosSubSMachine(void)
  1447. {
  1448. }
  1449. bool DiosSubSMachine::IsSMachine()
  1450. {
  1451. return true;
  1452. }