Added startAcquisition, acquisitionFinished and rawDataReady callbacks to receiver

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@480 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
bergamaschi 2013-03-05 15:34:19 +00:00
parent 6aa90bba79
commit b44cc16cff
5 changed files with 356 additions and 119 deletions

View File

@ -2,8 +2,6 @@
The port number is passed as an argument */
#include "sls_detector_defs.h"
#include "receiver_defs.h"
#include "MySocketTCP.h"
#include "slsReceiver_funcs.h"
@ -13,79 +11,80 @@
#include <iostream>
using namespace std;
//static MySocketTCP *mysocket = NULL;
void closeFile(int p){cout<<"in closefile in receiver"<<endl;
slsReceiverFuncs::closeFile(p);
//mysocket->Disconnect();
exit(0);
void closeFile(int p){
cout<<"close file in receiver"<<endl;
slsReceiverFuncs::closeFile(p);
exit(0);
}
int main(int argc, char *argv[])
{
int ret = slsDetectorDefs::OK;
MySocketTCP *mysocket = NULL;
string fname = "";
bool shortfname = false;
int main(int argc, char *argv[]) {
int ret = slsDetectorDefs::OK;
slsReceiverFuncs *receiver = new slsReceiverFuncs(argc, argv, ret);
if(ret==slsDetectorDefs::FAIL)
return -1;
//Catch signal SIGINT to close files properly
signal(SIGINT,closeFile);
//register callbacks
//parse command line for config
for(int iarg=1;iarg<argc;iarg++){
if((!strcasecmp(argv[iarg],"-config"))||(!strcasecmp(argv[iarg],"-f"))){
if(iarg+1==argc){
cout << "no config file name given. Exiting." << endl;
return -1;
}
fname.assign(argv[iarg+1]);
}
if(!strcasecmp(argv[iarg],"-shortfname"))
shortfname = true;
}
/**
callback arguments are
filepath
filename
fileindex
datasize
return value is
0 raw data ready callback takes care of open,close,write file
1 callback writes file, we have to open, close it
2 we open, close, write file, callback does not do anything
registerCallBackStartAcquisition(int (*func)(char*, char*,int, int, void*),void *arg);
*/
//receiver->registerCallBackStartAcquisition(func,arg);
/**
callback argument is
total farmes caught
registerCallBackAcquisitionFinished(void (*func)(int, void*),void *arg);
*/
//receiver->registerCallBackAcquisitionFinished(func,arg);
/**
args to raw data ready callback are
framenum
datapointer
file descriptor
guidatapointer (NULL, no data required)
NEVER DELETE THE DATA POINTER
REMEMBER THAT THE CALLBACK IS BLOCKING
registerCallBackRawDataReady(void (*func)(int, char*, FILE*, char*, void*),void *arg);
*/
//receiver->registerCallBackRawDataReady(func,arg);
//reads config file, creates socket, assigns function table
slsReceiverFuncs *receiver = new slsReceiverFuncs(mysocket,fname,ret, shortfname);
if(ret==slsDetectorDefs::FAIL)
return -1;
//Catch signal SIGINT to close files properly
signal(SIGINT,closeFile);
#ifdef VERBOSE
cout << "Function table assigned." << endl;
#endif
cout << " Ready..." << endl;
//waits for connection
while(ret!=GOODBYE) {
#ifdef VERBOSE
cout<< endl;
#endif
#ifdef VERY_VERBOSE
cout << "Waiting for client call" << endl;
#endif
if(mysocket->Connect()>=0){
#ifdef VERY_VERBOSE
cout << "Conenction accepted" << endl;
#endif
ret = receiver->decode_function();
#ifdef VERY_VERBOSE
cout << "function executed" << endl;
#endif
mysocket->Disconnect();
#ifdef VERY_VERBOSE
cout << "connection closed" << endl;
#endif
}
}
slsReceiverFuncs::closeFile(0);
cout << "Goodbye!" << endl;
delete mysocket;
return 0;
receiver->start();
return 0;
}

View File

@ -57,7 +57,14 @@ slsReceiverFunctionList::slsReceiverFunctionList(bool shortfname):
guiRequiresData(0),
currframenum(0),
writeReceiverData(0),
pwriteReceiverDataArg(0)
pwriteReceiverDataArg(0),
startAcquisitionCallBack(NULL),
pStartAcquisition(NULL),
acquisitionFinishedCallBack(NULL),
pAcquisitionFinished(NULL),
rawDataReadyCallBack(NULL),
pRawDataReady(NULL)
{
strcpy(savefilename,"");
strcpy(actualfilename,"");
@ -387,7 +394,7 @@ int slsReceiverFunctionList::startWriting(){
// Variable and structure definitions
int ret,sleepnumber=0;//currframenum,ret;
// dataStruct *dataWriteFrame;
char *guiData;
//reset variables for each acquisition
framesInFile=0;
framesCaught=0;
@ -401,19 +408,38 @@ int slsReceiverFunctionList::startWriting(){
cout << "Note: Data Write has been defined exernally" << endl;
cout << "Ready!" << endl;
if(!enableFileWrite)
cbAction=2;
if (startAcquisitionCallBack)
cbAction=startAcquisitionCallBack(filePath,fileName,fileIndex,bufferSize,pStartAcquisition);
if(enableFileWrite==0 || cbAction==0)
cout << endl << "Note: Data will not be saved" << endl;
//create file name
if(frameIndexNeeded==-1) sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex);
else sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex);
if(frameIndexNeeded==-1)
sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex);
else
sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex);
//for sebastian
if(!shortFileName) strcpy(actualfilename,savefilename);
else sprintf(actualfilename, "%s/%s_%d_%09d.raw", filePath,fileName,fileIndex, 0);
if(!shortFileName)
strcpy(actualfilename,savefilename);
else
sprintf(actualfilename, "%s/%s_%d_%09d.raw", filePath,fileName,fileIndex, 0);
//start writing
if(enableFileWrite){
if(enableFileWrite || cbAction>0){
sfilefd = fopen((const char *) (actualfilename), "w");
cout << actualfilename << endl;
}
@ -426,26 +452,26 @@ int slsReceiverFunctionList::startWriting(){
if (framesInFile == maxFramesPerFile) {
//create file name
if(frameIndexNeeded==-1) sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex);
else sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex);
if(frameIndexNeeded==-1) sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex);
else sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex);
//for sebastian
if(!shortFileName) strcpy(actualfilename,savefilename);
else sprintf(actualfilename, "%s/%s_%d_%09d.raw", filePath,fileName,fileIndex, shortFileNameIndex);
shortFileNameIndex++;
if(!shortFileName) strcpy(actualfilename,savefilename);
else sprintf(actualfilename, "%s/%s_%d_%09d.raw", filePath,fileName,fileIndex, shortFileNameIndex);
shortFileNameIndex++;
//start writing in new file
if(enableFileWrite){
fclose(sfilefd);
sfilefd = fopen((const char *) (actualfilename), "w");
}
if(enableFileWrite || cbAction>0){
fclose(sfilefd);
sfilefd = fopen((const char *) (actualfilename), "w");
}
//currframenum=(int)(*((int*)latestData));
cout << actualfilename << "\tpacket loss " << fixed << setprecision(4) << ((currframenum-prevframenum-(packetsPerFrame*framesInFile))/(double)(packetsPerFrame*framesInFile))*100.000 << "%\t\t"
"framenum " << currframenum << "\t\t"
"p " << prevframenum << endl;
cout << actualfilename << "\tpacket loss " << fixed << setprecision(4) << ((currframenum-prevframenum-(packetsPerFrame*framesInFile))/(double)(packetsPerFrame*framesInFile))*100.000 << "%\t\t"
"framenum " << currframenum << "\t\t"
"p " << prevframenum << endl;
prevframenum=currframenum;
framesInFile = 0;
prevframenum=currframenum;
framesInFile = 0;
}
@ -459,12 +485,21 @@ int slsReceiverFunctionList::startWriting(){
totalFramesCaught++;
currframenum = (int)(*((int*)dataWriteFrame->buffer));
if(guiRequiresData)
guiData=latestData;
else
guiData=NULL;
if(enableFileWrite){
if (writeReceiverData)
writeReceiverData(dataWriteFrame->buffer,dataWriteFrame->rc, sfilefd, pwriteReceiverDataArg);
else
fwrite(dataWriteFrame->buffer, 1, dataWriteFrame->rc, sfilefd);
if (writeReceiverData)
writeReceiverData(dataWriteFrame->buffer,dataWriteFrame->rc, sfilefd, pwriteReceiverDataArg);
if (cbAction<2) {
rawDataReadyCallBack(currframenum, dataWriteFrame->buffer,sfilefd, guiData,pRawDataReady);
} else {
fwrite(dataWriteFrame->buffer, 1, dataWriteFrame->rc, sfilefd);
}
}
if(guiRequiresData){
memcpy(latestData,dataWriteFrame->buffer,bufferSize);
guiRequiresData=0;
@ -485,11 +520,17 @@ int slsReceiverFunctionList::startWriting(){
cout << "Total Frames Caught:"<< totalFramesCaught << endl;
//close file
if(sfilefd) fclose(sfilefd);
#ifdef VERBOSE
cout << "sfield:" << (int)sfilefd << endl;
#endif
if (acquisitionFinishedCallBack)
acquisitionFinishedCallBack(totalFramesCaught, pAcquisitionFinished);
return 0;
}

View File

@ -1,4 +1,6 @@
#ifdef SLS_RECEIVER_FUNCTION_LIST
#ifndef SLS_RECEIVER_FUNCTION_LIST_H
#define SLS_RECEIVER_FUNCTION_LIST_H
/********************************************//**
* @file slsReceiverFunctionList.h
* @short does all the functions for a receiver, set/get parameters, start/stop etc.
@ -299,6 +301,50 @@ private:
int (*writeReceiverData)(char*,int,FILE*,void*);
void *pwriteReceiverDataArg;
/**
callback arguments are
filepath
filename
fileindex
data size
return value is
0 callback takes care of open,close,write file
1 callback writes file, we have to open, close it
2 we open, close, write file, callback does not do anything
*/
int (*startAcquisitionCallBack)(char*, char*,int, int, void*);
void *pStartAcquisition;
/**
args to acquisition finished callback
total frames caught
*/
void (*acquisitionFinishedCallBack)(int, void*);
void *pAcquisitionFinished;
/**
args to raw data ready callback are
framenum
datapointer
file descriptor
guidatapointer (NULL, no data required)
*/
void (*rawDataReadyCallBack)(int, char*, FILE*, char*, void*);
void *pRawDataReady;
int cbAction;
public:
/** File Descriptor */
static FILE *sfilefd;
@ -308,7 +354,59 @@ public:
dataStruct *dataWriteFrame;
/**
callback arguments are
filepath
filename
fileindex
datasize
return value is
0 callback takes care of open,close,wrie file
1 callback writes file, we have to open, close it
2 we open, close, write file, callback does not do anything
*/
void registerCallBackStartAcquisition(int (*func)(char*, char*,int, int, void*),void *arg){startAcquisitionCallBack=func; pStartAcquisition=arg;};
/**
callback argument is
toatal frames caught
*/
int registerCallBackAcquisitionFinished(void (*func)(int, void*),void *arg){acquisitionFinishedCallBack=func; pAcquisitionFinished=arg;};
/**
args to raw data ready callback are
framenum
datapointer
file descriptor
guidatapointer (NULL, no data required)
*/
int registerCallBackRawDataReady(void (*func)(int, char*, FILE*, char*, void*),void *arg){rawDataReadyCallBack=func; pRawDataReady=arg;};
};
#endif
#endif

View File

@ -19,8 +19,18 @@ using namespace std;
int slsReceiverFuncs::file_des(-1);
int slsReceiverFuncs::socketDescriptor(-1);
slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int &success, bool shortfname):
socket(mySocket),
slsReceiverFuncs::~slsReceiverFuncs() {
slsReceiverFuncs::closeFile(0);
cout << "Goodbye!" << endl;
delete socket;
}
slsReceiverFuncs::slsReceiverFuncs(int argc, char *argv[], int &success):
socket(NULL),
ret(OK),
lockStatus(0),
shortFrame(-1),
@ -31,6 +41,23 @@ slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int
string sLine,sargname;
int iline = 0;
success=slsDetectorDefs::OK;
string fname = "";
bool shortfname = false;
//parse command line for config
for(int iarg=1;iarg<argc;iarg++){
if((!strcasecmp(argv[iarg],"-config"))||(!strcasecmp(argv[iarg],"-f"))){
if(iarg+1==argc){
cout << "no config file name given. Exiting." << endl;
success=slsDetectorDefs::FAIL;
}
fname.assign(argv[iarg+1]);
}
if(!strcasecmp(argv[iarg],"-shortfname"))
shortfname = true;
}
if(!fname.empty()){
#ifdef VERBOSE
@ -84,14 +111,12 @@ slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int
if(success == OK){
//create socket
MySocketTCP* mySocket = new MySocketTCP(port_no);
if (mySocket->getErrorStatus())
socket = new MySocketTCP(port_no);
if (socket->getErrorStatus()) {
success = FAIL;
else{
delete socket;
socket = mySocket;
socket=NULL;
} else {
//initialize variables
strcpy(socket->lastClientIP,"none");
strcpy(socket->thisClientIP,"none1");
@ -100,16 +125,47 @@ slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int
function_table();
slsReceiverList = new slsReceiverFunctionList(shortfname);
#ifdef VERBOSE
cout << "Function table assigned." << endl;
#endif
file_des=socket->getFileDes();
socketDescriptor=socket->getsocketDescriptor();
success = OK;
//success = OK;
}
}
}
void slsReceiverFuncs::start(){
int v=slsDetectorDefs::OK;
while(v!=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
v = decode_function();
#ifdef VERY_VERBOSE
cout << "function executed" << endl;
#endif
socket->Disconnect();
#ifdef VERY_VERBOSE
cout << "connection closed" << endl;
#endif
}
}
}
int slsReceiverFuncs::function_table(){

View File

@ -8,6 +8,7 @@
#include "sls_detector_defs.h"
#include "receiver_defs.h"
#include "MySocketTCP.h"
#include "slsReceiverFunctionList.h"
class slsReceiverFunctionList;
@ -22,16 +23,63 @@ public:
/**
* Constructor
* reads config file, creates socket, assigns function table
* @param mySocket tcp socket connecting receiver and client
* @param fname name of config file
* @param success if socket creation was successfull
* @param shortfname true if short file name required
* @param argc from command line
* @param argv from command line
* @param succecc socket creation was successfull
*/
slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int &success, bool shortfname);
slsReceiverFuncs(int argc, char *argv[], int &success);
/** starts listening on the TCP port for client comminication */
void start();
/** Destructor */
virtual ~slsReceiverFuncs(){};
virtual ~slsReceiverFuncs();
/** Close File */
static void closeFile(int p);
/**
callback arguments are
filepath
filename
fileindex
data size
return value is
0 callback takes care of open,close,wrie file
1 callback writes file, we have to open, close it
2 we open, close, write file, callback does not do anything
*/
void registerCallBackStartAcquisition(int (*func)(char*, char*,int, int, void*),void *arg){slsReceiverList->registerCallBackStartAcquisition(func,arg);};;
/**
callback argument is
toatal farmes caught
*/
int registerCallBackAcquisitionFinished(void (*func)(int, void*),void *arg){slsReceiverList->registerCallBackAcquisitionFinished(func,arg);};
/**
args to raw data ready callback are
framenum
datapointer
file descriptor
guidatapointer (NULL, no data required)
*/
int registerCallBackRawDataReady(void (*func)(int, char*, FILE*, char*, void*),void *arg){slsReceiverList->registerCallBackRawDataReady(func,arg);};
private:
/** assigns functions to the fnum enum */
int function_table();
@ -41,9 +89,6 @@ public:
/** Unrecognized Function */
int M_nofunc();
/** Close File */
static void closeFile(int p);
/** Set File name without frame index, file index and extension */
int set_file_name();
@ -111,13 +156,7 @@ public:
/** Execute command */
int exec_command();
//private:
/** Socket */
MySocketTCP*& socket;
private:
//private:
/** slsReceiverFunctionList object */
slsReceiverFunctionList *slsReceiverList;
@ -145,6 +184,10 @@ private:
static int file_des;
static int socketDescriptor;
//private:
protected:
/** Socket */
MySocketTCP* socket;
};