From 863b662c6b5d7590fbc465fabf1babb9c977b7bf Mon Sep 17 00:00:00 2001 From: l_maliakal_d Date: Fri, 1 Mar 2013 13:21:07 +0000 Subject: [PATCH] gui client works for just getting status git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorGui@149 af1100a4-978c-4157-bff7-07162d2ba061 --- slsDetectorGui/Makefile | 3 +- slsDetectorGui/client/qClient.cpp | 103 +++++++++ slsDetectorGui/client/qClient.h | 46 ++++ slsDetectorGui/forms/form_detectormain.ui | 9 + slsDetectorGui/include/qDefs.h | 1 + slsDetectorGui/include/qDetectorMain.h | 4 + slsDetectorGui/include/qDrawPlot.h | 4 +- slsDetectorGui/include/qServer.h | 106 +++++++++ slsDetectorGui/include/qTabMeasurement.h | 2 - slsDetectorGui/include/svnInfoGui.h | 4 +- slsDetectorGui/slsDetectorGui.pro | 6 +- slsDetectorGui/src/qDetectorMain.cpp | 11 +- slsDetectorGui/src/qDrawPlot.cpp | 6 +- slsDetectorGui/src/qServer.cpp | 256 ++++++++++++++++++++++ slsDetectorGui/src/qTabMeasurement.cpp | 20 ++ 15 files changed, 564 insertions(+), 17 deletions(-) create mode 100644 slsDetectorGui/client/qClient.cpp create mode 100644 slsDetectorGui/client/qClient.h create mode 100644 slsDetectorGui/include/qServer.h create mode 100644 slsDetectorGui/src/qServer.cpp diff --git a/slsDetectorGui/Makefile b/slsDetectorGui/Makefile index 08268dd93..d1ff906aa 100644 --- a/slsDetectorGui/Makefile +++ b/slsDetectorGui/Makefile @@ -6,14 +6,13 @@ DOCDIR?=docs LIBDIR?=../slsDetectorSoftware -all: $(PROG) Makefile.gui +all: $(PROG) Makefile.gui client clean: if test -e Makefile.gui; then make -f Makefile.gui clean; make -f Makefile.gui mocclean; rm Makefile.gui; else make Makefile.gui; make -f Makefile.gui clean; make -f Makefile.gui mocclean; fi # cd manual && make clean - Makefile.gui: mm mm: diff --git a/slsDetectorGui/client/qClient.cpp b/slsDetectorGui/client/qClient.cpp new file mode 100644 index 000000000..ba6bce2e2 --- /dev/null +++ b/slsDetectorGui/client/qClient.cpp @@ -0,0 +1,103 @@ +/* + * qClient.cpp + * + * Created on: Feb 27, 2013 + * Author: Dhanya Maliakal + */ +// Qt Project Class Headers +#include "qClient.h" +// Project Class Headers +#include "MySocketTCP.h" +#include "slsDetectorBase.h" +// C++ Include Headers +#include +using namespace std; + + + + +int main(int argc, char *argv[]) + +{ + qClient *cl =new qClient(argv[1]); + cl->executeLine(argc-2, argv+2); + + delete cl; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +qClient::qClient(char* hostname){ + //create socket + mySocket = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO); + if (mySocket->getErrorStatus()){ + cout << "Error: could not connect to host:" << hostname << " with port " << DEFAULT_GUI_PORTNO << endl; + delete mySocket; + exit(-1); + } +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + +int qClient::executeLine(int narg, char *args[]){ + + char arg[MAX_STR_LENGTH] = ""; + int iarg = -1; + char answer[100]; + string retval = ""; + string cmd = args[0]; + + + //validate command structure + if(narg<1){ + cout << "Error: no command parsed" << endl; + return slsDetectorDefs::FAIL; + } + + + //file name + if (cmd == "status"){ + retval = getStatus(); + } + + + //unrecognized command + else{ + cout << "Error: unrecognized command" << endl; + return slsDetectorDefs::FAIL; + } + + + //print result + cout << cmd << ": " << retval << endl; + + return slsDetectorDefs::OK; +} + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +string qClient::getStatus(){ + int fnum = slsDetectorDefs::F_GET_RUN_STATUS; + int ret = slsDetectorDefs::FAIL; + int retval = -1; + slsDetectorDefs::runStatus s=slsDetectorDefs::ERROR; + + if (mySocket->Connect() >= 0) { + mySocket->SendDataOnly(&fnum,sizeof(fnum)); + mySocket->ReceiveDataOnly(&ret,sizeof(ret)); + mySocket->ReceiveDataOnly(&retval,sizeof(retval)); + } + mySocket->Disconnect(); + + if(retval==-1) + retval=slsDetectorDefs::ERROR; + + return slsDetectorBase::runStatusType((slsDetectorDefs::runStatus)retval); +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/slsDetectorGui/client/qClient.h b/slsDetectorGui/client/qClient.h new file mode 100644 index 000000000..8e91b0c34 --- /dev/null +++ b/slsDetectorGui/client/qClient.h @@ -0,0 +1,46 @@ +/* + * qClient.h + * + * Created on: Feb 27, 2013 + * Author: Dhanya Maliakal + */ +#ifndef QCLIENT_H +#define QCLIENT_H + + +/** Qt Project Class Headers */ +//#include "qDefs.h" +/** Project Class Headers */ +class MySocketTCP; +#include "sls_detector_defs.h" +/** C++ Include Headers */ +#include +#include +using namespace std; + +/** + *@short Sets up the gui server + */ +class qClient: public virtual slsDetectorDefs{ + + +public: + /** \short The constructor*/ + qClient(char* hostname); + /** Destructor */ + virtual ~qClient(){}; + + /**Execute command*/ + int executeLine(int narg, char *args[]); + +private: + /** Gets run status */ + string getStatus(); + + /** client socket */ + MySocketTCP *mySocket; +}; + + + +#endif /* QCLIENT_H */ diff --git a/slsDetectorGui/forms/form_detectormain.ui b/slsDetectorGui/forms/form_detectormain.ui index 60a1a4afe..4874af640 100644 --- a/slsDetectorGui/forms/form_detectormain.ui +++ b/slsDetectorGui/forms/form_detectormain.ui @@ -243,6 +243,7 @@ + @@ -503,6 +504,14 @@ p, li { white-space: pre-wrap; } Save C&alibration + + + true + + + Listen to Gui Client + + diff --git a/slsDetectorGui/include/qDefs.h b/slsDetectorGui/include/qDefs.h index 919ca26f7..1ed8ff2a4 100644 --- a/slsDetectorGui/include/qDefs.h +++ b/slsDetectorGui/include/qDefs.h @@ -28,6 +28,7 @@ public: static const int64_t GUI_VERSION=0x20121213; +#define GOODBYE -200 //------------------------------------------------------------------------------------------------------------------------------------------------- enum{ diff --git a/slsDetectorGui/include/qDetectorMain.h b/slsDetectorGui/include/qDetectorMain.h index cd0060775..a948b4b33 100644 --- a/slsDetectorGui/include/qDetectorMain.h +++ b/slsDetectorGui/include/qDetectorMain.h @@ -22,6 +22,7 @@ class qTabSettings; class qTabDebugging; class qTabDeveloper; class qTabMessages; +class qServer; /** Project Class Headers */ class multiSlsDetector; /** Qt Include Headers */ @@ -110,6 +111,9 @@ private: /**Messages tab */ qTabMessages *tab_messages; + /** server object*/ + qServer *myServer; + /**if the developer tab should be enabled,known from command line */ int isDeveloper; diff --git a/slsDetectorGui/include/qDrawPlot.h b/slsDetectorGui/include/qDrawPlot.h index c0dd76368..5206cebc4 100644 --- a/slsDetectorGui/include/qDrawPlot.h +++ b/slsDetectorGui/include/qDrawPlot.h @@ -2,7 +2,7 @@ * qDrawPlot.h * * Created on: May 7, 2012 - * Author: Ian Johnson + * Author: Dhanya Maliakal */ #ifndef QDRAWPLOT_H #define QDRAWPLOT_H @@ -117,8 +117,6 @@ public: int UpdateTrimbitPlot(bool fromDetector,bool Histogram); - - public slots: /** To select 1D or 2D plot @param i is 1 for 1D, else 2D plot */ diff --git a/slsDetectorGui/include/qServer.h b/slsDetectorGui/include/qServer.h new file mode 100644 index 000000000..fbace4321 --- /dev/null +++ b/slsDetectorGui/include/qServer.h @@ -0,0 +1,106 @@ +/* + * qServer.h.h + * + * Created on: Feb 27, 2013 + * Author: Dhanya Maliakal + */ +#ifndef QSERVER_H +#define QSERVER_H + + +/** Qt Project Class Headers */ +#include "sls_detector_defs.h" +#include "qDefs.h" +class qDrawPlot; +class qTabMeasurement; + +/** Project Class Headers */ +class multiSlsDetector; +class MySocketTCP; +/** C++ Include Headers */ + + +/** + *@short Sets up the gui server + */ +class qServer: public virtual slsDetectorDefs{ + + +public: + /** \short The constructor */ + qServer(multiSlsDetector*& detector, qTabMeasurement* m, qDrawPlot *d); + /** Destructor */ + ~qServer(); + + /** Start or Stop Gui Server + * @param start is 1 to start and 0 to stop + */ + int StartStopServer(int start); + +private: + /** assigns functions to the fnum enum */ + int function_table(); + + /** Decodes Function */ + int decode_function(); + + /** Unrecognized Function */ + int M_nofunc(); + + + /** + * Static function - Thread started which listens to client gui. + * Called by StartStopServer() + * @param this_pointer pointer to this object + */ + static void* StartServerThread(void *this_pointer); + + /** + * Thread started which listens to client gui. + * Called by startServerThread() + * + */ + int StartServer(); + + /** Get Detector Status */ + int get_status(); + + + + + /** The multi detector object */ + multiSlsDetector *myDet; + /**The measurement tab object*/ + qTabMeasurement *tab_measurement; + /**The plot widget object*/ + qDrawPlot *myPlot; + + + /** tcp socket to gui client */ + MySocketTCP *mySocket; + /** server port number*/ + int port_no; + /** Lock Status if server locked to a client */ + int lockStatus; + + /** Function List */ + static const int NUMBER_OF_FUNCTIONS = 256; + int (qServer::*flist[NUMBER_OF_FUNCTIONS])(); + + + /** if the gui server thread is running*/ + static int gui_server_thread_running; + /** thread listening to gui client*/ + pthread_t gui_server_thread; + + /** server started */ + int checkStarted; + + /** Message */ + char mess[MAX_STR_LENGTH]; + +}; + + + +#endif /* QSERVER_H */ diff --git a/slsDetectorGui/include/qTabMeasurement.h b/slsDetectorGui/include/qTabMeasurement.h index bd55b9baf..627b71df2 100644 --- a/slsDetectorGui/include/qTabMeasurement.h +++ b/slsDetectorGui/include/qTabMeasurement.h @@ -91,7 +91,6 @@ private: void Enable(bool enable); - private slots: /** Sets the timing mode * @ param mode cane be None, Auto, Gated, Trigger Exposure Series, @@ -156,7 +155,6 @@ private slots: void EnableFileWrite(bool enable); - private: /** The sls detector object */ multiSlsDetector *myDet; diff --git a/slsDetectorGui/include/svnInfoGui.h b/slsDetectorGui/include/svnInfoGui.h index ee580d7ef..deff612b6 100644 --- a/slsDetectorGui/include/svnInfoGui.h +++ b/slsDetectorGui/include/svnInfoGui.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorGui" //#define SVNREPPATH "" #define SVNREPUUID "af1100a4-978c-4157-bff7-07162d2ba061" -//#define SVNREV 0x147 +//#define SVNREV 0x148 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTH "l_maliakal_d" -#define SVNREV 0x147 +#define SVNREV 0x148 #define SVNDATE 0x20130226 // diff --git a/slsDetectorGui/slsDetectorGui.pro b/slsDetectorGui/slsDetectorGui.pro index 8fdd6b43f..6aafb84ad 100644 --- a/slsDetectorGui/slsDetectorGui.pro +++ b/slsDetectorGui/slsDetectorGui.pro @@ -82,7 +82,8 @@ SOURCES = \ src/qTabSettings.cpp\ src/qTabDebugging.cpp\ src/qTabDeveloper.cpp\ - src/qTabMessages.cpp + src/qTabMessages.cpp\ + src/qServer.cpp HEADERS = \ slsDetectorPlotting/include/SlsQt1DPlot.h\ @@ -110,7 +111,8 @@ HEADERS = \ include/qTabDeveloper.h\ include/qTabMessages.h\ include/svnInfoGui.h\ - ../slsDetectorSoftware/commonFiles/sls_detector_defs.h + ../slsDetectorSoftware/commonFiles/sls_detector_defs.h\ + include/qServer.h FORMS = \ diff --git a/slsDetectorGui/src/qDetectorMain.cpp b/slsDetectorGui/src/qDetectorMain.cpp index 2e9c2fa2d..25c42adf0 100644 --- a/slsDetectorGui/src/qDetectorMain.cpp +++ b/slsDetectorGui/src/qDetectorMain.cpp @@ -14,6 +14,7 @@ #include "qTabDebugging.h" #include "qTabDeveloper.h" #include "qTabMessages.h" +#include "qServer.h" // Project Class Headers #include "slsDetector.h" #include "multiSlsDetector.h" @@ -112,6 +113,8 @@ void qDetectorMain::SetUpWidgetWindow(){ tab_debugging = new qTabDebugging (this, myDet); cout<<"Debugging ready"<setChecked(myServer->StartStopServer(actionListenGuiClient->isChecked())); + connect(menuModes, SIGNAL(triggered(QAction*)), this,SLOT(EnableModes(QAction*))); + } //Set DebugMode - if(action==actionDebug){ + else if(action==actionDebug){ enable = actionDebug->isChecked(); tabs->setTabEnabled(Debugging,enable); #ifdef VERBOSE diff --git a/slsDetectorGui/src/qDrawPlot.cpp b/slsDetectorGui/src/qDrawPlot.cpp index 7158cf61d..f8bbf2da9 100644 --- a/slsDetectorGui/src/qDrawPlot.cpp +++ b/slsDetectorGui/src/qDrawPlot.cpp @@ -2,7 +2,7 @@ * qDrawPlot.cpp * * Created on: May 7, 2012 - * Author: Ian Johnson + * Author: Dhanya Maliakal */ // Qt Project Class Headers #include "qDrawPlot.h" @@ -174,9 +174,6 @@ void qDrawPlot::SetupWidgetWindow(){ pedestalCount = 0; - - - //widget related initialization // clone @@ -578,7 +575,6 @@ void qDrawPlot::SetupMeasurement(){ void* qDrawPlot::DataStartAcquireThread(void *this_pointer){ ((qDrawPlot*)this_pointer)->myDet->acquire(1); - return this_pointer; } diff --git a/slsDetectorGui/src/qServer.cpp b/slsDetectorGui/src/qServer.cpp new file mode 100644 index 000000000..18914db65 --- /dev/null +++ b/slsDetectorGui/src/qServer.cpp @@ -0,0 +1,256 @@ +/* + * qServer.cpp + * + * Created on: Feb 27, 2013 + * Author: Dhanya Maliakal + */ +// Qt Project Class Headers +#include "qServer.h" +#include "qTabMeasurement.h" +#include "qDrawPlot.h" +// Project Class Headers +#include "slsDetector.h" +#include "multiSlsDetector.h" +#include "MySocketTCP.h" +// C++ Include Headers +#include +#include +using namespace std; + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +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){ + + function_table(); + +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +qServer::~qServer(){ + delete myDet; + delete tab_measurement; + delete myPlot; + if(mySocket) delete mySocket; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +int qServer::function_table(){ + + for (int i=0;iReceiveDataOnly(&fnum,sizeof(fnum)); + if (n <= 0) { +#ifdef VERYVERBOSE + cout << "ERROR reading from socket " << n << ", " << fnum << endl; +#endif + return qDefs::FAIL; + } +#ifdef VERYVERBOSE + else + cout << "size of data received " << n <NUMBER_OF_FUNCTIONS-1) + fnum = NUMBER_OF_FUNCTIONS-1; + //calling function + (this->*flist[fnum])(); + if (ret==qDefs::FAIL) + cout << "Error executing the function = " << fnum << endl; + + return ret; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------ + + +int qServer::M_nofunc(){ + + int ret = qDefs::FAIL; + sprintf(mess,"Unrecognized Function\n"); + cout << mess << endl; + + mySocket->SendDataOnly(&ret,sizeof(ret)); + mySocket->SendDataOnly(mess,sizeof(mess)); + + return GOODBYE; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +int qServer::StartStopServer(int start){ + + //start server + if(start){ +#ifdef VERBOSE + cout << endl << "Starting Gui Server" << endl; +#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 + 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; + } + while(checkStarted); + checkStarted=0; +#ifdef VERBOSE + if(gui_server_thread_running) + cout << "Server thread created successfully." << endl; +#endif + } + } + + //stop server + else{ +#ifdef VERBOSE + cout << "Stopping Gui Server" << endl; +#endif + + if(gui_server_thread_running){ +#ifdef VERBOSE + cout << "Stopping gui server thread ...." << endl; +#endif + gui_server_thread_running=0; + mySocket->Disconnect(); + mySocket->ShutDownSocket(); + pthread_join(gui_server_thread,NULL); + delete mySocket; + mySocket = NULL; + } +#ifdef VERBOSE + cout << "Server stopped successfully." << endl; +#endif + } + + + return gui_server_thread_running; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +void* qServer::StartServerThread(void* this_pointer){ + ((qServer*)this_pointer)->StartServer(); + return this_pointer; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + + +int qServer::StartServer(){ +#ifdef VERYVERBOSE + cout << "In StartServer()\n"); +#endif + int ret = qDefs::OK; + + mySocket = new MySocketTCP(port_no); + if (mySocket->getErrorStatus()){ + gui_server_thread_running = 0; + qDefs::Message(qDefs::WARNING,"Could not start gui server socket","Server"); + } + checkStarted = 0; + + while ((gui_server_thread_running) && (ret!=GOODBYE)) { +#ifdef VERBOSE + cout<< endl; +#endif +#ifdef VERYVERBOSE + cout << "Waiting for client call" << endl; +#endif + if(mySocket->Connect()>=0){ +#ifdef VERYVERBOSE + cout << "Conenction accepted" << endl; +#endif + ret = decode_function(); +#ifdef VERYVERBOSE + cout << "function executed" << endl; +#endif + mySocket->Disconnect(); +#ifdef VERYVERBOSE + cout << "connection closed" << endl; +#endif + } + } +#ifdef VERBOSE + cout << "Stopped gui server thread" << endl; +#endif + gui_server_thread_running = 0; + return qDefs::OK; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------- + + +int qServer::get_status(){ + + int ret = qDefs::OK; + enum slsDetectorDefs::runStatus retval; + + // execute action if the arguments correctly arrived + if(myPlot->isRunning()) + retval = slsDetectorDefs::RUNNING; + else + retval = slsDetectorDefs::IDLE; + + // send answer + mySocket->SendDataOnly(&ret,sizeof(ret)); + mySocket->SendDataOnly(&retval,sizeof(retval)); + //return ok/fail + return ret; +} + + +//------------------------------------------------------------------------------------------------------------------------------------------ + diff --git a/slsDetectorGui/src/qTabMeasurement.cpp b/slsDetectorGui/src/qTabMeasurement.cpp index fcb07e505..effab7cca 100644 --- a/slsDetectorGui/src/qTabMeasurement.cpp +++ b/slsDetectorGui/src/qTabMeasurement.cpp @@ -83,6 +83,7 @@ void qTabMeasurement::SetupWidgetWindow(){ iconStart = new QIcon(":/icons/images/start.png"); iconStop = new QIcon(":/icons/images/stop.png"); + } @@ -323,6 +324,25 @@ 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"<