/*=====================================================================*/ /* Shmem Server and Logger */ /* Author: S. Furletov (furletov@jlab.org) */ /* Created: */ /* Modified: */ /*=====================================================================*/ #include #include #include #include #include #include #include #include //-- #include "shmem_roc.h" extern "C" { #include "tcplib.h" } #include "shmem_srv.hh" //---- after shmem_roc.h !!!! #ifdef USEVME extern "C" { #include "rc_vme.h" void* vmeReadTask(void* arg); } #endif #define DEBUG_SHMEM 1 /*=====================================================================*/ int Debug=0; int sig_int=0, sig_hup=0, sig_alarm=0,sig_pipe=0; int set_alarm=0; /*----------------------TCP--------------------------------------------*/ int PORT; tcpbuf MBUFF; #define TCPBUFF ((char *)&MBUFF) #define TCPBUFF4 ((int *)&MBUFF) #define DEV_N 7 //#define MAX_RATEAV 300 /* 1 min average => 60 sec : 0.2 sec // = 300 rates per 1 min */ #define MAX_RATEAV 10 /* 2 sec average => 2 sec : 0.2 sec = 10 rates*/ #define MAX_MxRMS 600 /* MAXIMUM 2 min average for RMSmean */ #define MAX_RATEAVRMS 300 int rem_port,sock_main; /*=====================================================================*/ static roc_shmem *shmem_ptr; static int TCP_FLAG=0; static int semid; static int shmid; static struct sembuf sbuf; /*=====================================================================*/ /* initial SHMEM global vars */ /*=====================================================================*/ unsigned int Client_mask=0,Nclnt=0,Kclnt=0; int Musers=0; int File_Flag=0, File_Format=0; int RUN_Number=0, FILE_Number=0; char File_Name[LFname]; char File_Prefix[LFname]; /*=====================================================================*/ /* f u n c t i o n s */ /*=====================================================================*/ int run_child(LogBuffer *logbuf); int run_child0(int *HEADER); int fork2(); void ctrl_c(int m) { sig_int=1; printf("\n CTRL-C pressed...\n"); tcp_close(0); TCP_FLAG=0; exit(0); // printf("\n CTRL-C pressed, cancel timer=%d\n",alarm(1)); } void falarm(int m) { sig_alarm=1; printf(" *** ALARM *** \n"); } void falarm_BOR(int m) { sig_alarm=1; set_alarm=0; printf(" *** ALARM Waiting for BOR Event !!!*** \n"); } void hungup(int m) { sig_hup++; printf("\n Signal HUP received sig_hup=%d\n\r",sig_hup); if (!(sig_hup%2)) printf(" STOP printout \n\r"); } void fpipe(int m) { sig_pipe=1; printf("\n Signal PIPE received \n\r");} void sys_err(const char *msg) { puts(msg); exit(1); } int STREQ(char*s1,const char*s2) { if (strncasecmp(s1,s2,strlen(s2))) return 0; else return 1; } //-------------------------------------------------------------------------- void usleep2(int udelay) { //-- usleep by gettimeofday() ; CPU time !!!! resolution OK; struct timeval tv1, tv2; gettimeofday(&tv1, NULL); do { gettimeofday(&tv2, NULL); } while ((tv2.tv_usec-tv1.tv_usec+1000000 * (tv2.tv_sec - tv1.tv_sec)) Create. ID=%#X\n",SEM_ID1); if ((semid = semget(SEM_ID1, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) != -1) { /* Initialize the semaphore. */ sbuf.sem_num = 0; sbuf.sem_op = 1; /* GREEN !!! */ sbuf.sem_flg = 0; if (semop(semid, &sbuf, 1) == -1) { perror("IPC error: semop"); exit(1); } } else if (errno == EEXIST) { if ((semid = semget(SEM_ID1, 0, 0)) == -1) { perror("IPC error 1: semget"); exit(1); } } else { perror("IPC error 2: semget"); exit(1); } } /* Initialize the semaphore. */ sbuf.sem_num = 0; sbuf.sem_op = 1; /* GREEN !!! */ sbuf.sem_flg = 0; if (semop(semid, &sbuf, 1) == -1) { perror("IPC error: semop"); exit(1); } return 0; } //---------------------------------------------------------------------- pthread_t rc_thread; int run_control (struct HOST* host); static void *run_control_thread (void* arg) { struct HOST *host = (struct HOST*) arg; run_control (host); return 0; } //---------------------------------------------------------------------- #ifdef USEVME //--------------------------------------------------------------------- // FADC scalers thread //--------------------------------------------------------------------- pthread_t vme_fadc_thread; static void *vme_fadc_scaler_thread (void* arg) { char srate[65536]; struct timeval tv0; static time_t tm0; for (int id=0;id<21;id++) { for(int ichan=0; ichan<=16; ichan++) { shmem_ptr->f250_scalers.counters[id][ichan]=0; } } if (Debug>5) printf("\n Enter: vme_fadc_scaler_thread \n"); while(!sig_int) { int nbrd=0; printlog("vme_fadc_scaler_thread:: while loop , time=",(int)tm0); if (Debug>5) printf("\n Loop: vme_fadc_scaler_thread tm0=%lu Debug=%d\n",tm0,Debug); for (int id=0;id<21;id++) { double *rates = shmem_ptr->f250_scalers.rates[id]; unsigned int *counters = shmem_ptr->f250_scalers.counters[id]; //memset(srate,0,sizeof(srate)); //vme_fadc_scaler_print(srate,id,1); //printf("%s",srate); //printf("next FADC slot=%d \n",id); if (vme_fadc_scaler_getrates(rates,counters,id,1)) continue; //-- 1:latch; 2:clear; nbrd++; //---- if (Debug>3) { for(int ichan=0; ichan<16; ichan++) { if( (ichan%4) == 0 ) printf("\n"); printf("| %2d: %10.3f Hz ",ichan,shmem_ptr->f250_scalers.rates[id][ichan]); } printf("\n timer: %.3f sec. \n",shmem_ptr->f250_scalers.rates[id][16]); } //---- } if (nbrd>0) { gettimeofday(&tv0, NULL); tm0=tv0.tv_usec+1000000*tv0.tv_sec; shmem_ptr->f250_scalers.update=tm0; } sleep(1); } return 0; } //--------------------------------------------------------------------- // DISCR scalers thread //--------------------------------------------------------------------- pthread_t vme_discr_thread; static void *vme_discr_scaler_thread (void* arg) { char srate[65536]; struct timeval tv0,tv1; static time_t tm0, tm1, tm2; int Dslots[MAX_SLOT+1]; int Nslots=0; static int FIRST=0; for (int id=0;id<=MAX_SLOT;id++) { for(int ichan=0; ichan<=16; ichan++) { shmem_ptr->discr_scalers.counters[id][ichan]=0; shmem_ptr->discr_scalers.rates[id][ichan]=1.1; shmem_ptr->discr_scalers.counters2[id][ichan]=0; shmem_ptr->discr_scalers.rates2[id][ichan]=1.1; } } shmem_ptr->discr_scalers.Nslots=0; shmem_ptr->discr_scalers.threshold_flag=0; shmem_ptr->discr_scalers.threshold_flag2=0; sleep(1); uint16_t THR1=100, THR2=100, pWidth=20; char my_host[80]; gethostname(my_host, 80); if (!strncasecmp("rocTAGM",my_host,7)) THR2=12; else if (!strncasecmp("rocTAGH",my_host,7)) THR2=45; else if (!strncasecmp("rocPS",my_host,5)) THR2=40; else if (!strncasecmp("rocBCAL",my_host,6)) THR2=35; else if (!strncasecmp("rocTOF",my_host,6)) THR2=30; else if (!strncasecmp("rocST",my_host,5)) THR2=50; else if (!strncasecmp("roctrig",my_host,7)) THR2=55; int ret = vme_discr_init(THR1,THR2,pWidth,&Nslots,Dslots,MAX_SLOT); //-- threshold for all -- shmem_ptr->discr_scalers.Nslots=Nslots; for (int ii=0;ii=MAX_SLOT) break; shmem_ptr->discr_scalers.slots[ii]=Dslots[ii]; } if (ret) while(1) sleep(10); //-- discr not found, sleep --- //vme_discr_print(); //--- TAGM DISC by Richard Jones ------ if (!strncasecmp("rocTAGM2",my_host,8)) system("/bin/tcsh /home/jonesrt/vmeinit_tagm_TRGthresholds.csh"); #define TDEL 1 while(!sig_int) { printlog("vme_discr_scaler_thread:: while loop , time=",(int)tm0); for (int id=0;id<=MAX_SLOT;id++) { // double *rates = shmem_ptr->discr_scalers.rates[id]; uint32_t *counters = shmem_ptr->discr_scalers.counters[id]; uint32_t *counters2 = shmem_ptr->discr_scalers.counters2[id]; uint32_t *thresholds2 = shmem_ptr->discr_scalers.thresholds2[id]; // TRG thresholds //printf("next DISC slot=%d MAX_SLOT=%d\n",id,MAX_SLOT); if (shmem_ptr->discr_scalers.threshold_flag2) vme_discr_set_thresholds(thresholds2, 2, id); // set TRG thresholds if (Debug>0 && FIRST==0) vme_discr_get_thresholds(id); if (vme_discr_getrates(counters,counters2,id)) continue; gettimeofday(&tv1, NULL); tm1=tv1.tv_usec+1000000*tv1.tv_sec; //-- tm1 too short if (Debug>0) printf("========================== DSC Slot=%d ============================ \n",id); tm2=tm1-shmem_ptr->discr_scalers.counters[id][16]; shmem_ptr->discr_scalers.counters[id][16]=tm1; double dt=tm2*1e-6; if ( (TDEL/2.)>dt || dt>(TDEL*3.) ) { printf("ERROR time interval: dt=%f, tm1=%lu(%lu) tm2=%lu\n",dt,tm1,tv1.tv_usec+1000000*tv1.tv_sec,tm2); continue; } //-- TDC_GRP1 (DAQ) ----- shmem_ptr->discr_scalers.rates[id][16]=dt; if (Debug>5) printf("%d: tm1=%lu(%lu) tm2=%lu(%f)\n",id-4,tm1,tv1.tv_usec+1000000*tv1.tv_sec,tm2,shmem_ptr->discr_scalers.rates[id][16]); //---- if (Debug>0) printf("----------------- DSC TDC_GRP1 (DAQ) ----- "); for(int ichan=0; ichan<16; ichan++) { if (Debug>0) if( (ichan%4) == 0 ) printf("\n"); shmem_ptr->discr_scalers.rates[id][ichan]=shmem_ptr->discr_scalers.counters[id][ichan]/shmem_ptr->discr_scalers.rates[id][16]; if (Debug>0) printf("| %2d: %10d ",ichan,shmem_ptr->discr_scalers.counters[id][ichan]); //printf("| %2d: %10.3f Hz ",ichan,shmem_ptr->discr_scalers.rates[id][ichan]); } if (Debug>0) printf("\n timer: %lu us. \n",shmem_ptr->discr_scalers.counters[id][16]); //printf(" timer: %.3f sec. \n",shmem_ptr->discr_scalers.rates[id][16]); //-- TRG_GRP1 (EPICS) ----- shmem_ptr->discr_scalers.rates2[id][16]=dt; if (Debug>5) printf("%d: tm1=%lu(%lu) tm2=%lu(%f)\n",id-4,tm1,tv1.tv_usec+1000000*tv1.tv_sec,tm2,shmem_ptr->discr_scalers.rates[id][16]); //---- if (Debug>0) printf("--------------- DSC TRG_GRP1 (EPICS) ----- "); for(int ichan=0; ichan<16; ichan++) { if (Debug>0) if( (ichan%4) == 0 ) printf("\n"); shmem_ptr->discr_scalers.rates2[id][ichan]=shmem_ptr->discr_scalers.counters2[id][ichan]/shmem_ptr->discr_scalers.rates2[id][16]; if (Debug>0) printf("| %2d: %10d ",ichan,shmem_ptr->discr_scalers.counters2[id][ichan]); //printf("| %2d: %10.3f Hz ",ichan,shmem_ptr->discr_scalers.rates2[id][ichan]); } if (Debug>0) printf("\n timer: %lu us. \n",shmem_ptr->discr_scalers.counters2[id][16]); //printf(" timer: %.3f sec. \n",shmem_ptr->discr_scalers.rates2[id][16]); //---- } if (shmem_ptr->discr_scalers.threshold_flag2) shmem_ptr->discr_scalers.threshold_flag2=0; gettimeofday(&tv0, NULL); tm0=tv0.tv_usec+1000000*tv0.tv_sec; shmem_ptr->discr_scalers.update=tm0; shmem_ptr->discr_scalers.tv_sec=tv0.tv_sec; shmem_ptr->discr_scalers.tv_usec=tv0.tv_usec; sleep(TDEL); FIRST=1; } return 0; } //--------------------------------------------------------------------- // DIRC scalers thread //--------------------------------------------------------------------- pthread_t vme_dirc_thread; static void *vme_dirc_scaler_thread (void* arg) { char srate[65536]; struct timeval tv0; static time_t tm0; /* for (int id=0;id<21;id++) { for(int ichan=0; ichan<=16; ichan++) { shmem_ptr->f250_scalers.counters[id][ichan]=0; } } */ if (Debug>5) printf("\n Enter: vme_dirc_scaler_thread \n"); while(!sig_int) { printlog("vme_dirc_scaler_thread:: while loop , time=",(int)tm0); printf("vme_dirc_scaler_thread:: while loop , time=%ld \n",tm0); if (Debug>5) printf("\n Loop: vme_dirc_scaler_thread tm0=%lu Debug=%d\n",tm0,Debug); /* for (int id=0;id<21;id++) { double *rates = shmem_ptr->f250_scalers.rates[id]; unsigned int *counters = shmem_ptr->f250_scalers.counters[id]; //memset(srate,0,sizeof(srate)); //vme_fadc_scaler_print(srate,id,1); //printf("%s",srate); //printf("next FADC slot=%d \n",id); if (vme_fadc_scaler_getrates(rates,counters,id,1)) continue; //-- 1:latch; 2:clear; //---- if (Debug>3) { for(int ichan=0; ichan<16; ichan++) { if( (ichan%4) == 0 ) printf("\n"); printf("| %2d: %10.3f Hz ",ichan,shmem_ptr->f250_scalers.rates[id][ichan]); } printf("\n timer: %.3f sec. \n",shmem_ptr->f250_scalers.rates[id][16]); } //---- } */ gettimeofday(&tv0, NULL); tm0=tv0.tv_usec+1000000*tv0.tv_sec; //shmem_ptr->f250_scalers.update=tm0; sleep(1); } return 0; } #endif /*=====================================================================*/ /* closeall() - close all FDs >= a specified value */ void closeall(int fd) { int fdlimit = sysconf(_SC_OPEN_MAX); while (fd < fdlimit) close(fd++); } void usage(char* name) { printf("usage: %s -h[elp] -p[ort]=PORT -cmd[_host]=HOST1:PORT -d[debug]=LEVEL \n",name); } /*=====================================================================*/ /* */ /*=====================================================================*/ int main(int argc,char ** argv) { //static char path[]="./prof/"; int Mod_tot; pid_t pid; char *substr1, *substr2; struct HOST cmd_host; struct HOST data_host; /*----------- default value for hosts/ports ---------------------------*/ strncpy(cmd_host.NAME,"localhost",LHOST); cmd_host.PORT=32767; strncpy(data_host.NAME,"localhost",LHOST); data_host.PORT=20248; Mod_tot=1; //--- !!!! check MAXMOD !!!; strncpy(File_Prefix,"Run",LFname); File_Format=0; printf("%d\n",argc); if (argc < 2) { usage(argv[0]); } else { for (int ii=1;ii ",ii,argv[ii]); //------------------------------------------------------------------- if(STREQ(argv[ii],"-cmd")) { if ((substr1=strstr(argv[ii],"="))) { printf("found /=/ |%s|\n",substr1); if ((substr2=strstr(argv[ii],":"))) { int Lhost=(substr2-substr1)-1; printf("found /:/ |%s|, Lhost=%d\n",substr2,Lhost); strncpy(cmd_host.NAME,&substr1[1],Lhost); cmd_host.NAME[Lhost]=0; cmd_host.PORT=atoi(&substr2[1]); } else { strncpy(cmd_host.NAME,&substr1[1],LHOST); } } } //------------------------------------------------------------------- if(STREQ(argv[ii],"-d")) { //-- port Debug=1; if ((substr1=strstr(argv[ii],"="))) { printf("found /=/ |%s|\n",substr1); Debug=atoi(&substr1[1]); } } //------------------------------------------------------------------- if(STREQ(argv[ii],"-p")) { //-- port if ((substr1=strstr(argv[ii],"="))) { printf("found /=/ |%s|\n",substr1); data_host.PORT=atoi(&substr1[1]); } } //------------------------------------------------------------------- if(STREQ(argv[ii],"-h")) { usage(argv[0]); exit(0); } } } printf(" CMD_HOST=%s PORT=%d\n",cmd_host.NAME,cmd_host.PORT); printf("DATA_HOST=%s PORT=%d\n",data_host.NAME,data_host.PORT); //------------------------------------------------------------------- if((tmplog=fopen("/tmp/shmem_srv.log","w")) == NULL) { printf("Can not open file: /tmp/shmem_srv.log \n"); } else { fprintf(tmplog,"start shmem_srv on port: %d\n",data_host.PORT); } PORT=data_host.PORT; sleep(1); /*--------------------------------------*/ signal(SIGINT,ctrl_c); signal(SIGALRM,falarm_BOR); signal(SIGHUP,hungup); signal(SIGPIPE,fpipe); /*--------------------------------------*/ printf("Start main thread: PID=%d\n",getpid()); printf("Create Semaphore ID=%#X\n",SEM_ID1); sem_get(); //-- SEMAPHORE //--------------------------------------- printf("Create shmem ID=%d\n",SHM_ID1); if ( (shmid=shmget(SHM_ID1,sizeof(roc_shmem), PERMS | IPC_CREAT ))<0) printf("shmem server: can't create shared memory segment \n"); else if ( (shmid=shmget(SHM_ID1,sizeof(roc_shmem),0))<0) sys_err("shmem server: can't FIND shared memory segment "); if ( (shmem_ptr=(roc_shmem*) shmat (shmid, 0, 0)) <0 ) sys_err("shmem server: shared memory attach error"); cmd_host.shmem_ptr=shmem_ptr; cmd_host.semid=semid; cmd_host.shmid=shmid; //................... INIT shmem.................... printf("INIT SHMEM =%p size of =%d ...",shmem_ptr,sizeof(roc_shmem)); sem_wait(semid); // SEM shmem_ptr->shmem_users=0; shmem_ptr->shmem_error=0; shmem_ptr->RUN_Flag=0; shmem_ptr->RUN_STATUS=0; sprintf(shmem_ptr->Run_Status,"INIT shmem DONE"); #ifdef USE_HIST //................... Reset Hist .................... mreset(&shmem_ptr->h_shm_rol2[0][0],h_shm_rol2_L); mreset(&shmem_ptr->h_shm_rol1[0][0],h_shm_rol1_L); mreset(&shmem_ptr->h_shm_evb[0][0],h_shm_evb_L); mreset(&shmem_ptr->h_shm_err[0][0],h_shm_err_L); hreset0(shmem_ptr->H_rol1, H_rol1_NH); hreset0(shmem_ptr->H_rol2, H_rol2_NH); hreset0(shmem_ptr->H_err, H_err_NH); #endif semctl (semid, 0, SETVAL, 0); //-- clear semaphore !! sem_post(semid); // SEM printf(" DONE !!! \n"); //........ end shmem access .............. LogBuffer *logbuf = new LogBuffer(&shmem_ptr->LogBuf,LLogBuffer); cmd_host.logbuffer_ptr=logbuf; /*-----------------------------------------------------------*/ while (TCP_FLAG==0) { sock_main=tcp_open(PORT,NULL); TCP_FLAG=1; if(sock_main<0){ tcp_close(0); TCP_FLAG=0; sleep(1); } } //-------------------------------------------------------------- int ret; #ifdef USEVME printf("vme_init() .... \n"); vme_init(1); //---------------------------------------------------------------------------- printf("Start vme fadc scaler thread \n"); ret = pthread_create (&vme_fadc_thread, 0, vme_fadc_scaler_thread, NULL); if (ret) { perror ("pthread_create"); } //---------------------------------------------------------------------------- printf("Start vme discr scaler thread \n"); ret = pthread_create (&vme_discr_thread, 0, vme_discr_scaler_thread, NULL); if (ret) { perror ("pthread_create"); } //---------------------------------------------------------------------------- char my_host[80]; gethostname(my_host, 80); if (!strncasecmp("rocDIRC",my_host,7) || !strncasecmp("rocTRD1",my_host,7)) { printf("Start vme dirc scaler thread \n"); //ret = pthread_create (&vme_dirc_thread, 0, vme_dirc_scaler_thread, NULL); ret = pthread_create (&vme_dirc_thread, 0, vmeReadTask,shmem_ptr); if (ret) { perror ("pthread_create"); } } //---------------------------------------------------------------------------- #endif //-------------------------------------------------------------- printf("Start run_control client thread \n"); ret = pthread_create (&rc_thread, 0, run_control_thread, &cmd_host); if (ret) { perror ("pthread_create"); } /* void *retval; if (pthread_join (rc_thread, &retval)) perror ("pthread_join"); */ //============================================================ while(!sig_int) { printf(" while_loop:: PORT=%d ",PORT); char host_name[LINFO]; //host_name[0]=0; rem_port=tcp_listen2(host_name,LINFO); //--- add new client here ----------- printf(" ADD NEW Client DONE N_clnt=%d Kclnt=%d Mask=%#x !!! \n",Nclnt,Kclnt,Client_mask); //--- end add new client ------- pid=fork2(); if (pid==0) { // chdir (path); //-- new home dir for profiling only tcp_close_old(); run_child(logbuf); //run_child0(logbuf); printf("EXIT child !!!! clean client here k=%d Mask=%#x !!!!\n",Kclnt,Client_mask); exit(0); } printf(" Create child pid=%d\n",pid); sem_wait(semid); // SEM #ifdef DEBUG_SHMEM if (shmem_ptr->shmem_users) { shmem_ptr->shmem_error++; printf("shmem in use 1 (listen) user=%d SEM=%d \n",shmem_ptr->shmem_users,semctl (semid, 0, GETVAL, 0)); } shmem_ptr->shmem_users=1; #endif shmem_ptr->shmem_users=0; sem_post(semid); // SEM tcp_close(-1); } //--- end While(1) Loop /*-----------------------------------------------------------*/ printf(" Close TCP ports \n"); tcp_close(0); shmdt ((char*)shmem_ptr); delete logbuf; exit(0); } /*====================================================================*/ #define LL 80 //--------------------------------------------------------------------------------------- int get_run_num(int run) { static FILE *RunNumFile=NULL; // static char DataDir[LL] = "DATA/"; static char WorkDir[LL] = "./"; static char RunNumName[LL]; sprintf(RunNumName, "%s%s",WorkDir,"RUN_NUM"); if (run==0) { /*--- get next run number ---*/ if((RunNumFile=fopen(RunNumName,"r")) == NULL) { printf("Can not open file %s \n",RunNumName); RUN_Number=1000; } else { fscanf(RunNumFile,"%d",&RUN_Number); fclose(RunNumFile); RunNumFile=NULL; } printf(" get_run_num():: Next Run=%d\n",RUN_Number); } else if (run>0) { RUN_Number=run; if((RunNumFile=fopen(RunNumName,"w")) == NULL) { printf("Can not open file %s \n",RunNumName); return -1; } fprintf(RunNumFile,"%d",RUN_Number); fclose(RunNumFile); RunNumFile=NULL; } else { RUN_Number=-run; printf(" get_run_num():: Next Run=%d\n",RUN_Number); } return RUN_Number; } /*====================================================================*/ /* */ /*====================================================================*/ int addlog(LogBuffer *logbuf,char *msg) { logbuf->write(msg,strlen(msg)); return 0; } int send_msg(char *msg) { TCP_FLAG=0; if (tcp_send3(msg, strlen(msg))) { printf("tcp_send2() : Error ....\n"); return 1; } return 0; } //--------------------------------------------------------------------- int run_child(LogBuffer *logbuf) { int pid,ppid, rc; char *substr1, *cmd; const int LBUF=1000; char buf[LBUF];//, rbuf[1000]; int *HEADER; char line[256]; pid=getpid(); ppid=getppid(); printf("=======> New process PID=%d PPID=%d shmem=%p \n",pid,ppid,shmem_ptr); HEADER=(int*) buf; while(!sig_int){ rc=tcp_get_fd2(buf,sizeof(buf)-1); //printf("run_child()::tcp_get_fd2():: rc=%d \n",rc); //sleep(1); int MARKER=HEADER[1]; if (MARKER==0xAABBCCDD) { printf("!!!!! run_child:: MARKER=%X call run_child0() \n",MARKER); run_child0(HEADER); return 0; } if (0 < rc && rc < LBUF) { buf[rc]=0; } else if (rc==0) { printf(" Timeout ??? rc=%d ...\n",rc); if (shmem_ptr->RUN_STATUS==2) { //--- 2 request stop run printf(" Flag EXIT ...\n"); addlog(logbuf," Flag EXIT ...\n"); rc=send_msg("exit\n"); shmem_ptr->RUN_STATUS=3; //--- 3 stoped run if (rc) addlog(logbuf,"Error Message sending ...\n"); else addlog(logbuf,"Message sent ...\n"); } if (shmem_ptr->Message_Flag>0) { //--- execute command on roc printf(" Process cmd: %s \n",shmem_ptr->Message); if ((substr1=strstr(shmem_ptr->Message,":"))) { cmd=&substr1[1]; printf("found command : |%s| \n",cmd); printf(" Request Execute command: %s \n",cmd); rc=send_msg(cmd); sprintf(line,"INFO: Request Execute command: %s rc=%d \n",cmd,rc); addlog(logbuf,line); if (rc) addlog(logbuf,"Error Message sending ...\n"); else addlog(logbuf,"Message cmd sent ...\n"); } shmem_ptr->Message_Flag=0; } continue; } //printf("run_child():: tcp_get_fd():: rc=%d %s \n",rc,buf); if(rc<0) { printf("run_child():: tcp_get error: rc=%d......\n",rc); break; } if (logbuf->write(buf,rc)) { printf("run_child()::WRITE buffer error: fill=%d\n",*logbuf->fill_level); } else printf("run_child()::WRITE buffer: fill=%d, buf=%.80s\n",logbuf->level(),buf); if(STREQ(buf,"Start_New_Run")) { printf(" Set Flag for New Run ...\n"); addlog(logbuf,"Set Flag for New Run ...\n"); shmem_ptr->RUN_STATUS=1; //--- 1 new run } if(STREQ(buf,"Please_Exit")) { printf(" Request EXIT ...\n"); addlog(logbuf," Request EXIT ...\n"); shmem_ptr->RUN_STATUS=2; //--- 2 request stop run } if(STREQ(buf,"Please_Execute:")) { printf(" Request Execute ...\n"); strncpy(shmem_ptr->Message,buf,LMessage); shmem_ptr->Message_Flag=1; } /* int rsize=logbuf->read(rbuf,999); rbuf[rsize]=0; if (rsize==0) { printf("READ buffer error: fill=%d\n",logbuf->fill_level); } else printf("READ buffer: size=%d fill=%d log=%s \n",rsize, logbuf->level(),rbuf ); */ } //--- end of while(!sigint) return 0; } /*====================================================================*/ int run_child0(int *pHEADER) { int i; time_t t2; double tr1=0.,tr2=0.; unsigned int nev1,nev2; struct timeval tv1,tv2; struct timezone tz1,tz2; #define MAXDATA 10000000 static int BUFFER[MAXDATA],HEADER[10]; static int icc=0, READY=0,READY2=0, READY3=0; int status; int LENEVENT, MAXLENEVENT, rc,startgate; unsigned int TriggerID=0,TriggerID_old=0, ModuleID=0,Nr_Modules,xxx; unsigned int evtTrigID,evtModID,evtSize; unsigned int MARKER,FIRST=0,REQUEST; //--------- for send shmem ----- // event_DEPFET copy_evt[MAXMOD]; #define MAXMOD 6 int lenEVENT[MAXMOD]; int shmemupdate=0,shmemupdate_old=0,nev=0; //------------------------------ // event_ptr->ptrDATA=NULL; MARKER=pHEADER[1]; printf("=======> run_child0:: New process PID=%d PPID=%d MARKER=0x%x run=%d\n",getpid(),getppid(), MARKER, shmem_ptr->RUN_Number); while(!sig_int){ // while(sig_hup<3){ if (TCP_FLAG==0) { printf(" EXIT PORT=%d ",PORT); return 1; } /*--------- send hallo ------------*/ // gettimeofday(&tv1,&tz1); tdel1=(float)tv1.tv_sec+ // (float)tv1.tv_usec*1e-6; // sprintf(DIR,"Privet ot servera"); // rc=tcp_send(DIR,MAXDIR); if(sig_hup%2) printf("================= wait new HEADER ====================\n"); if (MARKER==0) rc=tcp_get2(HEADER,sizeof(HEADER)); else { for (int ii=0; ii<10; ii++) HEADER[ii]=pHEADER[ii]; } if(rc<0) { printf(" 1 get error: rc<0 (%d)......\n",rc);return 1;} else if(rc>0) {printf("Need to get more data \n");}; REQUEST=HEADER[0]; MARKER=HEADER[1]; MAXLENEVENT=HEADER[2]; xxx=HEADER[3]; TriggerID=HEADER[4]; Nr_Modules=HEADER[5]; ModuleID=HEADER[6]; startgate=HEADER[7]; status=HEADER[8]; //======================================================================= if (MARKER==0xABCDEF00) { printf("!!!!! event-builder:: Test Connection MARKER=%X, return 0xABCDEF11 \n",MARKER); //-- send HEADER -- HEADER[1]=0xABCDEF11; HEADER[2]=0; HEADER[3]=0; HEADER[4]=0; HEADER[5]=0; HEADER[6]=0; if (tcp_send2(HEADER, sizeof(HEADER))) { perror("send"); TCP_FLAG=0; } continue; } if (MARKER!=0xAABBCCDD) { printf("!!!!! event-builder:: Error MARKER=%X\n",MARKER); continue; } //========================================================================= // --- send on request --- //========================================================================= if (REQUEST==0x2) { //--- send on request shmem_ptr->READY=1; // always ready if(sig_hup%2) printf("send_to_client:: MARKER=%X\n",MARKER); //........ READ from shmem.................... shmemupdate= shmem_ptr->counter; // printf("send_to_client:: lock shmem shmemupdate=%d shmemupdate_old=%d \n",shmemupdate,shmemupdate_old); //---- SHMEM ---- if( shmemupdate_old==shmemupdate) { //-- send HEADER -- HEADER[1]=0xAABBCCDD; HEADER[2]=0; HEADER[3]=0; HEADER[4]=0; HEADER[5]=0; HEADER[6]=0; if (tcp_send2(HEADER, sizeof(HEADER))) { perror("send"); TCP_FLAG=0; } if(sig_hup%2) printf("send_to_client:: shmem is not updated: %d=%d \n",shmemupdate_old,shmemupdate); //sleep(5); shmem_ptr->counter++; continue; } shmemupdate_old=shmemupdate; //READY3=shmem_ptr->READY2; READY3=shmem_ptr->READY; //printf("send_to_client:: READY3=%d \n",READY3); if(READY3) { nev++; //printf("send_to_client:: nev=%d, Nr_Modules=%d \n",nev,Nr_Modules); t2=shmem_ptr->Time_mark; if (sizeof(BUFFER)>sizeof(roc_shmem)) LENEVENT=sizeof(roc_shmem); else LENEVENT=sizeof(BUFFER); if (MAXLENEVENT ERROR size of buffers: %d %d %d %d \n",LENEVENT,MAXLENEVENT,sizeof(BUFFER),sizeof(roc_shmem)); } /* for (i=0;ilenDATA[i]; //printf("send_to_client:: COPY Mod=%d, LenEVENT=%d\n",i,event_ptr->lenDATA[i]); memcpy( (void*) ©_evt[i], (void*) &event_ptr->depfet_evt[i],lenEVENT[i]); } */ } //------- end shmem access -------------- if(!READY3) { continue ; } gettimeofday(&tv1,&tz1); tr2=(float)tv1.tv_sec+(float)tv1.tv_usec*1e-6; if ((tr2-tr1)>10.) { printf("SEND--> Event=%d rate=%f Hz \n",nev,(nev-nev1)/(tr2-tr1)); nev1=nev; tr1=tr2; } //-- send HEADER -- HEADER[1]=0xAABBCCDD; //HEADER[2]=lenEVENT[i]; //HEADER[2]=0; HEADER[2]=LENEVENT/4; HEADER[3]=0; HEADER[4]=TriggerID; HEADER[5]=Nr_Modules; HEADER[6]=i; if (tcp_send2(HEADER, sizeof(HEADER))) { perror("send"); TCP_FLAG=0; } //-- send DATA -- if (MAXLENEVENTRUN_Number, shmem_ptr->RUN_Number); tcp_send2((int*) shmem_ptr,LENEVENT); //========================================================================= // --- recv --- //========================================================================= } else if (REQUEST==0x5) { //--- recv ---; if(sig_hup%2) printf("get_from_client:: MARKER=%X\n",MARKER); if(LENEVENT>sizeof(BUFFER)) { printf("event size > buffsize %d %d \n", LENEVENT,sizeof(BUFFER) ); tcp_close(-1); return -1; } rc=tcp_get2(BUFFER,LENEVENT); if(rc<0) { printf(" 2 get errorr rc<0 (%d)......\n",rc); return 1;} else if(rc>0) { printf("Need to get more data=%d \n",rc); return 1;}; evtTrigID=BUFFER[1]; evtModID=(BUFFER[0]>>24)&0xf; evtSize=BUFFER[0]&0xfffff; if(sig_hup%2) printf("hdr:: TrID=%d ModNr=%d len=%d; evt:: TrgID=%d, ModID=%d, len=%d (%d)\n" ,TriggerID,ModuleID,LENEVENT,evtTrigID,evtModID,evtSize,evtSize*4); if (evtSize*4 != LENEVENT) printf("*****> %c ERROR RECV:: event size:: %d != %d",7,evtSize*4,LENEVENT); if (ModuleID==0) { TriggerID_old=TriggerID; } else if (ModuleID>0 && TriggerID!=TriggerID_old ) { printf(" Error TriggerID Mod=%d Trg=%d Trg_old=%d\n" ,ModuleID,TriggerID,TriggerID_old); // return -1; }; if (FIRST==0 && ModuleID==0) { FIRST=1; } READY2=0; if ((ModuleID+1)==Nr_Modules && FIRST==1) { READY=1; READY2=1; if(sig_hup%2) printf("----- READY -----\n"); } else if (ModuleID>Nr_Modules) { printf("ModuleID > Nr_Modules %d %d\n",ModuleID,Nr_Modules); return -1; } //---- Copy recv Buffer to SHMEM ----- } //--- end if of send/recv ----- MARKER=0; } //--- end of while(!sigint) return 0; } /*====================================================================*/ /*-------------------------------------------------------------------------*/ /* End. */