4 * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
6 * this program tries helping to understand the amorphous depuration
7 * and recrystallization of SiCx while ion implantation at temperatures
8 * below 400 degree celsius.
9 * hopefully the program will simulate the stabilization of the
10 * selforganizing lamella structure in the observed behaviour.
13 * - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
14 * - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
16 * Copyright (C) 2004 Frank Zirkelbach
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <sys/types.h>
52 #include "nlsop_general.h"
63 * server specific stuff
69 printf("%s <listen port>\n",prog);
73 int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
80 channel=network_manage_incoming(net);
81 if(channel==N_E_ACCEPT) {
82 printf("accept failed!\n");
85 if(channel==N_E_MAXC) {
86 printf("maximum connections reached!\n");
89 printf("connection from %s port %d (ch: %d)\n",net->connection[channel].ip,
90 net->connection[channel].port,
93 /* are you client or gui? */
94 network_receive_chan(net,channel,&data,1);
97 list_add_element(g_list,&gui_chan,sizeof(int));
98 printf("node is a gui\n");
100 else if(data==NLSOP_CLIENT) {
102 client.channel=channel;
103 list_add_element(c_list,&client,sizeof(t_client));
104 printf("node is a client\n");
107 printf("not a client or gui - lets kick that ass out of here!\n");
108 network_close(net,channel);
112 /* if we have a new node - care for it! */
113 event_math(net->connection[channel].fd,event,READ,ADD);
120 int save_job(t_net *net,int channel,t_job *job,unsigned char dc) {
129 printf("receiving data from client (%d)\n",job->size);
132 network_send_chan(net,channel,&data,sizeof(unsigned char));
134 network_receive_chan(net,channel,(unsigned char *)&d3l,
136 network_send_chan(net,channel,&data,sizeof(unsigned char));
137 printf("debug: got d3_lattice\n");
139 network_receive_chan(net,channel,(unsigned char *)&info,sizeof(info));
140 network_send_chan(net,channel,&data,sizeof(unsigned char));
141 printf("debug: got info\n");
143 network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
144 network_send_chan(net,channel,&data,sizeof(unsigned char));
145 printf("debug: got ac\n");
147 network_receive_chan(net,channel,(unsigned char *)job->cc,
148 job->size*sizeof(int));
149 network_send_chan(net,channel,&data,sizeof(unsigned char));
150 printf("debug: got cc\n");
152 network_receive_chan(net,channel,(unsigned char *)&(job->step),sizeof(int));
153 network_send_chan(net,channel,&data,sizeof(unsigned char));
154 printf("debug: got steps\n");
157 snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
158 job->info.b,job->info.c,job->info.s,
159 job->info.diff_rate,job->info.dr_ac,
160 job->step,job->info.steps);
161 if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
162 printf("FATAL: unable to open file %s\n",filename);
166 if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
167 printf("FATAL: write of d3_lattice failed\n");
171 if(write(fd,&info,sizeof(info))<sizeof(info)) {
172 printf("FATAL: write of info failed\n");
176 ret=write(fd,job->ac,job->size*sizeof(unsigned char));
177 if(ret<job->size*sizeof(unsigned char)) {
178 printf("FATAL: write of a/c states failed\n");
182 ret=write(fd,job->cc,job->size*sizeof(int));
183 if(ret<job->size*sizeof(int)) {
184 printf("FATAL: write of c.-conc. failed\n");
195 int add_job(t_net *net,int chan,t_list *jl) {
203 network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
204 network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
205 network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
206 network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
208 job.size=job.x*job.y*job.z;
210 job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
212 printf("unable to malloc a/c memory\n");
216 job.cc=(int *)malloc(job.size*sizeof(int));
218 printf("unable to malloc cc memory\n");
224 list_add_element(jl,&job,sizeof(t_job));
226 printf("job added: b=%f | c=%f | s=%f ...\n",
227 job.info.b,job.info.c,job.info.s);
232 int send_status(t_net *net,int chan,t_list *jl) {
240 count=list_count(jl);
242 printf("sending job info\n");
244 network_send_chan(net,chan,&data,sizeof(unsigned char));
245 network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
248 for(i=0;i<count;i++) {
249 network_send_chan(net,chan,jl->current->data,sizeof(t_job));
256 int handle_node(t_net *net,t_event *event,
257 t_list *c_list,t_list *g_list,t_list *job) {
266 for(i=0;i<MAX_CONNECTIONS;i++) {
267 if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
271 network_receive_chan(net,i,&data,1);
274 if(alert==1) return -1;
276 if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
278 list_search_data(job,&i,sizeof(int));
279 j=(t_job *)job->current->data;
280 c=(t_client *)c_list->current-data;
283 save_job(net,i,j,DC_END);
285 printf("client in state %c now\n",c->status&IDLE?'i':'a');
287 printf("now: %c\n",c->status&IDLE?'i':'a');
288 printf("it is channel %d\n",c->channel);
289 /* free job memory */
292 /* delete job entry */
293 list_del_current(job);
294 printf("job ended, saved and removed from list.\n");
298 save_job(net,i,j,DC_OK);
299 /* inc progress state */
301 printf("job at next level, saved.\n");
305 save_job(net,i,j,DC_QUIT);
306 /* network disconnect */
307 event_math(net->connection[i].fd,event,READ,REMOVE);
308 network_close(net,i);
309 /* del from client list */
310 list_del_current(c_list);
311 /* change job state */
313 printf("client terminating, job queued, client removed.\n");
317 else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
319 if(data==GUI_ADDJOB) add_job(net,i,job);
321 else if(data==GUI_INFO) send_status(net,i,job);
323 else if(data==GUI_QUIT) {
324 printf("disconnecting gui on channel %d\n",i);
325 event_math(net->connection[i].fd,event,READ,REMOVE);
326 network_close(net,i);
327 list_del_current(g_list);
331 printf("unknown gui command\n");
337 printf("this chan is not in client or gui list! i disconnect now!\n");
338 event_math(net->connection[i].fd,event,READ,REMOVE);
339 network_close(net,i);
349 int distribute_jobs(t_event *event,void *allineed) {
352 t_list *c_list,*g_list,*job;
353 int count_j,count_c,min;
358 unsigned int addr[4];
360 memcpy(addr,allineed,4*sizeof(unsigned int));
362 net=(t_net *)addr[0];
363 c_list=(t_list *)addr[1];
364 g_list=(t_list *)addr[2];
365 job=(t_list *)addr[3];
373 if((c_list->current==NULL)||(job->current==NULL)) return 2;
375 j=(t_job *)job->current->data;
376 c=(t_client *)c_list->current->data;
377 if(j->status==IN_QUEUE) count_j++;
378 if(c->status==IDLE) count_c++;
380 while(list_next(job)!=L_NO_NEXT_ELEMENT) {
381 j=(t_job *)job->current->data;
382 if(j->status==IN_QUEUE) count_j++;
384 while(list_next(c_list)!=L_NO_NEXT_ELEMENT) {
385 c=(t_client *)c_list->current->data;
386 if(c->status==IDLE) count_c++;
389 min=(count_c<count_j)?count_c:count_j;
392 printf("d: distributing jobs ...\n");
393 printf("%d queued jobs, %d idle clients\n\n",count_j,count_c);
400 j=(t_job *)job->current->data;
401 c=(t_client *)c_list->current->data;
402 while(c->status!=IDLE) {
404 c=(t_client *)c_list->current->data;
406 while(j->status!=IN_QUEUE) {
408 j=(t_job *)job->current->data;
411 /* direct current job to current client */
412 if(j->step==0) data=NLSOP_NJOB;
413 else data=NLSOP_CJOB;
416 j->channel=c->channel;
423 network_send_chan(net,c->channel,&data,sizeof(unsigned char));
424 network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
425 network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
427 if(data==NLSOP_CJOB) {
428 network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
429 network_receive_chan(net,c->channel,&data,sizeof(unsigned char));
430 network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
431 j->size*sizeof(int));
432 network_receive_chan(net,c->channel,&data,sizeof(unsigned char));
433 network_send_chan(net,c->channel,(unsigned char *)&(j->step),sizeof(int));
434 network_receive_chan(net,c->channel,&data,sizeof(unsigned char));
445 int parse_incoming(t_event *event,void *allineed) {
448 t_list *c_list,*g_list,*job;
449 unsigned int addr[4];
451 memcpy(addr,allineed,4*sizeof(unsigned int));
453 net=(t_net *)addr[0];
454 c_list=(t_list *)addr[1];
455 g_list=(t_list *)addr[2];
456 job=(t_list *)addr[3];
458 /* decide what to do */
459 if(FD_ISSET(net->l_fd,&(event->rfds))) {
461 printf("new node ...\n");
462 add_node(net,event,c_list,g_list);
465 /* client/gui interaction */
466 printf("node interaction ...\n");
467 handle_node(net,event,c_list,g_list,job);
473 void destroy_it(int signum) {
475 printf("connection to client (ch %d) fucked up!\n",*gi);
476 event_math(gnet->connection[*gi].fd,gevent,READ,REMOVE);
477 network_close(gnet,*gi);
478 if(list_search_data(gc_list,gi,sizeof(int))==L_SUCCESS) {
479 list_del_current(gc_list);
480 printf("removed client from list\n");
482 if(list_search_data(gg_list,gi,sizeof(int))==L_SUCCESS) {
483 list_del_current(gg_list);
484 printf("removed gui from list\n");
496 int main(int argc,char **argv)
506 unsigned int addr[4];
514 allyouneed=(void *)addr;
515 addr[0]=(unsigned int)&net;
516 addr[1]=(unsigned int)&c_list;
517 addr[2]=(unsigned int)&g_list;
518 addr[3]=(unsigned int)&job;
524 if(argc==2) port=atoi(argv[1]);
527 event_init(&event,1);
528 /* 10 sec event timeout - distributing jobs */
529 event_set_timeout(&event,10,0);
532 list_init(&c_list,1);
533 list_init(&g_list,1);
536 /* connect to server */
537 network_init(&net,1);
538 network_set_listen_port(&net,port);
539 if(network_listen(&net)!=N_SUCCESS) {
540 printf("unable to listen on port %d, aborting!\n",port);
544 /* install sighandler */
545 signal(SIGALRM,destroy_it);
547 /* wait for events :) */
548 event_math(net.l_fd,&event,READ,ADD);
549 printf("\nNLSOP_SERVER started!\n\n");
550 event_start(&event,allyouneed,parse_incoming,distribute_jobs);