|  | @@ -0,0 +1,190 @@
 | 
											
												
													
														|  | 
 |  | +#include "statemachine.h"
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: the constructor
 | 
											
												
													
														|  | 
 |  | + * @param: void
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +stateMachine::stateMachine()
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    init();
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: the destructor
 | 
											
												
													
														|  | 
 |  | + * @param: void
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +stateMachine::~stateMachine()
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    recvThread->quit();
 | 
											
												
													
														|  | 
 |  | +    delete recvThread;
 | 
											
												
													
														|  | 
 |  | +    recvThread =NULL;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    sendThread->quit();
 | 
											
												
													
														|  | 
 |  | +    delete sendThread;
 | 
											
												
													
														|  | 
 |  | +    sendThread = NULL;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    if(recvClient){
 | 
											
												
													
														|  | 
 |  | +        delete recvClient;
 | 
											
												
													
														|  | 
 |  | +        recvClient = NULL;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    if(sendClient){
 | 
											
												
													
														|  | 
 |  | +        delete sendClient;
 | 
											
												
													
														|  | 
 |  | +        sendClient = NULL;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: init
 | 
											
												
													
														|  | 
 |  | + * @param: void
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +void stateMachine::init(){
 | 
											
												
													
														|  | 
 |  | +    recvThread = new QThread();
 | 
											
												
													
														|  | 
 |  | +    recvClient = new rabbitClient("recvClient");
 | 
											
												
													
														|  | 
 |  | +    connect(recvClient,SIGNAL(recv(QString)),this,SLOT(recv(QString)));
 | 
											
												
													
														|  | 
 |  | +    connect(recvClient,SIGNAL(sendState_recv(bool)),this,SLOT(getState_Recv(bool)));
 | 
											
												
													
														|  | 
 |  | +    recvClient->moveToThread(recvThread);
 | 
											
												
													
														|  | 
 |  | +    connect(recvThread,&QThread::finished,[=](){
 | 
											
												
													
														|  | 
 |  | +        delete recvClient;
 | 
											
												
													
														|  | 
 |  | +        delete recvThread;
 | 
											
												
													
														|  | 
 |  | +        recvClient = NULL;
 | 
											
												
													
														|  | 
 |  | +        recvThread = NULL;
 | 
											
												
													
														|  | 
 |  | +    });
 | 
											
												
													
														|  | 
 |  | +    connect(recvThread,&QThread::started,[=](){
 | 
											
												
													
														|  | 
 |  | +        recvClient->recvMsg();
 | 
											
												
													
														|  | 
 |  | +    });
 | 
											
												
													
														|  | 
 |  | +    recvThread->start();
 | 
											
												
													
														|  | 
 |  | +    recvState = true;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    sendThread = new QThread();
 | 
											
												
													
														|  | 
 |  | +    sendClient = new rabbitClient("sendClient");
 | 
											
												
													
														|  | 
 |  | +    connect(this,SIGNAL(send(std::string)),sendClient,SLOT(sendMsg(std::string)));
 | 
											
												
													
														|  | 
 |  | +    connect(sendClient,SIGNAL(sendState_send(bool)),this,SLOT(getState_Send(bool)));
 | 
											
												
													
														|  | 
 |  | +    sendClient->moveToThread(sendThread);
 | 
											
												
													
														|  | 
 |  | +    connect(sendThread,&QThread::finished,sendClient,[=](){
 | 
											
												
													
														|  | 
 |  | +        delete sendClient;
 | 
											
												
													
														|  | 
 |  | +        delete sendThread;
 | 
											
												
													
														|  | 
 |  | +        sendClient = NULL;
 | 
											
												
													
														|  | 
 |  | +        sendThread = NULL;
 | 
											
												
													
														|  | 
 |  | +    });
 | 
											
												
													
														|  | 
 |  | +    //    connect(sendThread,&QThread::started,[=](){
 | 
											
												
													
														|  | 
 |  | +    //       // sendClient->sendMsg("str",NULL);
 | 
											
												
													
														|  | 
 |  | +    //    });
 | 
											
												
													
														|  | 
 |  | +    sendThread->start();
 | 
											
												
													
														|  | 
 |  | +    sendState = true;
 | 
											
												
													
														|  | 
 |  | +    //cout<<"hello world"<<endl;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    listenThread = new QThread();
 | 
											
												
													
														|  | 
 |  | +    this->moveToThread(listenThread);
 | 
											
												
													
														|  | 
 |  | +    connect(listenThread,SIGNAL(started()),this,SLOT(isLive()));
 | 
											
												
													
														|  | 
 |  | +    listenThread->start();
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: restart recvClient
 | 
											
												
													
														|  | 
 |  | + * @param: void
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +void stateMachine::restartRecv()
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    if(recvState) return;
 | 
											
												
													
														|  | 
 |  | +    if(recvClient){
 | 
											
												
													
														|  | 
 |  | +        delete recvClient;
 | 
											
												
													
														|  | 
 |  | +        recvClient = NULL;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    recvClient = new rabbitClient();
 | 
											
												
													
														|  | 
 |  | +    if(recvThread){
 | 
											
												
													
														|  | 
 |  | +        delete recvThread;
 | 
											
												
													
														|  | 
 |  | +        recvThread = NULL;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    recvThread = new QThread();
 | 
											
												
													
														|  | 
 |  | +    recvClient->moveToThread(recvThread);
 | 
											
												
													
														|  | 
 |  | +    recvThread->start();
 | 
											
												
													
														|  | 
 |  | +    recvState = true;
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: restart sendClient
 | 
											
												
													
														|  | 
 |  | + * @param: void
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +void stateMachine::restartSend()
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    if(sendState) return;
 | 
											
												
													
														|  | 
 |  | +    if(sendClient){
 | 
											
												
													
														|  | 
 |  | +        delete sendClient;
 | 
											
												
													
														|  | 
 |  | +        sendClient = NULL;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    sendClient = new rabbitClient();
 | 
											
												
													
														|  | 
 |  | +    if(sendThread){
 | 
											
												
													
														|  | 
 |  | +        delete sendThread;
 | 
											
												
													
														|  | 
 |  | +        sendThread = NULL;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    sendThread = new QThread();
 | 
											
												
													
														|  | 
 |  | +    sendClient->moveToThread(sendThread);
 | 
											
												
													
														|  | 
 |  | +    sendThread->start();
 | 
											
												
													
														|  | 
 |  | +    sendState = true;
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: judge sendClient is live //must move to Qthread
 | 
											
												
													
														|  | 
 |  | + * @param: void
 | 
											
												
													
														|  | 
 |  | + * @ret: bool
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +bool stateMachine::isLive()
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    while(true){
 | 
											
												
													
														|  | 
 |  | +        if(!recvState){
 | 
											
												
													
														|  | 
 |  | +            recvClient->recv_connect();
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        if(!sendState){
 | 
											
												
													
														|  | 
 |  | +            sendClient->send_connect();
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        QThread::sleep(15);
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: get the recvClient's message which is consumed and push them into queue
 | 
											
												
													
														|  | 
 |  | + * @param: recv msg
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +void stateMachine::recv(QString Recvmsg)
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    recvMsgQueue.push_back(Recvmsg);
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: judge recvClient state
 | 
											
												
													
														|  | 
 |  | + * @param: bool
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +void stateMachine::getState_Recv(bool state)
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    recvState = state;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/*
 | 
											
												
													
														|  | 
 |  | + * @brief: judge sendClient state
 | 
											
												
													
														|  | 
 |  | + * @param: bool
 | 
											
												
													
														|  | 
 |  | + * @ret: void
 | 
											
												
													
														|  | 
 |  | + * @birth: created by czm in 20230326
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +void stateMachine::getState_Send(bool state)
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    sendState = state;
 | 
											
												
													
														|  | 
 |  | +}
 |