From: hackbard Date: Tue, 2 Nov 2004 18:36:39 +0000 (+0000) Subject: modified nlsop_client.c + added nlsop_server code X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=5fab9a61a42474f7a0a2e54c5f2163e8fc241ca7;p=physik%2Fnlsop.git modified nlsop_client.c + added nlsop_server code --- diff --git a/nlsop_client.c b/nlsop_client.c index c635892..c314299 100644 --- a/nlsop_client.c +++ b/nlsop_client.c @@ -72,7 +72,7 @@ int get_data_and_calc(t_event *event,void *allineed); int usage(char *prog) { puts("usage:"); - printf("%s \n",prog); + printf("%s -i ip -p port -r/P/n random/profile/neloss file\n",prog); return 1; } @@ -238,59 +238,6 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g) return 1; } -/* save to file --> send to server :) --> T O D O <-- */ -void send_data(int signum) { - - int c; - - c=gd3_l->max_x*gd3_l->max_y*gd3_l->max_z; - - network_send(gnet->connection[0].fd,&dc,1); - network_send(gnet->connection[0].fd,(unsigned char *)gd3_l, - sizeof(d3_lattice)); - network_send(gnet->connection[0].fd,(unsigned char *)gmy_info,sizeof(info)); - network_send(gnet->connection[0].fd,gd3_l->status,c*sizeof(unsigned char)); - network_send(gnet->connection[0].fd,(unsigned char *)gd3_l->extra, - c*sizeof(int)); - network_send(gnet->connection[0].fd,(unsigned char *)gi,sizeof(int)); - -} - -int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf) -{ - int sf_fd,c; - - if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0) - { - puts("cannot open save file"); - return -1; - } - if(write(sf_fd,d3_l,sizeof(d3_lattice))max_x*d3_l->max_y*d3_l->max_z; - if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))extra,c*sizeof(int))max_x*gd3_l->max_y*gd3_l->max_z; + + network_send(gnet->connection[0].fd,&dc,1); + /* + network_send(gnet->connection[0].fd,(unsigned char *)gd3_l, + sizeof(d3_lattice)); + network_send(gnet->connection[0].fd,(unsigned char *)gmy_info,sizeof(info)); + */ + network_send(gnet->connection[0].fd,gd3_l->status,c*sizeof(unsigned char)); + network_send(gnet->connection[0].fd,(unsigned char *)gd3_l->extra, + c*sizeof(int)); + network_send(gnet->connection[0].fd,(unsigned char *)gi,sizeof(int)); + +} /* diff --git a/nlsop_server.c b/nlsop_server.c new file mode 100644 index 0000000..14cda4f --- /dev/null +++ b/nlsop_server.c @@ -0,0 +1,300 @@ +/* + * nlsop server code + * + * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de) + * + * this program tries helping to understand the amorphous depuration + * and recrystallization of SiCx while ion implantation at temperatures + * below 400 degree celsius. + * hopefully the program will simulate the stabilization of the + * selforganizing lamella structure in the observed behaviour. + * + * refs: + * - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg. + * - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg. + * + * Copyright (C) 2004 Frank Zirkelbach + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "nlsop.h" +#include "dfbapi.h" +#include "random.h" + +#include "network.h" +#include "event.h" +#include "list.h" + +#define NLSOP_GUI 'g' +#define NLSOP_CLIENT 'c' +#define NLSOP_NJOB 'N' +#define NLSOP_CJOB 'C' + +typedef struct s_client { + int channel; + unsigned char status; +#define IDLE (1<<0) +#define WORK (1<<1) +} t_client; + +typedef struct s_job { + int channel; + unsigned char status; +#define IN_QUEUE (1<<0) +#define IN_WORK (1<<1) + int progress; + unsigned char *ac; + int *cc; + int x,y,z; + info info; + int step; +} t_job; + +int usage(char *prog) +{ + puts("usage:"); + printf("%s \n",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; + unsigned char data; + t_client client; + t_job job; + int gui_chan; + + channel=network_manage_incoming(net); + if(channel==N_E_ACCEPT) { + printf("accept failed!\n"); + return -1; + } + if(channel==N_E_MAXC) { + 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); + + /* are you client or gui? */ + network_receive_chan(net,chanel,&data,1); + if(data==NLSOP_GUI) { + gui_chan=channel; + list_add_element(g_list,&gui_chan,sizeof(int)); + } + else if(data==NLSOP_CLIENT) { + client.status=IDLE; + client.channel=channel; + list_add_element(c_list,&client,sizeof(t_client)); + } + else { + printf("not a client or gui - lets kick that ass out of here!\n"); + network_close(net,channel); + return -1; + } + + /* if we have a new node - care for it! */ + event_math(net->connection[channel].fd,event,READ,ADD); + + return 1; +} + +int save_job(t_net *net,int channel,t_job *job,unsigned char dc) { + + char filename[128]; + inf fd; + int ret; + + 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_receice_chan(net,channel,job->cc,job->size*sizeof(int)); + if(ret==N_ERROR) printf("FATAL: getting cc failed\n"); + ret=network_receive_chan(net,channel,&(job->step),sizeof(int)); + if(ret==N_ERROR) printf("FATAL: getting step number failed\n"); + + if(dc!=DC_QUIT) { + snprintf(filename,"nlsop_b%f_c%f_s%f_ds%d_dr%f_Z%c__%d_of_%d.save", + job->info->b,job->info->c,job->info->s, + job->info->diff_rate,job->info->dr_ac, + job->info->z_diff?'y':'n', + job->step,job->info->steps); + if((fd=open(filename,O_WRONLY|O_CREAT))<0) { + printf("FATAL: unable to open file %s\n",filename); + return -1; + } + + + + } + + return 1; +} + +int handle_node(net,event,c_list,g_list,job) { + + int i; + unsigned char data; + t_client *c; + t_job *j; + + for(i=0;iconnection[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; + } + + if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) { + /* it's a client */ + list_search_data(job,&i,sizeof(int)); + j=(t_job *)job->current->data; + c=(t_client *)c_list->current-data; + + if(data==DC_END) { + save_job(net,i,j,DC_END); + /* reset client */ + c->channel=i; + c->status=IDLE; + /* delete job entry */ + list_del_current(job); + } + + if(data==DC_OK) { + save_job(net,i,j,DC_OK); + /* inc progress state */ + j->progress+=1; + } + + if(data==DC_QUIT) { + save_job(net,i,j,DC_QUIT); + /* network disconnect */ + event_math(net->connection[i].fd,event,READ,REMOVE); + network_close(net,i); + /* del from client list */ + list_del_current(c_list); + /* change job state */ + j->status=IN_QUEUE; + } + } + + else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) { + /* its a gui */ + + } + + 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); + } + } + } + + return 1; +} + +int parse_incoming(t_event *event,void *allineed) { + + t_net *net; + t_list *c_list,*g_list,*job; + + 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)); + + /* decide what to do */ + if(FD_ISSET(net->l.fd,&(event->rfds))) { + /* new node */ + printf("new node ...\n"); + add_node(net,event,c_list,g_list); + } + else { + /* client/gui interaction */ + printf("node interaction ...\n"); + handle_node(net,event,c_list,g_list,job); + } + + return 1; +} + +/* + * main program + */ + +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; + + /* tzzz ... */ + allyouneed=malloc(sizeof(t_net)+3*sizeof(t_list)); + memcpy(allyouneed,&net,sizeof(t_net)); + memcpy(allyouneed+sizeof(t_net),&c_list,sizeof(list)); + memcpy(allyouneed+sizeof(t_net)+sizeof(t_list),&g_list,sizeof(list)); + memcpy(allyouneed+sizeof(t_net)+2*sizeof(t_list),&job,sizeof(list)); + + /* default values */ + port=1025; + + /* parse argv */ + if(argc==2) port=atoi(argv[1]); + + /* event init */ + event_init(&event,1); + event_set_timeout(&event,0,0); + + /* connect to server */ + network_init(&net,1); + network_set_listen_port(&net,port); + if(network_listen(&net)!=N_SUCCESS) { + printf("unable to listen on port %d, aborting!\n",port); + return -1; + } + + /* wait for events :) */ + event_math(net.l_fd,&event,READ,ADD); + event_start(&event,allyouneed,parse_incoming,NULL); + + return 1; +} +