From 4c5373958d4b8d38e51560ba898cfd0473d7c108 Mon Sep 17 00:00:00 2001 From: hackbard Date: Fri, 12 Nov 2004 01:33:12 +0000 Subject: [PATCH] cleaned up dirty pointer handling -> global priv struct --- nlsop_server.c | 309 ++++++++++++++++++++++--------------------------- 1 file changed, 138 insertions(+), 171 deletions(-) diff --git a/nlsop_server.c b/nlsop_server.c index 232f5cc..353979b 100644 --- a/nlsop_server.c +++ b/nlsop_server.c @@ -51,13 +51,18 @@ #include "nlsop_general.h" -/* globals */ -int *gi; -t_net *gnet; -t_event *gevent; -t_list *gc_list; -t_list *gg_list; +typedef struct s_priv { + t_event event; + t_net net; + t_list client; + t_list gui; + t_list job; +} t_priv; + +/* global */ int alert; +int gi; +t_priv priv; /* * server specific stuff @@ -70,14 +75,14 @@ int usage(char *prog) return 1; } -int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) { +int add_node(void) { int channel; unsigned char data; t_client client; int gui_chan; - channel=network_manage_incoming(net); + channel=network_manage_incoming(&(priv.net)); if(channel==N_E_ACCEPT) { printf("accept failed!\n"); return -1; @@ -86,38 +91,38 @@ int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) { printf("maximum connections reached!\n"); return -1; } - printf("connection from %s port %d (ch: %d)\n",net->connection[channel].ip, - net->connection[channel].port, - channel); + printf("connection from %s port %d (ch: %d)\n", + priv.net.connection[channel].ip,priv.net.connection[channel].port, + channel); /* are you client or gui? */ - network_receive_chan(net,channel,&data,1); + network_receive_chan(&(priv.net),channel,&data,1); if(data==NLSOP_GUI) { gui_chan=channel; - list_add_element(g_list,&gui_chan,sizeof(int)); + list_add_element(&(priv.gui),&gui_chan,sizeof(int)); printf("node is a gui\n"); } else if(data==NLSOP_CLIENT) { client.status=IDLE; client.channel=channel; - list_add_element(c_list,&client,sizeof(t_client)); + list_add_element(&(priv.client),&client,sizeof(t_client)); printf("node is a client\n"); } else { printf("not a client or gui - lets kick that ass out of here!\n"); - network_close(net,channel); + network_close(&(priv.net),channel); return -1; } /* if we have a new node - care for it! */ - event_math(net->connection[channel].fd,event,READ,ADD); + event_math(priv.net.connection[channel].fd,&(priv.event),READ,ADD); printf("\n"); return 1; } -int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { +int save_job(unsigned char dc) { char filename[128]; int fd; @@ -125,39 +130,43 @@ int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { d3_lattice d3l; info info; unsigned char data; + t_job *j; - printf("receiving data from client (%d)\n",job->size); + j=(t_job *)priv.job.current->data; + + printf("receiving data from client (%d)\n",j->size); data=DATA_OK; - network_send_chan(net,channel,&data,sizeof(unsigned char)); + /* initial data_ok to start transmit on client */ + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); - network_receive_chan(net,channel,(unsigned char *)&d3l, - sizeof(d3_lattice)); - network_send_chan(net,channel,&data,sizeof(unsigned char)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&d3l, + sizeof(d3_lattice)); + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); printf("debug: got d3_lattice\n"); - network_receive_chan(net,channel,(unsigned char *)&info,sizeof(info)); - network_send_chan(net,channel,&data,sizeof(unsigned char)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&info,sizeof(info)); + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); printf("debug: got info\n"); - network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char)); - network_send_chan(net,channel,&data,sizeof(unsigned char)); + network_receive_chan(&(priv.net),gi,j->ac,j->size*sizeof(unsigned char)); + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); printf("debug: got ac\n"); - network_receive_chan(net,channel,(unsigned char *)job->cc, - job->size*sizeof(int)); - network_send_chan(net,channel,&data,sizeof(unsigned char)); + network_receive_chan(&(priv.net),gi,(unsigned char *)j->cc, + j->size*sizeof(int)); + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); printf("debug: got cc\n"); - network_receive_chan(net,channel,(unsigned char *)&(job->step),sizeof(int)); - network_send_chan(net,channel,&data,sizeof(unsigned char)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&(j->step),sizeof(int)); + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); printf("debug: got steps\n"); if(dc!=DC_QUIT) { snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save", - job->info.b,job->info.c,job->info.s, - job->info.diff_rate,job->info.dr_ac, - job->step,job->info.steps); + j->info.b,j->info.c,j->info.s, + j->info.diff_rate,j->info.dr_ac, + j->step,j->info.steps); if((fd=open(filename,O_WRONLY|O_CREAT))<0) { printf("FATAL: unable to open file %s\n",filename); return -1; @@ -173,14 +182,14 @@ int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { return -1; } - ret=write(fd,job->ac,job->size*sizeof(unsigned char)); - if(retsize*sizeof(unsigned char)) { + ret=write(fd,j->ac,j->size*sizeof(unsigned char)); + if(retsize*sizeof(unsigned char)) { printf("FATAL: write of a/c states failed\n"); return -1; } - ret=write(fd,job->cc,job->size*sizeof(int)); - if(retsize*sizeof(int)) { + ret=write(fd,j->cc,j->size*sizeof(int)); + if(retsize*sizeof(int)) { printf("FATAL: write of c.-conc. failed\n"); return -1; } @@ -192,7 +201,7 @@ int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { return 1; } -int add_job(t_net *net,int chan,t_list *jl) { +int add_job(void) { t_job job; @@ -200,10 +209,11 @@ int add_job(t_net *net,int chan,t_list *jl) { job.status=IN_QUEUE; job.progress=0; - network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int)); - network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int)); - network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int)); - network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.x),sizeof(int)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.y),sizeof(int)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.z),sizeof(int)); + network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.info), + sizeof(info)); job.size=job.x*job.y*job.z; @@ -221,7 +231,7 @@ int add_job(t_net *net,int chan,t_list *jl) { job.step=0; - list_add_element(jl,&job,sizeof(t_job)); + list_add_element(&(priv.job),&job,sizeof(t_job)); printf("job added: b=%f | c=%f | s=%f ...\n", job.info.b,job.info.c,job.info.s); @@ -229,7 +239,7 @@ int add_job(t_net *net,int chan,t_list *jl) { return 1; } -int send_status(t_net *net,int chan,t_list *jl) { +int send_status(void) { unsigned char data; int count; @@ -237,94 +247,87 @@ int send_status(t_net *net,int chan,t_list *jl) { data=GUI_INFO; - count=list_count(jl); + count=list_count(&(priv.job)); printf("sending job info\n"); - network_send_chan(net,chan,&data,sizeof(unsigned char)); - network_send_chan(net,chan,(unsigned char *)&count,sizeof(int)); + network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char)); + network_send_chan(&(priv.net),gi,(unsigned char *)&count,sizeof(int)); - list_reset(jl); + list_reset(&(priv.job)); for(i=0;icurrent->data,sizeof(t_job)); - list_next(jl); + network_send_chan(&(priv.net),gi,priv.job.current->data,sizeof(t_job)); + list_next(&(priv.job)); } return 1; } -int handle_node(t_net *net,t_event *event, - t_list *c_list,t_list *g_list,t_list *job) { +int handle_node(void) { - int i; unsigned char data; t_client *c; t_job *j; - gi=&i; - - for(i=0;iconnection[i].fd,&(event->rfds))) { + for(gi=0;gicurrent->data; - c=(t_client *)c_list->current-data; + list_search_data(&(priv.job),&gi,sizeof(int)); + j=(t_job *)priv.job.current->data; + c=(t_client *)priv.client.current->data; if(data==DC_END) { - save_job(net,i,j,DC_END); + save_job(DC_END); /* reset client */ - printf("client in state %c now\n",c->status&IDLE?'i':'a'); c->status=IDLE; - printf("now: %c\n",c->status&IDLE?'i':'a'); - printf("it is channel %d\n",c->channel); /* free job memory */ free(j->ac); free(j->cc); /* delete job entry */ - list_del_current(job); + list_del_current(&(priv.job)); printf("job ended, saved and removed from list.\n"); } if(data==DC_OK) { - save_job(net,i,j,DC_OK); + save_job(DC_OK); /* inc progress state */ j->progress+=1; printf("job at next level, saved.\n"); } if(data==DC_QUIT) { - save_job(net,i,j,DC_QUIT); + save_job(DC_QUIT); /* network disconnect */ - event_math(net->connection[i].fd,event,READ,REMOVE); - network_close(net,i); + event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE); + network_close(&(priv.net),gi); /* del from client list */ - list_del_current(c_list); + list_del_current(&(priv.client)); /* change job state */ j->status=IN_QUEUE; printf("client terminating, job queued, client removed.\n"); } } - else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) { + else if(list_search_data(&(priv.gui),&gi,sizeof(int))==L_SUCCESS) { /* its a gui */ - if(data==GUI_ADDJOB) add_job(net,i,job); + if(data==GUI_ADDJOB) add_job(); - else if(data==GUI_INFO) send_status(net,i,job); + else if(data==GUI_INFO) send_status(); else if(data==GUI_QUIT) { - printf("disconnecting gui on channel %d\n",i); - event_math(net->connection[i].fd,event,READ,REMOVE); - network_close(net,i); - list_del_current(g_list); + printf("disconnecting gui on channel %d\n",gi); + event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE); + network_close(&(priv.net),gi); + list_del_current(&(priv.gui)); } else { @@ -335,8 +338,8 @@ int handle_node(t_net *net,t_event *event, else { printf("this chan is not in client or gui list! i disconnect now!\n"); - event_math(net->connection[i].fd,event,READ,REMOVE); - network_close(net,i); + event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE); + network_close(&(priv.net),gi); } } } @@ -348,41 +351,31 @@ int handle_node(t_net *net,t_event *event, int distribute_jobs(t_event *event,void *allineed) { - t_net *net; - t_list *c_list,*g_list,*job; int count_j,count_c,min; t_job *j; t_client *c; unsigned char data; d3_lattice d3l; - unsigned int addr[4]; - - memcpy(addr,allineed,4*sizeof(unsigned int)); - net=(t_net *)addr[0]; - c_list=(t_list *)addr[1]; - g_list=(t_list *)addr[2]; - job=(t_list *)addr[3]; - - list_reset(job); - list_reset(c_list); + list_reset(&(priv.job)); + list_reset(&(priv.client)); count_j=0; count_c=0; - if((c_list->current==NULL)||(job->current==NULL)) return 2; + if((priv.client.current==NULL)||(priv.job.current==NULL)) return 2; - j=(t_job *)job->current->data; - c=(t_client *)c_list->current->data; + j=(t_job *)priv.job.current->data; + c=(t_client *)priv.client.current->data; if(j->status==IN_QUEUE) count_j++; if(c->status==IDLE) count_c++; - while(list_next(job)!=L_NO_NEXT_ELEMENT) { - j=(t_job *)job->current->data; + while(list_next(&(priv.job))!=L_NO_NEXT_ELEMENT) { + j=(t_job *)priv.job.current->data; if(j->status==IN_QUEUE) count_j++; } - while(list_next(c_list)!=L_NO_NEXT_ELEMENT) { - c=(t_client *)c_list->current->data; + while(list_next(&(priv.client))!=L_NO_NEXT_ELEMENT) { + c=(t_client *)priv.client.current->data; if(c->status==IDLE) count_c++; } @@ -393,19 +386,19 @@ int distribute_jobs(t_event *event,void *allineed) { printf("%d queued jobs, %d idle clients\n\n",count_j,count_c); } - list_reset(job); - list_reset(c_list); + list_reset(&(priv.job)); + list_reset(&(priv.client)); while(min) { - j=(t_job *)job->current->data; - c=(t_client *)c_list->current->data; + j=(t_job *)priv.job.current->data; + c=(t_client *)priv.client.current->data; while(c->status!=IDLE) { - list_next(c_list); - c=(t_client *)c_list->current->data; + list_next(&(priv.client)); + c=(t_client *)priv.client.current->data; } while(j->status!=IN_QUEUE) { - list_next(job); - j=(t_job *)job->current->data; + list_next(&(priv.job)); + j=(t_job *)priv.job.current->data; } /* direct current job to current client */ @@ -420,23 +413,27 @@ int distribute_jobs(t_event *event,void *allineed) { d3l.max_y=j->y; d3l.max_z=j->z; - network_send_chan(net,c->channel,&data,sizeof(unsigned char)); - network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice)); - network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info)); + network_send_chan(&(priv.net),c->channel,&data,sizeof(unsigned char)); + network_send_chan(&(priv.net),c->channel,(unsigned char *)&d3l, + sizeof(d3_lattice)); + network_send_chan(&(priv.net),c->channel,(unsigned char *)&(j->info), + sizeof(info)); if(data==NLSOP_CJOB) { - network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char)); - network_receive_chan(net,c->channel,&data,sizeof(unsigned char)); - network_send_chan(net,c->channel,(unsigned char *)&(j->cc), + network_send_chan(&(priv.net),c->channel,j->ac, + j->size*sizeof(unsigned char)); + network_receive_chan(&(priv.net),c->channel,&data,sizeof(unsigned char)); + network_send_chan(&(priv.net),c->channel,(unsigned char *)&(j->cc), j->size*sizeof(int)); - network_receive_chan(net,c->channel,&data,sizeof(unsigned char)); - network_send_chan(net,c->channel,(unsigned char *)&(j->step),sizeof(int)); - network_receive_chan(net,c->channel,&data,sizeof(unsigned char)); + network_receive_chan(&(priv.net),c->channel,&data,sizeof(unsigned char)); + network_send_chan(&(priv.net),c->channel,(unsigned char *)&(j->step), + sizeof(int)); + network_receive_chan(&(priv.net),c->channel,&data,sizeof(unsigned char)); } --min; - list_next(c_list); - list_next(job); + list_next(&(priv.client)); + list_next(&(priv.job)); } return 1; @@ -444,27 +441,16 @@ int distribute_jobs(t_event *event,void *allineed) { int parse_incoming(t_event *event,void *allineed) { - t_net *net; - t_list *c_list,*g_list,*job; - unsigned int addr[4]; - - memcpy(addr,allineed,4*sizeof(unsigned int)); - - net=(t_net *)addr[0]; - c_list=(t_list *)addr[1]; - g_list=(t_list *)addr[2]; - job=(t_list *)addr[3]; - /* decide what to do */ - if(FD_ISSET(net->l_fd,&(event->rfds))) { + if(FD_ISSET(priv.net.l_fd,&(priv.event.rfds))) { /* new node */ printf("new node ...\n"); - add_node(net,event,c_list,g_list); + add_node(); } else { /* client/gui interaction */ printf("node interaction ...\n"); - handle_node(net,event,c_list,g_list,job); + handle_node(); } return 1; @@ -472,15 +458,15 @@ int parse_incoming(t_event *event,void *allineed) { void destroy_it(int signum) { - printf("connection to client (ch %d) fucked up!\n",*gi); - event_math(gnet->connection[*gi].fd,gevent,READ,REMOVE); - network_close(gnet,*gi); - if(list_search_data(gc_list,gi,sizeof(int))==L_SUCCESS) { - list_del_current(gc_list); + printf("connection to client (ch %d) fucked up!\n",gi); + event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE); + network_close(&(priv.net),gi); + if(list_search_data(&(priv.client),&gi,sizeof(int))==L_SUCCESS) { + list_del_current(&(priv.client)); printf("removed client from list\n"); } - if(list_search_data(gg_list,gi,sizeof(int))==L_SUCCESS) { - list_del_current(gg_list); + if(list_search_data(&(priv.gui),&gi,sizeof(int))==L_SUCCESS) { + list_del_current(&(priv.gui)); printf("removed gui from list\n"); } @@ -497,26 +483,7 @@ int main(int argc,char **argv) { int port; - t_net net; - t_event event; - t_list c_list; - t_list g_list; - t_list job; - void *allyouneed; - unsigned int addr[4]; - - gnet=&net; - gevent=&event; - gc_list=&c_list; - gg_list=&g_list; - - /* tzzz ... */ - allyouneed=(void *)addr; - addr[0]=(unsigned int)&net; - addr[1]=(unsigned int)&c_list; - addr[2]=(unsigned int)&g_list; - addr[3]=(unsigned int)&job; - + /* default values */ port=1025; @@ -524,19 +491,19 @@ int main(int argc,char **argv) if(argc==2) port=atoi(argv[1]); /* event init */ - event_init(&event,1); + event_init(&(priv.event),1); /* 10 sec event timeout - distributing jobs */ - event_set_timeout(&event,10,0); + event_set_timeout(&(priv.event),10,0); /* list init */ - list_init(&c_list,1); - list_init(&g_list,1); - list_init(&job,1); + list_init(&(priv.client),1); + list_init(&(priv.gui),1); + list_init(&(priv.job),1); /* connect to server */ - network_init(&net,1); - network_set_listen_port(&net,port); - if(network_listen(&net)!=N_SUCCESS) { + network_init(&(priv.net),1); + network_set_listen_port(&(priv.net),port); + if(network_listen(&(priv.net))!=N_SUCCESS) { printf("unable to listen on port %d, aborting!\n",port); return -1; } @@ -545,9 +512,9 @@ int main(int argc,char **argv) signal(SIGALRM,destroy_it); /* wait for events :) */ - event_math(net.l_fd,&event,READ,ADD); + event_math(priv.net.l_fd,&(priv.event),READ,ADD); printf("\nNLSOP_SERVER started!\n\n"); - event_start(&event,allyouneed,parse_incoming,distribute_jobs); + event_start(&(priv.event),NULL,parse_incoming,distribute_jobs); return 1; } -- 2.39.2