From 22b51032dd98183f177f5dac6bfeb47d2528ecad Mon Sep 17 00:00:00 2001 From: l_maliakal_d Date: Mon, 4 Mar 2013 15:27:57 +0000 Subject: [PATCH] client connecting to gui serveR done git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorGui@150 af1100a4-978c-4157-bff7-07162d2ba061 --- slsDetectorGui/client/qClient.cpp | 101 ++++++++++++++++--- slsDetectorGui/client/qClient.h | 7 +- slsDetectorGui/include/qDetectorMain.h | 20 +++- slsDetectorGui/include/qServer.h | 29 ++++-- slsDetectorGui/include/qTabMeasurement.h | 11 ++ slsDetectorGui/src/qDetectorMain.cpp | 31 +++++- slsDetectorGui/src/qServer.cpp | 122 +++++++++++++++++++---- slsDetectorGui/src/qTabMeasurement.cpp | 19 ---- 8 files changed, 269 insertions(+), 71 deletions(-) diff --git a/slsDetectorGui/client/qClient.cpp b/slsDetectorGui/client/qClient.cpp index ba6bce2e2..5ad21e679 100644 --- a/slsDetectorGui/client/qClient.cpp +++ b/slsDetectorGui/client/qClient.cpp @@ -11,6 +11,7 @@ #include "slsDetectorBase.h" // C++ Include Headers #include +#include using namespace std; @@ -49,55 +50,129 @@ int qClient::executeLine(int narg, char *args[]){ char answer[100]; string retval = ""; string cmd = args[0]; + string argument; //validate command structure if(narg<1){ cout << "Error: no command parsed" << endl; - return slsDetectorDefs::FAIL; + return FAIL; } + //help + if (cmd == "help"){ + retval = printCommands(); + } + //file name - if (cmd == "status"){ + else if (cmd == "status"){ + + if(narg>1){ + argument = args[1]; + //start acquisition + if(argument == "start") + sendToGuiServer(F_START_ACQUISITION); + else if (argument == "stop") + sendToGuiServer(F_STOP_ACQUISITION); + else{ + cout << "Error: could not parse arguments: " << argument << endl; + printCommands(); + return FAIL; + } + } retval = getStatus(); } + else if (cmd == "acquire"){ + sendToGuiServer(F_START_AND_READ_ALL); + retval = getStatus(); + } + + + else if (cmd == "exit"){ + if (sendToGuiServer(F_EXIT_SERVER) == OK) + retval = "Gui Server Exited successfully."; + else + retval = "Gui Server could not exit successfully"; + } + + //unrecognized command else{ cout << "Error: unrecognized command" << endl; - return slsDetectorDefs::FAIL; + return FAIL; } //print result cout << cmd << ": " << retval << endl; - return slsDetectorDefs::OK; + return OK; } //------------------------------------------------------------------------------------------------------------------------------------------------- +string qClient::printCommands(){ + ostringstream os; + os << "\nexit \t exits server in gui" << std::endl; + os << "status \t gets status of acquisition in gui. - can be running or idle" << std::endl; + os << "status i starts/stops acquistion in gui-non blocking. i is start or stop" << std::endl; + os << "acquire starts acquistion in gui-blocking" << std::endl; + return os.str(); +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + string qClient::getStatus(){ - int fnum = slsDetectorDefs::F_GET_RUN_STATUS; - int ret = slsDetectorDefs::FAIL; - int retval = -1; - slsDetectorDefs::runStatus s=slsDetectorDefs::ERROR; + int fnum = F_GET_RUN_STATUS; + int ret = FAIL; + runStatus retval=ERROR; + int progress = 0; + char answer[100]; if (mySocket->Connect() >= 0) { mySocket->SendDataOnly(&fnum,sizeof(fnum)); mySocket->ReceiveDataOnly(&ret,sizeof(ret)); mySocket->ReceiveDataOnly(&retval,sizeof(retval)); - } - mySocket->Disconnect(); + mySocket->ReceiveDataOnly(&progress,sizeof(progress)); + mySocket->Disconnect(); + }else + exit(-1); - if(retval==-1) - retval=slsDetectorDefs::ERROR; - return slsDetectorBase::runStatusType((slsDetectorDefs::runStatus)retval); + sprintf(answer,"%d%% ",progress); + strcat(answer,slsDetectorBase::runStatusType((runStatus)retval).c_str()); + + return string(answer); } //------------------------------------------------------------------------------------------------------------------------------------------------- + + +int qClient::sendToGuiServer(int fnum){ + int ret = FAIL; + char mess[100] = ""; + + if (mySocket->Connect() >= 0) { + mySocket->SendDataOnly(&fnum,sizeof(fnum)); + mySocket->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret == FAIL){ + mySocket->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Gui returned error: " << mess << std::endl; + } + mySocket->Disconnect(); + }else + exit(-1); + + return ret; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + diff --git a/slsDetectorGui/client/qClient.h b/slsDetectorGui/client/qClient.h index 8e91b0c34..f68dddfde 100644 --- a/slsDetectorGui/client/qClient.h +++ b/slsDetectorGui/client/qClient.h @@ -9,7 +9,6 @@ /** Qt Project Class Headers */ -//#include "qDefs.h" /** Project Class Headers */ class MySocketTCP; #include "sls_detector_defs.h" @@ -34,6 +33,12 @@ public: int executeLine(int narg, char *args[]); private: + /** Print list of commands */ + string printCommands(); + + /** Send to Gui Server */ + int sendToGuiServer(int fnum); + /** Gets run status */ string getStatus(); diff --git a/slsDetectorGui/include/qDetectorMain.h b/slsDetectorGui/include/qDetectorMain.h index a948b4b33..7b0723e7e 100644 --- a/slsDetectorGui/include/qDetectorMain.h +++ b/slsDetectorGui/include/qDetectorMain.h @@ -12,8 +12,8 @@ /** Form Header */ #include "ui_form_detectormain.h" /** Qt Project Class Headers */ -class qDrawPlot; -class qTabMeasurement; +#include "qDrawPlot.h" +#include "qTabMeasurement.h" class qTabDataOutput; class qTabPlot; class qTabActions; @@ -64,6 +64,22 @@ public: * */ ~qDetectorMain(); + /** Starts or stops Acquisition From gui client + * @param start 1 for start and 0 to stop + /returns success or fail + */ + int StartStopAcquisitionFromClient(bool start); + + /** Returns if plot is running + */ + bool isPlotRunning(){return myPlot->isRunning();}; + + /** Returns progress bar value */ + int GetProgress(){return tab_measurement->GetProgress();}; + + /** Uncheck the Listen to Gui Client mode when server has exited using exit command */ + void GuiServerExited(){actionListenGuiClient->setChecked(false);}; + private: /** The Qt Application */ QApplication *theApp; diff --git a/slsDetectorGui/include/qServer.h b/slsDetectorGui/include/qServer.h index fbace4321..63044f6eb 100644 --- a/slsDetectorGui/include/qServer.h +++ b/slsDetectorGui/include/qServer.h @@ -11,8 +11,7 @@ /** Qt Project Class Headers */ #include "sls_detector_defs.h" #include "qDefs.h" -class qDrawPlot; -class qTabMeasurement; +class qDetectorMain; /** Project Class Headers */ class multiSlsDetector; @@ -28,7 +27,7 @@ class qServer: public virtual slsDetectorDefs{ public: /** \short The constructor */ - qServer(multiSlsDetector*& detector, qTabMeasurement* m, qDrawPlot *d); + qServer(multiSlsDetector*& detector, qDetectorMain *t); /** Destructor */ ~qServer(); @@ -39,10 +38,10 @@ public: private: /** assigns functions to the fnum enum */ - int function_table(); + int FunctionTable(); /** Decodes Function */ - int decode_function(); + int DecodeFunction(); /** Unrecognized Function */ int M_nofunc(); @@ -62,19 +61,27 @@ private: */ int StartServer(); - /** Get Detector Status */ - int get_status(); + /** Exit Server */ + int ExitServer(); + /** Get Detector Status */ + int GetStatus(); + + /** Starts Acquisition */ + int StartAcquisition(); + + /** Stops Acquisition */ + int StopsAcquisition(); + + /** Acquire - blocking */ + int Acquire(); /** The multi detector object */ multiSlsDetector *myDet; /**The measurement tab object*/ - qTabMeasurement *tab_measurement; - /**The plot widget object*/ - qDrawPlot *myPlot; - + qDetectorMain *myMainTab; /** tcp socket to gui client */ MySocketTCP *mySocket; diff --git a/slsDetectorGui/include/qTabMeasurement.h b/slsDetectorGui/include/qTabMeasurement.h index 627b71df2..34abeb458 100644 --- a/slsDetectorGui/include/qTabMeasurement.h +++ b/slsDetectorGui/include/qTabMeasurement.h @@ -48,6 +48,17 @@ public: */ void SetExpertMode(bool enable); + /** Returns the status of the Start/Stop Acquisition button + */ + bool GetStartStatus(){return btnStartStop->isChecked();}; + + /** Click the Start/Stop Acquisition button + * This is used if this command came from gui client + */ + void ClickStartStop(){btnStartStop->click();}; + + /** Returns progress bar value */ + int GetProgress(){return progressBar->value();}; public slots: diff --git a/slsDetectorGui/src/qDetectorMain.cpp b/slsDetectorGui/src/qDetectorMain.cpp index 25c42adf0..6784007a0 100644 --- a/slsDetectorGui/src/qDetectorMain.cpp +++ b/slsDetectorGui/src/qDetectorMain.cpp @@ -4,8 +4,6 @@ * ********************************************************************/ // Qt Project Class Headers #include "qDetectorMain.h" -#include "qDrawPlot.h" -#include "qTabMeasurement.h" #include "qTabDataOutput.h" #include "qTabPlot.h" #include "qTabActions.h" @@ -113,7 +111,7 @@ void qDetectorMain::SetUpWidgetWindow(){ tab_debugging = new qTabDebugging (this, myDet); cout<<"Debugging ready"<GetStartStatus() != start){ + if(start){ + //refresh all the required tabs + tab_actions->Refresh(); + tab_measurement->Refresh(); + tab_plot->Refresh(); + } + //click start/stop + tab_measurement->ClickStartStop(); + } + + if (myPlot->isRunning() == start) + ret = slsDetectorDefs::OK; + + return ret; +} + + //------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/slsDetectorGui/src/qServer.cpp b/slsDetectorGui/src/qServer.cpp index 18914db65..d9dfc89d5 100644 --- a/slsDetectorGui/src/qServer.cpp +++ b/slsDetectorGui/src/qServer.cpp @@ -6,8 +6,7 @@ */ // Qt Project Class Headers #include "qServer.h" -#include "qTabMeasurement.h" -#include "qDrawPlot.h" +#include "qDetectorMain.h" // Project Class Headers #include "slsDetector.h" #include "multiSlsDetector.h" @@ -27,10 +26,10 @@ int qServer::gui_server_thread_running(0); //------------------------------------------------------------------------------------------------------------------------------------------------- -qServer::qServer(multiSlsDetector*& detector, qTabMeasurement* m, qDrawPlot *d): - myDet(detector),tab_measurement(m),myPlot(d),mySocket(NULL),port_no(DEFAULT_GUI_PORTNO),lockStatus(0){ +qServer::qServer(multiSlsDetector*& detector, qDetectorMain *t): + myDet(detector), myMainTab(t), mySocket(NULL),port_no(DEFAULT_GUI_PORTNO),lockStatus(0){ - function_table(); + FunctionTable(); } @@ -40,8 +39,7 @@ qServer::qServer(multiSlsDetector*& detector, qTabMeasurement* m, qDrawPlot *d): qServer::~qServer(){ delete myDet; - delete tab_measurement; - delete myPlot; + delete myMainTab; if(mySocket) delete mySocket; } @@ -49,16 +47,16 @@ qServer::~qServer(){ //------------------------------------------------------------------------------------------------------------------------------------------------- -int qServer::function_table(){ +int qServer::FunctionTable(){ for (int i=0;iNUMBER_OF_FUNCTIONS-1) fnum = NUMBER_OF_FUNCTIONS-1; //calling function - (this->*flist[fnum])(); + ret = (this->*flist[fnum])(); if (ret==qDefs::FAIL) cout << "Error executing the function = " << fnum << endl; @@ -159,11 +157,13 @@ int qServer::StartStopServer(int start){ cout << "Stopping gui server thread ...." << endl; #endif gui_server_thread_running=0; - mySocket->Disconnect(); - mySocket->ShutDownSocket(); + if(mySocket) + mySocket->ShutDownSocket(); pthread_join(gui_server_thread,NULL); - delete mySocket; - mySocket = NULL; + if(mySocket){ + delete mySocket; + mySocket = NULL; + } } #ifdef VERBOSE cout << "Server stopped successfully." << endl; @@ -212,7 +212,7 @@ int qServer::StartServer(){ #ifdef VERYVERBOSE cout << "Conenction accepted" << endl; #endif - ret = decode_function(); + ret = DecodeFunction(); #ifdef VERYVERBOSE cout << "function executed" << endl; #endif @@ -226,6 +226,13 @@ int qServer::StartServer(){ cout << "Stopped gui server thread" << endl; #endif gui_server_thread_running = 0; + //delete socket(via exit server) + if(mySocket){ + delete mySocket; + mySocket = NULL; + } + //uncheck the server in modes(via exit server) + myMainTab->GuiServerExited(); return qDefs::OK; } @@ -233,20 +240,24 @@ int qServer::StartServer(){ //------------------------------------------------------------------------------------------------------------------------------------------------- -int qServer::get_status(){ +int qServer::GetStatus(){ int ret = qDefs::OK; enum slsDetectorDefs::runStatus retval; + int progress = 0; // execute action if the arguments correctly arrived - if(myPlot->isRunning()) + if(myMainTab->isPlotRunning()) retval = slsDetectorDefs::RUNNING; else retval = slsDetectorDefs::IDLE; + progress = myMainTab->GetProgress(); + // send answer mySocket->SendDataOnly(&ret,sizeof(ret)); mySocket->SendDataOnly(&retval,sizeof(retval)); + mySocket->SendDataOnly(&progress,sizeof(progress)); //return ok/fail return ret; } @@ -254,3 +265,70 @@ int qServer::get_status(){ //------------------------------------------------------------------------------------------------------------------------------------------ + +int qServer::StartAcquisition(){ + + strcpy(mess,"Could not start acquisition in gui. \n"); + + int ret = myMainTab->StartStopAcquisitionFromClient(true); + mySocket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + mySocket->SendDataOnly(mess,sizeof(mess)); + + return ret; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------ + + +int qServer::StopsAcquisition(){ + + strcpy(mess,"Could not stop acquisition in gui. \n"); + + int ret = myMainTab->StartStopAcquisitionFromClient(false); + mySocket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + mySocket->SendDataOnly(mess,sizeof(mess)); + + return ret; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------ + + +int qServer::Acquire(){ + + strcpy(mess,"Could not start blocking acquisition in gui. \n"); + + int ret = myMainTab->StartStopAcquisitionFromClient(true); + + if(ret == OK) + while(myMainTab->isPlotRunning()); + + mySocket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + mySocket->SendDataOnly(mess,sizeof(mess)); + + return ret; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------ + + +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; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------ diff --git a/slsDetectorGui/src/qTabMeasurement.cpp b/slsDetectorGui/src/qTabMeasurement.cpp index effab7cca..cecc5db7e 100644 --- a/slsDetectorGui/src/qTabMeasurement.cpp +++ b/slsDetectorGui/src/qTabMeasurement.cpp @@ -324,25 +324,6 @@ void qTabMeasurement::startStopAcquisition(){ } -//------------------------------------------------------------------------------------------------------------------------------------------------- - -/* -int qTabMeasurement::GetAcquisitionStarted(){ - - cout<<" acquisition started call back"<isChecked()){cout<<" hasnt started, so from client"<SetAcqFromClient(); - btnStartStop->click(); - //wait till the gui is ready to execute the acquire(), but doesnt execute it - //acqFromClient is reset when it is ready - while(myPlot->GetAcqFromClient()); - }else - cout<<" has started, so from gui"<