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
This commit is contained in:
l_maliakal_d 2013-03-04 15:27:57 +00:00
parent 863b662c6b
commit 22b51032dd
8 changed files with 269 additions and 71 deletions

View File

@ -11,6 +11,7 @@
#include "slsDetectorBase.h"
// C++ Include Headers
#include <iostream>
#include <sstream>
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;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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:

View File

@ -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"<<endl;
tab_developer = new qTabDeveloper (this, myDet); cout<<"Developer ready"<<endl;
myServer = new qServer(myDet, tab_measurement, myPlot);
myServer = new qServer(myDet, this);
// creating the scroll area widgets for the tabs
for(int i=0;i<NumberOfTabs;i++){
@ -719,4 +717,31 @@ void qDetectorMain::SetZoomToolTip(bool disable){
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
int qDetectorMain::StartStopAcquisitionFromClient(bool start){
#ifdef VERBOSE
cout << "Start/Stop Acquisition From Client:" << start << endl;
#endif
int ret = slsDetectorDefs::FAIL;
if (tab_measurement->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;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------

View File

@ -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;i<NUMBER_OF_FUNCTIONS;i++)
flist[i]=&qServer::M_nofunc;
flist[F_GET_RUN_STATUS] = &qServer::get_status;
flist[F_START_ACQUISITION] = &qServer::get_status;
flist[F_STOP_ACQUISITION] = &qServer::get_status;
flist[F_START_AND_READ_ALL] = &qServer::get_status;
flist[F_EXIT_SERVER] = &qServer::get_status;
flist[F_GET_RUN_STATUS] = &qServer::GetStatus;
flist[F_START_ACQUISITION] = &qServer::StartAcquisition;
flist[F_STOP_ACQUISITION] = &qServer::StopsAcquisition;
flist[F_START_AND_READ_ALL] = &qServer::Acquire;
flist[F_EXIT_SERVER] = &qServer::ExitServer;
return qDefs::OK;
}
@ -67,7 +65,7 @@ int qServer::function_table(){
//------------------------------------------------------------------------------------------------------------------------------------------
int qServer::decode_function(){
int qServer::DecodeFunction(){
int ret = qDefs::FAIL;
int n,fnum;
#ifdef VERYVERBOSE
@ -92,7 +90,7 @@ int qServer::decode_function(){
if (fnum<0 || fnum>NUMBER_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;
}
//------------------------------------------------------------------------------------------------------------------------------------------

View File

@ -324,25 +324,6 @@ void qTabMeasurement::startStopAcquisition(){
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
/*
int qTabMeasurement::GetAcquisitionStarted(){
cout<<" acquisition started call back"<<endl;
//if the acquisition started from client, the button wont be checked
if(!btnStartStop->isChecked()){cout<<" hasnt started, so from client"<<endl;
myPlot->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"<<endl;
return 0;
}
*/
//-------------------------------------------------------------------------------------------------------------------------------------------------