gui_client more stable with 2 servers for stop and acquire

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorGui@169 af1100a4-978c-4157-bff7-07162d2ba061
This commit is contained in:
l_maliakal_d
2013-03-21 09:27:16 +00:00
parent 303d84b24a
commit 10e47581e0
6 changed files with 281 additions and 80 deletions

View File

@ -27,7 +27,7 @@ int qServer::gui_server_thread_running(0);
qServer::qServer(qDetectorMain *t):
myMainTab(t), mySocket(NULL),port_no(DEFAULT_GUI_PORTNO),lockStatus(0){
myMainTab(t), mySocket(NULL),myStopSocket(NULL),port_no(DEFAULT_GUI_PORTNO),lockStatus(0){
FunctionTable();
@ -40,6 +40,7 @@ qServer::qServer(qDetectorMain *t):
qServer::~qServer(){
delete myMainTab;
if(mySocket) delete mySocket;
if(myStopSocket) delete myStopSocket;
}
@ -48,9 +49,6 @@ qServer::~qServer(){
int qServer::FunctionTable(){
for (int i=0;i<NUMBER_OF_FUNCTIONS;i++)
flist[i]=&qServer::M_nofunc;
flist[F_GET_RUN_STATUS] = &qServer::GetStatus;
flist[F_START_ACQUISITION] = &qServer::StartAcquisition;
flist[F_STOP_ACQUISITION] = &qServer::StopsAcquisition;
@ -64,13 +62,13 @@ int qServer::FunctionTable(){
//------------------------------------------------------------------------------------------------------------------------------------------
int qServer::DecodeFunction(){
int qServer::DecodeFunction(MySocketTCP* sock){
int ret = qDefs::FAIL;
int n,fnum;
#ifdef VERYVERBOSE
cout << "receive data" << endl;
#endif
n = mySocket->ReceiveDataOnly(&fnum,sizeof(fnum));
n = sock->ReceiveDataOnly(&fnum,sizeof(fnum));
if (n <= 0) {
#ifdef VERYVERBOSE
cout << "ERROR reading from socket " << n << ", " << fnum << endl;
@ -86,8 +84,26 @@ int qServer::DecodeFunction(){
cout << "calling function fnum = "<< fnum << hex << ":"<< flist[fnum] << endl;
#endif
if (fnum<0 || fnum>NUMBER_OF_FUNCTIONS-1)
fnum = NUMBER_OF_FUNCTIONS-1;
if (((sock == myStopSocket) && ((fnum == F_GET_RUN_STATUS) || (fnum == F_STOP_ACQUISITION) || (fnum == F_EXIT_SERVER))) ||
((sock == mySocket) && ((fnum == F_START_ACQUISITION) || (fnum == F_START_AND_READ_ALL))))
;
//unrecognized functions exit guis
else{
ret = qDefs::FAIL;
sprintf(mess,"Unrecognized Function\n");
cout << mess << endl;
if (mySocket)
mySocket->ShutDownSocket();
myStopSocket->SendDataOnly(&ret,sizeof(ret));
myStopSocket->SendDataOnly(mess,sizeof(mess));
return GOODBYE;
}
//calling function
ret = (this->*flist[fnum])();
if (ret==qDefs::FAIL)
@ -100,20 +116,24 @@ int qServer::DecodeFunction(){
//------------------------------------------------------------------------------------------------------------------------------------------
int qServer::M_nofunc(){
int qServer::ExitServer(){
int ret = qDefs::FAIL;
sprintf(mess,"Unrecognized Function\n");
int ret = OK;
strcpy(mess," Gui Server closed successfully\n");
if(mySocket)
mySocket->ShutDownSocket();
myStopSocket->SendDataOnly(&ret,sizeof(ret));
myStopSocket->SendDataOnly(mess,sizeof(mess));
cout << mess << endl;
mySocket->SendDataOnly(&ret,sizeof(ret));
mySocket->SendDataOnly(mess,sizeof(mess));
return GOODBYE;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------
int qServer::StartStopServer(int start){
@ -125,26 +145,43 @@ int qServer::StartStopServer(int start){
#endif
if(!gui_server_thread_running){
#ifdef VERBOSE
cout << "Starting gui server thread ...." << endl;
#endif
gui_server_thread_running=1;
checkStarted=1;
//error creating thread
checkStarted=1;
if (pthread_create(&gui_server_thread, NULL,StartServerThread, (void*) this)){
gui_server_thread_running=0;
qDefs::Message(qDefs::WARNING,"Can't create gui server thread", "Server");
return 0;
cout << "ERROR: Can't create gui server thread" << endl;
return FAIL;
}
while(checkStarted);
checkStarted=0;
checkStarted = 0;
#ifdef VERBOSE
if(gui_server_thread_running)
cout << "Server thread created successfully." << endl;
#endif
//error creating thread
checkStopStarted=1;
if (pthread_create(&gui_stop_server_thread, NULL,StopServerThread, (void*) this)){
gui_server_thread_running=0;
qDefs::Message(qDefs::WARNING,"Can't create gui stop server thread", "Server");
cout << "ERROR: Can't create gui stop server thread" << endl;
return FAIL;
}
while(checkStopStarted);
checkStopStarted=0;
#ifdef VERBOSE
if(gui_server_thread_running)
cout << "Server Stop thread created successfully." << endl;
#endif
}
}
//stop server
else{
#ifdef VERBOSE
@ -152,10 +189,8 @@ int qServer::StartStopServer(int start){
#endif
if(gui_server_thread_running){
#ifdef VERBOSE
cout << "Stopping gui server thread ...." << endl;
#endif
gui_server_thread_running=0;
if(mySocket)
mySocket->ShutDownSocket();
pthread_join(gui_server_thread,NULL);
@ -163,13 +198,20 @@ int qServer::StartStopServer(int start){
delete mySocket;
mySocket = NULL;
}
if(myStopSocket)
myStopSocket->ShutDownSocket();
pthread_join(gui_stop_server_thread,NULL);
if(myStopSocket){
delete myStopSocket;
myStopSocket = NULL;
}
}
#ifdef VERBOSE
cout << "Server stopped successfully." << endl;
cout << "Server threads stopped successfully." << endl;
#endif
}
return gui_server_thread_running;
}
@ -177,6 +219,71 @@ int qServer::StartStopServer(int start){
//-------------------------------------------------------------------------------------------------------------------------------------------------
void* qServer::StopServerThread(void* this_pointer){
((qServer*)this_pointer)->StopServer();
return this_pointer;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
int qServer::StopServer(){
#ifdef VERYVERBOSE
cout << "In StopServer()" << endl;
#endif
int ret = qDefs::OK;
myStopSocket = new MySocketTCP(port_no+1);
if (myStopSocket->getErrorStatus()){
gui_server_thread_running = 0;
qDefs::Message(qDefs::WARNING,"Could not start gui stop server socket","Server");
}
checkStopStarted = 0;
while ((gui_server_thread_running) && (ret!=GOODBYE)) {
#ifdef VERBOSE
cout<< endl;
#endif
#ifdef VERYVERBOSE
cout << "Waiting for client call" << endl;
#endif
if(myStopSocket->Connect()>=0){
#ifdef VERYVERBOSE
cout << "Conenction accepted" << endl;
#endif
ret = DecodeFunction(myStopSocket);
#ifdef VERYVERBOSE
cout << "function executed" << endl;
#endif
myStopSocket->Disconnect();
#ifdef VERYVERBOSE
cout << "connection closed" << endl;
#endif
}
}
#ifdef VERBOSE
cout << "Stopped gui stop server thread" << endl;
#endif
gui_server_thread_running = 0;
//delete socket(via exit server)
if(myStopSocket){
delete myStopSocket;
myStopSocket = NULL;
}
if(!gui_server_thread_running)
emit ServerStoppedSignal();
return qDefs::OK;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
void* qServer::StartServerThread(void* this_pointer){
((qServer*)this_pointer)->StartServer();
return this_pointer;
@ -189,7 +296,7 @@ void* qServer::StartServerThread(void* this_pointer){
int qServer::StartServer(){
#ifdef VERYVERBOSE
cout << "In StartServer()\n");
cout << "In StartServer()" << endl;
#endif
int ret = qDefs::OK;
@ -211,7 +318,7 @@ int qServer::StartServer(){
#ifdef VERYVERBOSE
cout << "Conenction accepted" << endl;
#endif
ret = DecodeFunction();
ret = DecodeFunction(mySocket);
#ifdef VERYVERBOSE
cout << "function executed" << endl;
#endif
@ -230,8 +337,10 @@ int qServer::StartServer(){
delete mySocket;
mySocket = NULL;
}
//uncheck the server in modes(via exit server)
myMainTab->GuiServerExited();
if(!gui_server_thread_running)
emit ServerStoppedSignal();
return qDefs::OK;
}
@ -254,9 +363,9 @@ int qServer::GetStatus(){
progress = myMainTab->GetProgress();
// send answer
mySocket->SendDataOnly(&ret,sizeof(ret));
mySocket->SendDataOnly(&retval,sizeof(retval));
mySocket->SendDataOnly(&progress,sizeof(progress));
myStopSocket->SendDataOnly(&ret,sizeof(ret));
myStopSocket->SendDataOnly(&retval,sizeof(retval));
myStopSocket->SendDataOnly(&progress,sizeof(progress));
//return ok/fail
return ret;
}
@ -286,9 +395,9 @@ int qServer::StopsAcquisition(){
strcpy(mess,"Could not stop acquisition in gui. \n");
int ret = myMainTab->StartStopAcquisitionFromClient(false);
mySocket->SendDataOnly(&ret,sizeof(ret));
myStopSocket->SendDataOnly(&ret,sizeof(ret));
if(ret==FAIL)
mySocket->SendDataOnly(mess,sizeof(mess));
myStopSocket->SendDataOnly(mess,sizeof(mess));
return ret;
}
@ -316,18 +425,3 @@ int qServer::Acquire(){
//------------------------------------------------------------------------------------------------------------------------------------------
int qServer::ExitServer(){
int ret = OK;
strcpy(mess,"closing gui server");
mySocket->SendDataOnly(&ret,sizeof(ret));
mySocket->SendDataOnly(mess,sizeof(mess));
cout << mess << endl;
return GOODBYE;
}
//------------------------------------------------------------------------------------------------------------------------------------------