diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index f3b18e102..b07f0481a 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -3808,3 +3808,21 @@ string multiSlsDetector::getReceiverLastClientIP() { } + + +int multiSlsDetector::exitReceiver() { + + int ival=FAIL, iv; + for (int idet=0; idetnumberOfDetectors; idet++) { + if (detectors[idet]) { + iv=detectors[idet]->exitReceiver(); + if (iv==OK) + ival=iv; + } + } + return ival; +} + + + + diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h index d668f8874..2137c2ac4 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h @@ -1112,6 +1112,11 @@ class multiSlsDetector : public slsDetectorUtils { */ string getReceiverLastClientIP(); + /** + Turns off the receiver server! + */ + int exitReceiver(); + int fillModuleMask(int *mM); protected: diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 63e68e12f..cda53e221 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -5805,3 +5805,30 @@ int slsDetector::updateReceiver() { } + + + + +int slsDetector::exitReceiver(){ + + int retval; + int fnum=F_EXIT_SERVER; + + if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) { + if (dataSocket) { + dataSocket->Connect(); + dataSocket->SendDataOnly(&fnum,sizeof(fnum)); + dataSocket->ReceiveDataOnly(&retval,sizeof(retval)); + dataSocket->Disconnect(); + } + } + if (retval!=OK) { + std::cout<< std::endl; + std::cout<< "Shutting down the receiver" << std::endl; + std::cout<< std::endl; + } + return retval; + +}; + + diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index ad45d8537..22e7212ec 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -1496,6 +1496,13 @@ class slsDetector : public slsDetectorUtils, public energyConversion { /returns OK */ int updateReceiver(); + + /** + Turns off the receiver server! + */ + int exitReceiver(); + + int fillModuleMask(int *mM); protected: diff --git a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp index 8d22d979a..c36fdcc36 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp @@ -83,6 +83,11 @@ slsDetectorCommand::slsDetectorCommand(slsDetectorUtils *det) { descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdExitServer; i++; + descrToFuncMap[i].m_pFuncName="exitreceiver";//OK + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdExitServer; + i++; + + /* data processing commands */ @@ -782,6 +787,11 @@ string slsDetectorCommand::cmdAcquire(int narg, char *args[], int action) { myDet->setOnline(ONLINE_FLAG); myDet->acquire(); + if(myDet->setReceiverOnline()==ONLINE_FLAG){ + char answer[100]; + sprintf(answer,"\n%d",myDet->getFramesCaughtByReceiver()); + return string(answer); + } return string(""); @@ -1209,19 +1219,32 @@ string slsDetectorCommand::cmdExitServer(int narg, char *args[], int action){ if (action==HELP_ACTION) { return helpExitServer(narg, args, action); } - myDet->setOnline(ONLINE_FLAG); + if (action==PUT_ACTION) { - if (myDet->exitServer()!=OK) - return string("Server shut down."); - else - return string("Error closing server\n"); + if (cmd=="exitserver"){ + myDet->setOnline(ONLINE_FLAG); + if (myDet->exitServer()!=OK) + return string("Server shut down."); + else + return string("Error closing server\n"); + } + else if (cmd=="exitreceiver"){ + if(myDet->exitReceiver()!=OK) + return string("Receiver shut down\n"); + else + return string("Error closing receiver\n"); + } + else return("cannot decode command\n"); } else return ("cannot get"); } string slsDetectorCommand::helpExitServer(int narg, char *args[], int action){ - return string("exitserver \t shuts down all the detector servers. Don't use it!!!!"); + ostringstream os; + os << string("exitserver \t shuts down all the detector servers. Don't use it!!!!\n"); + os << string("exitreceiver \t shuts down all the receiver servers.\n"); + return os.str(); } diff --git a/slsDetectorSoftware/slsDetector/slsDetectorUtils.h b/slsDetectorSoftware/slsDetector/slsDetectorUtils.h index c486ad53d..e182d3079 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorUtils.h +++ b/slsDetectorSoftware/slsDetector/slsDetectorUtils.h @@ -637,6 +637,11 @@ virtual int resetFramesCaught(int index=-1)=0; virtual int* readFrameFromReceiver(char* fName, int &fIndex)=0; +/** + Turns off the receiver server! +*/ +virtual int exitReceiver()=0; + protected: static const int64_t thisSoftwareVersion=0x20120124; diff --git a/slsDetectorSoftware/slsReceiver/circularFifo.h b/slsDetectorSoftware/slsReceiver/circularFifo.h index bad15bd2e..325ea6030 100644 --- a/slsDetectorSoftware/slsReceiver/circularFifo.h +++ b/slsDetectorSoftware/slsReceiver/circularFifo.h @@ -10,7 +10,7 @@ * Code & platform dependent issues with it was originally * published at http://www.kjellkod.cc/threadsafecircularqueue * 2009-11-02 -* @author Kjell Hedström, hedstrom@kjellkod.cc */ +* @author Kjell Hedstr�m, hedstrom@kjellkod.cc */ #ifndef CIRCULARFIFO_H_ #define CIRCULARFIFO_H_ @@ -25,15 +25,15 @@ public: CircularFifo() : tail(0), head(0){} virtual ~CircularFifo() {} - bool push(Element& item_); - bool pop(Element& item_); + bool push(Element*& item_); + bool pop(Element*& item_); bool isEmpty() const; bool isFull() const; private: volatile unsigned int tail; // input index - Element array[Capacity]; + Element* array[Capacity]; volatile unsigned int head; // output index unsigned int increment(unsigned int idx_) const; @@ -49,7 +49,7 @@ private: * \param item_ copy by reference the input item * \return whether operation was successful or not */ template -bool CircularFifo::push(Element& item_) +bool CircularFifo::push(Element*& item_) { int nextTail = increment(tail); if(nextTail != head) @@ -70,7 +70,7 @@ bool CircularFifo::push(Element& item_) * \param item_ return by reference the wanted item * \return whether operation was successful or not */ template -bool CircularFifo::pop(Element& item_) +bool CircularFifo::pop(Element*& item_) { if(head == tail) return false; // empty queue diff --git a/slsDetectorSoftware/slsReceiver/receiver_defs.h b/slsDetectorSoftware/slsReceiver/receiver_defs.h index 6f4fe46c8..67252144d 100755 --- a/slsDetectorSoftware/slsReceiver/receiver_defs.h +++ b/slsDetectorSoftware/slsReceiver/receiver_defs.h @@ -9,6 +9,7 @@ #define BUFFER_SIZE 1286*2 #define DATABYTES 2560 #define DEFAULT_UDP_PORT 50001 +#define FIFO_SIZE 25000 diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp index 0d8160edc..2a54ad3cc 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp @@ -79,8 +79,10 @@ int main(int argc, char *argv[]) } } - delete mysocket; + + slsReceiverFuncs::closeFile(0); cout << "Goodbye!" << endl; + delete mysocket; return 0; } diff --git a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp index ada7e73d1..eacd8de1e 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp @@ -15,6 +15,8 @@ #include // sock_addr_in, htonl, INADDR_ANY #include // exit() +#include //set precision + #include #include @@ -35,15 +37,18 @@ slsReceiverFunctionList::slsReceiverFunctionList(bool shortfname): startAcquisitionIndex(-1), acquisitionIndex(0), framesInFile(0), + prevframenum(0), status(IDLE), + latestData(NULL), udpSocket(NULL), - server_port(DEFAULT_UDP_PORT) + server_port(DEFAULT_UDP_PORT), + fifo(NULL) { strcpy(savefilename,""); strcpy(actualfilename,""); strcpy(filePath,""); strcpy(fileName,"run"); - strcpy(buffer,""); + } @@ -52,7 +57,7 @@ int slsReceiverFunctionList::getFrameIndex(){ if(startFrameIndex==-1) frameIndex=0; else - frameIndex=((int)(*((int*)buffer)) - startFrameIndex)/2; + frameIndex=((int)(*((int*)latestData)) - startFrameIndex)/2; return frameIndex; } @@ -62,7 +67,7 @@ int slsReceiverFunctionList::getAcquisitionIndex(){ if(startAcquisitionIndex==-1) acquisitionIndex=0; else - acquisitionIndex=((int)(*((int*)buffer)) - startAcquisitionIndex)/2; + acquisitionIndex=((int)(*((int*)latestData)) - startAcquisitionIndex)/2; return acquisitionIndex; } @@ -112,18 +117,38 @@ int slsReceiverFunctionList::startReceiver(){ #endif int err = 0; if(!listening_thread_running){ - printf("Starting new acquisition threadddd ....\n"); + cout << "Starting new acquisition threadddd ...." << endl; 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{ + + fifo = new CircularFifo(); + + //error creating writing thread + err = pthread_create(&writing_thread, NULL,startWritingThread, (void*) this); + if(err){ listening_thread_running=0; status = IDLE; - cout << endl << "Cant create thread. Status:" << status << endl; + if(fifo) delete fifo; + cout << "Cant create writing thread. Status:" << status << endl << endl; return FAIL; } + cout << "Writing thread created successfully." << endl; + + + //error creating listenign thread + err = 0; + err = pthread_create(&listening_thread, NULL,startListeningThread, (void*) this); + if(err){ + listening_thread_running=0; + status = IDLE; + //stop writing thread + pthread_join(writing_thread,NULL); + if(fifo) delete fifo; + cout << endl << "Cant create listening thread. Status:" << status << endl << endl; + return FAIL; + } + + while(status!=RUNNING); + cout << "Listening thread created successfully." << endl; } return OK; @@ -140,14 +165,19 @@ int slsReceiverFunctionList::stopReceiver(){ if(listening_thread_running){ cout << "Stopping new acquisition threadddd ...." << endl; - //stop thread + //stop listening thread listening_thread_running=0; udpSocket->ShutDownSocket(); pthread_join(listening_thread,NULL); status = IDLE; + + //stop writing thread + pthread_join(writing_thread,NULL); + if(fifo) delete fifo; } cout << "Status:" << status << endl; + return OK; } @@ -161,33 +191,26 @@ void* slsReceiverFunctionList::startListeningThread(void* this_pointer){ + + + + + + + int slsReceiverFunctionList::startListening(){ #ifdef VERYVERBOSE cout << "In startListening()\n"); #endif - // Variable and structure definitions - int rc, currframenum, prevframenum; + int rc; + dataStruct *dataReadFrame; //reset variables for each acquisition - framesInFile=0; - framesCaught=0; startFrameIndex=-1; - frameIndex=0; shortFileNameIndex=1; - //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); - - if(!shortFileName) - strcpy(actualfilename,savefilename); - else - sprintf(actualfilename, "%s/%s_%d_%09d.raw", filePath,fileName,fileIndex, 0); - // 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. @@ -198,60 +221,42 @@ int slsReceiverFunctionList::startListening(){ break; } - sfilefd = fopen((const char *) (actualfilename), "w"); - cout << "Saving to " << actualfilename << ". Ready! " << endl; while (listening_thread_running) { - - 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); - - if(!shortFileName) - strcpy(actualfilename,savefilename); - else - sprintf(actualfilename, "%s/%s_%d_%09d.raw", filePath,fileName,fileIndex, shortFileNameIndex); - - shortFileNameIndex++; - - cout << "saving to " << actualfilename << "\t\t" - "packet loss " << ((currframenum-prevframenum-(2*framesInFile))/(double)(2*framesInFile))*100.000 << "%\t\t" - "framenum " << currframenum << endl; - sfilefd = fopen((const char *) (actualfilename), "w"); - prevframenum=currframenum; - framesInFile = 0; - } status = RUNNING; - + buffer = new char[BUFFER_SIZE]; //receiver 2 half frames rc = udpSocket->ReceiveDataOnly(buffer,sizeof(buffer)); if( rc < 0) cerr << "recvfrom() failed" << endl; - //for each scan + //start for each scan if(startFrameIndex==-1){ startFrameIndex=(int)(*((int*)buffer))-2; + //cout<<"startFrameIndex:"<isFull()) + cout<<"**********************FIFO FULLLLLLLL************************"<buffer=buffer; + dataReadFrame->rc=rc; + //cout<<"read buffer:"<push(dataReadFrame); + } } } @@ -262,10 +267,108 @@ int slsReceiverFunctionList::startListening(){ //Close down any open socket descriptors udpSocket->Disconnect(); - //close file - fclose(sfilefd); + #ifdef VERBOSE cout << "listening_thread_running:" << listening_thread_running << endl; +#endif + + return 0; +} + + + + + + +void* slsReceiverFunctionList::startWritingThread(void* this_pointer){ + ((slsReceiverFunctionList*)this_pointer)->startWriting(); + + return this_pointer; +} + + + + +int slsReceiverFunctionList::startWriting(){ +#ifdef VERYVERBOSE + cout << "In startWriting()" <isEmpty()){ + + dataWriteFrame = new dataStruct; + if(fifo->pop(dataWriteFrame)){ + //cout<<"write buffer:"<buffer,BUFFER_SIZE); + fwrite(dataWriteFrame->buffer, 1, dataWriteFrame->rc, sfilefd); + framesInFile++; + delete dataWriteFrame->buffer; + delete dataWriteFrame; + + } + } + } + + cout << "Total Frames Caught:"<< totalFramesCaught << endl; + //delete latestData; + //close file + fclose(sfilefd); + +#ifdef VERBOSE cout << "sfield:" << (int)sfilefd << endl; #endif @@ -277,9 +380,13 @@ int slsReceiverFunctionList::startListening(){ + + char* slsReceiverFunctionList::readFrame(char* c){ + //cout<<"latestdata:"<<(int)(*(int*)latestData)< #include #include @@ -28,7 +30,7 @@ public: /** * Destructor */ - virtual ~slsReceiverFunctionList(){}; + virtual ~slsReceiverFunctionList(){ if(latestData) delete latestData;}; /** * Returns status of receiver: idle, running or error @@ -132,6 +134,20 @@ public: */ int startListening(); + /** + * Static function - Thread started which writes packets to file. + * Called by startReceiver() + * @param this_pointer pointer to this object + */ + static void* startWritingThread(void *this_pointer); + + /** + * Thread started which writes packets to file. + * Called by startReceiver() + * + */ + int startWriting(); + /** * Returns the buffer-current frame read by receiver */ @@ -185,20 +201,23 @@ private: /** 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; + /** Previous Frame number from buffer */ + int prevframenum; /** thread listening to packets */ pthread_t listening_thread; + /** thread writing packets */ + pthread_t writing_thread; + /** status of receiver */ runStatus status; - /** File Descriptor */ - //static FILE *sfilefd; - /** Receiver buffer */ - char buffer[BUFFER_SIZE]; + char* buffer; + + /** latest data */ + char* latestData; /** UDP Socket between Receiver and Detector */ genericSocket* udpSocket; @@ -206,6 +225,17 @@ private: /** Server UDP Port*/ int server_port; + /** Element structure to put inside a fifo */ + struct dataStruct { + char* buffer; + int rc; + }; + + + //dataStruct* dataReadFrame; + /** circular fifo to read and write data*/ + CircularFifo* fifo; + public: /** File Descriptor */ static FILE *sfilefd; diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp index 24ebde9ee..e092f2141 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp @@ -208,7 +208,7 @@ void slsReceiverFuncs::closeFile(int p){ close(file_des); - shutdown(socketDescriptor,SHUT_RDWR); + //shutdown(socketDescriptor,SHUT_RDWR); close(socketDescriptor); } @@ -479,7 +479,6 @@ int slsReceiverFuncs::get_frames_caught(){ #ifdef SLS_RECEIVER_FUNCTION_LIST retval=slsReceiverList->getTotalFramesCaught(); #endif - if(ret==OK && socket->differentClients){ cout << "Force update" << endl; ret=FORCE_UPDATE; @@ -564,32 +563,31 @@ int slsReceiverFuncs::reset_frames_caught(){ int slsReceiverFuncs::read_frame(){ ret=OK; - int nel = BUFFER_SIZE/(sizeof(int)); char fName[MAX_STR_LENGTH]; - int arg = -1; + int nel = BUFFER_SIZE/(sizeof(int)); int onebuffersize = BUFFER_SIZE/2; int onedatasize = DATABYTES/2; - char* raw=NULL; - char* retval2=NULL; - int* origVal=new int[nel]; - int* retval=new int[nel]; + char* raw = new char[BUFFER_SIZE]; + int* origVal = new int[nel]; + int* retval = new int[nel]; int index=-1,index2=-1; - int i,startIndex=-1; + int startIndex=-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++){ + for(int i=0;i<10;i++){ startIndex=slsReceiverList->getStartFrameIndex(); if(startIndex==-1) usleep(1000000); @@ -599,6 +597,7 @@ int slsReceiverFuncs::read_frame(){ } } } + //got atleast first frame, read buffer if(ret==OK){ int count=0; @@ -606,10 +605,14 @@ int slsReceiverFuncs::read_frame(){ while(count<20){ //get frame raw=slsReceiverList->readFrame(fName); - index=(int)(*((int*)raw)); + //(int)(*(int*)buffer) + index=(int)(*(int*)raw); index2= (int)(*((int*)((char*)(raw+onebuffersize)))); memcpy(origVal,raw,BUFFER_SIZE); + //cout<<"index:"<SendDataOnly(&ret,sizeof(ret)); strcpy(mess,"closing server"); - cout << mess << endl; socket->SendDataOnly(mess,sizeof(mess)); - return GOODBYE; + cout << mess << endl; + return ret; }