My Project
 All Classes Files Functions Variables Macros
mainReceiver.cpp
Go to the documentation of this file.
1 
19 #include "sls_receiver_defs.h"
20 #include "slsReceiverUsers.h"
21 
22 #include <iostream>
23 #include <string.h>
24 #include <signal.h> //SIGINT
25 #include <cstdlib> //system
26 //#include "utilities.h"
27 //#include "logger.h"
28 #include <sys/types.h> //wait
29 #include <sys/wait.h> //wait
30 #include <string>
31 #include <unistd.h> //usleep
32 #include <errno.h>
33 #include <syscall.h> //tid
34 using namespace std;
35 
36 
38 #define PRINT_IN_COLOR(c,f, ...) printf ("\033[%dm" f RESET, 30 + c+1, ##__VA_ARGS__)
39 
40 
43 
48 void sigInterruptHandler(int p){
49  keeprunning = false;
50 }
51 
55 void printHelp() {
56  cprintf(RESET, "Usage:\n"
57  "./slsMultiReceiver(detReceiver) [start_tcp_port] [num_receivers] [1 for call back, 0 for none]\n\n");
58  exit(EXIT_FAILURE);
59 }
60 
72 int StartAcq(char* filepath, char* filename, uint64_t fileindex, uint32_t datasize, void*p){
73  cprintf(BLUE, "#### StartAcq: filepath:%s filename:%s fileindex:%llu datasize:%u ####\n",
74  filepath, filename, (long long unsigned int)fileindex, datasize);
75 
76  cprintf(BLUE, "--StartAcq: returning 0\n");
77  return 0;
78 }
79 
85 void AcquisitionFinished(uint64_t frames, void*p){
86  cprintf(BLUE, "#### AcquisitionFinished: frames:%llu ####\n",(long long unsigned int)frames);
87 }
88 
89 
98 void GetData(char* metadata, char* datapointer, uint32_t datasize, void* p){
99  slsReceiverDefs::sls_receiver_header* header = (slsReceiverDefs::sls_receiver_header*)metadata;
100  slsReceiverDefs::sls_detector_header detectorHeader = header->detHeader;
101 
102  PRINT_IN_COLOR (detectorHeader.modId?detectorHeader.modId:detectorHeader.row,
103  "#### %d GetData: ####\n"
104  "frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu"
105  "\t\ttimestamp: %llu\t\tmodId: %u\t\t"
106  "row: %u\t\tcolumn: %u\t\treserved: %u\t\tdebug: %u"
107  "\t\troundRNumber: %u\t\tdetType: %u\t\tversion: %u"
108  //"\t\tpacketsMask:%s"
109  "\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n",
110  detectorHeader.row, (long long unsigned int)detectorHeader.frameNumber,
111  detectorHeader.expLength, detectorHeader.packetNumber, (long long unsigned int)detectorHeader.bunchId,
112  (long long unsigned int)detectorHeader.timestamp, detectorHeader.modId,
113  detectorHeader.row, detectorHeader.column, detectorHeader.reserved,
114  detectorHeader.debug, detectorHeader.roundRNumber,
115  detectorHeader.detType, detectorHeader.version,
116  //header->packetsMask.to_string().c_str(),
117  ((uint8_t)(*((uint8_t*)(datapointer)))), datasize);
118 }
119 
120 
121 
132 void GetData(char* metadata, char* datapointer, uint32_t &revDatasize, void* p){
133  slsReceiverDefs::sls_receiver_header* header = (slsReceiverDefs::sls_receiver_header*)metadata;
134  slsReceiverDefs::sls_detector_header detectorHeader = header->detHeader;
135 
136  PRINT_IN_COLOR (detectorHeader.modId?detectorHeader.modId:detectorHeader.row,
137  "#### %d GetData: ####\n"
138  "frameNumber: %llu\t\texpLength: %u\t\tpacketNumber: %u\t\tbunchId: %llu"
139  "\t\ttimestamp: %llu\t\tmodId: %u\t\t"
140  "row: %u\t\tcolumn: %u\t\treserved: %u\t\tdebug: %u"
141  "\t\troundRNumber: %u\t\tdetType: %u\t\tversion: %u"
142  //"\t\tpacketsMask:%s"
143  "\t\tfirstbytedata: 0x%x\t\tdatsize: %u\n\n",
144  detectorHeader.row, (long long unsigned int)detectorHeader.frameNumber,
145  detectorHeader.expLength, detectorHeader.packetNumber, (long long unsigned int)detectorHeader.bunchId,
146  (long long unsigned int)detectorHeader.timestamp, detectorHeader.modId,
147  detectorHeader.row, detectorHeader.column, detectorHeader.reserved,
148  detectorHeader.debug, detectorHeader.roundRNumber,
149  detectorHeader.detType, detectorHeader.version,
150  //header->packetsMask.to_string().c_str(),
151  ((uint8_t)(*((uint8_t*)(datapointer)))), revDatasize);
152 
153  // if data is modified, eg ROI and size is reduced
154  revDatasize = 26000;
155 }
156 
157 
158 
159 
167 int main(int argc, char *argv[]) {
168 
170  int numReceivers = 1;
171  int startTCPPort = 1954;
172  int withCallback = 0;
173  keeprunning = true;
174 
176  if ( (argc != 4) || (!sscanf(argv[1],"%d", &startTCPPort)) || (!sscanf(argv[2],"%d", &numReceivers)) || (!sscanf(argv[3],"%d", &withCallback)) )
177  printHelp();
178  cprintf(BLUE,"Parent Process Created [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
179  cprintf(RESET, "Number of Receivers: %d\n", numReceivers);
180  cprintf(RESET, "Start TCP Port: %d\n", startTCPPort);
181  cprintf(RESET, "Callback Enable: %d\n", withCallback);
182 
183 
184 
186  struct sigaction sa;
187  sa.sa_flags=0; // no flags
188  sa.sa_handler=sigInterruptHandler; // handler function
189  sigemptyset(&sa.sa_mask); // dont block additional signals during invocation of handler
190  if (sigaction(SIGINT, &sa, NULL) == -1) {
191  cprintf(RED, "Could not set handler function for SIGINT\n");
192  }
193 
196  struct sigaction asa;
197  asa.sa_flags=0; // no flags
198  asa.sa_handler=SIG_IGN; // handler function
199  sigemptyset(&asa.sa_mask); // dont block additional signals during invocation of handler
200  if (sigaction(SIGPIPE, &asa, NULL) == -1) {
201  cprintf(RED, "Could not set handler function for SIGPIPE\n");
202  }
203 
204 
206  for (int i = 0; i < numReceivers; ++i) {
207 
209  pid_t pid = fork();
210 
212  if (pid < 0) {
213  cprintf(RED,"fork() failed. Killing all the receiver objects\n");
214  raise(SIGINT);
215  }
216 
218  else if (pid == 0) {
219  cprintf(BLUE,"Child process %d [ Tid: %ld ]\n", i, (long)syscall(SYS_gettid));
220 
221  char temp[10];
222  sprintf(temp,"%d",startTCPPort + i);
223  char* args[] = {(char*)"ignored", (char*)"--rx_tcpport", temp};
224  int ret = slsReceiverDefs::OK;
226  slsReceiverUsers *receiver = new slsReceiverUsers(3, args, ret);
227  if(ret==slsReceiverDefs::FAIL){
228  delete receiver;
229  exit(EXIT_FAILURE);
230  }
231 
232 
235  if (withCallback) {
236 
238  cprintf(BLUE, "Registering StartAcq()\n");
240 
242  cprintf(BLUE, "Registering AcquisitionFinished()\n");
244 
245  /* - Call back for raw data */
246  cprintf(BLUE, "Registering GetData() \n");
247  if (withCallback == 1) receiver->registerCallBackRawDataReady(GetData,NULL);
248  else if (withCallback == 2) receiver->registerCallBackRawDataModifyReady(GetData,NULL);
249  }
250 
251 
252 
254  if (receiver->start() == slsReceiverDefs::FAIL){
255  delete receiver;
256  cprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
257  exit(EXIT_FAILURE);
258  }
259 
261  while(keeprunning)
262  pause();
264  delete receiver;
265  cprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long)syscall(SYS_gettid));
266  exit(EXIT_SUCCESS);
267  break;
268  }
269  }
270 
272  sa.sa_flags=0; // no flags
273  sa.sa_handler=SIG_IGN; // handler function
274  sigemptyset(&sa.sa_mask); // dont block additional signals during invocation of handler
275  if (sigaction(SIGINT, &sa, NULL) == -1) {
276  cprintf(RED, "Could not set handler function for SIGINT\n");
277  }
278 
279 
281  cout << "Ready ... " << endl;
282  cprintf(RESET, "\n[ Press \'Ctrl+c\' to exit ]\n");
283 
285  for(;;) {
286  pid_t childPid = waitpid (-1, NULL, 0);
287 
288  // no child closed
289  if (childPid == -1) {
290  if (errno == ECHILD) {
291  cprintf(GREEN,"All Child Processes have been closed\n");
292  break;
293  } else {
294  cprintf(RED, "Unexpected error from waitpid(): (%s)\n",strerror(errno));
295  break;
296  }
297  }
298 
299  //child closed
300  cprintf(BLUE,"Exiting Child Process [ Tid: %ld ]\n", (long int) childPid);
301  }
302 
303  cout << "Goodbye!" << endl;
304  return 0;
305 }
306 
void registerCallBackRawDataReady(void(*func)(char *header, char *datapointer, uint32_t datasize, void *), void *arg)
register callback to be called when data are available (to process and/or save the data)...
bool keeprunning
int StartAcq(char *filepath, char *filename, uint64_t fileindex, uint32_t datasize, void *p)
int main(int argc, char **argv)
Definition: mainClient.cpp:49
void registerCallBackAcquisitionFinished(void(*func)(uint64_t nf, void *), void *arg)
register callback for end of acquisition
void AcquisitionFinished(uint64_t frames, void *p)
void printHelp()
Class for implementing the SLS data receiver in the users application. Callbacks can be defined for p...
#define PRINT_IN_COLOR(c, f,...)
void sigInterruptHandler(int p)
void GetData(char *metadata, char *datapointer, uint32_t datasize, void *p)
void registerCallBackStartAcquisition(int(*func)(char *filepath, char *filename, uint64_t fileindex, uint32_t datasize, void *), void *arg)
register calbback for starting the acquisition
void registerCallBackRawDataModifyReady(void(*func)(char *header, char *datapointer, uint32_t &revDatasize, void *), void *arg)
register callback to be called when data are available (to process and/or save the data)...