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 <iostream>
#include <string>
#include <zmq.h>
using namespace std;
@ -267,30 +268,28 @@ multiSlsDetector::multiSlsDetector(int id) : slsDetectorUtils(), shmId(-1)
getNMods();
getMaxMods();
threadpool = 0;
zmqthreadpool = 0;
if(createThreadPool(&threadpool) == FAIL)
if(createThreadPool() == FAIL)
exit(-1);
}
multiSlsDetector::~multiSlsDetector() {
//removeSlsDetector();
destroyThreadPool(&threadpool);
destroyThreadPool(&zmqthreadpool);
destroyThreadPool();
}
int multiSlsDetector::createThreadPool(ThreadPool** t){
if(*t){
(ThreadPool*)(*t)->destroy_threadpool();
*t=0;
int multiSlsDetector::createThreadPool(){
if(threadpool){
threadpool->destroy_threadpool();
threadpool=0;
}
if(thisMultiDetector->numberOfDetectors < 1){
cout << "No detectors attached to create threadpool" << endl;
return OK;
}
*t = new ThreadPool(thisMultiDetector->numberOfDetectors);
switch(((ThreadPool*)(*t))->initialize_threadpool()){
threadpool = new ThreadPool(thisMultiDetector->numberOfDetectors);
switch(threadpool->initialize_threadpool()){
case 0:
cerr << "Failed to initialize thread pool!" << endl;
return FAIL;
@ -301,19 +300,19 @@ int multiSlsDetector::createThreadPool(ThreadPool** t){
break;
default:
#ifdef VERBOSE
cout << "Initialized Threadpool " << *t << endl;
cout << "Initialized Threadpool " << threadpool << endl;
#endif
break;
}
return OK;
}
void multiSlsDetector::destroyThreadPool(ThreadPool** t){
if(*t){
(ThreadPool*)(*t)->destroy_threadpool();
*t=0;
void multiSlsDetector::destroyThreadPool(){
if(threadpool){
threadpool->destroy_threadpool();
threadpool=0;
#ifdef VERBOSE
cout<<"Destroyed Threadpool "<< *t << endl;
cout<<"Destroyed Threadpool "<< threadpool << endl;
#endif
}
}
@ -394,7 +393,7 @@ int multiSlsDetector::addSlsDetector(int id, int pos) {
//set offsets
updateOffsets();
if(createThreadPool(&threadpool) == FAIL)
if(createThreadPool() == FAIL)
exit(-1);
@ -865,7 +864,7 @@ int multiSlsDetector::removeSlsDetector(int pos) {
}
updateOffsets();
if(createThreadPool(&threadpool) == FAIL)
if(createThreadPool() == FAIL)
exit(-1);
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(){
//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
int maxX=0,maxY=0;
@ -4980,125 +5075,131 @@ void multiSlsDetector::readFrameFromReceiver(){
int numReadouts = numReadoutPerDetector * thisMultiDetector->numberOfDetectors;
//start all socket tasks
volatile uint64_t runningMask = 0x0;
int slsdatabytes = 0, slsmaxchannels = 0, bytesperchannel = 0, slsmaxX = 0, slsmaxY=0;
if(!zmqthreadpool){
cout << "Error in creating threadpool. Exiting" << endl;
return;
}else{
for(int idet=0; idet<thisMultiDetector->numberOfDetectors; idet++){
if(detectors[idet]){
sem_init(&sem_slswait[idet*numReadoutPerDetector],1,0);
sem_init(&sem_slsdone[idet*numReadoutPerDetector],1,0);
sem_init(&sem_multiwait[idet*numReadoutPerDetector],1,0);
if(numReadoutPerDetector>1){
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));
}
//create threads
/** Data Callback Threads */
pthread_t receivingDataThreads[numReadouts];
volatile uint64_t expectedMask = 0x0;
receivingDataThreadMask = 0x0;
currentThreadIndex = -1;
for(int i = 0; i < numReadouts; ++i){
threadStarted = false;
currentThreadIndex = i;
sem_init(&sem_singlewait[i],1,0);
sem_init(&sem_singledone[i],1,0);
if(pthread_create(&receivingDataThreads[i], NULL,startReceivingDataThread, (void*) this)){
cprintf(RED, "ERROR: Could not create receiving thread with index %d\n",i);
return;
}
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
sem_wait(&sem_multiwait[i]);
sem_post(&dataThreadStartedSemaphore); //let utils:acquire continue to start measurement/acquisition
//cout<<"multi waiting for all threads to be created "<<hex<<receivingDataThreadMask<<" to be matched with " <<expectedMask<< endl;
//wait for the last few threads remaining to be ready
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);
if(nel <= 0){
cout << "Multislsdetector databytes not valid :" << thisMultiDetector->dataBytes << endl;
cprintf(RED,"Error: Multislsdetector databytes not valid : %d\n", thisMultiDetector->dataBytes);
return;
}
int* multiframe=new int[nel];
int* p = multiframe;
int idet,offsetY,offsetX;
int halfreadoutoffset = (slsmaxX/numReadoutPerDetector);
//after reconstruction
int framecount=0;
int nx =getTotalNumberOfChannels(slsDetectorDefs::X);
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
while(true){
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){
idet = ireadout/numReadoutPerDetector;
if(detectors[idet]){
if((1 << ireadout) & runningMask){ //if running
sem_post(&sem_slswait[ireadout]); //sls to continue
sem_wait(&sem_slsdone[ireadout]); //wait for sls to copy
if((1 << ireadout) & receivingDataThreadMask){ //if running
//wait for single to copy
//cprintf(BLUE,"multi waiting done by det %d\n",ireadout);
sem_wait(&sem_singledone[ireadout]); //wait for sls to copy
//cprintf(BLUE,"multi got done by det %d\n",ireadout);
//this socket closed
if(slsframe[ireadout] == NULL){
runningMask^=(1<<ireadout);
if(!runningMask){ //all done, get out
break;
}
continue;
//this socket closed
if(!((1 << ireadout) & receivingDataThreadMask)){ //if running
continue;
}
//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/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);
}
//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
//top
else{
memcpy(p,multiframe,slsdatabytes);
p+=slsdatabytes/sizeof(int);
for(int i=0;i<slsmaxY;++i)
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;
}
//send data to callback
fdata = decodeData(multiframe);
if ((fdata) && (dataReady)){
@ -5112,13 +5213,14 @@ void multiSlsDetector::readFrameFromReceiver(){
setCurrentProgress(framecount);
}
zmqthreadpool->wait_for_tasks_to_complete();
for(int i=0;i<numReadouts;++i){
sem_destroy(&sem_slsdone[i]);
sem_destroy(&sem_slswait[i]);
sem_destroy(&sem_multiwait[i]);
//free resources
for(int i = 0; i < numReadouts; ++i){
pthread_join(receivingDataThreads[i],NULL);
sem_destroy(&sem_singlewait[i]);
sem_destroy(&sem_singledone[i]);
delete [] singleframe[i];
}
destroyThreadPool(&zmqthreadpool);
delete[] multiframe;
}

View File

@ -245,10 +245,10 @@ class multiSlsDetector : public slsDetectorUtils {
* Creates all the threads in the threadpool
\returns OK or FAIL
*/
int createThreadPool(ThreadPool** t);
int createThreadPool();
/** destroys all the threads in the threadpool */
void destroyThreadPool(ThreadPool** t);
void destroyThreadPool();
/** frees the shared memory occpied by the sharedMultiSlsDetector structure */
int freeSharedMemory() ;
@ -1352,11 +1352,27 @@ class multiSlsDetector : public slsDetectorUtils {
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:
@ -1372,7 +1388,6 @@ class multiSlsDetector : public slsDetectorUtils {
private:
ThreadPool* threadpool;
ThreadPool* zmqthreadpool;
};

View File

@ -10,7 +10,6 @@
#include <cstdlib>
#include <math.h>
#include "gitInfoLib.h"
#include <zmq.h>
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 fnum=F_LOCK_RECEIVER;

View File

@ -1570,7 +1570,7 @@ class slsDetector : public slsDetectorUtils, public energyConversion {
/** Reads frames from receiver through a constant socket
*/
void readFrameFromReceiver();
void readFrameFromReceiver(){};
/** Locks/Unlocks the connection to the receiver
/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);
stopReceiver();
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*);
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
else{
if(dataReady){
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;