//extern "C" { #include "rc_lib.h" //} #include #include #include #include #include //-- gcc 4.3 -- #include extern int h_errno; #define MIN_ANALYZER 3 #define MIN_ED 3 #define MAX_AGENT 32 #define RC_PORT 7050 #define BLOCKING 0 #define NONBLOCKING 1 #define InitStructure(Xmsg) \ { \ memset ((void *)&Xmsg, 0, sizeof(Xmsg)); \ } //================================================================= char *jobname = NULL; /* job name */ char *e_in = NULL; /* zmp agents of "external" task */ char *scr_in = NULL; /* script name */ char scr_str[256]; char *str = NULL; /* free to use char pointer */ //================================================================= char RUN_TRIGGER[32]; char RUN_TYPE[32]; char RUN_CNUM[32]; char VERSION[32]; int FLAG_SETUP=0,RUN_NUM=0; char *RCM_message; int RCM_message_len; #define LHname 128 char host_name[LHname]; const int LNAME=128; struct ADDRESS { int FLAG; int SOCK; char NAME[LNAME]; char HOST[LNAME]; }; struct ADDRESS addr_in; struct ADDRESS addr_out; //================================================================= #include "Alist.hh" static Alist *ai,*nlist; static int EVB_count, PROD_count, MON_count, RC_count; struct hostent *hp; int fill_status(Alist *ai, char *msg); void process_msg (char *buf, char** tk, int *ntk); //int STREQ(char*s1,char*s2) { if (strcasecmp(s1,s2)) return 0; else return 1; } int STREQ(const char*s1,const char*s2) { if (strncasecmp(s1,s2,strlen(s2))) return 0; else return 1; } int field_cmd_1(Alist *ai,char *msg); int field_cmd_2(Alist *ai,char *msg); int rcm_cmd_3(Alist *ai,char *msg); //----------------------------- int close_ports(); int cmd_2=0, cmd_1=0;; int sig_int=0; void ctrl_c(int m) { sig_int=1; printf("\n CTRL-C pressed...\n"); close_ports(); exit(0); } void usage(char *prog) { printf("usage: %s [-p] [-n] [-h] \n",prog); printf(" -p : use another PORT \n"); printf(" -n : do not check domain name of clients\n"); printf(" -h : help \n"); } //------------------------------------------------------------- int strcasecmp2(const char *s1, const char *s2) { while ( (*s1 != '\0') && (tolower(*(unsigned char *) s1) == tolower(*(unsigned char *) s2)) ) { s1++; s2++; } if (*s1!='\0' && *s2!='\0') return -2; else if (*s1!='\0' && *s2=='\0') return 1; else if (*s1=='\0' && *s2!='\0') return -1; else if (*s1=='\0' && *s2=='\0') return 0; else {printf("it may not be !!!!\n"); exit(1); } } //------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// // // G L O B A L V A R // int Index; int MYPORT=32767; int Socket; struct sockaddr_in MyAddr; const int TcpBytes=65536; char *TcpBuffer,*TcpBuffer2; struct sockaddr_in TheirAddr; int Client; int Result; int AgentCount; int NO_DNS=0; int Count,Nclient=0; int Status,status; struct timespec Time; struct timeval to; /* -fsv- */ const char test[]={"RCM STS IDLE ONLINE some other info"}; const char *STATUS; const char *STATUS_IDLE="STS IDLE ONLINE"; const char *STATUS_READY="STS READY ONLINE"; const char *STATUS_ACTIVE="STS ACTIVE ONLINE"; const char *STATUS_TRANS="STS TRANS ONLINE READY"; const char *STATUS_ERROR="STS ERROR ONLINE"; const char *STATUS_UNDEF="STS UNDEF OFFLINE"; int ntk; char *tk[MAX_TOKENS], *cmd; int ret; const char *sn_idle = "IDLE"; const char *sn_ready = "READY"; const char *sn_setup = "SETUP"; const char *sn_active = "ACTIVE"; const char *sn_trans = "TRANS"; const char *sn_undef = "UNDEF"; const char *sn_error = "ERROR"; const char *state_new, *state_old, *state_now; const char *state_lev[] = {"UNDEF","ERROR","TRANS","IDLE","READY","ACTIVE"}; #define MAX_LEV 6 int state_max; //////////////////////////////////////////////////////////////////////////////// // // main // int main(int argc, char **argv) { int rc_sock; // for end of run control struct sockaddr_in rc_addr; fd_set mask0,mask1,wmask0,wmask1; int wait_time=0; ///////////////////////////////////////////////////////////////////////////// // // input parameters // jobname = *argv; signal(SIGINT,ctrl_c); if ( argc <= 0 ) { printf("usage: %s ",jobname); printf(" -p \n"); return -1; } while (--argc > 0) { str = *++argv; if ( strlen(str) < 2 ) { printf("error: input parameter %s too short\n",*argv); return -1; } else if ( *str != '-' ) { printf("error: input parameter %s has no prefixed identifier\n",*argv); return -1; } str++; if ( *str == 'e' || *str == 'E' ) { e_in = ++str; } else if ( *str == 'p' || *str == 'P' ) { MYPORT=atoi(++str); } else if ( *str == 's' || *str == 'S' ) { scr_in = ++str; } else if ( *str == 'n' || *str == 'N' ) { NO_DNS=1; } else if ( *str == 'h' || *str == 'H' ) { usage(jobname); exit(0);} else { printf("error: input parameter %s unknown\n",*argv); return -1; } } /* input parameter validation */ if ( scr_in != NULL) { memset(scr_str, 0, sizeof(scr_str)); strcpy(scr_str, scr_in); printf(" \n Executable script name while going to IDLE: %s\n", scr_str); } else { printf(" \n No executable script while going to IDLE\n"); } ///////////////////////////////////////////////////////////////////////////// // // Create Nodes List // nlist=new Alist(0,-1,"status"); nlist->SetDebug(1); cout << "\n== fsv = start main ======= size of ALIST="<< sizeof(Alist)<<"!!!! \n"; //////////////////////////////////////////////////////////////////////////////// // // // MyAddr.sin_family = AF_INET; MyAddr.sin_port = htons(MYPORT); TcpBuffer = (char *)malloc(TcpBytes); if (TcpBuffer == 0x0) { printf("could not malloc buffer of byte length %d\n",TcpBytes); exit (102); } TcpBuffer2 = (char *)malloc(TcpBytes); if (TcpBuffer2 == 0x0) { printf("could not malloc buffer2 of byte length %d\n",TcpBytes); exit (102); } AgentCount = 1; // atoi(argv[4]); //////////////////////////////////////////////////////////////////////////////// // // socket // if ((Socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf ("Can't open datagram socket"); exit (106); } int on = 1; printf("open_local:: setsockopt(sock=%d) SO_REUSEADDR !!!\n",Socket); if (setsockopt(Socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) { perror("setsockopt(SO_REUSEADDR) failed"); } //////////////////////////////////////////////////////////////////////////////// // // bind // wait_time=0; wait_bind: printf ("bind\n"); printf("MY Address:1: %s \n",inet_ntoa(MyAddr.sin_addr)); if ( (bind(Socket, (struct sockaddr *)&MyAddr, sizeof(MyAddr))) < 0) { perror("bind"); printf ("Can't bind local address for %d sec. (max 1min)\n",wait_time); wait_time+=10; sleep(10); goto wait_bind; } printf("MY Address:2: %s \n",inet_ntoa(MyAddr.sin_addr)); //////////////////////////////////////////////////////////////////////////////// // // listen // printf ("listen\n"); listen (Socket, 10); printf ("listen Socket=%d \n",Socket); fflush(stdout); //////////////////////////////////////////////////////////////////////////////// // // open a TCP nonblocking socket in server mode. // It is meant for direct access from any task // memset((char*)&rc_addr, 0, sizeof(rc_addr)); rc_sock = arrange_listen_socket(RC_PORT, BLOCKING); // listen(rc_sock, 15); //--- off temp. //////////////////////////////////////////////////////////////////////////////// // // fill wait mask // FD_ZERO(&wmask0); // -fsv- reset write mask printf ("++++++++>fill wait mask :FD_SET() \n"); FD_ZERO(&mask0); // reset read mask FD_SET(Socket,&mask0); // add read socket mask for accepts // FD_SET(rc_sock,&mask0); // add read socket for End of RUN state_new=sn_idle; state_now=sn_undef; state_old=sn_idle; STATUS=STATUS_UNDEF; //////////////////////////////////////////////////////////////////////////////// // // main loop // printf ("++++++++> start waiting loop <++++++++ \n"); while ( !sig_int && cmd_2!=2 ) { printf ("++++++++> waiting loop <++++++++ \n"); /*--------------------------------------------------------------------------*/ /* Select function */ /*--------------------------------------------------------------------------*/ to.tv_sec = 10; /*main_loop_timeout*/ ; to.tv_sec=60; to.tv_usec = 1; /* usec */ mask1=mask0; wmask1=wmask0; ret=select(FD_SETSIZE, &mask1, NULL, NULL, &to); //ret=select(FD_SETSIZE, &mask1, &wmask1, NULL, &to); time_t tm=time(NULL); if (!ret) { printf(" select Timeout .... Time=%s FD_SETSIZE=%d ",ctime(&tm),FD_SETSIZE); continue; } //////////////////////////////////////////////////////////////////////////////// // // Tcp clients accept // // if( FD_ISSET(Socket, &mask1) ) { printf ("=======================================================================\n"); printf ("== TCP accept signalled ==\n"); printf ("=======================================================================\n"); Result = sizeof(TheirAddr); if ((Client = accept(Socket, (struct sockaddr *)&TheirAddr, (socklen_t*)&Result)) < 0) { printf ("Can't accept\n"); } else { memset(host_name,0,sizeof(host_name)); sprintf(host_name,"%s","UNKNOWN"); if (( hp = gethostbyaddr(&TheirAddr.sin_addr,sizeof(TheirAddr.sin_addr),AF_INET) ) == 0) { perror("tcp_listen()::gethostbyaddr"); } else { sprintf(host_name,"%s",hp->h_name); } int rem_port=ntohs(TheirAddr.sin_port); printf("listen:: Connection from %s(%s) accept at sock=%d rem_port=%d\n" ,host_name,inet_ntoa(TheirAddr.sin_addr),rem_port); //-- check host domain ---- char * domain = NULL; if ((domain=strstr(inet_ntoa(TheirAddr.sin_addr),"192.168"))) { //-- first check for local network -- printf("accept domain=|%s|\n",domain); sprintf(host_name,"%s",inet_ntoa(TheirAddr.sin_addr)); } else if ((domain=strstr(inet_ntoa(TheirAddr.sin_addr),"10.0"))) { //-- first check for local network -- printf("accept domain=|%s|\n",domain); sprintf(host_name,"%s",inet_ntoa(TheirAddr.sin_addr)); } else if ((domain=strstr(host_name,"jlab.org"))) { printf("accept domain=|%s|\n",domain); } else if ((domain=strstr(host_name,"localhost"))) { printf("accept domain=|%s|\n",domain); } else if ((domain=strstr(host_name,"UNKNOWN"))) { if ((domain=strstr(inet_ntoa(TheirAddr.sin_addr),"192.168"))) { //-- check another networks -- printf("accept domain=|%s|\n",domain); sprintf(host_name,"%s",inet_ntoa(TheirAddr.sin_addr)); } } else { if (NO_DNS) { printf("accept domain=|%s|\n",domain); sprintf(host_name,"%s",inet_ntoa(TheirAddr.sin_addr)); } else { close(Client); printf("refuse domain=|%s|\n",domain); continue; } } // nlist->append(first); nlist->append(new Alist(Client,Client,host_name)); printf ("accept Client = %d\n",Client); fflush(stdout); FD_SET(Client,&mask0); Nclient++; struct timeval tv; /* timeval and timeout stuff added by davekw7x */ tv.tv_sec = 10; tv.tv_usec = 0; if (setsockopt(Client, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv)) { perror("setsockopt"); return -1; } } printf ("=======================================================================\n"); printf ("== END TCP accept ==\n"); printf ("=======================================================================\n"); } //////////////////////////////////////////////////////////////////////////////// // // Tcp recv // nlist->GoHome(); printf("*************************************************************** \n"); while(ai=nlist->GetNext()) { // ai->print(); if( FD_ISSET(ai->a_socket, &mask1) ) { printf ("TCP Recv signalled for socket=%d\n",ai->a_socket); memset(TcpBuffer,0,TcpBytes); Count = TcpBytes; if ((Result=recv(ai->a_socket, (void *)TcpBuffer, Count, 0)) < 0) { perror ("Can't recv"); close(ai->a_socket); FD_CLR(ai->a_socket, &mask0); ai->remove(); //--- remove node from list Nclient--; continue; } else if (Result == 0) { printf ("EOF\n"); close(ai->a_socket); FD_CLR(ai->a_socket, &mask0); ai->remove(); //--- remove node from list Nclient--; continue; } Count -= Result; printf ("Read %d bytes, remaining %d\n", Result, Count); printf ("Read: %.50s \n",TcpBuffer); addr_in.SOCK=ai->a_socket; strncpy(addr_in.NAME,ai->prog,LNAME); strncpy(addr_in.HOST,ai->host,LNAME); cmd_2=field_cmd_2(ai,TcpBuffer); printf("cmd_2=%d\n",cmd_2); if (!cmd_2) { //-- if not RCM command -> brodcast to clients //printf("cmd_1=%d before:: msg(%d)=%s\n",cmd_1,strlen(TcpBuffer),TcpBuffer); cmd_1=field_cmd_1(ai,TcpBuffer); Result=strlen(TcpBuffer); //printf("cmd_1=%d after:: msg(%d)=%s\n",cmd_1,strlen(TcpBuffer),TcpBuffer); if (cmd_1<0) { //-- no address ot address is ALL: -> brodcast to all clients broadcast_to_clients(TcpBuffer,Result); } else { //-- brodcast to addressed clients broadcast_to_clients3(TcpBuffer,Result); } } //fill_status(ai,TcpBuffer); fflush(stdout); } //--- if( FD_ISSET() ) } //--- end loop over clients //////////////////////////////////////////////////////////////////////////////// // // DATA transfer ENTRY point // // // if( FD_ISSET(rc_sock, &mask1) ) // { // printf ("-------------------------------------------------------------------\n"); // printf (" TCP GENERAL signalled\n"); /* status = pthread_create (&entry->thread, 0, handle_testdev, entry); if (status) { perror ("pthread_create"); continue; } */ //Status = ext_job_act(rc_sock); // } //////////////////////////////////////////////////////////////////////////////// // // for any receives // // //if (get_status(state_new)) send_to_rcm(state_lev[state_max]); /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ } /* loop while(1) */ close_ports(); //////////////////////////////////////////////////////////////////////////////// // // exit // exit (0); } /********************************************************************************/ int close_ports() { printf("___________ EXIT, close ALL interfaces ____________ \n"); nlist->GoHome(); while(ai=nlist->GetNext()) { shutdown(ai->a_socket,SHUT_RDWR); close(ai->a_socket); }; shutdown(Socket,SHUT_RDWR); int fcloseall(); close(Socket); } /********************************************************************************/ int broadcast_to_clients( char *msg, int size) { /************** BROADCAST MSG to ALL Clients **************/ Alist *entry; printf ("TCP broadcast COMMAND(L=%d,%d)=%.50s to ALL\n",size,strlen(msg),msg); for (entry = nlist->next; entry; entry = entry->next) { //printf ("TCP broadcast COMMAND(L=%d,%d)=%.50s to %d\n",size,strlen(msg),msg,entry->a_socket); if(send(entry->a_socket, msg, size , MSG_NOSIGNAL) < 0) { perror ("Can't send\n"); close(entry->a_socket); entry->remove(); //--- remove node from list --- Nclient--; } } return 0; } /********************************************************************************/ int broadcast_to_clients2( char *msg, int size, const char *WHOM) { /************** BROADCAST MSG to ALL Clients **************/ Alist *entry; for (entry = nlist->next; entry; entry = entry->next) { if(STREQ(WHOM,"ALL") || STREQ(WHOM,entry->prog) ) { printf ("TCP broadcast2 COMMAND to %d\n",entry->a_socket); if(send(entry->a_socket, msg, size , MSG_NOSIGNAL) < 0) { perror ("Can't send\n"); close(entry->a_socket); entry->remove(); //--- remove node from list Nclient--; } } } return 0; } /********************************************************************************/ int broadcast_to_clients3( char *msg, int size) { /************** BROADCAST MSG to ALL Clients **************/ Alist *entry; for (entry = nlist->next; entry; entry = entry->next) { if ( addr_out.SOCK<0 && addr_out.FLAG>0 && (STREQ(addr_out.NAME,"ALL") || strcasecmp2(entry->prog,addr_out.NAME)>=0) || addr_out.SOCK<0 && addr_out.FLAG==0 && (STREQ(addr_out.NAME,"ALL") || strcasecmp2(entry->prog,addr_out.NAME)==0) || addr_out.SOCK==entry->a_socket ) { printf ("TCP broadcast3 COMMAND(L=%d,%d,cmp=%d)=%.50s to %d %s\n",size,strlen(msg),strcasecmp2(entry->prog,addr_out.NAME),msg, entry->a_socket,entry->prog ); if(send(entry->a_socket, msg, size , MSG_NOSIGNAL) < 0) { perror ("Can't send\n"); close(entry->a_socket); entry->remove(); //--- remove node from list Nclient--; } } } return 0; } /********************************************************************************/ /** **/ /********************************************************************************/ int send_to_rcm (const char *cmd) { /////////////////////////////////////////////////////////////////////////////// // // send to LOG // STATUS=NULL; if (!strcmp(cmd,sn_idle)) { STATUS=STATUS_IDLE; state_now=sn_idle; } if (!strcmp(cmd,sn_ready)) { STATUS=STATUS_READY; state_now=sn_ready; } if (!strcmp(cmd,sn_active)) { STATUS=STATUS_ACTIVE; state_now=sn_active; } if (!strcmp(cmd,sn_trans)) { STATUS=STATUS_TRANS; state_now=sn_trans; } if (!strcmp(cmd,sn_undef)) { STATUS=STATUS_UNDEF; state_now=sn_undef; } if (!strcmp(cmd,sn_error)) { STATUS=STATUS_ERROR; state_now=sn_error; } if (STATUS==NULL) printf("++> send_to_rcm():: Error unknown state = %s \n",cmd); printf("+++++++++++++++++> RC -> RCM: %s <++++++++++++++++++++\n",STATUS); if (Status!=1) printf("*** Send status %d \n", Status); state_old=state_now; return Status; } /********************************************************************************/ /** g e t c l i e n t s s t a t u s **/ /********************************************************************************/ char* get_status(const char *sts) { static char sndstr[65536]; Alist *ai; int i,state_req=-1,state_i=0; //nlist->GoHome(); EVB_count=0; PROD_count=0; MON_count=0; RC_count=0; state_max=0; printf ("get_status():: \n"); for (i=0;i %s <-------- \n",host_name); sprintf(&sndstr[strlen(sndstr)],"RCM:: Sock Name Stat. Mode Host \n"); printf("*************************************************************** \n"); printf(" Check for status = %s lev = %d \n",sts,state_req); // while(ai=nlist->GetNext()) { for (ai = nlist->next; ai; ai = ai->next) { //-- ?? error :: nlist->first ??? ai->print(); for (i=0;istatus,state_lev[i])) { state_i=i; if (i>state_max) state_max=i; } } if (!strcmp(ai->prog,"EVB") ) { EVB_count++; } if (strcmp(ai->prog,"ROC")>=0 ) { PROD_count++;} if (!strcmp(ai->prog,"MON") ) { MON_count++; } if (!strcmp(ai->prog,"RC_GUI") ) { RC_count++; } sprintf(&sndstr[strlen(sndstr)],"RCM:: %4d %-10s %-6s %-6s %s\n" ,ai->a_socket,ai->prog,ai->status,ai->onl,ai->host); } sprintf(&sndstr[strlen(sndstr)],"======================================================================== \n"); sprintf(&sndstr[strlen(sndstr)],"===> Nclient=%d n_EVB=%d n_PROD=%d n_MON=%d n_RC=%d <=== \n" ,Nclient,EVB_count,PROD_count,MON_count,RC_count); sprintf(&sndstr[strlen(sndstr)],"======================================================================== \n"); //printf("%s",sndstr); return sndstr; } /********************************************************************************/ /** f i l l c l i e n t s t a t u s **/ /********************************************************************************/ int fill_status(Alist *ai, char *msg_in) { char msg[TcpBytes]; // AC STS IDLE ONLINE zen305f.desy.de // RCM INFO EVB IDLE ONLINE DATA_HOST strncpy(msg,msg_in,TcpBytes); printf(" fill_status():: TcpBuffer2=%s\n",msg); ai->print(); process_msg(msg, tk, &ntk); if ( !strcmp(tk[1],"INFO") && ntk>2 ) { strncpy(ai->prog,tk[2],sizeof(ai->prog)); if (ntk>3) strncpy(ai->status,tk[3],sizeof(ai->status)); if (ntk>4) strncpy(ai->onl,tk[4],sizeof(ai->onl)); if (ntk>5) strncpy(ai->branch,tk[5],sizeof(ai->branch)); } else { printf (" Unknown MSG: %s\n",msg); }; ai->print(); return 0; } /********************************************************************************/ /** f i e l d _ c m d _ 1 **/ /********************************************************************************/ int field_cmd_1(Alist *ai, char *msg) { char *substr1, *substr2; int ret=0; const int LMES=TcpBytes; char ss[LMES],ss2[LMES],args[LMES],command[LMES], LogName[LMES]; memset(args, 0, sizeof(args)); memset(ss2, 0, sizeof(ss2)); memset(ss, 0, sizeof(ss)); strncpy(ss, msg,sizeof(ss)); process_msg(ss, tk, &ntk); /* get tokens */ printf ("field_cmd_1():: ntk=%d tk0=%s tk1=%s msg(%d)=%.50s\n",ntk,tk[0],tk[1],strlen(msg),msg); if (ntk < 2) { printf ("field_cmd_1():: Error: wrong message format (%d tokens)\n", ntk); return -1; } addr_in.SOCK=-1; addr_out.SOCK=-1; addr_in.FLAG=0; // -- string compare addr_out.FLAG=0; // -- string compare if ( strcasecmp(tk[0],"CMD")==0 || strncasecmp(tk[0],"CMD:",4)==0 || strcasecmp(tk[0],"MSG")==0 || strncasecmp(tk[0],"MSG:",4)==0 ) { if ((substr1=strstr(tk[0],":"))) { substr1++; //-- address TO: (OUT) printf("found first /:/ |%s|\n",substr1); if ((substr2=strstr(substr1,":"))) { substr2++; //--- address FROM: (IN) int Lhost=(substr2-substr1)-1; printf("found second /:/ |%s|, Lhost=%d\n",substr2,Lhost); printf(" isdigit(%c)=%d str=%s \n",*substr1, isdigit(*substr1),substr1); if ( isdigit(*substr1) ) { addr_out.SOCK=atoi(substr1); sprintf(addr_out.NAME,"sock%d",addr_out.SOCK); } else { strncpy(addr_out.NAME,substr1,LNAME); addr_out.NAME[Lhost]=0; } if ( isdigit(*substr2) ) { addr_in.SOCK=atoi(substr2); sprintf(addr_in.NAME,"sock%d",addr_in.SOCK); } else { sprintf(addr_in.NAME,"%s",substr2); } ret=2; } else { //-- only TO: address if ( isdigit(*substr1) ) { addr_out.SOCK=atoi(substr1); sprintf(addr_out.NAME,"sock%s",substr1); } else { strncpy(addr_out.NAME,substr1,LNAME); char *ptr=strchr(addr_out.NAME,'*'); if (ptr) { //-- dot/star in the name addr_out.FLAG=1; *ptr=0; } } addr_in.SOCK=ai->a_socket; sprintf(addr_in.NAME,"sock%d",addr_in.SOCK); ret=1; } } else { //-- no address -> use ALL addr_in.SOCK=ai->a_socket; sprintf(addr_in.NAME,"sock%d",addr_in.SOCK); strncpy(addr_out.NAME,"ALL",LNAME); ret=0; } if (ret<2) { //-- set full address -- printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!! set full address:: addr_out.SOCK=%d addr_in.SOCK=%d \n",addr_out.SOCK,addr_in.SOCK); if (addr_out.SOCK>=0) { if (addr_in.SOCK>0) sprintf(ss2,"CMD:%d:%d ",addr_out.SOCK,addr_in.SOCK); else sprintf(ss2,"CMD:%d:%s ",addr_out.SOCK,addr_in.NAME); } else { if (addr_in.SOCK>0) sprintf(ss2,"CMD:%s:%d ",addr_out.NAME,addr_in.SOCK); else sprintf(ss2,"CMD:%s:%s ",addr_out.NAME,addr_in.NAME); } printf("TEST TK:1: tk0=%s tk1=%s len(ss2)=%d LMES=%d len(args)=%d \n",tk[0],tk[1],strlen(ss2),LMES,strlen(args) ); strncpy(args,strstr(msg,tk[1]),LMES); strncpy(&ss2[strlen(ss2)],args,LMES-strlen(ss2)-1); strncpy(msg,ss2,LMES); //-- overwrite mesage !! printf("TEST TK:2: tk0=%s tk1=%s \n",tk[0],tk[1]); //--------- check RCM ----------- if (STREQ(addr_out.NAME,"RCM")) { printf("RCM cmd ?? msg=%s \n",msg); if (addr_in.SOCK>0) sprintf(ss2,"MSG:%d:%s ",addr_in.SOCK,"RCM"); else sprintf(ss2,"MSG:%s:%s ",addr_in.NAME,"RCM"); printf("found MY address: ss2=%s args=%s tk0=%s tk1=%s \n",ss2,args,tk[0],tk[1]); strncpy(msg,ss2,LMES); //-- overwrite mesage !! //sprintf(&msg[strlen(msg)],"message:::found MY address: %s \n",ss2); sprintf(addr_out.NAME,"%s",addr_in.NAME); sprintf(addr_in.NAME,"RCM"); addr_out.SOCK=addr_in.SOCK; addr_in.SOCK=-1; rcm_cmd_3(ai, msg); } //--------- end RCM ------------- } //-- end full address } else { //-- no prefix (CMD,RCM,MSG) no address .... send to ALL strncpy(addr_out.NAME,"ALL",LNAME); ret=-1; } printf ("field_cmd_1():: ret=%d addr TO:%s(%d) FROM:%s(%d); SEND:: msg=%.50s\n",ret,addr_out.NAME,addr_out.SOCK, addr_in.NAME,addr_in.SOCK, msg); return ret; } /********************************************************************************/ /** f i e l d _ c m d _ 3 **/ /********************************************************************************/ int rcm_cmd_3(Alist *ai, char *msg) { const int LPATH=512; char path[LPATH]; char address[LPATH]; printf ("rcm_cmd_3():: msg=%s tk1=%s \n",msg,tk[1]); /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"GETPATH")) { /* this is GET PATH */ printf("rcm_cmd_3():: %s\n",msg); if (readlink ("/proc/self/exe", path, LPATH) != -1) { dirname (path); //strcat (path, "/"); //return path; printf("rcm_cmd_3():: path=%s\n",path); sprintf(&msg[strlen(msg)],"RCM:: rcm_path=%s \n",path); } return 0; } if(STREQ(tk[1],"GETID")) { /* this is GET ID */ strncpy(address,msg,LNAME); printf("rcm_cmd_3():: %s address=%s\n",msg,address); sprintf(&msg[strlen(msg)],"RCM:: rcm_address=%s \n",address); return 0; } if(STREQ(tk[1],"ECHO")) { /* this is ECHO */ printf("rcm_cmd_3():: echo alive \n"); sprintf(&msg[strlen(msg)],"RCM:: rcm_status=ALIVE \n"); return 0; } if(STREQ(tk[1],"STATUS")) { /* this is GIVE_STATUS */ printf("field_cmd_3():: RCM STATUS %s\n",msg); char* cstat=get_status(sn_idle); printf(" Broadcast:: lenstat=%d stat=%s \n",strlen(cstat),cstat); sprintf(&msg[strlen(msg)],"%s",cstat); //broadcast_to_clients2(cstat, strlen(cstat), "ALL" ); //broadcast_to_clients(cstat, strlen(cstat)); return 0; } } /********************************************************************************/ /** f i e l d _ c m d _ 2 **/ /********************************************************************************/ int field_cmd_2(Alist *ai, char *msg) { int ret=0; const int LMES=TcpBytes; char ss[LMES],command[LMES], LogName[LMES]; static FILE *LogFile=NULL; memset(ss, 0, sizeof(ss)); /* get tokens */ strncpy(ss, msg,sizeof(ss)); process_msg(ss, tk, &ntk); if (ntk < 2) { printf ("field_cmd_2():: Error: wrong message format (%d tokens)\n", ntk); return 0; } printf ("field_cmd_2():: tokens:: tk0=%s tk1=%s ntk=%d \n",tk[0],tk[1], ntk); /*------------------------------------------------------------------------------------------*/ /* ** If it RCM prefixed command ? */ if (!STREQ(tk[0],"RCM") ) { //printf("field_cmd_2():: Unknown command ignored: %s \n",msg); return 0; /* Unknown command ignored */ } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"EXIT")) { /* this is EXIT */ printf("field_cmd_2():: %s\n",msg); return 2; } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"INFO")) { printf("field_cmd_2():: %s\n",msg); fill_status(ai,msg); return 1; } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"ABORT")) { /* this is ABORT */ printf("field_cmd_2():: RCM ABORT ,FLAG_SETUP=%d \n",FLAG_SETUP); send_to_rcm(sn_trans); if (FLAG_SETUP==2) { sprintf(command," %s %d %s %s ",scr_str, RUN_NUM, "END", RUN_TYPE); printf("field_cmd_2():: Execute command = %s \n",command); //system(command); } else { printf("field_cmd_2():: Fail SETUP Run = %d %s \n", RUN_NUM, RUN_TYPE); } // if (FLAG_SETUP==2) start_child(scr_str, RUN_NUM, "END", RUN_TYPE); FLAG_SETUP=0; return 3; } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"ACTIVATE")) { /* this is ACTIVATE */ printf("field_cmd_2():: RCM ACTIVATE\n"); send_to_rcm(sn_trans); if (FLAG_SETUP==1) FLAG_SETUP=2; sprintf(command," %s "," cmd_ACTIVATE.sh "); printf("field_cmd_2():: ACTIVATE: Execute command = %s \n",command); //system(command); return 4; } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"SETUP")) { /* this is SETUP */ printf(" >>>>>>>>>>>>>>>>>>>>> RCM SETUP assign PARAMETERS: <<<<<<<<<<<<<<<<<<<\n"); FLAG_SETUP=1; send_to_rcm(sn_trans); sprintf(command," %s "," cmd_SETUP.sh"); printf("field_cmd_2():: SETUP: Execute command = %s \n",command); system(command); if (ntk>2) { RUN_NUM=atoi(tk[2]); strncpy(RUN_CNUM,tk[2], sizeof(RUN_CNUM)); printf(" SETUP ===> RUN_NUMBER= %s <=== \n",RUN_CNUM ); } printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); return 5; } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"LOG")) { /* this is LOGGER */ printf(" >>>>>>>>>>>>>>>>>>>>> RCM SETUP assign PARAMETERS: <<<<<<<<<<<<<<<<<<<\n"); memset(LogName, 0, sizeof(LogName)); sprintf(LogName,"%s%d%s", "DATA/Run",RUN_NUM,".log"); if((LogFile = fopen(LogName,"a")) == NULL) { printf("Can not open file %s\n",LogName); return -1; } else { printf(" Add to LogName=%s\n",LogName); time_t tm=time(NULL); fprintf(LogFile,"-------------------> Time %s ",ctime(&tm)); fputs(msg,LogFile); fclose(LogFile); } } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"STATUS")) { /* this is GIVE_STATUS */ printf("field_cmd_2():: RCM STATUS %s\n",msg); char* cstat=get_status(sn_idle); printf(" Broadcast:: lenstat=%d stat=%s \n",strlen(cstat),cstat); broadcast_to_clients2(cstat, strlen(cstat), "ALL" ); //broadcast_to_clients(cstat, strlen(cstat)); return 6; } /*------------------------------------------------------------------------------------------*/ cmd=tk[1]; if (!strcmp(cmd,"SETUP")) { state_new=sn_ready; } if (!strcmp(cmd,"ACTIVATE")) { state_new=sn_active; } if (!strcmp(cmd,"ABORT")) { state_new=sn_idle; } if (!strcmp(cmd,"END")) { state_new=sn_idle; } return 7; } ///////////////////////////////////////////////////////////////////