From 97ed3e8872e40965e015473ac1cfc125b6dbb0ed Mon Sep 17 00:00:00 2001 From: l_maliakal_d Date: Mon, 19 Nov 2012 16:29:17 +0000 Subject: [PATCH] made udp socket in receiver use generic socket, included a incrememnt frame index in acquire before a break, got rid of infinite loop for unmatched index in receiver git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@350 951219d9-93cf-4727-9268-0efd64621fa3 --- .../MySocketTCP/genericSocket.h | 38 +++++----- .../slsDetector/slsDetectorUtils.cpp | 15 +++- .../slsReceiver/receiver_defs.h | 2 +- .../slsReceiver/slsReceiver.cpp | 3 +- .../slsReceiver/slsReceiverFunctionList.cpp | 74 ++++--------------- .../slsReceiver/slsReceiverFunctionList.h | 25 ++----- .../slsReceiver/slsReceiver_funcs.cpp | 9 ++- 7 files changed, 65 insertions(+), 101 deletions(-) diff --git a/slsDetectorSoftware/MySocketTCP/genericSocket.h b/slsDetectorSoftware/MySocketTCP/genericSocket.h index eb142a991..a06f1d3b4 100644 --- a/slsDetectorSoftware/MySocketTCP/genericSocket.h +++ b/slsDetectorSoftware/MySocketTCP/genericSocket.h @@ -90,9 +90,6 @@ enum communicationProtocol{ // portno(port_number), protocol(p), is_a_server(0), socketDescriptor(-1),file_des(-1), packet_size(DEFAULT_PACKET_SIZE)// sender (client): where to? ip { -/* pthread_mutex_t mp1 = PTHREAD_MUTEX_INITIALIZER; */ -/* mp=mp1; */ -/* pthread_mutex_init(&mp, NULL); */ // strcpy(hostname,host_ip_or_name); struct hostent *hostInfo = gethostbyname(host_ip_or_name); @@ -346,9 +343,6 @@ protocol(p), is_a_server(0), socketDescriptor(-1),file_des(-1), packet_size(DEFA file_des = -1; } else{ file_des = socketDescriptor; - /*cout<<"locking"<=0){ //then was open - if(is_a_server){ - close(file_des); + if (protocol==UDP){ + close(socketDescriptor); + socketDescriptor=-1; + } + else{ + if(file_des>=0){ //then was open + if(is_a_server){ + close(file_des); + } + else { + close(socketDescriptor); + socketDescriptor=-1; + } + file_des=-1; } - else { - close(socketDescriptor); - socketDescriptor=-1; - } - file_des=-1; - /*cout<<"unlocking"< #define GOODBYE -200 -#define HALF_BUFFER_SIZE 1286 +#define BUFFER_SIZE 1286*2 #define DEFAULT_UDP_PORT 50001 diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp index d5880f707..7539f72a3 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp @@ -22,9 +22,10 @@ int main(int argc, char *argv[]) //assign function table slsReceiverFuncs *receiver = new slsReceiverFuncs(socket); #ifdef VERBOSE - cout << "Function table assigned.\n Ready..." << endl; + cout << "Function table assigned." << endl; #endif + cout << " Ready..." << endl; //waits for connection while(retval!=GOODBYE) { #ifdef VERBOSE diff --git a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp index 2bfd1a063..ed4991181 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp @@ -33,15 +33,14 @@ slsReceiverFunctionList::slsReceiverFunctionList(): startAcquisitionIndex(-1), acquisitionIndex(0), framesInFile(0), - udpSocket(-1), status(IDLE), + udpSocket(NULL), server_port(DEFAULT_UDP_PORT) { strcpy(savefilename,""); strcpy(filePath,""); strcpy(fileName,"run"); strcpy(buffer,""); - } @@ -90,7 +89,6 @@ char* slsReceiverFunctionList::setFilePath(char c[]){ int slsReceiverFunctionList::setFileIndex(int i){ if(i>=0) fileIndex = i; - return getFileIndex(); } @@ -153,8 +151,7 @@ int slsReceiverFunctionList::stopReceiver(){ cout << "Stopping new acquisition threadddd ...." << endl; //stop thread listening_thread_running=0; - if (udpSocket != -1) - shutdown(udpSocket, SHUT_RDWR); + udpSocket->ShutDownSocket(); pthread_join(listening_thread,NULL); status = IDLE; } @@ -179,12 +176,7 @@ int slsReceiverFunctionList::startListening(){ #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); + int rc, currframenum, prevframenum; //reset variables for each acquisition framesInFile=0; @@ -208,42 +200,17 @@ int slsReceiverFunctionList::startListening(){ // 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; + + udpSocket = new genericSocket(server_port,genericSocket::UDP); + if (udpSocket->getErrorStatus()){ 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); @@ -263,12 +230,11 @@ int slsReceiverFunctionList::startListening(){ } 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); + //receiver 2 half frames + rc = udpSocket->ReceiveDataOnly(buffer,sizeof(buffer)); + if( rc < 0) + cerr << "recvfrom() failed" << endl; //for each scan if(startFrameIndex==-1){ @@ -279,35 +245,29 @@ int slsReceiverFunctionList::startListening(){ 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); + fwrite(buffer, 1, rc, 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); - } + udpSocket->Disconnect(); //close file fclose(sfilefd); +#ifdef VERBOSE cout << "sfield:" << (int)sfilefd << endl; +#endif return 0; } @@ -317,8 +277,6 @@ int slsReceiverFunctionList::startListening(){ - - char* slsReceiverFunctionList::readFrame(char* c){ strcpy(c,savefilename); return buffer; diff --git a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h index 1355c981d..14aafaefd 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h +++ b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.h @@ -7,6 +7,7 @@ #include "sls_detector_defs.h" #include "receiver_defs.h" +#include "genericSocket.h" #include #include @@ -186,11 +187,11 @@ private: /** File Descriptor */ static FILE *sfilefd; - /** UDP Socket - with server */ - int udpSocket; - /** Receiver buffer */ - char buffer[HALF_BUFFER_SIZE*2]; + char buffer[BUFFER_SIZE]; + + /** UDP Socket between Receiver and Detector */ + genericSocket* udpSocket; /** Server UDP Port*/ int server_port; @@ -198,22 +199,6 @@ private: }; /* - - - - - - - - - - - - - - -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 */ diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp index a1e9c1579..fd61726b4 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp @@ -496,7 +496,14 @@ int slsReceiverFuncs::read_frame(){ if(ret==OK){ int count=0; do{ - if(count>0){ cout << endl << "unmatching: index:" << index <<" index2:" << index2 << endl;} + if(count>0){ + cout << endl << "unmatching: index:" << index <<" index2:" << index2 << endl; + if(count>10){ + strcpy(mess,"unmatching index. could not read frame.\n"); + ret=FAIL; + break; + } + } retval=slsReceiverList->readFrame(fName); index=(int)(*((int*)retval)); char* retval2= retval+1286;