works, need to do json header and send dataready

This commit is contained in:
Dhanya Maliakal
2016-09-15 17:15:55 +02:00
parent a821442b1a
commit 1da4b07e73
7 changed files with 247 additions and 418 deletions

View File

@ -23,6 +23,7 @@ ID: $Id$
#include <sys/shm.h> #include <sys/shm.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <zmq.h>
using namespace std; using namespace std;
@ -267,30 +268,28 @@ multiSlsDetector::multiSlsDetector(int id) : slsDetectorUtils(), shmId(-1)
getNMods(); getNMods();
getMaxMods(); getMaxMods();
threadpool = 0; threadpool = 0;
zmqthreadpool = 0; if(createThreadPool() == FAIL)
if(createThreadPool(&threadpool) == FAIL)
exit(-1); exit(-1);
} }
multiSlsDetector::~multiSlsDetector() { multiSlsDetector::~multiSlsDetector() {
//removeSlsDetector(); //removeSlsDetector();
destroyThreadPool(&threadpool); destroyThreadPool();
destroyThreadPool(&zmqthreadpool);
} }
int multiSlsDetector::createThreadPool(ThreadPool** t){ int multiSlsDetector::createThreadPool(){
if(*t){ if(threadpool){
(ThreadPool*)(*t)->destroy_threadpool(); threadpool->destroy_threadpool();
*t=0; threadpool=0;
} }
if(thisMultiDetector->numberOfDetectors < 1){ if(thisMultiDetector->numberOfDetectors < 1){
cout << "No detectors attached to create threadpool" << endl; cout << "No detectors attached to create threadpool" << endl;
return OK; return OK;
} }
*t = new ThreadPool(thisMultiDetector->numberOfDetectors); threadpool = new ThreadPool(thisMultiDetector->numberOfDetectors);
switch(((ThreadPool*)(*t))->initialize_threadpool()){ switch(threadpool->initialize_threadpool()){
case 0: case 0:
cerr << "Failed to initialize thread pool!" << endl; cerr << "Failed to initialize thread pool!" << endl;
return FAIL; return FAIL;
@ -301,19 +300,19 @@ int multiSlsDetector::createThreadPool(ThreadPool** t){
break; break;
default: default:
#ifdef VERBOSE #ifdef VERBOSE
cout << "Initialized Threadpool " << *t << endl; cout << "Initialized Threadpool " << threadpool << endl;
#endif #endif
break; break;
} }
return OK; return OK;
} }
void multiSlsDetector::destroyThreadPool(ThreadPool** t){ void multiSlsDetector::destroyThreadPool(){
if(*t){ if(threadpool){
(ThreadPool*)(*t)->destroy_threadpool(); threadpool->destroy_threadpool();
*t=0; threadpool=0;
#ifdef VERBOSE #ifdef VERBOSE
cout<<"Destroyed Threadpool "<< *t << endl; cout<<"Destroyed Threadpool "<< threadpool << endl;
#endif #endif
} }
} }
@ -394,7 +393,7 @@ int multiSlsDetector::addSlsDetector(int id, int pos) {
//set offsets //set offsets
updateOffsets(); updateOffsets();
if(createThreadPool(&threadpool) == FAIL) if(createThreadPool() == FAIL)
exit(-1); exit(-1);
@ -865,7 +864,7 @@ int multiSlsDetector::removeSlsDetector(int pos) {
} }
updateOffsets(); updateOffsets();
if(createThreadPool(&threadpool) == FAIL) if(createThreadPool() == FAIL)
exit(-1); exit(-1);
return thisMultiDetector->numberOfDetectors; return thisMultiDetector->numberOfDetectors;
@ -4957,17 +4956,113 @@ int multiSlsDetector::resetFramesCaught() {
} }
void* multiSlsDetector::startReceivingDataThread(void* this_pointer){
((multiSlsDetector*)this_pointer)->startReceivingData();
return this_pointer;
}
void multiSlsDetector::startReceivingData(){
int ithread = currentThreadIndex; //set current thread value index
threadStarted = true; //let calling function know thread started and obtained current
int numReadoutPerDetector = 1;
bool jungfrau = false;
if(getDetectorsType() == EIGER){
numReadoutPerDetector = 2;
}else if(getDetectorsType() == JUNGFRAU)
jungfrau = true;
//server details
char hostname[100];
int portno;
int singleDatabytes = detectors[ithread/numReadoutPerDetector]->getDataBytes();
int nel=(singleDatabytes/numReadoutPerDetector)/sizeof(int);
portno = DEFAULT_ZMQ_PORTNO + (ithread);
sprintf(hostname, "%s%d", "tcp://127.0.0.1:",portno);
//cout << "ZMQ Client of " << ithread << " at " << hostname << endl;
singleframe[ithread]=new int[nel];
//loop though the half readouts to start sockets
void *context;
void *zmqsocket;
context = zmq_ctx_new();
zmqsocket = zmq_socket(context, ZMQ_PULL);
//zmq_setsockopt(zmqsocket, ZMQ_SUBSCRIBE, "", 0); // an empty string implies receiving any messages
zmq_connect(zmqsocket, hostname); // connect to publisher,the publisher server does not have to be started
pthread_mutex_lock(&ms);
receivingDataThreadMask|=(1<<(ithread));
//cout<<ithread<< " single created "<<hex<<receivingDataThreadMask<<endl;
pthread_mutex_unlock(&ms);
//receive msgs and let multi know
zmq_msg_t message;
int len,idet = 0;
int framecount=0;
//read frame
while(true){
//cprintf(GREEN,"single %d waiting for multi\n",ithread);
sem_wait(&sem_singlewait[ithread]); //wait for it to be copied
//cprintf(GREEN,"single %d got multi\n",ithread);
if(!idet) framecount++; //update indices, count only once
// receive a message, this is a blocking function
len = zmq_msg_init (&message); /* is this required? Xiaoqiang didnt have it*/
if(len) {cprintf(RED,"Failed to initialize message %d for %d\n",len,ithread); continue; } //error
len = zmq_msg_recv(&message, zmqsocket, 0);
//if(len<1024*256)
//cprintf(RED,"got less than planned for socket %d\n",ithread);
//end of socket
if (len <= 3 ) {
if(!len) cprintf(RED,"Received no data in socket for %d\n", ithread);
//cout<<ithread <<" sls Received end data"<<endl;
singleframe[ithread] = NULL;
pthread_mutex_lock(&ms);
receivingDataThreadMask^=(1<<ithread);
pthread_mutex_unlock(&ms);
sem_post(&sem_singledone[ithread]); //let multi know is ready
//cprintf(GREEN,"single %d posted done (finished) for multi\n",ithread);
//cout << ithread << " single finished" << endl;
break;
}
if(zmq_msg_data(&message)==NULL) cprintf(RED,"GOT NULL FROM ZMQ\n"); /*not needed most likely*/
//cout<<"Received on " << ithread << " for frame " << framecount << endl;
//if(len == singleDatabytes/numReadoutPerDetector){//hoow to solve this
memcpy((char*)(singleframe[ithread]),(char*)zmq_msg_data(&message),singleDatabytes/numReadoutPerDetector);
//check header, if incorrect frame, copy somewhere and assign a blank subframe and also check size
//jungfrau masking adcval
if(jungfrau){
for(unsigned int i=0;i<nel;i++){
singleframe[ithread][i] = (singleframe[ithread][i] & 0x3FFF3FFF);
}
}
//}
sem_post(&sem_singledone[ithread]);//let multi know is ready
//cprintf(GREEN,"single %d posted done for multi\n",ithread);
}
zmq_msg_close(&message);
//close socket
zmq_disconnect(zmqsocket, hostname);
zmq_close(zmqsocket);
zmq_ctx_destroy(context);
}
void multiSlsDetector::readFrameFromReceiver(){ void multiSlsDetector::readFrameFromReceiver(){
//Note:num threads = (num slsDets = num tasks)
//so, half slsdet readouts read serially in each task (eiger udp ports)
//create zmq threads
if(createThreadPool(&zmqthreadpool) == FAIL){
cprintf(BG_RED,"Error: Could not create the zmq threads\n");
return;
}
zmqthreadpool->setzeromqThread(); //for debugging
//determine number of half readouts and maxX and maxY //determine number of half readouts and maxX and maxY
int maxX=0,maxY=0; int maxX=0,maxY=0;
@ -4980,125 +5075,131 @@ void multiSlsDetector::readFrameFromReceiver(){
int numReadouts = numReadoutPerDetector * thisMultiDetector->numberOfDetectors; int numReadouts = numReadoutPerDetector * thisMultiDetector->numberOfDetectors;
//create threads
//start all socket tasks /** Data Callback Threads */
volatile uint64_t runningMask = 0x0; pthread_t receivingDataThreads[numReadouts];
int slsdatabytes = 0, slsmaxchannels = 0, bytesperchannel = 0, slsmaxX = 0, slsmaxY=0; volatile uint64_t expectedMask = 0x0;
if(!zmqthreadpool){ receivingDataThreadMask = 0x0;
cout << "Error in creating threadpool. Exiting" << endl; currentThreadIndex = -1;
return; for(int i = 0; i < numReadouts; ++i){
}else{ threadStarted = false;
for(int idet=0; idet<thisMultiDetector->numberOfDetectors; idet++){ currentThreadIndex = i;
if(detectors[idet]){ sem_init(&sem_singlewait[i],1,0);
sem_init(&sem_slswait[idet*numReadoutPerDetector],1,0); sem_init(&sem_singledone[i],1,0);
sem_init(&sem_slsdone[idet*numReadoutPerDetector],1,0); if(pthread_create(&receivingDataThreads[i], NULL,startReceivingDataThread, (void*) this)){
sem_init(&sem_multiwait[idet*numReadoutPerDetector],1,0); cprintf(RED, "ERROR: Could not create receiving thread with index %d\n",i);
if(numReadoutPerDetector>1){ return;
sem_init(&sem_slswait[idet*numReadoutPerDetector+1],1,0);
sem_init(&sem_slsdone[idet*numReadoutPerDetector+1],1,0);
sem_init(&sem_multiwait[idet*numReadoutPerDetector+1],1,0);
}
Task* task = new Task(new func00_t<void, slsDetector>(&slsDetector::readFrameFromReceiver,detectors[idet]));
zmqthreadpool->add_task(task);
if(!slsdatabytes){
slsdatabytes = detectors[idet]->getDataBytes();
slsmaxchannels = detectors[idet]->getMaxNumberOfChannels();
bytesperchannel = slsdatabytes/slsmaxchannels;
slsmaxX = detectors[idet]->getTotalNumberOfChannels(X);
slsmaxY = detectors[idet]->getTotalNumberOfChannels(Y);
}
//set mask
runningMask|=(1<<(idet*numReadoutPerDetector));
if(numReadoutPerDetector>1)
runningMask|=(1<<(idet*numReadoutPerDetector+1));
}
} }
while(!threadStarted);
//cout << "Data Thread created successfully for " << i << endl;
expectedMask|=(1<<i);
} }
zmqthreadpool->startExecuting(); //tell them to start
for(int i=0;i<numReadouts;++i) //wait for all of them to have started //cout<<"multi waiting for all threads to be created "<<hex<<receivingDataThreadMask<<" to be matched with " <<expectedMask<< endl;
sem_wait(&sem_multiwait[i]); //wait for the last few threads remaining to be ready
sem_post(&dataThreadStartedSemaphore); //let utils:acquire continue to start measurement/acquisition while(receivingDataThreadMask != expectedMask);
//cout<<"multi threads created"<<endl;
int slsdatabytes = 0, slsmaxchannels = 0, bytesperchannel = 0, slsmaxX = 0, slsmaxY=0;
if(detectors[0]){
slsdatabytes = detectors[0]->getDataBytes();
slsmaxchannels = detectors[0]->getMaxNumberOfChannels();
bytesperchannel = slsdatabytes/slsmaxchannels;
slsmaxX = detectors[0]->getTotalNumberOfChannels(X);
slsmaxY = detectors[0]->getTotalNumberOfChannels(Y);
}
int nel=(thisMultiDetector->dataBytes)/sizeof(int); int nel=(thisMultiDetector->dataBytes)/sizeof(int);
if(nel <= 0){ if(nel <= 0){
cout << "Multislsdetector databytes not valid :" << thisMultiDetector->dataBytes << endl; cprintf(RED,"Error: Multislsdetector databytes not valid : %d\n", thisMultiDetector->dataBytes);
return; return;
} }
int* multiframe=new int[nel]; int* multiframe=new int[nel];
int* p = multiframe; int* p = multiframe;
int idet,offsetY,offsetX; int idet,offsetY,offsetX;
int halfreadoutoffset = (slsmaxX/numReadoutPerDetector); int halfreadoutoffset = (slsmaxX/numReadoutPerDetector);
//after reconstruction
int framecount=0; int framecount=0;
int nx =getTotalNumberOfChannels(slsDetectorDefs::X); int nx =getTotalNumberOfChannels(slsDetectorDefs::X);
int ny =getTotalNumberOfChannels(slsDetectorDefs::Y); int ny =getTotalNumberOfChannels(slsDetectorDefs::Y);
sem_post(&dataThreadStartedSemaphore); //let utils:acquire continue to start measurement/acquisition
//cprintf(BLUE,"all sockets created\n");
//construct complete image and send to callback //construct complete image and send to callback
while(true){ while(true){
memset(((char*)multiframe),0x0,slsdatabytes*thisMultiDetector->numberOfDetectors); //reset frame memory memset(((char*)multiframe),0x0,slsdatabytes*thisMultiDetector->numberOfDetectors); //reset frame memory
//post all of them to start
for(int ireadout=0; ireadout<numReadouts; ++ireadout){
if((1 << ireadout) & receivingDataThreadMask){
sem_post(&sem_singlewait[ireadout]); //sls to continue
//cprintf(BLUE,"multi posted det %d\n",ireadout);
}
}
//get each frame
for(int ireadout=0; ireadout<numReadouts; ++ireadout){ for(int ireadout=0; ireadout<numReadouts; ++ireadout){
idet = ireadout/numReadoutPerDetector; idet = ireadout/numReadoutPerDetector;
if(detectors[idet]){ if((1 << ireadout) & receivingDataThreadMask){ //if running
if((1 << ireadout) & runningMask){ //if running //wait for single to copy
sem_post(&sem_slswait[ireadout]); //sls to continue //cprintf(BLUE,"multi waiting done by det %d\n",ireadout);
sem_wait(&sem_slsdone[ireadout]); //wait for sls to copy sem_wait(&sem_singledone[ireadout]); //wait for sls to copy
//cprintf(BLUE,"multi got done by det %d\n",ireadout);
//this socket closed //this socket closed
if(slsframe[ireadout] == NULL){ if(!((1 << ireadout) & receivingDataThreadMask)){ //if running
runningMask^=(1<<ireadout); continue;
if(!runningMask){ //all done, get out }
break;
} //assemble data
continue; if(maxX){ //eiger, so interleaving between ports in one readout itself
offsetY = (maxY - (thisMultiDetector->offsetY[idet] + slsmaxY)) * maxX * bytesperchannel;
//the left half or right half
if(!(ireadout%numReadoutPerDetector))
offsetX = thisMultiDetector->offsetX[idet];
else
offsetX = thisMultiDetector->offsetX[idet] + halfreadoutoffset;
offsetX *= bytesperchannel;
//cprintf(BLUE,"offsetx:%d offsety:%d maxx:%d slsmaxX:%d slsmaxY:%d bytesperchannel:%d\n",
// offsetX,offsetY,maxX,slsmaxX,slsmaxY,bytesperchannel);
// cprintf(BLUE,"copying bytes:%d\n", (slsmaxX/numReadoutPerDetector)*bytesperchannel);
//itnerleaving with other detectors
//bottom
if(((idet+1)%2) == 0){
for(int i=0;i<slsmaxY;++i)
memcpy(((char*)multiframe) + offsetY + offsetX + ((slsmaxY-i)*maxX*bytesperchannel),
(char*)singleframe[ireadout]+ i*(slsmaxX/numReadoutPerDetector)*bytesperchannel,
(slsmaxX/numReadoutPerDetector)*bytesperchannel);
} }
//top
//assemble data
if(maxX){ //eiger, so interleaving between ports in one readout itself
offsetY = (maxY - (thisMultiDetector->offsetY[idet] + slsmaxY)) * maxX * bytesperchannel;
//the left half or right half
if(!(ireadout%numReadoutPerDetector))
offsetX = thisMultiDetector->offsetX[idet];
else
offsetX = thisMultiDetector->offsetX[idet] + halfreadoutoffset;
offsetX *= bytesperchannel;
//cprintf(BLUE,"offsetx:%d offsety:%d maxx:%d slsmaxX:%d slsmaxY:%d bytesperchannel:%d\n",
// offsetX,offsetY,maxX,slsmaxX,slsmaxY,bytesperchannel);
//cprintf(BLUE,"copying bytes:%d\n", (slsmaxX/numReadout)*bytesperchannel);
//itnerleaving with other detectors
//bottom
if(((idet+1)%2) == 0){
for(int i=0;i<slsmaxY;++i)
memcpy(((char*)multiframe) + offsetY + offsetX + ((slsmaxY-i)*maxX*bytesperchannel),
(char*)slsframe[ireadout]+ i*(slsmaxX/numReadoutPerDetector)*bytesperchannel,
(slsmaxX/numReadoutPerDetector)*bytesperchannel);
}
//top
else{
for(int i=0;i<slsmaxY;++i)
memcpy(((char*)multiframe) + offsetY + offsetX + (i*maxX*bytesperchannel),
(char*)slsframe[ireadout]+ i*(slsmaxX/numReadoutPerDetector)*bytesperchannel,
(slsmaxX/numReadoutPerDetector)*bytesperchannel);
}
}
//no interleaving, just add to the end
//numReadout always 1 here
else{ else{
memcpy(p,multiframe,slsdatabytes); for(int i=0;i<slsmaxY;++i)
p+=slsdatabytes/sizeof(int); memcpy(((char*)multiframe) + offsetY + offsetX + (i*maxX*bytesperchannel),
(char*)singleframe[ireadout]+ i*(slsmaxX/numReadoutPerDetector)*bytesperchannel,
(slsmaxX/numReadoutPerDetector)*bytesperchannel);
} }
} }
//no interleaving, just add to the end
//numReadout always 1 here
else{
memcpy(p,multiframe,slsdatabytes);
p+=slsdatabytes/sizeof(int);
}
} }
}//end of for loop }
if(!runningMask){ //cout<<"receivingDataThreadMask:"<<hex<<receivingDataThreadMask<<endl;
if(!receivingDataThreadMask){
break; break;
} }
//send data to callback //send data to callback
fdata = decodeData(multiframe); fdata = decodeData(multiframe);
if ((fdata) && (dataReady)){ if ((fdata) && (dataReady)){
@ -5112,13 +5213,14 @@ void multiSlsDetector::readFrameFromReceiver(){
setCurrentProgress(framecount); setCurrentProgress(framecount);
} }
zmqthreadpool->wait_for_tasks_to_complete();
for(int i=0;i<numReadouts;++i){ //free resources
sem_destroy(&sem_slsdone[i]); for(int i = 0; i < numReadouts; ++i){
sem_destroy(&sem_slswait[i]); pthread_join(receivingDataThreads[i],NULL);
sem_destroy(&sem_multiwait[i]); sem_destroy(&sem_singlewait[i]);
sem_destroy(&sem_singledone[i]);
delete [] singleframe[i];
} }
destroyThreadPool(&zmqthreadpool);
delete[] multiframe; delete[] multiframe;
} }

View File

@ -245,10 +245,10 @@ class multiSlsDetector : public slsDetectorUtils {
* Creates all the threads in the threadpool * Creates all the threads in the threadpool
\returns OK or FAIL \returns OK or FAIL
*/ */
int createThreadPool(ThreadPool** t); int createThreadPool();
/** destroys all the threads in the threadpool */ /** destroys all the threads in the threadpool */
void destroyThreadPool(ThreadPool** t); void destroyThreadPool();
/** frees the shared memory occpied by the sharedMultiSlsDetector structure */ /** frees the shared memory occpied by the sharedMultiSlsDetector structure */
int freeSharedMemory() ; int freeSharedMemory() ;
@ -1352,11 +1352,27 @@ class multiSlsDetector : public slsDetectorUtils {
bool getAcquiringFlag(); bool getAcquiringFlag();
private:
/**
* Static function - Starts Data Thread of this object
* @param this_pointer pointer to this object
*/
static void* startReceivingDataThread(void *this_pointer);
/**
* Thread that receives data packets from receiver
*/
void startReceivingData();
sem_t sem_singledone[MAXDET];
sem_t sem_singlewait[MAXDET];
int* singleframe[MAXDET];
/** Ensures if threads created successfully */
bool threadStarted;
/** Current Thread Index*/
int currentThreadIndex;
/** Mask with each bit indicating status of each receiving data thread */
volatile uint64_t receivingDataThreadMask;
protected: protected:
@ -1372,7 +1388,6 @@ class multiSlsDetector : public slsDetectorUtils {
private: private:
ThreadPool* threadpool; ThreadPool* threadpool;
ThreadPool* zmqthreadpool;
}; };

View File

@ -10,7 +10,6 @@
#include <cstdlib> #include <cstdlib>
#include <math.h> #include <math.h>
#include "gitInfoLib.h" #include "gitInfoLib.h"
#include <zmq.h>
int slsDetector::initSharedMemory(detectorType type, int id) { int slsDetector::initSharedMemory(detectorType type, int id) {
@ -7145,108 +7144,6 @@ int slsDetector::resetFramesCaught(){
void slsDetector::readFrameFromReceiver(){
//determine number of half readouts
int numReadout = 1;
if(thisDetector->myDetectorType == EIGER) numReadout = 2;
int readoutId = detId*numReadout;
volatile uint64_t runningMask = 0x0;
//server details
char hostname[numReadout][100];
int portno[numReadout];
int nel=(thisDetector->dataBytes/numReadout)/sizeof(int);
for(int i=0;i<numReadout;++i){
portno[i] = DEFAULT_ZMQ_PORTNO + (readoutId+i);
sprintf(hostname[i], "%s%d", "tcp://127.0.0.1:",portno[i]);
//cout << "ZMQ Client of " << readoutId+i << " at " << hostname[i] << endl;
parentDet->slsframe[readoutId+i]=new int[nel];
}
//loop though the half readouts to start sockets
void *context[numReadout];
void *zmqsocket[numReadout];
for(int i=0;i<numReadout;++i){
context[i] = zmq_ctx_new();
zmqsocket[i] = zmq_socket(context[i], ZMQ_SUB);
zmq_setsockopt(zmqsocket[i], ZMQ_SUBSCRIBE, "", 0); // an empty string implies receiving any messages
zmq_connect(zmqsocket[i], hostname[i]);// connect to publisher,the publisher server does not have to be started
runningMask|=(1<<(i));
sem_post(&parentDet->sem_multiwait[readoutId+i]); //let multi know socket created
}
//receive msgs and let multi know
zmq_msg_t message;
int len,idet = 0;
int framecount=0;
//read frame
while(true){
for(int idet=0; idet<numReadout; ++idet){
if((1 << idet) & runningMask){
sem_wait(&parentDet->sem_slswait[readoutId+idet]);//wait for it to be copied
if(!idet) framecount++; //update indices, count only once
// receive a message, this is a blocking function
len = zmq_msg_init (&message); /* is this required? Xiaoqiang didnt have it*/
if(len) {cprintf(RED,"Failed to initialize message %d for %d\n",len,readoutId+idet); continue; }//error
len = zmq_msg_recv(&message, zmqsocket[idet], 0);
//end of socket
if (len <= 3 ) {
if(!len) cprintf(RED,"Received no data in socket for %d\n", readoutId+idet);
//cout<<readoutId+idet <<" sls Received end data"<<endl;
parentDet->slsframe[readoutId+idet] = NULL;
sem_post(&parentDet->sem_slsdone[readoutId+idet]); //let multi know is ready
runningMask^=(1<<idet);
//cout<<detId<<" " << idet << " finished"<<endl;
if(!runningMask){ //all done, get out
//cout<<detId<<" all done"<<endl;
break;
}
continue;
}
if(zmq_msg_data(&message)==NULL) cprintf(RED,"GOT NULL FROM ZMQ\n"); /*not needed most likely*/
//cout<<"Received on " << readoutId+idet << " for frame " << framecount << endl;
//if(len == thisDetector->dataBytes/numReadout){//hoow to solve this
memcpy((char*)(parentDet->slsframe[readoutId+idet]),(char*)zmq_msg_data(&message),thisDetector->dataBytes/numReadout);
//check header, if incorrect frame, copy somewhere and assign a blank subframe and also check size
//jungfrau masking adcval
if(thisDetector->myDetectorType == JUNGFRAU){
for(unsigned int i=0;i<nel;i++){
parentDet->slsframe[readoutId+idet][i] = (parentDet->slsframe[readoutId+idet][i] & 0x3FFF3FFF);
}
}
//}
sem_post(&parentDet->sem_slsdone[readoutId+idet]);//let multi know is ready
}
}//end of for loop
if(!runningMask){
break;
}
}
zmq_msg_close(&message);
//close socket
for(int i=0;i<numReadout;i++){
zmq_disconnect(zmqsocket[i], hostname[i]);
zmq_close(zmqsocket[i]);
zmq_ctx_destroy(context[i]);
delete [] parentDet->slsframe[readoutId+i];
}
}
int slsDetector::lockReceiver(int lock){ int slsDetector::lockReceiver(int lock){
int fnum=F_LOCK_RECEIVER; int fnum=F_LOCK_RECEIVER;

View File

@ -1570,7 +1570,7 @@ class slsDetector : public slsDetectorUtils, public energyConversion {
/** Reads frames from receiver through a constant socket /** Reads frames from receiver through a constant socket
*/ */
void readFrameFromReceiver(); void readFrameFromReceiver(){};
/** Locks/Unlocks the connection to the receiver /** Locks/Unlocks the connection to the receiver
/param lock sets (1), usets (0), gets (-1) the lock /param lock sets (1), usets (0), gets (-1) the lock

View File

@ -350,6 +350,7 @@ int slsDetectorUtils::acquire(int delflag){
pthread_mutex_lock(&mg); pthread_mutex_lock(&mg);
stopReceiver(); stopReceiver();
pthread_mutex_unlock(&mg); pthread_mutex_unlock(&mg);
// cout<<"***********receiver stopped"<<endl;
} }

View File

@ -845,14 +845,6 @@ virtual int setReceiverFifoDepth(int i = -1)=0;
int (*progress_call)(double,void*); int (*progress_call)(double,void*);
void *pProgressCallArg; void *pProgressCallArg;
public:
//data call back
//individual sls and multi
sem_t sem_slsdone[MAXDET];
sem_t sem_slswait[MAXDET];
sem_t sem_multiwait[MAXDET];
int* slsframe[MAXDET];
}; };

View File

@ -483,6 +483,7 @@ void* postProcessing::processData(int delflag) {
//receiver //receiver
else{ else{
if(dataReady){ if(dataReady){
readFrameFromReceiver(); readFrameFromReceiver();
} }
@ -514,188 +515,9 @@ void* postProcessing::processData(int delflag) {
} }
} }
cout<<"exiting from proccessing thread"<<endl; //cout<<"exiting from proccessing thread"<<endl;
/*
int progress = 0;
char currentfName[MAX_STR_LENGTH]="";
int caught = -1;
int currentAcquisitionIndex = -1;
int currentFrameIndex = -1;
int currentSubFrameIndex = -1;
bool newData = false;
int nthframe = setReadReceiverFrequency(0);
int nx =getTotalNumberOfChannels(slsDetectorDefs::X);
int ny =getTotalNumberOfChannels(slsDetectorDefs::Y);
#ifdef VERBOSE
std::cout << "receiver read freq:" << nthframe << std::endl;
#endif
//repeat forever until joined by the calling thread
while(1){
cout.flush();
cout<<flush;
usleep(20000); //20ms need this else connecting error to receiver (too fast)
//get progress
if(setReceiverOnline() == ONLINE_FLAG){
pthread_mutex_lock(&mg);
caught = getFramesCaughtByReceiver();
pthread_mutex_unlock(&mg);
}
//updating progress
if(caught!= -1){
setCurrentProgress(caught);
#ifdef VERY_VERY_DEBUG
cout << "caught:" << caught << endl;
#endif
}
//detector acquistion done, wait for all frames received
if(acquiringDone > 0){
#ifdef VERY_VERY_DEBUG
if(acquiringDone == 1) cout << "acquiring seems to be done" << endl;
#endif
//IF GUI, check for last frames (counter upto 5)
if(dataReady){
pthread_mutex_lock(&mg);
acquiringDone++;
pthread_mutex_unlock(&mg);
#ifdef VERY_VERY_DEBUG
cout << "acquiringDone :" << acquiringDone << endl;
#endif
}
//post to stopReceiver in acquire(), but continue reading frames
if (!dataReady || (acquiringDone >= 5)){
if(!dataReady || (!nthframe) ||(!newData)){
#ifdef VERY_VERY_DEBUG
cout << "gonna post for it to end" << endl;
#endif
sem_post(&sem_queue);
#ifdef VERY_VERY_DEBUG
cout << "Sem posted" << endl;
#endif
}
}
}
//random reads and for nthframe, checks if there is no new data
else if((!nthframe) ||(!newData)){
//cout <<"cecking now" << endl;
if (checkJoinThread()){
break;
}
}
//for random reads, ask only if it has new data
if(!newData){
if(caught > progress){
newData = true;
// If new data and acquiringDone>0 (= det acq over), reset to get more frames
if(dataReady && (acquiringDone > 0)){
pthread_mutex_lock(&mg);
acquiringDone = 1;
#ifdef VERY_VERY_DEBUG
cout << "Keeping acquiringDone at 1 " << endl;
#endif
pthread_mutex_unlock(&mg);
}
}
}
if(newData){
#ifdef VERY_VERY_DEBUG
cout << "new data" << endl;
#endif
//no gui
if (!dataReady){
progress = caught;
#ifdef VERY_VERY_DEBUG
cout << "progress:" << progress << endl;
#endif
newData = false;
#ifdef VERY_VERY_DEBUG
cout << "newData set to false" << endl;
#endif
}
//gui
else{
if(setReceiverOnline()==ONLINE_FLAG){
//get data
strcpy(currentfName,"");
pthread_mutex_lock(&mg);
//int* receiverData = new int [getTotalNumberOfChannels()];
int* receiverData = readFrameFromReceiver(currentfName,currentAcquisitionIndex,currentFrameIndex,currentSubFrameIndex);
pthread_mutex_unlock(&mg);
//if detector returned null
if(setReceiverOnline()==OFFLINE_FLAG)
receiverData = NULL;
//no data or wrong data for print out
if(receiverData == NULL){
currentAcquisitionIndex = -1;
cout<<"****Detector Data returned is NULL***"<<endl;
}
// garbage frame
if(currentAcquisitionIndex < 0){
#ifdef VERY_VERY_DEBUG
cout<<"****Detector returned mismatched indices/garbage or acquisition is over. Trying again.***"<<endl;
#endif
if(receiverData)
delete [] receiverData;
}
//not garbage frame
else{// if (currentAcquisitionIndex > progress){
#ifdef VERY_VERY_DEBUG
cout << "GOT data" << endl;
#endif
fdata = decodeData(receiverData);
delete [] receiverData;
if ((fdata) && (dataReady)){
// cout << "DATAREADY 3" << endl;
thisData = new detectorData(fdata,NULL,NULL,getCurrentProgress(),currentfName,nx,ny);
dataReady(thisData, currentFrameIndex, currentSubFrameIndex, pCallbackArg);
delete thisData;
fdata = NULL;
progress = caught;
#ifdef VERY_VERY_DEBUG
cout << "progress:" << progress << endl;
#endif
newData = false;
#ifdef VERY_VERY_DEBUG
cout << "newData set to false" << endl;
#endif
}
}
}
}
}
}
*/
} }
return 0; return 0;