X-Git-Url: https://hackdaworld.org/gitweb/?p=physik%2Fnlsop.git;a=blobdiff_plain;f=nlsop_server.c;h=232f5ccfae0a44c64b5c473ffc939064683ccb6d;hp=5d11e484a312c7832b5ce93d21cb6fb00c6f3084;hb=3c6b696777d3ecc13534fe4054fc1ffa8c706044;hpb=5246e11671ef0cceef2086f1c9d75c35198683e2 diff --git a/nlsop_server.c b/nlsop_server.c index 5d11e48..232f5cc 100644 --- a/nlsop_server.c +++ b/nlsop_server.c @@ -39,6 +39,7 @@ #include #include #include +#include #include "nlsop.h" #include "dfbapi.h" @@ -50,6 +51,18 @@ #include "nlsop_general.h" +/* globals */ +int *gi; +t_net *gnet; +t_event *gevent; +t_list *gc_list; +t_list *gg_list; +int alert; + +/* + * server specific stuff + */ + int usage(char *prog) { puts("usage:"); @@ -57,10 +70,6 @@ int usage(char *prog) return 1; } -/* - * server specific stuff - */ - int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) { int channel; @@ -86,11 +95,13 @@ int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) { if(data==NLSOP_GUI) { gui_chan=channel; list_add_element(g_list,&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)); + printf("node is a client\n"); } else { printf("not a client or gui - lets kick that ass out of here!\n"); @@ -101,6 +112,8 @@ int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) { /* if we have a new node - care for it! */ event_math(net->connection[channel].fd,event,READ,ADD); + printf("\n"); + return 1; } @@ -111,15 +124,34 @@ int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { int ret; d3_lattice d3l; info info; + unsigned char data; + + printf("receiving data from client (%d)\n",job->size); + data=DATA_OK; + + network_send_chan(net,channel,&data,sizeof(unsigned char)); - ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char)); - if(ret==N_ERROR) printf("FATAL: getting ac status failed\n"); - ret=network_receive_chan(net,channel,(unsigned char *)job->cc, + network_receive_chan(net,channel,(unsigned char *)&d3l, + sizeof(d3_lattice)); + network_send_chan(net,channel,&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)); + 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)); + printf("debug: got ac\n"); + + network_receive_chan(net,channel,(unsigned char *)job->cc, job->size*sizeof(int)); - if(ret==N_ERROR) printf("FATAL: getting cc failed\n"); - ret=network_receive_chan(net,channel,(unsigned char *)&(job->step), - sizeof(int)); - if(ret==N_ERROR) printf("FATAL: getting step number failed\n"); + network_send_chan(net,channel,&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)); + 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", @@ -131,16 +163,12 @@ int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { return -1; } - memset(&d3l,0,sizeof(d3_lattice)); - d3l.max_x=job->x; - d3l.max_y=job->y; - d3l.max_z=job->z; if(write(fd,&d3l,sizeof(d3_lattice))info),sizeof(info))connection[i].fd,&(event->rfds))) { - if(network_receive_chan(net,i,&data,1)==N_ERROR) { - printf("connection to client (ch %d) fucked up!\n",i); - event_math(net->connection[i].fd,event,READ,REMOVE); - network_close(net,i); - list_del_current(c_list); - return -1; - } + alert=0; + alarm(1); + network_receive_chan(net,i,&data,1); + alarm(0); + + if(alert==1) return -1; if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) { /* it's a client */ @@ -247,15 +282,23 @@ int handle_node(t_net *net,t_event *event, if(data==DC_END) { save_job(net,i,j,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); + printf("job ended, saved and removed from list.\n"); } if(data==DC_OK) { save_job(net,i,j,DC_OK); /* inc progress state */ j->progress+=1; + printf("job at next level, saved.\n"); } if(data==DC_QUIT) { @@ -267,6 +310,7 @@ int handle_node(t_net *net,t_event *event, list_del_current(c_list); /* change job state */ j->status=IN_QUEUE; + printf("client terminating, job queued, client removed.\n"); } } @@ -296,6 +340,8 @@ int handle_node(t_net *net,t_event *event, } } } + + printf("\n"); return 1; } @@ -304,23 +350,53 @@ int distribute_jobs(t_event *event,void *allineed) { t_net *net; t_list *c_list,*g_list,*job; - int count_j,count_c; + int count_j,count_c,min; t_job *j; t_client *c; unsigned char data; d3_lattice d3l; + unsigned int addr[4]; - net=(t_net *)allineed; - c_list=(t_list *)(allineed+sizeof(t_net)); - g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list)); - job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list)); + memcpy(addr,allineed,4*sizeof(unsigned int)); - count_j=list_count(job); - count_c=list_count(c_list); + 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); - while((count_c!=0)&&(count_j!=0)) { + + count_j=0; + count_c=0; + + if((c_list->current==NULL)||(job->current==NULL)) return 2; + + j=(t_job *)job->current->data; + c=(t_client *)c_list->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; + if(j->status==IN_QUEUE) count_j++; + } + while(list_next(c_list)!=L_NO_NEXT_ELEMENT) { + c=(t_client *)c_list->current->data; + if(c->status==IDLE) count_c++; + } + + min=(count_ccurrent->data; c=(t_client *)c_list->current->data; while(c->status!=IDLE) { @@ -350,12 +426,15 @@ int distribute_jobs(t_event *event,void *allineed) { 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), 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)); } - --count_c; - --count_j; + --min; list_next(c_list); list_next(job); } @@ -367,11 +446,14 @@ 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 *)allineed; - c_list=(t_list *)(allineed+sizeof(t_net)); - g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list)); - job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list)); + 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))) { @@ -388,6 +470,25 @@ int parse_incoming(t_event *event,void *allineed) { return 1; } +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("removed client from list\n"); + } + if(list_search_data(gg_list,gi,sizeof(int))==L_SUCCESS) { + list_del_current(gg_list); + printf("removed gui from list\n"); + } + + alert=1; + alarm(0); + +} + /* * main program */ @@ -402,13 +503,19 @@ int main(int argc,char **argv) 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=malloc(sizeof(t_net)+3*sizeof(t_list)); - memcpy(allyouneed,&net,sizeof(t_net)); - memcpy(allyouneed+sizeof(t_net),&c_list,sizeof(t_list)); - memcpy(allyouneed+sizeof(t_net)+sizeof(t_list),&g_list,sizeof(t_list)); - memcpy(allyouneed+sizeof(t_net)+2*sizeof(t_list),&job,sizeof(t_list)); + 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; @@ -421,6 +528,11 @@ int main(int argc,char **argv) /* 10 sec event timeout - distributing jobs */ event_set_timeout(&event,10,0); + /* list init */ + list_init(&c_list,1); + list_init(&g_list,1); + list_init(&job,1); + /* connect to server */ network_init(&net,1); network_set_listen_port(&net,port); @@ -429,8 +541,12 @@ int main(int argc,char **argv) return -1; } + /* install sighandler */ + signal(SIGALRM,destroy_it); + /* wait for events :) */ event_math(net.l_fd,&event,READ,ADD); + printf("\nNLSOP_SERVER started!\n\n"); event_start(&event,allyouneed,parse_incoming,distribute_jobs); return 1;