- Main Page
- Classes
- Files

- File List
- File Members

# mainReceiver.cpp

Go to the documentation of this file.

    00001
    00019 #include "sls_receiver_defs.h"
    00020 #include "slsReceiverUsers.h"
    00021
    00022 #include <iostream>
    00023 #include <string.h>
    00024 #include <signal.h>     //SIGINT
    00025 #include <cstdlib>              //system
    00026 //#include "utilities.h"
    00027 //#include "logger.h"
    00028 #include <sys/types.h>  //wait
    00029 #include <sys/wait.h>   //wait
    00030 #include <string>
    00031 #include <unistd.h>     //usleep
    00032 #include <errno.h>
    00033 #include <syscall.h>    //tid
    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         bprintf(GRAY, "Usage:\n"
    00057                         "./detReceiver\n"
    00058                         "or ./detReceiver [start_tcp_port] [num_receivers]\n"
    00059                         "Default values: start_tcp_port - 1954, num_receivers - 1\n\n");
    00060 }
    00061
    00073 int StartAcq(char* filepath, char* filename, uint64_t fileindex, uint32_t datasize, void*p){
    00074         bprintf(BLUE, "#### StartAcq:  filepath:%s  filename:%s fileindex:%llu  datasize:%u ####\n",
    00075                         filepath, filename, fileindex, datasize);
    00076
    00077         bprintf(BLUE, "--StartAcq: returning 0\n");
    00078         return 0;
    00079 }
    00080
    00086 void AcquisitionFinished(uint64_t frames, void*p){
    00087         bprintf(BLUE, "#### AcquisitionFinished: frames:%llu ####\n",frames);
    00088 }
    00089
    00110 void GetData(uint64_t frameNumber, uint32_t expLength, uint32_t packetNumber, uint64_t bunchId, uint64_t timestamp,
    00111                 uint16_t modId, uint16_t xCoord, uint16_t yCoord, uint16_t zCoord, uint32_t debug, uint16_t roundRNumber, uint8_t detType, uint8_t version,
    00112                 char* datapointer, uint32_t datasize, void* p){
    00113
    00114         PRINT_IN_COLOR (xCoord,
    00115                         "#### %d GetData: ####\n"
    00116                         "frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu\t\ttimestamp: %llu\t\tmodId: %u\t\t"
    00117                         "xCoord: %u\t\tyCoord: %u\t\tzCoord: %u\t\tdebug: %u\t\troundRNumber: %u\t\tdetType: %u\t\t"
    00118                         "version: %u\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n",
    00119                         xCoord, frameNumber, expLength, packetNumber, bunchId, timestamp, modId,
    00120                         xCoord, yCoord, zCoord, debug, roundRNumber, detType, version,
    00121                         ((uint8_t)(*((uint8_t*)(datapointer)))), datasize);
    00122 }
    00123
    00124
    00125
    00133 int main(int argc, char *argv[]) {
    00134
    00136         int numReceivers = 1;
    00137         int startTCPPort = 1954;
    00138         keeprunning = true;
    00139
    00141         if (argc > 1 && ((argc < 3) || (!sscanf(argv[1],"%d", &startTCPPort)) || (!sscanf(argv[2],"%d", &numReceivers))    ))
    00142                 printHelp();
    00143         bprintf(BLUE,"Parent Process Created [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
    00144         bprintf(GRAY, "Number of Receivers: %d\n", numReceivers);
    00145         bprintf(GRAY, "Start TCP Port: %d\n", startTCPPort);
    00146
    00147
    00148
    00150         struct sigaction sa;
    00151         sa.sa_flags=0;                                                  // no flags
    00152         sa.sa_handler=sigInterruptHandler;              // handler function
    00153         sigemptyset(&sa.sa_mask);                               // dont block additional signals during invocation of handler
    00154         if (sigaction(SIGINT, &sa, NULL) == -1) {
    00155                 bprintf(RED, "Could not set handler function for SIGINT\n");
    00156         }
    00157
    00160         struct sigaction asa;
    00161         asa.sa_flags=0;                                                 // no flags
    00162         asa.sa_handler=SIG_IGN;                                 // handler function
    00163         sigemptyset(&asa.sa_mask);                              // dont block additional signals during invocation of handler
    00164         if (sigaction(SIGPIPE, &asa, NULL) == -1) {
    00165                 bprintf(RED, "Could not set handler function for SIGPIPE\n");
    00166         }
    00167
    00168
    00170         for (int i = 0; i < numReceivers; ++i) {
    00171
    00173                 pid_t pid = fork();
    00174
    00176                 if (pid < 0) {
    00177                         bprintf(RED,"fork() failed. Killing all the receiver objects\n");
    00178                         raise(SIGINT);
    00179                 }
    00180
    00182                 else if (pid == 0) {
    00183                         bprintf(BLUE,"Child process %d [ Tid: %ld ]\n", i, (long)syscall(SYS_gettid));
    00184
    00185                         char temp[10];
    00186                         sprintf(temp,"%d",startTCPPort + i);
    00187                         char* args[] = {(char*)"ignored", (char*)"--rx_tcpport", temp};
    00188                         int ret = slsReceiverDefs::OK;
    00190                         slsReceiverUsers *receiver = new slsReceiverUsers(3, args, ret);
    00191                         if(ret==slsReceiverDefs::FAIL){
    00192                                 delete receiver;
    00193                                 exit(EXIT_FAILURE);
    00194                         }
    00195
    00200                         bprintf(BLUE, "Registering      StartAcq()\n");
    00201                         receiver->registerCallBackStartAcquisition(StartAcq, NULL);
    00202
    00204                         bprintf(BLUE, "Registering      AcquisitionFinished()\n");
    00205                         receiver->registerCallBackAcquisitionFinished(AcquisitionFinished, NULL);
    00206
    00207                         /*      - Call back for raw data */
    00208                         bprintf(BLUE, "Registering     GetData() \n");
    00209                         receiver->registerCallBackRawDataReady(GetData,NULL);
    00210
    00211
    00213                         if (receiver->start() == slsReceiverDefs::FAIL){
    00214                                 delete receiver;
    00215                                 bprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
    00216                                 exit(EXIT_FAILURE);
    00217                         }
    00218
    00220                         while(keeprunning)
    00221                                 pause();
    00223                         delete receiver;
    00224                         bprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
    00225                         exit(EXIT_SUCCESS);
    00226                         break;
    00227                 }
    00228         }
    00229
    00231         sa.sa_flags=0;                                                  // no flags
    00232         sa.sa_handler=SIG_IGN;                                  // handler function
    00233         sigemptyset(&sa.sa_mask);                               // dont block additional signals during invocation of handler
    00234         if (sigaction(SIGINT, &sa, NULL) == -1) {
    00235                 bprintf(RED, "Could not set handler function for SIGINT\n");
    00236         }
    00237
    00238
    00240         cout << "Ready ... " << endl;
    00241         bprintf(GRAY, "\n[ Press \'Ctrl+c\' to exit ]\n");
    00242
    00244         for(;;) {
    00245                 pid_t childPid = waitpid (-1, NULL, 0);
    00246
    00247                 // no child closed
    00248                 if (childPid == -1) {
    00249                         if (errno == ECHILD) {
    00250                                 bprintf(GREEN,"All Child Processes have been closed\n");
    00251                                 break;
    00252                         } else {
    00253                                 bprintf(RED, "Unexpected error from waitpid(): (%s)\n",strerror(errno));
    00254                                 break;
    00255                         }
    00256                 }
    00257
    00258                 //child closed
    00259                 bprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long int) childPid);
    00260         }
    00261
    00262         cout << "Goodbye!" << endl;
    00263         return 0;
    00264 }
    00265

All Classes Files Functions Variables Defines