for users ensured proper destructors, and ctrl c should kill it

This commit is contained in:
Dhanya Maliakal 2017-07-13 12:18:45 +02:00
parent f430152f61
commit a1fd34ebb6
2 changed files with 128 additions and 98 deletions

View File

@ -6,12 +6,15 @@
#include <iostream> #include <iostream>
#include <string.h> #include <string.h>
#include <signal.h> //SIGINT #include <signal.h> //SIGINT
#include <cstdlib> //system #include <cstdlib> //system
#include "utilities.h" #include "utilities.h"
#include "logger.h" #include "logger.h"
#include <sys/types.h> //wait
#include <sys/wait.h> //wait
#include <string> #include <string>
using namespace std; using namespace std;
@ -19,35 +22,35 @@ using namespace std;
#define START_TCP_PORT 1954 #define START_TCP_PORT 1954
#define PRINT_IN_COLOR(c,f, ...) printf ("\033[%dm" f RESET, 30 + c+1, ##__VA_ARGS__) #define PRINT_IN_COLOR(c,f, ...) printf ("\033[%dm" f RESET, 30 + c+1, ##__VA_ARGS__)
slsReceiverUsers *receivers[NUM_RECEIVERS];
void deleteReceiver(slsReceiverUsers* r[]){ pid_t childPid[NUM_RECEIVERS];
for (int i = 0; i < NUM_RECEIVERS; ++i) bool keeprunning;
if (r[i]) { int numrunning;
delete r[i];
r[i] = 0;
}
}
void closeFile(int p){
deleteReceiver(receivers);
void sigChildExitedHandler(int sig) {
pid_t pid = wait(NULL);
bprintf(GRAY, "\nChild Process Pid %d exited.\n", pid);
numrunning--;
} }
void sigInterruptHandler(int p){
keeprunning = false;
}
int StartAcq(char* filepath, char* filename, uint64_t fileindex, uint32_t datasize, void*p){ int StartAcq(char* filepath, char* filename, uint64_t fileindex, uint32_t datasize, void*p){
printf("--StartAcq: filepath:%s filename:%s fileindex:%llu datasize:%u\n", bprintf(BLUE, "#### StartAcq: filepath:%s filename:%s fileindex:%llu datasize:%u ####\n",
filepath, filename, fileindex, datasize); filepath, filename, fileindex, datasize);
printf("--StartAcq: returning 0\n"); bprintf(BLUE, "--StartAcq: returning 0\n");
return 0; return 0;
} }
void AcquisitionFinished(uint64_t frames, void*p){ void AcquisitionFinished(uint64_t frames, void*p){
printf("AcquisitionFinished: frames:%llu \n",frames); bprintf(BLUE, "#### AcquisitionFinished: frames:%llu ####\n",frames);
} }
@ -56,7 +59,7 @@ void GetData(uint64_t frameNumber, uint32_t expLength, uint32_t packetNumber, ui
char* datapointer, uint32_t datasize, void* p){ char* datapointer, uint32_t datasize, void* p){
PRINT_IN_COLOR (xCoord, PRINT_IN_COLOR (xCoord,
"%d GetData: \n" "#### %d GetData: ####\n"
"frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu\t\ttimestamp: %llu\t\tmodId: %u\t\t" "frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu\t\ttimestamp: %llu\t\tmodId: %u\t\t"
"xCoord: %u\t\tyCoord: %u\t\tzCoord: %u\t\tdebug: %u\t\troundRNumber: %u\t\tdetType: %u\t\t" "xCoord: %u\t\tyCoord: %u\t\tzCoord: %u\t\tdebug: %u\t\troundRNumber: %u\t\tdetType: %u\t\t"
"version: %u\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n", "version: %u\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n",
@ -70,104 +73,131 @@ void GetData(uint64_t frameNumber, uint32_t expLength, uint32_t packetNumber, ui
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
//Catch signal SIGINT to close files properly // set default child process pid values
signal(SIGINT,closeFile); for (int i = 0; i < NUM_RECEIVERS; ++i)
childPid[i] = -1;
keeprunning = true;
numrunning = 0;
// Catch signal SIGINT to close files and call destructors properly
struct sigaction sa;
sa.sa_flags=0; // no flags
sa.sa_handler=sigInterruptHandler; // handler function
sigemptyset(&sa.sa_mask); // dont block additional signals during invocation of handler
if (sigaction(SIGINT, &sa, NULL) == -1) {
bprintf(RED, "Could not set handler function for SIGINT\n");
}
// wait for all the SIGCHILD signals
struct sigaction asa;
asa.sa_flags=0; // no flags
asa.sa_handler=sigChildExitedHandler; // handler function
sigemptyset(&asa.sa_mask); // dont block additional signals during invocation of handler
if (sigaction(SIGCHLD, &asa, NULL) == -1) {
bprintf(RED, "Could not set handler function for SICHILD\n");
}
int ret = slsReceiverDefs::OK;
int narg= 3; int narg= 3;
for (int i = 0; i < NUM_RECEIVERS; ++i) { for (int i = 0; i < NUM_RECEIVERS; ++i) {
char temp[10]; childPid[i] = fork();
sprintf(temp,"%d",START_TCP_PORT + i);
char* args[] = {(char*)"ignored", (char*)"--rx_tcpport", temp};
cprintf(BLUE,"Starting Receiver %d\n", i); // fork failed
receivers[i] = new slsReceiverUsers(narg, args, ret); if (childPid[i] < 0) {
if(ret==slsReceiverDefs::FAIL){ bprintf(RED,"fork() failed. Killing all the receiver objects\n");
deleteReceiver(receivers); raise(SIGINT);
return -1;
} }
//register callbacks // child process
//remember to set file write enable to 0 (using the client) if we should not write files and else if (childPid[i] == 0) {
//you will write data using the callbacks bprintf(BLUE,"Starting Receiver %d with pid %ld\n", i, (long)getpid());
/** char temp[10];
* Call back for start acquisition sprintf(temp,"%d",START_TCP_PORT + i);
* callback arguments are char* args[] = {(char*)"ignored", (char*)"--rx_tcpport", temp};
* filepath int ret = slsReceiverDefs::OK;
* filename slsReceiverUsers *receiver = new slsReceiverUsers(narg, args, ret);
* fileindex if(ret==slsReceiverDefs::FAIL){
* datasize delete receiver;
* exit(EXIT_FAILURE);
* return value is insignificant at the moment }
* we write depending on file write enable
* users get data to write depending on call backs registered
*/
printf("Registering StartAcq()\n");
receivers[i]->registerCallBackStartAcquisition(StartAcq, NULL);
/** //register callbacks
* Call back for acquisition finished //remember to set file write enable to 0 (using the client) if we should not write files and
* callback argument is //you will write data using the callbacks
* total frames caught
*/
printf("Registering AcquisitionFinished()\n");
receivers[i]->registerCallBackAcquisitionFinished(AcquisitionFinished, NULL);
/** /**
* Call back for raw data * Call back for start acquisition
* args to raw data ready callback are * callback arguments are
* frameNumber is the frame number * filepath
* expLength is the subframe number (32 bit eiger) or real time exposure time in 100ns (others) * filename
* packetNumber is the packet number * fileindex
* bunchId is the bunch id from beamline * datasize
* timestamp is the time stamp with 10 MHz clock *
* modId is the unique module id (unique even for left, right, top, bottom) * return value is insignificant at the moment
* xCoord is the x coordinate in the complete detector system * we write depending on file write enable
* yCoord is the y coordinate in the complete detector system * users get data to write depending on call backs registered
* zCoord is the z coordinate in the complete detector system */
* debug is for debugging purposes bprintf(BLUE, "Registering StartAcq()\n");
* roundRNumber is the round robin set number receiver->registerCallBackStartAcquisition(StartAcq, NULL);
* detType is the detector type see :: detectorType
* version is the version number of this structure format /**
* dataPointer is the pointer to the data * Call back for acquisition finished
* dataSize in bytes is the size of the data in bytes * callback argument is
*/ * total frames caught
printf("Registering GetData() \n"); */
receivers[i]->registerCallBackRawDataReady(GetData,NULL); bprintf(BLUE, "Registering AcquisitionFinished()\n");
receiver->registerCallBackAcquisitionFinished(AcquisitionFinished, NULL);
/**
* Call back for raw data
* args to raw data ready callback are
* frameNumber is the frame number
* expLength is the subframe number (32 bit eiger) or real time exposure time in 100ns (others)
* packetNumber is the packet number
* bunchId is the bunch id from beamline
* timestamp is the time stamp with 10 MHz clock
* modId is the unique module id (unique even for left, right, top, bottom)
* xCoord is the x coordinate in the complete detector system
* yCoord is the y coordinate in the complete detector system
* zCoord is the z coordinate in the complete detector system
* debug is for debugging purposes
* roundRNumber is the round robin set number
* detType is the detector type see :: detectorType
* version is the version number of this structure format
* dataPointer is the pointer to the data
* dataSize in bytes is the size of the data in bytes
*/
bprintf(BLUE, "Registering GetData() \n");
receiver->registerCallBackRawDataReady(GetData,NULL);
//start tcp server thread //start tcp server thread
ret = receivers[i]->start(); if (receiver->start() == slsReceiverDefs::FAIL){
if(ret == slsReceiverDefs::FAIL){ delete receiver;
for (int i = 0; i < i; ++i) exit(EXIT_FAILURE);
receivers[i]->stop(); }
deleteReceiver(receivers);
return -1; while(keeprunning);
delete receiver;
exit(EXIT_SUCCESS);
} }
// parent process
else
numrunning++;
} }
FILE_LOG(logINFO) << "Ready ... ";
bprintf(GRAY, "\n[ Press \'Ctrl+c\' to exit ]\n");
FILE_LOG(logDEBUG1) << "DONE!" << endl; // wait for all child processes to exit
cprintf( BLUE, "Type \'q\' to exit\n"); while(numrunning);
string str; FILE_LOG(logINFO) << "Goodbye!";
cin>>str;
//wait and look for an exit keyword
while(str.find("q") == string::npos)
cin>>str;
//stop tcp server thread, stop udp socket
for (int i = 0; i < NUM_RECEIVERS; ++i) {
cprintf(BLUE,"Stopping Receiver %d\n",i);
receivers[i]->stop();
}
deleteReceiver(receivers);
return 0; return 0;
} }

Binary file not shown.