From 3c94d2cb4008e552680609bbfedc012d53c2ec2d Mon Sep 17 00:00:00 2001 From: l_maliakal_d Date: Mon, 19 Nov 2012 12:41:35 +0000 Subject: [PATCH] converted to c++ git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@347 951219d9-93cf-4727-9268-0efd64621fa3 --- slsDetectorSoftware/slsReceiver/Makefile | 38 +- .../slsReceiver/receiver_defs.h | 15 + .../slsReceiver/slsReceiver.cpp | 56 ++ .../slsReceiver/slsReceiverFunctionList.cpp | 329 ++++++++ .../slsReceiver/slsReceiverFunctionList.h | 224 ++++- .../slsReceiver/slsReceiver_funcs.cpp | 792 ++++++++++++++++++ .../slsReceiver/slsReceiver_funcs.h | 132 ++- 7 files changed, 1515 insertions(+), 71 deletions(-) create mode 100755 slsDetectorSoftware/slsReceiver/receiver_defs.h create mode 100644 slsDetectorSoftware/slsReceiver/slsReceiver.cpp create mode 100644 slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp create mode 100644 slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp diff --git a/slsDetectorSoftware/slsReceiver/Makefile b/slsDetectorSoftware/slsReceiver/Makefile index a4eb637e8..d2e83e968 100644 --- a/slsDetectorSoftware/slsReceiver/Makefile +++ b/slsDetectorSoftware/slsReceiver/Makefile @@ -1,36 +1,28 @@ -CFLAGS+= -Wall -DC_ONLY -DSLS_RECEIVER_FUNCTION_LIST -DGOTTHARDD #-DVERBOSE -LDLIBS+= -lm -lstdc++ -lpthread -PROGS= slsReceiver -INSTDIR= bin -INSTMODE= 0777 +CC = g++ +CLAGS += -DSLS_RECEIVER_FUNCTION_LIST -DGOTTHARDD +LDLIBS += -lm -lstdc++ -lpthread -SRC_CLNT = slsReceiver.c communication_funcs.c slsReceiverFunctionList.c slsReceiver_funcs.c -OBJS = $(SRC_CLNT:.c=.o) +INCLUDES = -I ../MySocketTCP -I ../commonFiles -I . +SRC_CLNT = slsReceiver.cpp ../MySocketTCP/MySocketTCP.cpp slsReceiver_funcs.cpp slsReceiverFunctionList.cpp + +PROGS = slsReceiver +INSTDIR = bin +INSTMODE = 0777 + +OBJS = $(SRC_CLNT:.cpp=.o) -all: clean $(PROGS) +all: clean $(PROGS) boot: $(OBJS) -#doc: $(SRC_CLNT) -# doxygen doxy.config - -#%.o : %.c %.h -# $(CXX) -Wall -o $@ -c $< $(INCLUDES) $(CFLAGS) $(FLAGS) $(LDLIBS) -fPIC - - - $(PROGS): echo $(OBJS) mkdir -p $(INSTDIR) - $(CC) $(SRC_CLNT) $(LDFLAGS) $(LDLIBS) $(CFLAGS) -o $@ + gcc $(SRC_CLNT) $(INCLUDES) $(CLAGS) $(LDLIBS) -o $@ mv $(PROGS) $(INSTDIR) - - + + clean: rm -rf $(PROGS) *.o $(INSTDIR)/* - - - - diff --git a/slsDetectorSoftware/slsReceiver/receiver_defs.h b/slsDetectorSoftware/slsReceiver/receiver_defs.h new file mode 100755 index 000000000..1ea15c06a --- /dev/null +++ b/slsDetectorSoftware/slsReceiver/receiver_defs.h @@ -0,0 +1,15 @@ +#ifndef RECEIVER_DEFS_H +#define RECEIVER_DEFS_H + +#include "sls_detector_defs.h" + +#include + +#define GOODBYE -200 +#define HALF_BUFFER_SIZE 1286 +#define DEFAULT_UDP_PORT 50001 + + + +//#define THIS_SOFTWARE_VERSION 0x20120919 +#endif diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp new file mode 100644 index 000000000..d5880f707 --- /dev/null +++ b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp @@ -0,0 +1,56 @@ +/* A simple server in the internet domain using TCP + The port number is passed as an argument */ + +#include "receiver_defs.h" +#include "MySocketTCP.h" +#include "slsReceiver_funcs.h" + +#include +using namespace std; + + + +int main(int argc, char *argv[]) +{ + int portno = DEFAULT_PORTNO+2; + int retval = slsDetectorDefs::OK; + + MySocketTCP *socket = new MySocketTCP(portno); + if (socket->getErrorStatus()) + return -1; + + //assign function table + slsReceiverFuncs *receiver = new slsReceiverFuncs(socket); +#ifdef VERBOSE + cout << "Function table assigned.\n Ready..." << endl; +#endif + + //waits for connection + while(retval!=GOODBYE) { +#ifdef VERBOSE + cout<< endl; +#endif +#ifdef VERY_VERBOSE + cout << "Waiting for client call" << endl; +#endif + if(socket->Connect()>=0){ +#ifdef VERY_VERBOSE + cout << "Conenction accepted" << endl; +#endif + retval = receiver->decode_function(); +#ifdef VERY_VERBOSE + cout << "function executed" << endl; +#endif + socket->Disconnect(); +#ifdef VERY_VERBOSE + cout << "connection closed" << endl; +#endif + } + } + + delete socket; + cout << "Goodbye!" << endl; + + return 0; +} + diff --git a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp new file mode 100644 index 000000000..2bfd1a063 --- /dev/null +++ b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp @@ -0,0 +1,329 @@ +#ifdef SLS_RECEIVER_FUNCTION_LIST +/********************************************//** + * @file slsReceiverFunctionList.cpp + * @short does all the functions for a receiver, set/get parameters, start/stop etc. + ***********************************************/ + + +#include "slsReceiverFunctionList.h" + + + +#include // SIGINT +#include // stat +#include // socket(), bind(), listen(), accept(), shut down +#include // sock_addr_in, htonl, INADDR_ANY +#include // exit() + + +#include +#include +using namespace std; + +FILE* slsReceiverFunctionList::sfilefd(NULL); +int slsReceiverFunctionList::listening_thread_running(0); + +slsReceiverFunctionList::slsReceiverFunctionList(): + fileIndex(0), + frameIndexNeeded(true), + framesCaught(0), + startFrameIndex(-1), + frameIndex(0), + totalFramesCaught(0), + startAcquisitionIndex(-1), + acquisitionIndex(0), + framesInFile(0), + udpSocket(-1), + status(IDLE), + server_port(DEFAULT_UDP_PORT) +{ + strcpy(savefilename,""); + strcpy(filePath,""); + strcpy(fileName,"run"); + strcpy(buffer,""); + +} + + + +int slsReceiverFunctionList::getFrameIndex(){ + if(startFrameIndex==-1) + frameIndex=0; + else + frameIndex=((int)(*((int*)buffer)) - startFrameIndex)/2; + return frameIndex; +} + + + +int slsReceiverFunctionList::getAcquisitionIndex(){ + if(startAcquisitionIndex==-1) + acquisitionIndex=0; + else + acquisitionIndex=((int)(*((int*)buffer)) - startAcquisitionIndex)/2; + return acquisitionIndex; +} + + + +char* slsReceiverFunctionList::setFileName(char c[]){ + if(strlen(c)) + strcpy(fileName,c); + + return getFileName(); +} + + + +char* slsReceiverFunctionList::setFilePath(char c[]){ + if(strlen(c)){ + //check if filepath exists + struct stat st; + if(stat(c,&st) == 0) + strcpy(filePath,c); + } + return getFilePath(); +} + + + +int slsReceiverFunctionList::setFileIndex(int i){ + if(i>=0) + fileIndex = i; + + return getFileIndex(); +} + + +bool slsReceiverFunctionList::resetTotalFramesCaught(bool i){ + frameIndexNeeded = i; + startAcquisitionIndex = -1; + totalFramesCaught = 0; + + return frameIndexNeeded; +} + + + +void slsReceiverFunctionList::closeFile(int p){ + if(listening_thread_running){ + cout << "Closing file and Exiting." << endl; + fclose(sfilefd); + } + exit(0); +} + + + + + +int slsReceiverFunctionList::startReceiver(){ +#ifdef VERBOSE + cout << "Starting Receiver" << endl; +#endif + int err = 0; + if(!listening_thread_running){ + printf("Starting new acquisition threadddd ....\n"); + listening_thread_running=1; + err = pthread_create(&listening_thread, NULL,startListeningThread, (void*) this); + if(!err){ + while(status!=RUNNING); + cout << endl << "Thread created successfully." << endl; + }else{ + listening_thread_running=0; + status = IDLE; + cout << endl << "Cant create thread. Status:" << status << endl; + return FAIL; + } + } + + return OK; +} + + + + + +int slsReceiverFunctionList::stopReceiver(){ +#ifdef VERBOSE + cout << "Stopping Receiver" << endl; +#endif + + if(listening_thread_running){ + cout << "Stopping new acquisition threadddd ...." << endl; + //stop thread + listening_thread_running=0; + if (udpSocket != -1) + shutdown(udpSocket, SHUT_RDWR); + pthread_join(listening_thread,NULL); + status = IDLE; + } + cout << "Status:" << status << endl; + + return OK; +} + + + +void* slsReceiverFunctionList::startListeningThread(void* this_pointer){ + ((slsReceiverFunctionList*)this_pointer)->startListening(); + + return this_pointer; +} + + + +int slsReceiverFunctionList::startListening(){ +#ifdef VERYVERBOSE + cout << "In startListening()\n"); +#endif + + // Variable and structure definitions + /*udpSocket = -1;*/ + int rc1, rc2, rc; + int currframenum, prevframenum; + struct sockaddr_in serveraddr; + struct sockaddr_in clientaddr; + socklen_t clientaddrlen = sizeof(clientaddr); + + //reset variables for each acquisition + framesInFile=0; + framesCaught=0; + startFrameIndex=-1; + frameIndex=0; + + + //Catch signal SIGINT to close files properly + signal(SIGINT,closeFile); + + + //create file name + if(!frameIndexNeeded) + sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex); + else + sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex); + + + // A do/while(FALSE) loop is used to make error cleanup easier. The + // close() of each of the socket descriptors is only done once at the + // very end of the program. + do { + // The socket() function returns a socket descriptor, which represents + // an endpoint. The statement also identifies that the INET + // (Internet Protocol) address family with the UDP transport + // (SOCK_DGRAM) will be used for this socket. + udpSocket = socket(AF_INET, SOCK_DGRAM, 0); + if (udpSocket < 0) { + cerr << "socket() failed" << endl; + break; + } + + + // After the socket descriptor is created, a bind() function gets a + // unique name for the socket. In this example, the user sets the + // s_addr to zero, which means that the UDP port of 3555 will be + // bound to all IP addresses on the system. + + memset(&serveraddr, 0, sizeof(serveraddr)); + serveraddr.sin_family = AF_INET; + serveraddr.sin_port = htons(server_port); + //serveraddr.sin_addr.s_addr = inet_addr(server_ip); + serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); + + rc = bind(udpSocket, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); + if (rc < 0) { + cerr << "bind() failed" << endl; + break; + } + + + sfilefd = fopen((const char *) (savefilename), "w"); + cout << "Saving to " << savefilename << ". Ready! " << endl; + + while (listening_thread_running) { + + // The server uses the recvfrom() function to receive that data. + // The recvfrom() function waits indefinitely for data to arrive. + if (framesInFile == 20000) { + fclose(sfilefd); + + currframenum=(int)(*((int*)buffer)); + //create file name + if(!frameIndexNeeded) + sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex); + else + sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex); + + cout << "saving to " << savefilename << "\t\t" + "packet loss " << ((currframenum-prevframenum-(2*framesInFile))/(double)(2*framesInFile))*100.000 << "%\t\t" + "framenum " << currframenum << endl; + sfilefd = fopen((const char *) (savefilename), "w"); + prevframenum=currframenum; + framesInFile = 0; + } + status = RUNNING; + + rc1 = recvfrom(udpSocket, buffer, HALF_BUFFER_SIZE, 0, + (struct sockaddr *) &clientaddr, &clientaddrlen); + //cout << "rc1 done" << endl; + rc2 = recvfrom(udpSocket, buffer+HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, 0, + (struct sockaddr *) &clientaddr, &clientaddrlen); + + + //for each scan + if(startFrameIndex==-1){ + startFrameIndex=(int)(*((int*)buffer))-2; + prevframenum=startFrameIndex; + } + //start of acquisition + if(startAcquisitionIndex==-1) + startAcquisitionIndex=startFrameIndex; + + //cout << "rc2 done" << endl; + if ((rc1 < 0) || (rc2 < 0)) { + perror("recvfrom() failed"); + break; + } + + //so that it doesnt write the last frame twice + if(listening_thread_running){ + fwrite(buffer, 1, rc1, sfilefd); + fwrite(buffer+HALF_BUFFER_SIZE, 1, rc2, sfilefd); + framesInFile++; + framesCaught++; + totalFramesCaught++; + //cout << "saving" << endl; + } + } + } while (listening_thread_running); + listening_thread_running=0; + status = IDLE; + + //Close down any open socket descriptors + if (udpSocket != -1){ + cout << "Closing udpSocket" << endl; + close(udpSocket); + } + + //close file + fclose(sfilefd); + cout << "sfield:" << (int)sfilefd << endl; + + return 0; +} + + + + + + + + +char* slsReceiverFunctionList::readFrame(char* c){ + strcpy(c,savefilename); + return buffer; +} + + +#endif + diff --git a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h index 83bbf1330..1355c981d 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h +++ b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h @@ -1,35 +1,221 @@ #ifdef SLS_RECEIVER_FUNCTION_LIST +/********************************************//** + * @file slsReceiverFunctionList.h + * @short does all the functions for a receiver, set/get parameters, start/stop etc. + ***********************************************/ + + +#include "sls_detector_defs.h" +#include "receiver_defs.h" + +#include +#include + +/** + * @short does all the functions for a receiver, set/get parameters, start/stop etc. + */ + +class slsReceiverFunctionList : private virtual slsDetectorDefs { + +public: + /** + * Constructor + */ + slsReceiverFunctionList(); + + /** + * Destructor + */ + virtual ~slsReceiverFunctionList(){}; + + /** + * Returns status of receiver: idle, running or error + */ + runStatus getStatus(){return status;}; + + /** + * Returns File Name + */ + char* getFileName(){return fileName;}; + + /** + * Returns File Path + */ + char* getFilePath(){return filePath;}; + + /** + * Returns File Index + */ + int getFileIndex(){return fileIndex;}; + + /** + * Returns Frames Caught for each real time acquisition (eg. for each scan) + */ + int getFramesCaught(){return framesCaught;}; + + /** + * Returns Total Frames Caught for an entire acquisition (including all scans) + */ + int getTotalFramesCaught(){ return totalFramesCaught;}; + + /** + * Returns the frame index at start of each real time acquisition (eg. for each scan) + */ + int getStartFrameIndex(){return startFrameIndex;}; + + /** + * Returns current Frame Index for each real time acquisition (eg. for each scan) + */ + int getFrameIndex(); + + /** + * Returns current Frame Index Caught for an entire acquisition (including all scans) + */ + int getAcquisitionIndex(); + + /** + * Set File Name (without frame index, file index and extension) + * @param c file name + */ + char* setFileName(char c[]); + + /** + * Set File Path + * @param c file path + */ + char* setFilePath(char c[]); + + /** + * Set File Index + * @param i file index + */ + int setFileIndex(int i); + + /** + * Resets the Total Frames Caught + * This is how the receiver differentiates between entire acquisitions + * @param i true if frame index in file name is required, else false + * Returns frames needed in file name + */ + bool resetTotalFramesCaught(bool i); + + /** + * Close File + */ + static void closeFile(int p); + + /** + * Starts Receiver - starts to listen for packets + * Returns success + */ + int startReceiver(); + + /** + * Stops Receiver - stops listening for packets + * Returns success + */ + int stopReceiver(); + + /** + * Static function - Thread started which listens to packets. + * Called by startReceiver() + * @param this_pointer pointer to this object + */ + static void* startListeningThread(void *this_pointer); + + /** + * Thread started which listens to packets. + * Called by startReceiver() + * + */ + int startListening(); + + /** + * Returns the buffer-current frame read by receiver + */ + char* readFrame(char* c); + + + +private: + /** Complete File name */ + char savefilename[MAX_STR_LENGTH]; + + /** File Name without frame index, file index and extension*/ + char fileName[MAX_STR_LENGTH]; + + /** File Path */ + char filePath[MAX_STR_LENGTH]; + + /** File Index */ + int fileIndex; + + /** if frame index required in file name */ + bool frameIndexNeeded; + + /** Frames Caught for each real time acquisition (eg. for each scan) */ + int framesCaught; + + /** Frame index at start of each real time acquisition (eg. for each scan) */ + int startFrameIndex; + + /** Actual current frame index of each time acquisition (eg. for each scan) */ + int frameIndex; + + /** Total Frames Caught for an entire acquisition (including all scans) */ + int totalFramesCaught; + + /** Frame index at start of an entire acquisition (including all scans) */ + int startAcquisitionIndex; + + /** Actual current frame index of an entire acquisition (including all scans) */ + int acquisitionIndex; + + /** Frames currently in current file, starts new file when it reaches max */ + int framesInFile; + + /** if the listening thread is running*/ + static int listening_thread_running; + + /** thread listening to packets */ + pthread_t listening_thread; + + /** status of receiver */ + runStatus status; + + /** File Descriptor */ + static FILE *sfilefd; + + /** UDP Socket - with server */ + int udpSocket; + + /** Receiver buffer */ + char buffer[HALF_BUFFER_SIZE*2]; + + /** Server UDP Port*/ + int server_port; + +}; +/* + + + + + + -//int initializeReceiver(); -void closeFile(); -enum runStatus getReceiverStatus(); -char* getFileName(); -char* setFileName(char fName[]); -char* getFilePath(); -char* setFilePath(char fName[]); -int getFileIndex(); -int setFileIndex(int index); -int getFrameIndex(); -int getAcquisitionIndex(); -int getFramesCaught(); -int getTotalFramesCaught(); -int resetTotalFramesCaught(int index); -void* startListening(void *arg); -int startReceiver(); -int stopReceiver(); -char* readFrame(char *fName); int getStartFrameIndex(); //int setUDPPortNumber(int p=-1); //sets/gets port number to listen to for data from the detector //int setTCPPortNumber(int p=-1); //sets/get port number for communication to client - +*/ #endif diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp new file mode 100644 index 000000000..a1e9c1579 --- /dev/null +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp @@ -0,0 +1,792 @@ +/********************************************//** + * @file slsReceiver_funcs.h + * @short interface between receiver and client + ***********************************************/ + +#include "slsReceiver_funcs.h" +#include "slsReceiverFunctionList.h" + +#include +using namespace std; + + +slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *socket): + socket(socket), + ret(OK), + lockStatus(0){ + //initialize variables + strcpy(mess,"dummy message"); + strcpy(socket->lastClientIP,"none"); + strcpy(socket->thisClientIP,"none1"); + + function_table(); + slsReceiverList = new slsReceiverFunctionList(); +} + + +int slsReceiverFuncs::function_table(){ + + for (int i=0;iReceiveDataOnly(&fnum,sizeof(fnum)); + if (n <= 0) { +#ifdef VERBOSE + cout << "ERROR reading from socket " << n << ", " << fnum << endl; +#endif + return FAIL; + } +#ifdef VERBOSE + else + cout << "size of data received " << n <SendDataOnly(&ret,sizeof(ret)); + socket->SendDataOnly(mess,sizeof(mess)); + + return GOODBYE; +} + + + + + + +int slsReceiverFuncs::set_file_name() { + ret=OK; + char retval[MAX_STR_LENGTH]=""; + char fName[MAX_STR_LENGTH]; + strcpy(mess,"Could not set file name"); + + // receive arguments + if(socket->ReceiveDataOnly(fName,MAX_STR_LENGTH) < 0 ){ + strcpy(mess,"Error reading from socket\n"); + ret = FAIL; + } + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + if (ret==OK) { + + if (lockStatus==1 && socket->differentClients==1){ + sprintf(mess,"Receiver locked by %s\n", socket->lastClientIP); + ret=FAIL; + } + else + strcpy(retval,slsReceiverList->setFileName(fName)); + } +#ifdef VERBOSE + if(ret!=FAIL) + cout << "file name:" << retval << endl; + else + cout << mess << endl; +#endif +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + socket->SendDataOnly(retval,MAX_STR_LENGTH); + + //return ok/fail + return ret; +} + + + + + + +int slsReceiverFuncs::set_file_dir() { + ret=OK; + char retval[MAX_STR_LENGTH]=""; + char fPath[MAX_STR_LENGTH]; + strcpy(mess,"Could not set file path\n"); + + // receive arguments + if(socket->ReceiveDataOnly(fPath,MAX_STR_LENGTH) < 0 ){ + strcpy(mess,"Error reading from socket\n"); + ret = FAIL; + } + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + if (ret==OK) { + if (lockStatus==1 && socket->differentClients==1){ + sprintf(mess,"Receiver locked by %s\n", socket->lastClientIP); + ret=FAIL; + } + else if((strlen(fPath))&&(slsReceiverList->getStatus()==RUNNING)){ + strcpy(mess,"Can not set file path while receiver running\n"); + ret = FAIL; + } + else{ + strcpy(retval,slsReceiverList->setFilePath(fPath)); + // if file path doesnt exist + if(strlen(fPath)) + if (strcmp(retval,fPath)){ + strcpy(mess,"receiver file path does not exist\n"); + ret=FAIL; + } + } + + } +#ifdef VERBOSE + if(ret!=FAIL) + cout << "file path:" << retval << endl; + else + cout << mess << endl; +#endif +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + socket->SendDataOnly(retval,MAX_STR_LENGTH); + + //return ok/fail + return ret; +} + + + + + + + + +int slsReceiverFuncs::set_file_index() { + ret=OK; + int retval=-1; + int index; + strcpy(mess,"Could not set file index\n"); + + + // receive arguments + if(socket->ReceiveDataOnly(&index,sizeof(index)) < 0 ){ + strcpy(mess,"Error reading from socket\n"); + ret = FAIL; + } + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + if (ret==OK) { + if (lockStatus==1 && socket->differentClients==1){//necessary??? + sprintf(mess,"Receiver locked by %s\n", socket->lastClientIP); + ret=FAIL; + } + else + retval=slsReceiverList->setFileIndex(index); + } +#ifdef VERBOSE + if(ret!=FAIL) + cout << "file index:" << retval << endl; + else + cout << mess << endl; +#endif +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + socket->SendDataOnly(&retval,sizeof(retval)); + + //return ok/fail + return ret; +} + + + + + +int slsReceiverFuncs::start_receiver(){ + ret=OK; + + strcpy(mess,"Could not start receiver\n"); + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + if (lockStatus==1 && socket->differentClients==1){ + sprintf(mess,"Receiver locked by %s\n", socket->lastClientIP); + ret=FAIL; + } + else if(!strlen(slsReceiverList->setFilePath(""))){ + strcpy(mess,"receiver not set up. set receiver ip again.\n"); + ret = FAIL; + } + else if(slsReceiverList->getStatus()!=RUNNING) + ret=slsReceiverList->startReceiver(); +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + //return ok/fail + return ret; + + +} + + +int slsReceiverFuncs::stop_receiver(){ + ret=OK; + + strcpy(mess,"Could not stop receiver\n"); + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + if (lockStatus==1 && socket->differentClients==1){ + sprintf(mess,"Receiver locked by %s\n", socket->lastClientIP); + ret=FAIL; + } + else if(slsReceiverList->getStatus()!=IDLE) + ret=slsReceiverList->stopReceiver(); +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + //return ok/fail + return ret; + + +} + + +int slsReceiverFuncs::get_status(){ + ret=OK; + enum runStatus retval; + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + retval=slsReceiverList->getStatus(); +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + socket->SendDataOnly(&retval,sizeof(retval)); + //return ok/fail + return ret; + + +} + + +int slsReceiverFuncs::get_frames_caught(){ + ret=OK; + int retval=-1; + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + retval=slsReceiverList->getTotalFramesCaught(); +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + socket->SendDataOnly(&retval,sizeof(retval)); + //return ok/fail + return ret; + + +} + + +int slsReceiverFuncs::get_frame_index(){ + ret=OK; + int retval=-1; + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + retval=slsReceiverList->getAcquisitionIndex(); +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + socket->SendDataOnly(&retval,sizeof(retval)); + //return ok/fail + return ret; + + +} + + +int slsReceiverFuncs::reset_frames_caught(){ + ret=OK; + int retval=-1; + int index=-1; + + strcpy(mess,"Could not reset frames caught\n"); + + + // receive arguments + if(socket->ReceiveDataOnly(&index,sizeof(index)) < 0) {; + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + if (ret==OK) { + if (lockStatus==1 && socket->differentClients==1){ + sprintf(mess,"Receiver locked by %s\n", socket->lastClientIP); + ret=FAIL; + } + else + retval=slsReceiverList->resetTotalFramesCaught(index); + } +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + socket->SendDataOnly(&retval,sizeof(retval)); + //return ok/fail + return ret; + + +} + + +int slsReceiverFuncs::read_frame(){ + ret=OK; + int i,startIndex=-1; + + char* retval=NULL; + char buffer[1286*2]; + char fName[MAX_STR_LENGTH]; + int arg[2]; + arg[1]=1;//do not flip + int index=-1,index2=-1,fIndex=-1;; + + + strcpy(mess,"Could not read frame\n"); + + + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + //wait till you get first frame 1. to get index(from startindex 2. filename corresponds to buffer value + if(startIndex==-1){ + ret=FAIL; + strcpy(mess,"did not start index\n"); + for(i=0;i<10;i++){ + startIndex=slsReceiverList->getStartFrameIndex(); + if(startIndex==-1) + usleep(1000000); + else { + ret=OK; + break; + } + } + } + //got atleast first frame, read buffer + if(ret==OK){ + int count=0; + do{ + if(count>0){ cout << endl << "unmatching: index:" << index <<" index2:" << index2 << endl;} + retval=slsReceiverList->readFrame(fName); + index=(int)(*((int*)retval)); + char* retval2= retval+1286; + index2= (int)(*((int*)retval2)); + count++; + + }while((index%2)==(index2%2)); + + fIndex=((int)(*((int*)retval)) - startIndex)/2; + arg[0]=fIndex-1; + arg[1]=(index%2); + +#ifdef VERBOSE + cout << "\nstartIndex:" << startIndex << endl; + cout << "fName:" << fName << endl; + if((index%2)==0) + cout << "\nEven Index, must flip:" << index << endl; +#endif + } + + +#endif + + if(ret==OK && socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(ret==FAIL){ + cout<<"mess:"<SendDataOnly(mess,sizeof(mess)); + } + else{ + socket->SendDataOnly(fName,MAX_STR_LENGTH); + socket->SendDataOnly(arg,sizeof(arg)); + socket->SendDataOnly(retval,sizeof(buffer)); + } + //return ok/fail + return ret; +} + + + + + + + +int slsReceiverFuncs::lock_receiver() { + ret=OK; + int lock; + + // receive arguments + if(socket->ReceiveDataOnly(&lock,sizeof(lock)) < 0 ){ + sprintf(mess,"Error reading from socket\n"); + cout << "Error reading from socket (lock)" << endl; + ret=FAIL; + } + // execute action if the arguments correctly arrived + if(ret==OK){ + if (lock>=0) { + if (lockStatus==0 || strcmp(socket->lastClientIP,socket->thisClientIP)==0 || strcmp(socket->lastClientIP,"none")==0) { + lockStatus=lock; + strcpy(socket->lastClientIP,socket->thisClientIP); + } else { + ret=FAIL; + sprintf(mess,"Receiver already locked by %s\n", socket->lastClientIP); + } + } + } + + if (socket->differentClients && ret==OK) + ret=FORCE_UPDATE; + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if (ret==FAIL) + socket->SendDataOnly(mess,sizeof(mess)); + else + socket->SendDataOnly(&lockStatus,sizeof(lockStatus)); + + //return ok/fail + return ret; +} + + + + + + + +int slsReceiverFuncs::set_port() { + ret=OK; + int sd=-1; + enum portType p_type; /** data? control? stop? Unused! */ + int p_number; /** new port number */ + + // receive arguments + if(socket->ReceiveDataOnly(&p_type,sizeof(p_type)) < 0 ){ + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (ptype)\n"); + ret=FAIL; + } + + if(socket->ReceiveDataOnly(&p_number,sizeof(p_number)) < 0 ){ + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (pnum)\n"); + ret=FAIL; + } + + // execute action if the arguments correctly arrived + if (ret==OK) { + if (socket->differentClients==1 && lockStatus==1 ) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",socket->lastClientIP); + } else { + if (p_number<1024) { + sprintf(mess,"Too low port number %d\n", p_number); + cout << endl; + ret=FAIL; + } + cout << "set port " << p_type << " to " << p_number <getErrorStatus()){ + ret=OK; + if (socket->differentClients) + ret=FORCE_UPDATE; + } else { + ret=FAIL; + sprintf(mess,"Could not bind port %d\n", p_number); + cout << "Could not bind port " << p_number << endl; + if (socket->getErrorStatus()==-10) { + sprintf(mess,"Port %d already set\n", p_number); + cout << "Port " << p_number << " already set" << endl; + } + } + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if (ret==FAIL) { + socket->SendDataOnly(mess,sizeof(mess)); + } else { + socket->SendDataOnly(&p_number,sizeof(p_number)); + socket->Disconnect(); + delete socket; + } + + //return ok/fail + return ret; +} + + + + + + +int slsReceiverFuncs::get_last_client_ip() { + ret=OK; + + if (socket->differentClients ) + ret=FORCE_UPDATE; + + socket->SendDataOnly(&ret,sizeof(ret)); + socket->SendDataOnly(socket->lastClientIP,sizeof(socket->lastClientIP)); + + return ret; +} + + + + + + + +int slsReceiverFuncs::send_update() { + ret=OK; + int ind; + char path[MAX_STR_LENGTH]; + + socket->SendDataOnly(socket->lastClientIP,sizeof(socket->lastClientIP)); + + //index +#ifdef SLS_RECEIVER_FUNCTION_LIST + ind=slsReceiverList->getFileIndex(); +#endif + socket->SendDataOnly(&ind,sizeof(ind)); + + + //filepath +#ifdef SLS_RECEIVER_FUNCTION_LIST + strcpy(path,slsReceiverList->getFilePath()); +#endif + socket->SendDataOnly(path,MAX_STR_LENGTH); + + + //filename +#ifdef SLS_RECEIVER_FUNCTION_LIST + strcpy(path,slsReceiverList->getFileName()); +#endif + socket->SendDataOnly(path,MAX_STR_LENGTH); + + + if (lockStatus==0) { + strcpy(socket->lastClientIP,socket->thisClientIP); + } + + return ret; + + +} + + + + + + +int slsReceiverFuncs::update_client() { + ret=OK; + socket->SendDataOnly(&ret,sizeof(ret)); + + return send_update(); +} + + + + + + + +int slsReceiverFuncs::exit_server() { + ret=FAIL; + socket->SendDataOnly(&ret,sizeof(ret)); + strcpy(mess,"closing server"); + cout << mess << endl; + socket->SendDataOnly(mess,sizeof(mess)); + return GOODBYE; +} + + + + + +int slsReceiverFuncs::exec_command() { + ret = OK; + char cmd[MAX_STR_LENGTH]; + char answer[MAX_STR_LENGTH]; + int sysret=0; + + // receive arguments + if(socket->ReceiveDataOnly(cmd,MAX_STR_LENGTH) < 0 ){ + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + // execute action if the arguments correctly arrived + if (ret==OK) { +#ifdef VERBOSE + cout << "executing command " << cmd << endl; +#endif + if (lockStatus==0 || socket->differentClients==0) + sysret=system(cmd); + + //should be replaced by popen + if (sysret==0) { + strcpy(answer,"Succeeded\n"); + if (lockStatus==1 && socket->differentClients==1) + sprintf(answer,"Detector locked by %s\n", socket->lastClientIP); + } else { + strcpy(answer,"Failed\n"); + ret=FAIL; + } + } else + strcpy(answer,"Could not receive the command\n"); + + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + if(socket->SendDataOnly(answer,MAX_STR_LENGTH) < 0){ + strcpy(mess,"Error writing to socket"); + ret=FAIL; + } + + //return ok/fail + return ret; +} + + + + + diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h index a5827640b..85152d935 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h @@ -1,45 +1,119 @@ + /********************************************//** + * @file slsReceiver_funcs.h + * @short interface between receiver and client + ***********************************************/ #ifndef RECEIVER_H #define RECEIVER_H -#include -#include "communication_funcs.h" + +#include "sls_detector_defs.h" +#include "receiver_defs.h" +#include "MySocketTCP.h" + +class slsReceiverFunctionList; +/** + *@short interface between receiver and client + */ + +class slsReceiverFuncs : private virtual slsDetectorDefs { + +public: + /** + * Constructor + * @param socket tcp socket connecting receiver and client + */ + slsReceiverFuncs(MySocketTCP *socket); + + /** Destructor */ + virtual ~slsReceiverFuncs(){}; + + /** assigns functions to the fnum enum */ + int function_table(); + + /** Decodes Function */ + int decode_function(); + + /** Unrecognized Function */ + int M_nofunc(); + + /** Set File name without frame index, file index and extension */ + int set_file_name(); + + /** Set File path */ + int set_file_dir(); + + /** Set File index */ + int set_file_index(); + + /** Start Receiver - starts listening to udp packets from detector */ + int start_receiver(); + + /** Stop Receiver - stops listening to udp packets from detector*/ + int stop_receiver(); + + /** Gets receiver status */ + int get_status(); + + /** Gets Total Frames Caught */ + int get_frames_caught(); + + /** Gets frame index for each acquisition */ + int get_frame_index(); + + /** Resets Total Frames Caught */ + int reset_frames_caught(); + + /** Reads Frame/ buffer */ + int read_frame(); + + //General Functions + /** Locks Receiver */ + int lock_receiver(); + + /** Set port */ + int set_port(); + + /** Get Last Client IP*/ + int get_last_client_ip(); + + /** Updates Client if different clients connect */ + int update_client(); + + /** Sends the updated parameters to client */ + int send_update(); + + /** Exit Receiver Server */ + int exit_server(); + + /** Execute command */ + int exec_command(); -#define GOODBYE -200 +private: -int sockfd; + /** Socket */ + MySocketTCP *socket; -int function_table(); -int decode_function(int); -int M_nofunc(int); -int exit_server(int); -int exec_command(int); + /** slsReceiverFunctionList object */ + slsReceiverFunctionList *slsReceiverList; + /** Number of functions */ + static const int numberOfFunctions = 256; -int lock_receiver(int); -int set_port(int); -int get_last_client_ip(int); -int update_client(int); -int send_update(int); -//int set_master(int); -//int set_synchronization(int); + /** Function List */ + int (slsReceiverFuncs::*flist[numberOfFunctions])(); -// General purpose functions + /** Message */ + char mess[MAX_STR_LENGTH]; -//int init_receiver(); -int set_file_name(int); -int set_file_dir(int); -int set_file_index(int); -int start_receiver(int); -int stop_receiver(int); -int get_receiver_status(int); -int get_frames_caught(int); -int get_frame_index(int); -int reset_frame_index(int); -int reset_frames_caught(int); -int read_frame(int); + /** success/failure */ + int ret; + /** Lock Status if server locked to a client */ + int lockStatus; + +}; #endif