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) {
128 ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
129 if(ret==N_ERROR) printf("FATAL: getting ac status failed\n");
130 ret=network_receive_chan(net,channel,(unsigned char *)job->cc,
131 job->size*sizeof(int));
132 if(ret==N_ERROR) printf("FATAL: getting cc failed\n");
133 ret=network_receive_chan(net,channel,(unsigned char *)&(job->step),
135 if(ret==N_ERROR) printf("FATAL: getting step number failed\n");
138 snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
139 job->info.b,job->info.c,job->info.s,
140 job->info.diff_rate,job->info.dr_ac,
141 job->step,job->info.steps);
142 if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
143 printf("FATAL: unable to open file %s\n",filename);
147 memset(&d3l,0,sizeof(d3_lattice));
151 if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
152 printf("FATAL: write of d3_lattice failed\n");
156 if(write(fd,&(job->info),sizeof(info))<sizeof(info)) {
157 printf("FATAL: write of info failed\n");
161 ret=write(fd,job->ac,job->size*sizeof(unsigned char));
162 if(ret<job->size*sizeof(unsigned char)) {
163 printf("FATAL: write of a/c states failed\n");
167 ret=write(fd,job->cc,job->size*sizeof(int));
168 if(ret<job->size*sizeof(int)) {
169 printf("FATAL: write of c.-conc. failed\n");
180 int add_job(t_net *net,int chan,t_list *jl) {
188 network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
189 network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
190 network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
191 network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
193 job.size=job.x*job.y*job.z;
195 job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
197 printf("unable to malloc a/c memory\n");
201 job.cc=(int *)malloc(job.size*sizeof(int));
203 printf("unable to malloc cc memory\n");
209 list_add_element(jl,&job,sizeof(t_job));
211 printf("job added: b=%f | c=%f | s=%f ...\n",
212 job.info.b,job.info.c,job.info.s);
217 int send_status(t_net *net,int chan,t_list *jl) {
225 count=list_count(jl);
227 printf("sending job info\n");
229 network_send_chan(net,chan,&data,sizeof(unsigned char));
230 network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
233 for(i=0;i<count;i++) {
234 network_send_chan(net,chan,jl->current->data,sizeof(t_job));
241 int handle_node(t_net *net,t_event *event,
242 t_list *c_list,t_list *g_list,t_list *job) {
251 for(i=0;i<MAX_CONNECTIONS;i++) {
252 if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
256 network_receive_chan(net,i,&data,1);
259 if(alert==1) return -1;
261 if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
263 list_search_data(job,&i,sizeof(int));
264 j=(t_job *)job->current->data;
265 c=(t_client *)c_list->current-data;
268 save_job(net,i,j,DC_END);
271 /* delete job entry */
272 list_del_current(job);
273 printf("job ended, saved and removed from list.\n");
277 save_job(net,i,j,DC_OK);
278 /* inc progress state */
280 printf("job at next level, saved.\n");
284 save_job(net,i,j,DC_QUIT);
285 /* network disconnect */
286 event_math(net->connection[i].fd,event,READ,REMOVE);
287 network_close(net,i);
288 /* del from client list */
289 list_del_current(c_list);
290 /* change job state */
292 printf("client terminating, job queued, client removed.\n");
296 else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
298 if(data==GUI_ADDJOB) add_job(net,i,job);
300 else if(data==GUI_INFO) send_status(net,i,job);
302 else if(data==GUI_QUIT) {
303 printf("disconnecting gui on channel %d\n",i);
304 event_math(net->connection[i].fd,event,READ,REMOVE);
305 network_close(net,i);
306 list_del_current(g_list);
310 printf("unknown gui command\n");
316 printf("this chan is not in client or gui list! i disconnect now!\n");
317 event_math(net->connection[i].fd,event,READ,REMOVE);
318 network_close(net,i);
328 int distribute_jobs(t_event *event,void *allineed) {
331 t_list *c_list,*g_list,*job;
332 int count_j,count_c,min;
337 unsigned int addr[4];
339 memcpy(addr,allineed,4*sizeof(unsigned int));
341 net=(t_net *)addr[0];
342 c_list=(t_list *)addr[1];
343 g_list=(t_list *)addr[2];
344 job=(t_list *)addr[3];
352 if((c_list->current==NULL)||(job->current==NULL)) return 2;
354 j=(t_job *)job->current->data;
355 c=(t_client *)c_list->current->data;
356 if(j->status==IN_QUEUE) count_j++;
357 if(c->status==IDLE) count_c++;
359 while(list_next(job)!=L_NO_NEXT_ELEMENT) {
360 j=(t_job *)job->current->data;
361 if(j->status==IN_QUEUE) count_j++;
363 while(list_next(c_list)!=L_NO_NEXT_ELEMENT) {
364 if(j->status==IN_QUEUE) count_j++;
365 if(c->status==IDLE) count_c++;
368 min=(count_c<count_j)?count_c:count_j;
371 printf("d: distributing jobs ...\n");
372 printf("%d queued jobs, %d idle clients\n",count_j,count_c);
379 j=(t_job *)job->current->data;
380 c=(t_client *)c_list->current->data;
381 while(c->status!=IDLE) {
383 c=(t_client *)c_list->current->data;
385 while(j->status!=IN_QUEUE) {
387 j=(t_job *)job->current->data;
390 /* direct current job to current client */
391 if(j->step==0) data=NLSOP_NJOB;
392 else data=NLSOP_CJOB;
395 j->channel=c->channel;
402 network_send_chan(net,c->channel,&data,sizeof(unsigned char));
403 network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
404 network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
406 if(data==NLSOP_CJOB) {
407 network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
408 network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
409 j->size*sizeof(int));
420 int parse_incoming(t_event *event,void *allineed) {
423 t_list *c_list,*g_list,*job;
424 unsigned int addr[4];
426 memcpy(addr,allineed,4*sizeof(unsigned int));
428 net=(t_net *)addr[0];
429 c_list=(t_list *)addr[1];
430 g_list=(t_list *)addr[2];
431 job=(t_list *)addr[3];
433 /* decide what to do */
434 if(FD_ISSET(net->l_fd,&(event->rfds))) {
436 printf("new node ...\n");
437 add_node(net,event,c_list,g_list);
440 /* client/gui interaction */
441 printf("node interaction ...\n");
442 handle_node(net,event,c_list,g_list,job);
448 void destroy_it(int signum) {
450 printf("connection to client (ch %d) fucked up!\n",*gi);
451 event_math(gnet->connection[*gi].fd,gevent,READ,REMOVE);
452 network_close(gnet,*gi);
453 if(list_search_data(gc_list,gi,sizeof(int))==L_SUCCESS) {
454 list_del_current(gc_list);
455 printf("removed client from list\n");
457 if(list_search_data(gg_list,gi,sizeof(int))==L_SUCCESS) {
458 list_del_current(gg_list);
459 printf("removed gui from list\n");
471 int main(int argc,char **argv)
481 unsigned int addr[4];
489 allyouneed=(void *)addr;
490 addr[0]=(unsigned int)&net;
491 addr[1]=(unsigned int)&c_list;
492 addr[2]=(unsigned int)&g_list;
493 addr[3]=(unsigned int)&job;
499 if(argc==2) port=atoi(argv[1]);
502 event_init(&event,1);
503 /* 10 sec event timeout - distributing jobs */
504 event_set_timeout(&event,10,0);
507 list_init(&c_list,1);
508 list_init(&g_list,1);
511 /* connect to server */
512 network_init(&net,1);
513 network_set_listen_port(&net,port);
514 if(network_listen(&net)!=N_SUCCESS) {
515 printf("unable to listen on port %d, aborting!\n",port);
519 /* install sighandler */
520 signal(SIGALRM,destroy_it);
522 /* wait for events :) */
523 event_math(net.l_fd,&event,READ,ADD);
524 printf("\nNLSOP_SERVER started!\n\n");
525 event_start(&event,allyouneed,parse_incoming,distribute_jobs);