12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847 |
- // CcosSMachine.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "CcosSMachine.h"
- #include "common_api.h"
- #include <sstream>
- #include "LogLocalHelper.h"
- #include "Log4CPP.h"
- using namespace std;
- //Log4CPP::Logger* mLog::gLogger = nullptr;
- //-----------------------------
- CcosStMEvt::CcosStMEvt()
- {
- m_pEvt = new ResDataObject();
- m_pInfo = new ResDataObject();
- }
- CcosStMEvt::CcosStMEvt(const CcosStMEvt &tValue)
- {
- m_pEvt = new ResDataObject();
- m_pInfo = new ResDataObject();
- (*m_pEvt) = (*tValue.m_pEvt);
- (*m_pInfo) = (*tValue.m_pInfo);
- }
- CcosStMEvt::~CcosStMEvt()
- {
- delete m_pEvt;
- m_pEvt = NULL;
- delete m_pInfo;
- m_pInfo = NULL;
- }
- bool CcosStMEvt::SetEvt(const char* pKey, int Val, const char *pInfo)
- {
- m_pEvt->clear();
- m_pInfo->clear();
- if (pKey)
- {
- m_pEvt->add(pKey, Val);
- m_pInfo->add(pKey, Val);
- }
- else
- {
- //m_pEvt->add("", Val);
- //m_pInfo->add("", Val);
- return true;
- }
- if (pInfo)
- {
- m_pInfo->add(CcosStmEvtInfo, pInfo);
- }
- else
- {
- m_pInfo->add(CcosStmEvtInfo, "");
- }
- return true;
- }
- bool CcosStMEvt::IsEmpty()
- {
- return (m_pEvt->size() == 0);
- }
- bool CcosStMEvt::SetEvt(const char* pKey, const char *pVal, const char *pInfo)
- {
- bool ret = true;
- m_pEvt->clear();
- m_pInfo->clear();
- if (pKey == NULL)
- {
- return true;
- }
- if (pVal)
- {
- ret = m_pEvt->add(pKey, pVal);
- m_pInfo->add(pKey, pVal);
- }
- else
- {
- ret = m_pEvt->add(pKey, "");
- m_pInfo->add(pKey, "");
- }
- if (pInfo)
- {
- m_pInfo->add(CcosStmEvtInfo, pInfo);
- }
- else
- {
- m_pInfo->add(CcosStmEvtInfo, "");
- }
- return ret;
- }
- ResDataObject& CcosStMEvt::GetEvtContext()
- {
- return (*m_pEvt);
- }
- CcosStMEvt& CcosStMEvt::operator = (const CcosStMEvt &tValue)
- {
- if (this != &tValue)
- {
- (*m_pEvt) = (*tValue.m_pEvt);
- (*m_pInfo) = (*tValue.m_pInfo);
- }
- return (*this);
- }
- bool CcosStMEvt::operator == (const CcosStMEvt &Obj)
- {
- return (*m_pEvt == (*(Obj.m_pEvt)));
- }
- const char *CcosStMEvt::encode()
- {
- return m_pInfo->encode();
- }
- bool CcosStMEvt::decode(const char *pdata)
- {
- ResDataObject Obj;
- bool ret = m_pInfo->decode(pdata);
- if (ret)
- {
- size_t size = m_pInfo->size();
- if ((size > 0) && (size <= 2))
- {
- ret = false;
- string strStmEvtInfo = CcosStmEvtInfo;
- for (size_t i = 0; i < size; i++)
- {
- const char *pKey = (*m_pInfo).GetKey(i);
- if (strStmEvtInfo == pKey)
- {
- continue;
- }
- m_pEvt->clear();
- m_pEvt->add(pKey, (*m_pInfo)[i]);
- ret = true;
- }
- }
- else
- {
- ret = false;
- }
- }
- if (ret == false)
- {
- m_pEvt->clear();
- m_pInfo->clear();
- }
- return ret;
- }
- //-----------------------------
- CcosStMRouteLine::CcosStMRouteLine()
- {
- m_ActiveState = false;
- m_InRoute = true;
- m_pEvt = new CcosStMEvt();
- m_pSrcRoutePos = new string();
- m_pGuard = new string();
- m_pDesRoutePos = new string();
- m_Timeout = TIMEOUT_TEMP;
- }
- CcosStMRouteLine::CcosStMRouteLine(const CcosStMRouteLine &tValue)
- {
- m_ActiveState = false;
- m_InRoute = true;
- m_pEvt = new CcosStMEvt();
- m_pSrcRoutePos = new string();
- m_pGuard = new string();
- m_pDesRoutePos = new string();
- m_Timeout = TIMEOUT_TEMP;
- if (tValue.m_InRoute)
- {
- SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
- }
- else
- {
- SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
- }
- }
- CcosStMRouteLine::~CcosStMRouteLine()
- {
- delete m_pEvt;
- delete m_pSrcRoutePos;
- delete m_pDesRoutePos;
- delete m_pGuard;
- }
- void CcosStMRouteLine::SetRoute(CcosStMEvt &evt, const char* pGuard, const char* pDes)
- {
- m_InRoute = true;
- (*m_pSrcRoutePos) = "";
- (*m_pEvt) = evt;
- if (pGuard)
- {
- (*m_pGuard) = pGuard;
- }
- else
- {
- (*m_pGuard) = "";
- }
- if (pDes)
- {
- (*m_pDesRoutePos) = pDes;
- }
- else
- {
- (*m_pDesRoutePos) = "";
- }
- }
- void CcosStMRouteLine::SetRoute(CcosStMEvt &evt, const char* pSrc, const char* pGuard, const char* pDes)
- {
- m_InRoute = false;
- (*m_pEvt) = evt;
- if (pSrc)
- {
- (*m_pSrcRoutePos) = pSrc;
- }
- else
- {
- (*m_pSrcRoutePos) = "";
- }
- if (pGuard)
- {
- (*m_pGuard) = pGuard;
- }
- else
- {
- (*m_pGuard) = "";
- }
- if (pDes)
- {
- (*m_pDesRoutePos) = pDes;
- }
- else
- {
- (*m_pDesRoutePos) = "";
- }
- }
- const char* CcosStMRouteLine::GetDesName()
- {
- return m_pDesRoutePos->c_str();
- }
- const char* CcosStMRouteLine::GetSrcName()
- {
- if (m_InRoute == false)
- {
- return m_pSrcRoutePos->c_str();
- }
- return NULL;
- }
- const char* CcosStMRouteLine::GetGuardName()
- {
- return m_pGuard->c_str();
- }
- //true:in,false:out
- bool CcosStMRouteLine::GetRouteType()
- {
- return m_InRoute;
- }
- CcosStMRouteLine::operator CcosStMEvt *()
- {
- return m_pEvt;
- }
- CcosStMRouteLine& CcosStMRouteLine::operator = (const CcosStMRouteLine &tValue)
- {
- if (&tValue != this)
- {
- if (tValue.m_InRoute)
- {
- SetRoute(*(tValue.m_pEvt), tValue.m_pSrcRoutePos->c_str(), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
- }
- else
- {
- SetRoute(*(tValue.m_pEvt), tValue.m_pGuard->c_str(), tValue.m_pDesRoutePos->c_str());
- }
- }
- return (*this);
- }
- void CcosStMRouteLine::Active(bool state, DWORD timeout)
- {
- m_ActiveState = state;
- if (m_ActiveState == false)
- {
- m_Timeout = timeout;
- }
- else
- {
- //get maximum timeperiod
- if (timeout > m_Timeout)
- {
- m_Timeout = timeout;
- }
- }
- }
- bool CcosStMRouteLine::GetActiveState()
- {
- return m_ActiveState;
- }
- DWORD CcosStMRouteLine::GetTimeout()
- {
- return m_Timeout;
- }
- //================================
- CcosStMRoutePos::CcosStMRoutePos()
- {
- m_ActiveState = false;
- m_pRoutePosName = new string();
- m_pOutRouteLineVec = new vector<CcosStMRouteLine *>();
- m_Timeout = TIMEOUT_TEMP;
- }
- CcosStMRoutePos::CcosStMRoutePos(const char *pName)
- {
- m_ActiveState = false;
- m_pRoutePosName = new string();
- (*m_pRoutePosName) = pName;
- m_pOutRouteLineVec = new vector<CcosStMRouteLine *>();
- m_Timeout = TIMEOUT_TEMP;
- }
- CcosStMRoutePos::~CcosStMRoutePos()
- {
- delete m_pRoutePosName;
- m_pRoutePosName = NULL;
- if (m_pOutRouteLineVec != NULL)
- {
- if (m_pOutRouteLineVec->size() > 0)
- {
- for (size_t i = 0; i < m_pOutRouteLineVec->size(); i++)
- {
- CcosStMRouteLine *p = (*m_pOutRouteLineVec)[i];
- delete p;
- }
- }
- delete m_pOutRouteLineVec;
- m_pOutRouteLineVec = NULL;
- }
- }
- void CcosStMRoutePos::Active(bool state,DWORD timeout)
- {
- m_ActiveState = state;
- if (m_ActiveState == false)
- {
- m_Timeout = timeout;
- }
- else
- {
- //get maximum timeperiod
- if (timeout > m_Timeout)
- {
- m_Timeout = timeout;
- }
- }
- }
- bool CcosStMRoutePos::GetActiveState()
- {
- return m_ActiveState;
- }
- bool CcosStMRoutePos::IsSMachine()
- {
- return false;
- }
- bool CcosStMRoutePos::PosAddOutRouteLine(CcosStMEvt &Evt, const char *pSrcPosName, const char *pGuardName, const char *pDesPosName)
- {
- CcosStMRouteLine *pLine = new CcosStMRouteLine();
- pLine->SetRoute(Evt, pSrcPosName, pGuardName, pDesPosName);
- m_pOutRouteLineVec->push_back(pLine);
- return true;
- }
- void CcosStMRoutePos::SetName(const char *pName)
- {
- (*m_pRoutePosName) = pName;
- }
- const char* CcosStMRoutePos::GetName()
- {
- return m_pRoutePosName->c_str();
- }
- DWORD CcosStMRoutePos::GetRouteLineCount()
- {
- if (m_pOutRouteLineVec)
- {
- return (DWORD)m_pOutRouteLineVec->size();
- }
- return 0;
- }
- CcosStMRouteLine **CcosStMRoutePos::GetOutRouteLineVec()
- {
- if (m_pOutRouteLineVec)
- {
- if (m_pOutRouteLineVec->size() > 0)
- {
- return &((*m_pOutRouteLineVec)[0]);
- }
- }
- return NULL;
- }
- CcosStMRouteLine *CcosStMRoutePos::operator [](CcosStMEvt &Evt)
- {
- if (m_pOutRouteLineVec)
- {
- if (m_pOutRouteLineVec->size() > 0)
- {
- for (size_t i = 0; i < m_pOutRouteLineVec->size(); i++)
- {
- if ((*(CcosStMEvt *)(*(*m_pOutRouteLineVec)[i])) == Evt)
- {
- return (*m_pOutRouteLineVec)[i];
- }
- }
- }
- }
- return NULL;
- }
- DWORD CcosStMRoutePos::GetTimeout()
- {
- return m_Timeout;
- }
- //================================
- CcosSMachineIF::CcosSMachineIF()
- {
- m_pStateMachineName = new string();
- m_pParent = 0;
- m_pCurrentRoutePos = NULL;
- //evt list
- m_pArrivedEvts = new MsgCircle<CcosStMEvt>();
- //routepos
- m_pRoutePosMap = new map<string, CcosStMRoutePos*>();
- CcosStMRoutePos *p = NULL;
- p = new CcosStMRoutePos(CcosStmEntryPosName);
- p->Active(true, TIMEOUT_TEMP);
- (*m_pRoutePosMap)[CcosStmEntryPosName] = p;
- //printf(" == this[% 08X] Add Route Pos [%s] \n", this, CcosStmEntryPosName);
- p = new CcosStMRoutePos(CcosStmExitPosName);
- p->Active(true, TIMEOUT_TEMP);
- (*m_pRoutePosMap)[CcosStmExitPosName] = p;
- //inline
- m_pRouteInLineMap = new vector<CcosStMRouteLine*>();
- //external
- m_RouteExternalEvtCount = 0;
- m_pRouteExternalMap = NULL;
- //running state
- m_RunningState = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET,false);
- m_StateCangeEvt = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false);
- }
- CcosSMachineIF::~CcosSMachineIF()
- {
- delete m_pStateMachineName;
- m_pStateMachineName = 0;
- //cp
- m_pCurrentRoutePos = NULL;
- //external
- m_RouteExternalEvtCount = 0;
- m_pRouteExternalMap = NULL;
- //clear inline path
- for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
- {
- delete (*m_pRouteInLineMap)[i];
- }
- delete m_pRouteInLineMap;
- m_pRouteInLineMap = NULL;
- //clear routepos
- map<string, CcosStMRoutePos*>::iterator iter = m_pRoutePosMap->begin();
- while (iter != m_pRoutePosMap->end())
- {
- if (iter->second->IsSMachine())
- {
- CcosSubSMachine* pSub = (CcosSubSMachine*)iter->second;
- delete pSub;
- }
- else
- {
- delete iter->second;
- }
- ++iter;
- }
- delete m_pRoutePosMap;
- m_pRoutePosMap = NULL;
- //evt list
- delete m_pArrivedEvts;
- m_pArrivedEvts = NULL;
- return;
- }
- void CcosSMachineIF::SetStateMachineName(const char *pszName)
- {
- (*m_pStateMachineName) = pszName;
- }
- const char *CcosSMachineIF::GetStateMachineName()
- {
- return m_pStateMachineName->c_str();
- }
- void CcosSMachineIF::SetParentSMachine(CcosSMachineIF *pParent)
- {
- m_pParent = pParent;
- }
- void CcosSMachineIF::PushStateChange(ResDataObject &ChangedPos)
- {
- if (m_pParent)
- {
- m_pParent->PushStateChange(ChangedPos);
- }
- }
- bool CcosSMachineIF::IsSMachine()
- {
- return true;
- }
- std::shared_ptr<LinuxEvent> CcosSMachineIF::GetStateChangeEvtHandle()
- {
- return m_StateCangeEvt;
- }
- void CcosSMachineIF::SetRunningState(bool Running)
- {
- Thread_Lock();
- if (Running)
- {
- m_RunningState->SetEvent();
- }
- else
- {
- m_RunningState->ResetEvent();
- }
- Thread_UnLock();
- }
- bool CcosSMachineIF::GetRunningState(DWORD waittime)
- {
- bool ret = (m_RunningState-> Wait(waittime));
- if (ret == false)
- {
- //printf("GetRunningState Failed.WTF??\n");
- }
- return ret;
- }
- //init state machine
- //第一个节点的接入比较特殊,所以做了个函数
- bool CcosSMachineIF::AddEntryRoutePos(CcosStMRoutePos *pPos)
- {
- if (strlen(pPos->GetName()) == 0)
- {
- return false;
- }
- if ((*m_pRoutePosMap)[CcosStmEntryPosName]->GetRouteLineCount() > 0)
- {
- return false;
- }
- //add pos
- (*m_pRoutePosMap)[pPos->GetName()] = pPos;
- CcosStMEvt evt;
- //make a line
- (*m_pRoutePosMap)[CcosStmEntryPosName]->PosAddOutRouteLine(evt, CcosStmEntryPosName, NULL, pPos->GetName());
- return true;
- }
- bool CcosSMachineIF::AddRoutePos(CcosStMRoutePos *pPos)
- {
- if (strlen(pPos->GetName()) == 0)
- {
- return false;
- }
- //add pos
- //printf("=== this [%08X] Add RoutePos [%s] \n",this, pPos->GetName());
- if (string(pPos->GetName()) == "FrameStart")
- {
- //printf("stop");
- }
- (*m_pRoutePosMap)[pPos->GetName()] = pPos;
- return true;
- }
- bool CcosSMachineIF::AddInRouteLine(CcosStMEvt &Evt, const char *pGuardName, const char *pDesPosName)
- {
- CcosStMRouteLine *pLine = new CcosStMRouteLine();
- pLine->SetRoute(Evt, pGuardName, pDesPosName);
- pLine->Active(true, 0);//timeout没必要,因为是全局事件
- m_pRouteInLineMap->push_back(pLine);
- return true;
- }
- bool CcosSMachineIF::AddOutRouteLine(CcosStMEvt &Evt, const char *pSrcPosName, const char *pGuardName, const char *pDesPosName)
- {
- bool ret = true;
- try{
- auto route = (*m_pRoutePosMap)[pSrcPosName];
- auto itf = m_pRoutePosMap->find(pSrcPosName);
- if (itf != m_pRoutePosMap->end())
- {
- //printf("\n Got it %s \n", pSrcPosName);
- }
- else
- {
- //printf("\n Route Pos %s has not set....\n", pSrcPosName);
- }
- ret = route->PosAddOutRouteLine(Evt, pSrcPosName, pGuardName, pDesPosName);
- }
- catch (...)
- {
- ret = false;
- }
- return ret;
- }
- //active state machine
- bool CcosSMachineIF::ActiveRoutePos(const char *pPosName, DWORD timeout)
- {
- bool ret = true;
- try{
- map<string, CcosStMRoutePos*>::iterator iterOut = m_pRoutePosMap->find(pPosName);
- if (iterOut != m_pRoutePosMap->end())
- {
- (*m_pRoutePosMap)[pPosName]->Active(true, timeout);
- }
-
-
- //find sub route machine
- iterOut = (*m_pRoutePosMap).begin();
- while (iterOut != (*m_pRoutePosMap).end())
- {
- if (iterOut->second->IsSMachine() == true)
- {
- CcosSubSMachine *pSub = (CcosSubSMachine *)(iterOut->second);
- pSub->ActiveRoutePos(pPosName, timeout);
- }
- ++iterOut;
- }
- }
- catch (...)
- {
- ret = false;
- }
- return ret;
- }
- //bool CcosSMachineIF::ActiveInRouteLine(CcosStMEvt &Evt, DWORD timeout)
- //{
- // bool ret = false;
- // try{
- // //find In Route Line
- // for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
- // {
- // if ((*(CcosStMEvt*)(*(*m_pRouteInLineMap)[i])) == Evt)
- // {
- // (*m_pRouteInLineMap)[i]->Active(true,timeout);
- //
- // ret = true;
- //
- // break;
- // }
- // }
- //
- // }
- // catch (...)
- // {
- // ret = false;
- // }
- //
- // return ret;
- //
- //}
- bool CcosSMachineIF::ActiveRouteLine(CcosStMEvt &Evt, DWORD timeout)
- {
- bool ret = true;
- try{
- //out path
- map<string, CcosStMRoutePos*>::iterator iterOut = (*m_pRoutePosMap).begin();
- while (iterOut != (*m_pRoutePosMap).end())
- {
- if (iterOut->second->IsSMachine() == false)
- {
- DWORD RouteCount = iterOut->second->GetRouteLineCount();
- CcosStMRouteLine **RouteLines = iterOut->second->GetOutRouteLineVec();
- for (DWORD i = 0; i < RouteCount; i++)
- {
- if ((*(CcosStMEvt *)(*(RouteLines[i]))) == Evt)
- {
- RouteLines[i]->Active(true, timeout);
- }
- }
- }
- else
- {
- //子对象自行激活自己
- //CcosSubSMachine *pSub = (CcosSubSMachine *)(iterOut->second);
- //pSub->ActiveRouteLine(Evt, timeout);
-
- }
- ++iterOut;
- }
- //in path
- for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
- {
- if ((*(CcosStMEvt *)(*((*m_pRouteInLineMap)[i]))) == Evt)
- {
- (*m_pRouteInLineMap)[i]->Active(true, timeout);
- }
- }
- //CcosStMRouteLine* pLine = (*(*m_pRoutePosMap)[pPosName])[Evt];
- //if (pLine)
- //{
- // pLine->Active(true, timeout);
- // ret = true;
- //}
- }
- catch (...)
- {
- ret = false;
- }
- return ret;
- }
- bool CcosSMachineIF::DeActiveAll()
- {
- bool ret = true;
- try{
- //out path
- map<string, CcosStMRoutePos*>::iterator iterOut = (*m_pRoutePosMap).begin();
- while (iterOut != (*m_pRoutePosMap).end())
- {
- if (iterOut->second->IsSMachine() == false)
- {
- //deactive pos
- iterOut->second->Active(false, 0);
- //deactive line
- DWORD RouteCount = iterOut->second->GetRouteLineCount();
- CcosStMRouteLine **RouteLines = iterOut->second->GetOutRouteLineVec();
- for (DWORD i = 0; i < RouteCount; i++)
- {
- RouteLines[i]->Active(false, 0);
- }
- }
- else
- {
- CcosSubSMachine *pSub = (CcosSubSMachine *)(iterOut->second);
- pSub->DeActiveAll();
- }
- ++iterOut;
- }
- //in path
- for (size_t i = 0; i < m_pRouteInLineMap->size(); i++)
- {
- (*m_pRouteInLineMap)[i]->Active(false, 0);
- }
- }
- catch (...)
- {
- ret = false;
- }
- return ret;
- }
- void CcosSMachineIF::ClearState(bool bClearEvent)
- {
- Thread_Lock();
- SetRunningState(false);
- map<string, CcosStMRoutePos*>::iterator iterOut = (*m_pRoutePosMap).begin();
- while (iterOut != (*m_pRoutePosMap).end())
- {
- if (iterOut->second->IsSMachine())
- {
- CcosSubSMachine *pSub = (CcosSubSMachine *)(iterOut->second);
- pSub->ClearState(bClearEvent);
- }
- ++iterOut;
- }
- m_pCurrentRoutePos = NULL;
- if(bClearEvent)
- m_pArrivedEvts->Clear();
- m_RouteExternalEvtCount = 0;
- m_pRouteExternalMap = NULL;
- Thread_UnLock();
- }
- std::shared_ptr<LinuxEvent> CcosSMachineIF::GetEvtNotifyHandle()
- {
- return (*m_pArrivedEvts).GetNotifyHandle();
- }
- bool CcosSMachineIF::PeekEvent(CcosStMEvt &Evt)
- {
- //check it's existance
- return (*m_pArrivedEvts).Peek(Evt);
- }
- //evt
- bool CcosSMachineIF::PopEvent(CcosStMEvt &Evt)
- {
- return (*m_pArrivedEvts).DeQueue(Evt);
- }
- bool CcosSMachineIF::PushEvent(CcosStMEvt &Evt)
- {
- bool ret = false;
- Thread_Lock();
- ResDataObject& EvtContext = Evt.GetEvtContext();
- FINFO("Push Key[{$}]:Val[{$}]", EvtContext.GetKey(0), (const char*)EvtContext[0]);
- if (m_pCurrentRoutePos)
- {
- if (m_pCurrentRoutePos->IsSMachine())
- {
- CcosSubSMachine* pMachine = (CcosSubSMachine*)m_pCurrentRoutePos;
- pMachine->Thread_Lock();
- if (pMachine->GetRunningState() == true)
- {
- FINFO("Enter SubStateMachine ");
- ret = pMachine->PushEvent(Evt);
- pMachine->Thread_UnLock();
- Thread_UnLock();
- return ret;
- }
- pMachine->Thread_UnLock();
- }
- }
- Thread_UnLock();
- //check it's existance
- (*m_pArrivedEvts).Lock();
- DWORD size = (*m_pArrivedEvts).size();
- for (DWORD i = 0; i < size; i++)
- {
- if ((*m_pArrivedEvts)[i] == Evt)
- {
- (*m_pArrivedEvts).UnLock();
- return ret;
- }
- }
- ret = true;
- FINFO("m_pArrivedEvts InQueue This Event");
- (*m_pArrivedEvts).InQueue(Evt);
- (*m_pArrivedEvts).UnLock();
- return true;
- }
- //for state machine thread
- CCOSSTMRET CcosSMachineIF::StateMachineEntry(DWORD timeout)
- {
- return CCOSSMRET_OK;
- }
- CCOSSTMRET CcosSMachineIF::StateMachineExit(DWORD timeout)
- {
- return CCOSSMRET_OK;
- }
- CCOSSTMRET CcosSMachineIF::StateMachineAction(const char *pAction, DWORD timeout)
- {
- return CCOSSMRET_OK;
- }
- CCOSSTMRET CcosSMachineIF::StateMachineGuard(const char *pGuard, DWORD timeout)
- {
- return CCOSSMRET_OK;
- }
- /// <summary>
- /// 实际的状态机等待事件
- /// </summary>
- /// <param name="pLocalEvts"></param>
- /// <param name="CountOfLocal"></param>
- /// <param name="pExternalEvts"></param>
- /// <param name="CountOfExternal"></param>
- /// <param name="pOutpathEvts"></param>
- /// <param name="CountOfOutpath"></param>
- /// <param name="timeout"></param>
- /// <param name="Evt"></param>
- /// <returns>
- /// 返回值为负:退出状态机;返回值为等到的 事件的下标
- /// </returns>
- int CcosSMachineIF::StateMachineWaitForEvents(
- CcosStMRouteLine *pLocalEvts[], DWORD CountOfLocal,
- CcosStMRouteLine *pExternalEvts[], DWORD CountOfExternal,
- CcosStMRouteLine *pOutpathEvts[], DWORD CountOfOutpath,
- DWORD timeout, CcosStMEvt& Evt
- )
- {
- //执行最基础的处理
- return -1;
- }
- CcosStMRoutePos *CcosSMachineIF::GetCurrentRoutePos()
- {
- return m_pCurrentRoutePos;
- }
- CCOSSTMRET CcosSMachineIF::TransToPos(const char *pPosName)
- {
- CCOSSTMRET ret = CCOSSMRET_OK;
- //des check
- try{
- Thread_Lock();
- m_pCurrentRoutePos = (*m_pRoutePosMap)[string(pPosName)];
- Thread_UnLock();
- m_StateCangeEvt->SetEvent();
- //printf("Trans To %s \n", pPosName);
- }
- catch (...)
- {
- assert(0);//never gonna reach
- ret = CCOSSMRET_NG;
- }
- return ret;
- }
- void CcosSMachineIF::PostError(const char *pErrorVal, const char *pErrorInfo)
- {
- CcosStMEvt evt;
- if (pErrorVal)
- {
- //printf("--PostError:%s--\n", pErrorVal);
- }
- else
- {
- //printf("--PostError:Empty--\n");
- }
- evt.SetEvt(CcosErrorType, pErrorVal,pErrorInfo);
- PushEvent(evt);
- }
- CcosStMRouteLine *CcosSMachineIF::GetEntryRouteLine()
- {
- //routepos
- CcosStMRoutePos *pFirst = (*m_pRoutePosMap)[string(CcosStmEntryPosName)];
- if (pFirst)
- {
- if (pFirst->GetRouteLineCount() == 1)
- {
- return ((m_pCurrentRoutePos->GetOutRouteLineVec())[0]);
- }
- }
- return NULL;
- }
- void CcosSMachineIF::CopyEvtTo(CcosSMachineIF *pDes)
- {
- //排序上先ORG的消息,然后des的消息
- bool ret = true;
- CcosStMEvt Evt;
- FINFO("Begin from {$} To {$}", GetStateMachineName(), pDes->GetStateMachineName());
- //copy des to org
- while (ret)
- {
- ret = pDes->PopEvent(Evt);
- if (ret)
- {
- PushEvent(Evt);
- }
- }
- //org -> des
- ret = true;
- while (ret)
- {
- ret = PopEvent(Evt);
- if (ret)
- {
- pDes->PushEvent(Evt);
- }
- }
- FINFO("End with {$}", GetStateMachineName());
- }
- //检查:路径,状态点,以及它们的ActiveState
- bool CcosSMachineIF::PrePareStateMachine()
- {
- //状态点的检查
- map<string, CcosStMRoutePos*>::iterator iter = m_pRoutePosMap->begin();
- while (iter != m_pRoutePosMap->end())
- {
- if (iter->second->IsSMachine())
- {
- //sub machine
- CcosSubSMachine* pSub = static_cast<CcosSubSMachine*>(iter->second);
- if (pSub->PrePareStateMachine() == false)
- {
- ostringstream buf;
- buf << "Sub StateMachine Error.Name:" << (iter->second)->GetName();
- PostError(CcosFrameError,buf.str().c_str());
- FERROR("Post FrameError.{$}",buf.str().c_str());
- return false;//submachine err
- }
- }
- else
- {
- //route pos
- bool RouteLineActived = false;
- //OutRouteLine的检查
- DWORD VecCount = iter->second->GetRouteLineCount();
- CcosStMRouteLine **pVec = iter->second->GetOutRouteLineVec();
- for (DWORD i = 0; i < VecCount; i++)
- {
- //Activestate
- RouteLineActived |= pVec[i]->GetActiveState();
- //Des的有效性
- const char *pDes = pVec[i]->GetDesName();
- map<string, CcosStMRoutePos*>::iterator desok = m_pRoutePosMap->find(string(pDes));
- if (desok == m_pRoutePosMap->end())
- {
- //no des
- //ostringstream buf;
- string buf;
- buf = "No Destination of RouteLine.RouteName:" + string(iter->second->GetName()) + "DesName:" + string(pDes);
- PostError(CcosFrameError, buf.c_str());
- FERROR("Post FrameError.{$}", buf.c_str());
- return false;
- }
- }
- //多个路径情况下,是否有一个Active
- if (VecCount > 1)
- {
- if (RouteLineActived == false)
- {
- string buf;
- buf = "No Specific Route Actived in Multiple OutRouteLines.RouteName:" + string(iter->second->GetName());
- PostError(CcosFrameError, buf.c_str());
- FERROR("Post FrameError.{$}", buf.c_str());
- return false;
- }
- }
- }
- ++iter;
- }
- //InRouteLine的检查
- for (DWORD i = 0; i < m_pRouteInLineMap->size(); i++)
- {
- //Des的有效性
- const char *pDes = (*m_pRouteInLineMap)[i]->GetDesName();
- map<string, CcosStMRoutePos*>::iterator desok = m_pRoutePosMap->find(string(pDes));
- if (desok == m_pRoutePosMap->end())
- {
- //no des
- string buf;
- buf = "No Destination of InRouteLine.DesName:" + string(pDes);
- PostError(CcosFrameError, buf.c_str());
- FERROR("Post FrameError.{$}", buf.c_str());
- return false;
- }
- }
- //第一个RouteLine
- // 允许有多个
- //DWORD firstRTLCount = (*m_pRoutePosMap)[CcosStmEntryPosName]->GetRouteLineCount();
- //if (firstRTLCount == 0 || firstRTLCount > 1)
- //{
- // string buf;
- // buf = "No First RouteLine or Multiple Routeline";
- // PostError(CcosFrameError, buf.c_str());
- // FERROR("Post FrameError.{$}", buf.c_str());
- // return false;
- //}
- //无法检查全局ERROR事件的状态点
- return true;
- }
- DWORD CalcMaximumTimeout(CcosStMRouteLine **pVec,DWORD Count)
- {
- DWORD timeout = 0;
- if (pVec != NULL && Count > 0)
- {
- for (DWORD i = 0; i < Count; i++)
- {
- if (pVec[i]->GetActiveState())
- {
- if (pVec[i]->GetTimeout() > timeout)
- {
- timeout = pVec[i]->GetTimeout();
- }
- }
- }
- }
- return timeout;
- }
- /// <summary>
- /// 状态机顶层等待事件
- /// </summary>
- /// <param name="ExtEvtIndex"></param>
- /// <param name="Evt"></param>
- /// <returns>实际的状态机 跳转到的状态</returns>
- CcosStMRouteLine* CcosSMachineIF::StateMachineWaitForEvents(int &ExtEvtIndex, CcosStMEvt& Evt)
- {
- ExtEvtIndex = -1;
- FINFO("Begin with {$}", GetStateMachineName());
- //after state action,do notify hit a state pos.
- CcosStMRoutePos *pCurPos = GetCurrentRoutePos();
- if (pCurPos)
- {
- ResDataObject StatePos;
- StatePos.add(GetStateMachineName(), pCurPos->GetName());
- PushStateChange(StatePos);
- FINFO("PushStateChange {$} with {$}", pCurPos->GetName(), GetStateMachineName());
- }
- CcosStMRouteLine **pLocal = NULL;
- DWORD LocalCount = (DWORD)m_pRouteInLineMap->size();
- if (LocalCount > 0)
- {
- pLocal = &((*m_pRouteInLineMap)[0]);
- }
- DWORD OutLineCount = m_pCurrentRoutePos->GetRouteLineCount();
- CcosStMRouteLine **pOutline = m_pCurrentRoutePos->GetOutRouteLineVec();
- DWORD MaxTimeout = CalcMaximumTimeout(pLocal, LocalCount);//local
- DWORD timeout = CalcMaximumTimeout(pOutline, OutLineCount);//outline
- if (timeout > MaxTimeout)
- {
- MaxTimeout = timeout;
- }
- timeout = CalcMaximumTimeout(m_pRouteExternalMap, (DWORD)m_RouteExternalEvtCount);//external
- if (timeout > MaxTimeout)
- {
- MaxTimeout = timeout;
- }
- FINFO("Wait for Events LocalCount={$} OutLineCount={$} MaxTimeout={$} m_RouteExternalEvtCount={$}",
- LocalCount, OutLineCount, MaxTimeout, m_RouteExternalEvtCount);
- for (int i = 0; i < 2; i++)
- {
- int ret = 0;
- ret = StateMachineWaitForEvents(
- pLocal, LocalCount,
- m_pRouteExternalMap, (DWORD)m_RouteExternalEvtCount,
- pOutline, OutLineCount,
- MaxTimeout, Evt);
- FINFO("StateMachine {$} WaitForEvents Return={$}", GetStateMachineName(), ret);
- if (ret >= 0)
- {
- if ((DWORD)ret < LocalCount)
- {
- //local
- return pLocal[ret];
- }
- else if ((DWORD)ret < LocalCount + m_RouteExternalEvtCount)
- {
- //external
- ExtEvtIndex = ret - LocalCount;
- return NULL;
- }
- //outline
- ret -= (LocalCount + (DWORD)m_RouteExternalEvtCount);
- return pOutline[ret];
- }
- else if (ret == -1)
- {
- PostError(CcosFrameError);
- FERROR("Timeout .Post FrameError.");
- }
- else if (ret == -2)
- {
- PostError(CcosFrameError);
- FERROR("Device Error .Post FrameError.");
- }
- else if (ret == -3)
- {
- ExtEvtIndex = -2;//thread exit
- return NULL;
- }
- }
- assert(0);//必须要有异常处理的方案,没有情况直接蹦.
- FERROR("No Exption Method.");
- return NULL;
- }
- int CcosSMachineIF::EnterSubStateMachine(std::shared_ptr<LinuxEvent> ThreadExitEvt, CcosStMEvt& Evt)
- {
- CcosSubSMachine* pSub = (CcosSubSMachine*)m_pCurrentRoutePos;
- size_t WaitCount = m_RouteExternalEvtCount + m_pRouteInLineMap->size();
- vector<CcosStMRouteLine *> ExternalWaitEvts;
- //merge externalevts + internalevts
- for (DWORD i = 0; i < m_RouteExternalEvtCount; i++)
- {
- ExternalWaitEvts.push_back(m_pRouteExternalMap[i]);
- }
- for (DWORD i = 0; i < m_pRouteInLineMap->size(); i++)
- {
- ExternalWaitEvts.push_back((*m_pRouteInLineMap)[i]);
- }
- pSub->PushEvent(Evt);
- return pSub->ExecStateMachine(ThreadExitEvt,&(ExternalWaitEvts[0]), WaitCount, false);
- }
- /// <summary>
- /// 状态机主体执行函数,也可以是子状态机
- /// </summary>
- /// <param name="ThreadExitEvt"></param>
- /// <param name="pExternalWaitEvts"></param>
- /// <param name="WaitCount"></param>
- /// <param name="bClear"></param>
- /// <returns></returns>
- int CcosSMachineIF::ExecStateMachine(std::shared_ptr<LinuxEvent> ThreadExitEvt, CcosStMRouteLine *pExternalWaitEvts[], size_t WaitCount, bool bClear)
- {
- CCOSSTMRET ret = CCOSSMRET_OK;
- ClearState(bClear);
- SetRunningState(true);
- if (pExternalWaitEvts != NULL && WaitCount > 0)
- {
- m_RouteExternalEvtCount = WaitCount;
- m_pRouteExternalMap = &pExternalWaitEvts[0];
- }
- //m_pCurrentRoutePos = (*m_pRoutePosMap)[string(CcosStmEntryPosName)];
- TransToPos(CcosStmEntryPosName);
- //入口Action
- ret = StateMachineEntry(m_pCurrentRoutePos->GetTimeout());
- if (ret != CCOSSMRET_OK)
- {
- PostError(CcosFrameError);
- FERROR("StateMachineEntry Failed.");
- }
- do
- {
- //wait events
- int RouteWay = -1;
- CcosStMEvt Evt;
- Evt.SetEvt("TestNoFind", "");
- CcosStMRouteLine *pRouteLine = StateMachineWaitForEvents(RouteWay, Evt);
- RouteLine_Process:
- if (RouteWay >= 0)
- {
- //External way
- //goto exitpos
- FINFO("StateMachineWaitForEvents got external evt.exit sub state machine [{$}].", Evt.GetEvtContext().encode());
- TransToPos(CcosStmExitPosName);
- //run action without results
- ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
- //一旦在Exit出错,会再次进入WaitEvt,再执行Exit,.........dead loop
- //if (ret != CCOSSMRET_OK)
- //{
- // continue;
- //}
- SetRunningState(false);
- return RouteWay;
- }
- else if (RouteWay == -1)
- {
- //this machine
- if (pRouteLine->GetActiveState())
- {
- //Guard
- const char *pGuard = pRouteLine->GetGuardName();
- if (pGuard != NULL && strlen(pGuard) > 0)
- {
- ret = StateMachineGuard(pGuard, TIMEOUT_TEMP);
- if (ret != CCOSSMRET_OK)
- {
- continue;
- }
- }
- }
- //TransToPos
- TransToPos(pRouteLine->GetDesName());
- FINFO("TransToPos:{$} from evnt {$}", pRouteLine->GetDesName(), Evt.GetEvtContext().encode());
- if (string(CcosStmExitPosName) == m_pCurrentRoutePos->GetName())
- {
- //exit
- //run action without results
- ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
- //check ret
- if (ret != CCOSSMRET_OK)
- {
- PostError(CcosFrameError);
- FERROR("PostError CcosFrameError .curPos:{$}", m_pCurrentRoutePos->GetName());
- continue;
- }
- //退出的时候,把事件带出去
- PushEvent(Evt);
- //exit here
- SetRunningState(false);
- return -1;
- }
- //statepos or statemachine
- if (m_pCurrentRoutePos->GetActiveState())
- {
- if (m_pCurrentRoutePos->IsSMachine())
- {
- //子状态机嵌套
- //copy evt in
- CopyEvtTo((CcosSubSMachine*)m_pCurrentRoutePos);
- FINFO("Enter Sub StateMachine to {$} from Evt[{$}]", m_pCurrentRoutePos->GetName(), Evt.GetEvtContext().GetKey(0));
- //run sub state machine
- int ExitWay = EnterSubStateMachine(ThreadExitEvt, Evt);
- FINFO("exit from Sub StateMachine.{$},ret:{$}", m_pCurrentRoutePos->GetName(),ExitWay);
- //copy evt out
- ((CcosSubSMachine*)m_pCurrentRoutePos)->CopyEvtTo(this);
- //trans to position
- if (ExitWay >= 0)
- {
- if ((size_t)ExitWay < m_RouteExternalEvtCount)
- {
- FINFO("exit from statemachine.{$} from event {$}", m_pCurrentRoutePos->GetName(), Evt.GetEvtContext().encode());
- //external evt
- TransToPos(CcosStmExitPosName);
- //run action without results
- ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
- //一旦在Exit出错,会再次进入WaitEvt,再执行Exit,.........dead loop
- //if (ret != CCOSSMRET_OK)
- //{
- // continue;
- //}
- SetRunningState(false);
- return ExitWay;
- }
- else
- {
- //local evt
- //trans to pos
- RouteWay = -1;
- pRouteLine = (*m_pRouteInLineMap)[ExitWay - m_RouteExternalEvtCount];
- goto RouteLine_Process;
- }
- }
- else
- {
- //outline .normal exit from statemachine
- FINFO("exit from sub statemachine");
- }
- }
- else
- {
- //just route pos
- //check exitposition
- //if (string(CcosStmExitPosName) == m_pCurrentRoutePos->GetName())
- //{
- // //exit
- // //run action without results
- // ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
- // //check ret
- // if (ret != CCOSSMRET_OK)
- // {
- // PostError(CcosFrameError);
- // continue;
- // }
- // //exit here
- // SetRunningState(false);
- // return -1;
- //}
- //run action
- ret = StateMachineAction(m_pCurrentRoutePos->GetName(), m_pCurrentRoutePos->GetTimeout());
- FINFO("SmAction:{$}.RET:{$}", m_pCurrentRoutePos->GetName(),(int)ret);
- //check ret
- if (ret != CCOSSMRET_OK)
- {
- if (ret == CCOSSMRET_EXIT)
- {
- //exit thread
- //goto exitpos
- TransToPos(CcosStmExitPosName);
- //run action without results
- ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
- SetRunningState(false);
- return -1;
- }
- PostError(CcosFrameError);
- FERROR("StateMachineAction Error Post CcosFrameError .curPos:{$}", m_pCurrentRoutePos->GetName());
- }
- }
- }
- }
- else
- {
- //exit thread
- //goto exitpos
- TransToPos(CcosStmExitPosName);
- //run action without results
- ret = StateMachineExit(m_pCurrentRoutePos->GetTimeout());
- SetRunningState(false);
- return -1;
- }
- } while (1);
- SetRunningState(false);
- return -1;
- }
- //-------------------------------------
- CcosSMachine::CcosSMachine()
- {
- m_pStatePosList = new MsgQueue<ResDataObject>();
- m_StateQuedEvent = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET,false);
- string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
- string LogHost = "";//(string)getRootpath();
- std::string moduleName = "StateMachine";
- bool ret = initLogModule(
- LogHost, // 主机名(用于日志路径中的{host}占位符)
- moduleName, // 唯一模块名
- strLogPath, // 配置文件路径
- true // 是否输出到控制台(可选)
- );
- if (!ret) {
- std::cerr << "Log init failed!" << std::endl;
- }
- // 绑定当前动态库的模块名(调用自身实现的接口)
- Machine_SetLocalModuleName(moduleName);
- FINFO("Code Build datetime [{$} {$}]", __DATE__, __TIME__);
- }
- CcosSMachine::~CcosSMachine()
- {
- delete m_pStatePosList;
-
- //if (GetLogger() != 0)
- //{
- // ReleseLogger((Logger*)GetLogger());
- // SetLogger(0);
- //}
- }
- void CcosSMachine::SetStateMachineLog(const char *pszLogTitle)
- {
- //if (GetLogger() == 0)
- //{
- // string logfile = GetProcessDirectory() + "\\logs\\";
- // logfile = FormatstdString("%s%s.log", logfile.c_str(), pszLogTitle);
- // Logger *pLog = CreateLogger();
- // pLog->SetLogFilepath(logfile.c_str());
- // SetLogger(pLog);
- //}
- }
- bool CcosSMachine::OnStartThread()
- {
- return PrePareStateMachine();
- }
- bool CcosSMachine::Exec()
- {
- ExecStateMachine(GetExitEvt(),NULL, 0);
- return false;
- }
- void CcosSMachine::PushStateChange(ResDataObject &ChangedPos)
- {
- m_pStatePosList->InQueue(ChangedPos);
- m_StateQuedEvent->SetEvent();
- }
- bool CcosSMachine::PopStateChange(ResDataObject &LastPos)
- {
- bool ret = false;
- if (m_pStatePosList->size() > 0)
- {
- m_pStatePosList->DeQueue(LastPos);
- m_LastHitStatePos = LastPos;
- ret = true;
- }
- if (m_pStatePosList->size() == 0)
- {
- m_StateQuedEvent->ResetEvent();
- }
- else
- {
- m_StateQuedEvent->SetEvent();
- }
- if (ret == false)
- {
- if (m_LastHitStatePos.size() > 0)
- {
- LastPos = m_LastHitStatePos;
- ret = true;
- }
- }
- return ret;
- }
- std::shared_ptr<LinuxEvent> CcosSMachine::GetStateQuedEvent()
- {
- return m_StateQuedEvent;
- }
- bool CcosSMachine::StartStateMachine(CcosStMRouteLine *pExternalWaitEvts[], DWORD WaitCount)
- {
- m_LastHitStatePos.clear();
- if (WaitTheThreadEnd(0) == false)
- {
- //it's in running state
- //printf("StartStateMachine Failed.it's in running state\n");
- return false;
- }
- if (pExternalWaitEvts != NULL && WaitCount > 0)
- {
- m_RouteExternalEvtCount = WaitCount;
- m_pRouteExternalMap = &pExternalWaitEvts[0];
- }
- bool ret = StartThread();
- if (ret)
- {
- return GetRunningState(TIMEOUT_TEMP);
- }
- return ret;
- }
- void CcosSMachine::StopStateMachine(DWORD timeout)
- {
- //直接StopThread好像不是个办法,得让状态机退出前进行OnExit操作.
- StopThread(timeout);
- ClearState();
- }
- CcosSubSMachine::CcosSubSMachine(void)
- {
- }
- CcosSubSMachine::CcosSubSMachine(const char *pName) : CcosStMRoutePos(pName)
- {
- SetStateMachineName(pName);
- }
- CcosSubSMachine::~CcosSubSMachine(void)
- {
- }
- bool CcosSubSMachine::IsSMachine()
- {
- return true;
- }
|