- 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 using namespace std;
    00032
    00033 /* Define Number of receivers */
    00034 #define NUM_RECEIVERS   1
    00035
    00036 #define START_TCP_PORT  1954
    00037
    00038 #define PRINT_IN_COLOR(c,f, ...)        printf ("\033[%dm" f RESET, 30 + c+1, ##__VA_ARGS__)
    00039
    00040 /* List of process IDs of all child receiver processes */
    00041 pid_t childPid[NUM_RECEIVERS];
    00043 bool keeprunning;
    00045 int numrunning;
    00046
    00047
    00055 void sigChildExitedHandler(int sig) {
    00056         pid_t pid = wait(NULL);
    00057         bprintf(GRAY, "\nChild Process Pid %d exited.\n", pid);
    00058         numrunning--;
    00059 }
    00060
    00065 void sigInterruptHandler(int p){
    00066         keeprunning = false;
    00067 }
    00068
    00080 int StartAcq(char* filepath, char* filename, uint64_t fileindex, uint32_t datasize, void*p){
    00081         bprintf(BLUE, "#### StartAcq:  filepath:%s  filename:%s fileindex:%llu  datasize:%u ####\n",
    00082                         filepath, filename, fileindex, datasize);
    00083
    00084         bprintf(BLUE, "--StartAcq: returning 0\n");
    00085         return 0;
    00086 }
    00087
    00093 void AcquisitionFinished(uint64_t frames, void*p){
    00094         bprintf(BLUE, "#### AcquisitionFinished: frames:%llu ####\n",frames);
    00095 }
    00096
    00117 void GetData(uint64_t frameNumber, uint32_t expLength, uint32_t packetNumber, uint64_t bunchId, uint64_t timestamp,
    00118                 uint16_t modId, uint16_t xCoord, uint16_t yCoord, uint16_t zCoord, uint32_t debug, uint16_t roundRNumber, uint8_t detType, uint8_t version,
    00119                 char* datapointer, uint32_t datasize, void* p){
    00120
    00121         PRINT_IN_COLOR (xCoord,
    00122                         "#### %d GetData: ####\n"
    00123                         "frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu\t\ttimestamp: %llu\t\tmodId: %u\t\t"
    00124                         "xCoord: %u\t\tyCoord: %u\t\tzCoord: %u\t\tdebug: %u\t\troundRNumber: %u\t\tdetType: %u\t\t"
    00125                         "version: %u\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n",
    00126                         xCoord, frameNumber, expLength, packetNumber, bunchId, timestamp, modId,
    00127                         xCoord, yCoord, zCoord, debug, roundRNumber, detType, version,
    00128                         ((uint8_t)(*((uint8_t*)(datapointer)))), datasize);
    00129
    00130 }
    00131
    00132
    00140 int main(int argc, char *argv[]) {
    00141
    00143         for (int i = 0; i < NUM_RECEIVERS; ++i)
    00144                 childPid[i] = -1;
    00145         keeprunning = true;
    00146         numrunning = 0;
    00147
    00149         struct sigaction sa;
    00150         sa.sa_flags=0;                                                  // no flags
    00151         sa.sa_handler=sigInterruptHandler;              // handler function
    00152         sigemptyset(&sa.sa_mask);                               // dont block additional signals during invocation of handler
    00153         if (sigaction(SIGINT, &sa, NULL) == -1) {
    00154                 bprintf(RED, "Could not set handler function for SIGINT\n");
    00155         }
    00156
    00158         struct sigaction asa;
    00159         asa.sa_flags=0;                                                 // no flags
    00160         asa.sa_handler=sigChildExitedHandler;   // handler function
    00161         sigemptyset(&asa.sa_mask);                              // dont block additional signals during invocation of handler
    00162         if (sigaction(SIGCHLD, &asa, NULL) == -1) {
    00163                 bprintf(RED, "Could not set handler function for SICHILD\n");
    00164         }
    00165
    00166
    00168         for (int i = 0; i < NUM_RECEIVERS; ++i) {
    00169
    00171                 childPid[i] = fork();
    00172
    00174                 if (childPid[i] < 0) {
    00175                         bprintf(RED,"fork() failed. Killing all the receiver objects\n");
    00176                         raise(SIGINT);
    00177                 }
    00178
    00180                 else if (childPid[i] == 0) {
    00181                         bprintf(BLUE,"Starting Receiver %d with pid %ld\n", i, (long)getpid());
    00182
    00183                         char temp[10];
    00184                         sprintf(temp,"%d",START_TCP_PORT + i);
    00185                         char* args[] = {(char*)"ignored", (char*)"--rx_tcpport", temp};
    00186                         int ret = slsReceiverDefs::OK;
    00189                         slsReceiverUsers *receiver = new slsReceiverUsers(3, args, ret);
    00190                         if(ret==slsReceiverDefs::FAIL){
    00191                                 delete receiver;
    00192                                 exit(EXIT_FAILURE);
    00193                         }
    00194
    00199                         bprintf(BLUE, "Registering      StartAcq()\n");
    00200                         receiver->registerCallBackStartAcquisition(StartAcq, NULL);
    00201
    00203                         bprintf(BLUE, "Registering      AcquisitionFinished()\n");
    00204                         receiver->registerCallBackAcquisitionFinished(AcquisitionFinished, NULL);
    00205
    00206                         /*      - Call back for raw data */
    00207                         bprintf(BLUE, "Registering     GetData() \n");
    00208                         receiver->registerCallBackRawDataReady(GetData,NULL);
    00209
    00210
    00212                         if (receiver->start() == slsReceiverDefs::FAIL){
    00213                                 delete receiver;
    00214                                 exit(EXIT_FAILURE);
    00215                         }
    00216
    00218                         while(keeprunning)
    00219                                 usleep(1 * 1000 * 1000);
    00221                         delete receiver;
    00222                         exit(EXIT_SUCCESS);
    00223                 }
    00224
    00226                 else
    00227                         numrunning++;
    00228
    00229         }
    00230
    00232         cout << "Ready ... " << endl;
    00233         bprintf(GRAY, "\n[ Press \'Ctrl+c\' to exit ]\n");
    00234
    00236         while(numrunning)
    00237                 usleep(1 * 1000 * 1000);
    00238         cout << "Goodbye!" << endl;
    00239         return 0;
    00240 }
    00241

All Classes Files Functions Variables Defines