00001
00019 #include "sls_receiver_defs.h"
00020 #include "slsReceiverUsers.h"
00021
00022 #include <iostream>
00023 #include <string.h>
00024 #include <signal.h>
00025 #include <cstdlib>
00026
00027
00028 #include <sys/types.h>
00029 #include <sys/wait.h>
00030 #include <string>
00031 #include <unistd.h>
00032 #include <errno.h>
00033 #include <syscall.h>
00034 using namespace std;
00035
00036
00038 #define PRINT_IN_COLOR(c,f, ...) printf ("\033[%dm" f RESET, 30 + c+1, ##__VA_ARGS__)
00039
00040
00042 bool keeprunning;
00043
00048 void sigInterruptHandler(int p){
00049 keeprunning = false;
00050 }
00051
00055 void printHelp() {
00056 cprintf(RESET, "Usage:\n"
00057 "./detReceiver [start_tcp_port] [num_receivers] [1 for call back, 0 for none]\n\n");
00058 exit(EXIT_FAILURE);
00059 }
00060
00072 int StartAcq(char* filepath, char* filename, uint64_t fileindex, uint32_t datasize, void*p){
00073 cprintf(BLUE, "#### StartAcq: filepath:%s filename:%s fileindex:%llu datasize:%u ####\n",
00074 filepath, filename, fileindex, datasize);
00075
00076 cprintf(BLUE, "--StartAcq: returning 0\n");
00077 return 0;
00078 }
00079
00085 void AcquisitionFinished(uint64_t frames, void*p){
00086 cprintf(BLUE, "#### AcquisitionFinished: frames:%llu ####\n",frames);
00087 }
00088
00109 void GetData(uint64_t frameNumber, uint32_t expLength, uint32_t packetNumber, uint64_t bunchId, uint64_t timestamp,
00110 uint16_t modId, uint16_t xCoord, uint16_t yCoord, uint16_t zCoord, uint32_t debug, uint16_t roundRNumber, uint8_t detType, uint8_t version,
00111 char* datapointer, uint32_t datasize, void* p){
00112
00113 PRINT_IN_COLOR (modId?modId:xCoord,
00114 "#### %d GetData: ####\n"
00115 "frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu\t\ttimestamp: %llu\t\tmodId: %u\t\t"
00116 "xCoord: %u\t\tyCoord: %u\t\tzCoord: %u\t\tdebug: %u\t\troundRNumber: %u\t\tdetType: %u\t\t"
00117 "version: %u\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n",
00118 xCoord, frameNumber, expLength, packetNumber, bunchId, timestamp, modId,
00119 xCoord, yCoord, zCoord, debug, roundRNumber, detType, version,
00120 ((uint8_t)(*((uint8_t*)(datapointer)))), datasize);
00121 }
00122
00123
00124
00132 int main(int argc, char *argv[]) {
00133
00135 int numReceivers = 1;
00136 int startTCPPort = 1954;
00137 int withCallback = 0;
00138 keeprunning = true;
00139
00141 if ( (argc != 4) || (!sscanf(argv[1],"%d", &startTCPPort)) || (!sscanf(argv[2],"%d", &numReceivers)) || (!sscanf(argv[3],"%d", &withCallback)) )
00142 printHelp();
00143 cprintf(BLUE,"Parent Process Created [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
00144 cprintf(RESET, "Number of Receivers: %d\n", numReceivers);
00145 cprintf(RESET, "Start TCP Port: %d\n", startTCPPort);
00146 cprintf(RESET, "Callback Enable: %d\n", withCallback);
00147
00148
00149
00151 struct sigaction sa;
00152 sa.sa_flags=0;
00153 sa.sa_handler=sigInterruptHandler;
00154 sigemptyset(&sa.sa_mask);
00155 if (sigaction(SIGINT, &sa, NULL) == -1) {
00156 cprintf(RED, "Could not set handler function for SIGINT\n");
00157 }
00158
00161 struct sigaction asa;
00162 asa.sa_flags=0;
00163 asa.sa_handler=SIG_IGN;
00164 sigemptyset(&asa.sa_mask);
00165 if (sigaction(SIGPIPE, &asa, NULL) == -1) {
00166 cprintf(RED, "Could not set handler function for SIGPIPE\n");
00167 }
00168
00169
00171 for (int i = 0; i < numReceivers; ++i) {
00172
00174 pid_t pid = fork();
00175
00177 if (pid < 0) {
00178 cprintf(RED,"fork() failed. Killing all the receiver objects\n");
00179 raise(SIGINT);
00180 }
00181
00183 else if (pid == 0) {
00184 cprintf(BLUE,"Child process %d [ Tid: %ld ]\n", i, (long)syscall(SYS_gettid));
00185
00186 char temp[10];
00187 sprintf(temp,"%d",startTCPPort + i);
00188 char* args[] = {(char*)"ignored", (char*)"--rx_tcpport", temp};
00189 int ret = slsReceiverDefs::OK;
00191 slsReceiverUsers *receiver = new slsReceiverUsers(3, args, ret);
00192 if(ret==slsReceiverDefs::FAIL){
00193 delete receiver;
00194 exit(EXIT_FAILURE);
00195 }
00196
00197
00200 if (withCallback) {
00201
00203 cprintf(BLUE, "Registering StartAcq()\n");
00204 receiver->registerCallBackStartAcquisition(StartAcq, NULL);
00205
00207 cprintf(BLUE, "Registering AcquisitionFinished()\n");
00208 receiver->registerCallBackAcquisitionFinished(AcquisitionFinished, NULL);
00209
00210
00211 cprintf(BLUE, "Registering GetData() \n");
00212 receiver->registerCallBackRawDataReady(GetData,NULL);
00213 }
00214
00215
00216
00218 if (receiver->start() == slsReceiverDefs::FAIL){
00219 delete receiver;
00220 cprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
00221 exit(EXIT_FAILURE);
00222 }
00223
00225 while(keeprunning)
00226 pause();
00228 delete receiver;
00229 cprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
00230 exit(EXIT_SUCCESS);
00231 break;
00232 }
00233 }
00234
00236 sa.sa_flags=0;
00237 sa.sa_handler=SIG_IGN;
00238 sigemptyset(&sa.sa_mask);
00239 if (sigaction(SIGINT, &sa, NULL) == -1) {
00240 cprintf(RED, "Could not set handler function for SIGINT\n");
00241 }
00242
00243
00245 cout << "Ready ... " << endl;
00246 cprintf(RESET, "\n[ Press \'Ctrl+c\' to exit ]\n");
00247
00249 for(;;) {
00250 pid_t childPid = waitpid (-1, NULL, 0);
00251
00252
00253 if (childPid == -1) {
00254 if (errno == ECHILD) {
00255 cprintf(GREEN,"All Child Processes have been closed\n");
00256 break;
00257 } else {
00258 cprintf(RED, "Unexpected error from waitpid(): (%s)\n",strerror(errno));
00259 break;
00260 }
00261 }
00262
00263
00264 cprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long int) childPid);
00265 }
00266
00267 cout << "Goodbye!" << endl;
00268 return 0;
00269 }
00270