4f02c1f483bafee45b4dc3d149fbb89b246d5b26
[physik/nlsop.git] / nlsop_server.c
1 /*
2  * nlsop server code
3  *
4  * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
5  *
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.
11  *
12  * refs: 
13  *  - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
14  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
15  *
16  * Copyright (C) 2004 Frank Zirkelbach
17  *
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.
22  *
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.
27  *
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
31  *
32  */
33
34 #define _GNU_SOURCE
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <signal.h>
43
44 #include "nlsop.h"
45 #include "dfbapi.h"
46 #include "random.h"
47
48 #include "network.h"
49 #include "event.h"
50 #include "list.h"
51
52 #include "nlsop_general.h"
53
54 /* globals */
55 int *gi;
56 t_net *gnet;
57 t_event *gevent;
58 t_list *gc_list;
59 t_list *gg_list;
60 int alert;
61
62 /*
63  * server specific stuff
64  */
65
66 int usage(char *prog)
67 {
68  puts("usage:");
69  printf("%s <listen port>\n",prog);
70  return 1;
71 }
72
73 int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
74
75   int channel;
76   unsigned char data;
77   t_client client;
78   int gui_chan;
79
80   channel=network_manage_incoming(net);
81   if(channel==N_E_ACCEPT) {
82     printf("accept failed!\n");
83     return -1;
84   }
85   if(channel==N_E_MAXC) {
86     printf("maximum connections reached!\n");
87     return -1;
88   }
89   printf("connection from %s port %d (ch: %d)\n",net->connection[channel].ip,
90                                                  net->connection[channel].port,
91                                                  channel);
92
93   /* are you client or gui? */
94   network_receive_chan(net,channel,&data,1);
95   if(data==NLSOP_GUI) {
96     gui_chan=channel;
97     list_add_element(g_list,&gui_chan,sizeof(int));
98     printf("node is a gui\n");
99   }
100   else if(data==NLSOP_CLIENT) {
101     client.status=IDLE;
102     client.channel=channel;
103     list_add_element(c_list,&client,sizeof(t_client));
104     printf("node is a client\n");
105   }
106   else {
107     printf("not a client or gui - lets kick that ass out of here!\n");
108     network_close(net,channel);
109     return -1;
110   }
111
112   /* if we have a new node - care for it! */
113   event_math(net->connection[channel].fd,event,READ,ADD);
114
115   printf("\n");
116
117   return 1;
118 }
119
120 int save_job(t_net *net,int channel,t_job *job,unsigned char dc) {
121
122   char filename[128];
123   int fd;
124   int ret;
125   d3_lattice d3l;
126   info info;
127   unsigned char data;
128
129   printf("receiving data from client (%d)\n",job->size);
130   data=DATA_OK;
131
132   ret=network_receive_chan(net,channel,&d3l,sizeof(d3_lattice));
133   network_send_chan(net,channel,&data,sizeof(unsigned char));
134   printf("debug: got d3_lattice\n");
135
136   ret=network_receive_chan(net,channel,&info,sizeof(info));
137   network_send_chan(net,channel,&data,sizeof(unsigned char));
138   printf("debug: got info\n");
139
140   ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
141   network_send_chan(net,channel,&data,sizeof(unsigned char));
142   printf("debug: got ac\n");
143
144   ret=network_receive_chan(net,channel,(unsigned char *)job->cc,
145                            job->size*sizeof(int));
146   network_send_chan(net,channel,&data,sizeof(unsigned char));
147   printf("debug: got cc\n");
148
149   ret=network_receive_chan(net,channel,(unsigned char *)&(job->step),
150                            sizeof(int));
151   network_send_chan(net,channel,&data,sizeof(unsigned char));
152   printf("debug: got steps\n");
153
154   if(dc!=DC_QUIT) {
155     snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
156              job->info.b,job->info.c,job->info.s,
157              job->info.diff_rate,job->info.dr_ac,
158              job->step,job->info.steps);
159     if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
160       printf("FATAL: unable to open file %s\n",filename);
161       return -1;
162     }
163
164     if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
165       printf("FATAL: write of d3_lattice failed\n");
166       return -1;
167     }
168
169     if(write(fd,&info,sizeof(info))<sizeof(info)) {
170       printf("FATAL: write of info failed\n");
171       return -1;
172     }
173
174     ret=write(fd,job->ac,job->size*sizeof(unsigned char));
175     if(ret<job->size*sizeof(unsigned char)) {
176       printf("FATAL: write of a/c states failed\n");
177       return -1;
178     }
179    
180     ret=write(fd,job->cc,job->size*sizeof(int));
181     if(ret<job->size*sizeof(int)) {
182       printf("FATAL: write of c.-conc. failed\n");
183       return -1;
184     }
185
186     close(fd);
187     
188   }
189
190   return 1;
191 }
192
193 int add_job(t_net *net,int chan,t_list *jl) {
194
195   t_job job;
196
197   job.channel=-1;
198   job.status=IN_QUEUE;
199   job.progress=0;
200
201   network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
202   network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
203   network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
204   network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
205
206   job.size=job.x*job.y*job.z;
207
208   job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
209   if(job.ac==NULL) {
210     printf("unable to malloc a/c memory\n");
211     return -1;
212   }
213
214   job.cc=(int *)malloc(job.size*sizeof(int));
215   if(job.cc==NULL) {
216     printf("unable to malloc cc memory\n");
217     return -1;
218   }
219
220   job.step=0;
221
222   list_add_element(jl,&job,sizeof(t_job));
223   
224   printf("job added: b=%f | c=%f | s=%f ...\n",
225          job.info.b,job.info.c,job.info.s);
226
227   return 1;
228 }
229
230 int send_status(t_net *net,int chan,t_list *jl) {
231
232   unsigned char data;
233   int count;
234   int i;
235
236   data=GUI_INFO;
237
238   count=list_count(jl);
239
240   printf("sending job info\n");
241
242   network_send_chan(net,chan,&data,sizeof(unsigned char));
243   network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
244
245   list_reset(jl);
246   for(i=0;i<count;i++) {
247     network_send_chan(net,chan,jl->current->data,sizeof(t_job));
248     list_next(jl);
249   }
250
251   return 1;
252 }
253
254 int handle_node(t_net *net,t_event *event,
255                 t_list *c_list,t_list *g_list,t_list *job) {
256
257   int i;
258   unsigned char data;
259   t_client *c;
260   t_job *j;
261
262   gi=&i;
263
264   for(i=0;i<MAX_CONNECTIONS;i++) {
265     if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
266
267       alert=0;
268       alarm(1);
269       network_receive_chan(net,i,&data,1);
270       alarm(0);
271
272       if(alert==1) return -1;
273
274       if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
275         /* it's a client */
276         list_search_data(job,&i,sizeof(int));
277         j=(t_job *)job->current->data;
278         c=(t_client *)c_list->current-data;
279
280         if(data==DC_END) {
281           save_job(net,i,j,DC_END);
282           /* reset client */
283           c->status=IDLE;
284           /* free job memory */
285           free(j->ac);
286           free(j->cc);
287           /* delete job entry */
288           list_del_current(job);
289           printf("job ended, saved and removed from list.\n");
290         }
291
292         if(data==DC_OK) {
293           save_job(net,i,j,DC_OK);
294           /* inc progress state */
295           j->progress+=1;
296           printf("job at next level, saved.\n");
297         }
298
299         if(data==DC_QUIT) {
300           save_job(net,i,j,DC_QUIT);
301           /* network disconnect */
302           event_math(net->connection[i].fd,event,READ,REMOVE);
303           network_close(net,i);
304           /* del from client list */
305           list_del_current(c_list);
306           /* change job state */
307           j->status=IN_QUEUE;
308           printf("client terminating, job queued, client removed.\n");
309         }
310       }
311
312       else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
313         /* its a gui */
314         if(data==GUI_ADDJOB) add_job(net,i,job);
315
316         else if(data==GUI_INFO) send_status(net,i,job);
317
318         else if(data==GUI_QUIT) {
319           printf("disconnecting gui on channel %d\n",i);
320           event_math(net->connection[i].fd,event,READ,REMOVE);
321           network_close(net,i);
322           list_del_current(g_list);
323         }
324
325         else {
326           printf("unknown gui command\n");
327           return -1;
328         }
329       }
330
331       else {
332         printf("this chan is not in client or gui list! i disconnect now!\n");
333         event_math(net->connection[i].fd,event,READ,REMOVE);
334         network_close(net,i);
335       }
336     }
337   }
338
339   printf("\n");
340    
341   return 1;
342 }
343
344 int distribute_jobs(t_event *event,void *allineed) {
345
346   t_net *net;
347   t_list *c_list,*g_list,*job;
348   int count_j,count_c,min;
349   t_job *j;
350   t_client *c;
351   unsigned char data;
352   d3_lattice d3l;
353   unsigned int addr[4];
354
355   memcpy(addr,allineed,4*sizeof(unsigned int));
356
357   net=(t_net *)addr[0];
358   c_list=(t_list *)addr[1];
359   g_list=(t_list *)addr[2];
360   job=(t_list *)addr[3];
361
362   list_reset(job);
363   list_reset(c_list);
364
365   count_j=0;
366   count_c=0;
367
368   if((c_list->current==NULL)||(job->current==NULL)) return 2;
369
370   j=(t_job *)job->current->data;
371   c=(t_client *)c_list->current->data;
372   if(j->status==IN_QUEUE) count_j++;
373   if(c->status==IDLE) count_c++;
374
375   while(list_next(job)!=L_NO_NEXT_ELEMENT) {
376     j=(t_job *)job->current->data;
377     if(j->status==IN_QUEUE) count_j++;
378   }
379   while(list_next(c_list)!=L_NO_NEXT_ELEMENT) {
380     c=(t_client *)c_list->current->data;
381     if(c->status==IDLE) count_c++;
382   }
383  
384   min=(count_c<count_j)?count_c:count_j;
385
386   if(min!=0) {
387     printf("d: distributing jobs ...\n");
388     printf("%d queued jobs, %d idle clients\n\n",count_j,count_c);
389   }
390
391   list_reset(job);
392   list_reset(c_list);
393
394   while(min) {
395     j=(t_job *)job->current->data;
396     c=(t_client *)c_list->current->data;
397     while(c->status!=IDLE) {
398       list_next(c_list);
399       c=(t_client *)c_list->current->data;
400     }
401     while(j->status!=IN_QUEUE) {
402       list_next(job);
403       j=(t_job *)job->current->data;
404     }
405
406     /* direct current job to current client */
407     if(j->step==0) data=NLSOP_NJOB;
408     else data=NLSOP_CJOB;
409
410     c->status=WORK;
411     j->channel=c->channel;
412     j->status=IN_WORK;
413
414     d3l.max_x=j->x;
415     d3l.max_y=j->y;
416     d3l.max_z=j->z;
417
418     network_send_chan(net,c->channel,&data,sizeof(unsigned char));
419     network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
420     network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
421
422     if(data==NLSOP_CJOB) {
423       network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
424       network_receive_chan(net,c->channel,&data,sizeof(unsigned char));
425       network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
426                         j->size*sizeof(int));
427       network_receive_chan(net,c->channel,&data,sizeof(unsigned char));
428       network_send_chan(net,c->channel,(unsigned char *)&(j->step),sizeof(int));
429       network_receive_chan(net,c->channel,&data,sizeof(unsigned char));
430     }
431
432     --min;
433     list_next(c_list);
434     list_next(job);
435   }
436
437   return 1;
438 }
439
440 int parse_incoming(t_event *event,void *allineed) {
441
442   t_net *net;
443   t_list *c_list,*g_list,*job;
444   unsigned int addr[4];
445
446   memcpy(addr,allineed,4*sizeof(unsigned int));
447
448   net=(t_net *)addr[0];
449   c_list=(t_list *)addr[1];
450   g_list=(t_list *)addr[2];
451   job=(t_list *)addr[3];
452
453   /* decide what to do */
454   if(FD_ISSET(net->l_fd,&(event->rfds))) {
455     /* new node */
456     printf("new node ...\n");
457     add_node(net,event,c_list,g_list);
458   }
459   else {
460     /* client/gui interaction */
461     printf("node interaction ...\n");
462     handle_node(net,event,c_list,g_list,job);
463   }
464     
465   return 1;
466 }
467
468 void destroy_it(int signum) {
469
470   printf("connection to client (ch %d) fucked up!\n",*gi);
471   event_math(gnet->connection[*gi].fd,gevent,READ,REMOVE);
472   network_close(gnet,*gi);
473   if(list_search_data(gc_list,gi,sizeof(int))==L_SUCCESS) {
474     list_del_current(gc_list);
475     printf("removed client from list\n");
476   }
477   if(list_search_data(gg_list,gi,sizeof(int))==L_SUCCESS) {
478     list_del_current(gg_list);
479     printf("removed gui from list\n");
480   }
481
482   alert=1;
483   alarm(0);
484
485 }
486
487 /*
488  * main program
489  */
490
491 int main(int argc,char **argv)
492 {
493
494   int port;
495   t_net net;
496   t_event event;
497   t_list c_list;
498   t_list g_list;
499   t_list job;
500   void *allyouneed;
501   unsigned int addr[4];
502
503   gnet=&net;
504   gevent=&event;
505   gc_list=&c_list;
506   gg_list=&g_list;
507
508   /* tzzz ... */
509   allyouneed=(void *)addr;
510   addr[0]=(unsigned int)&net;
511   addr[1]=(unsigned int)&c_list;
512   addr[2]=(unsigned int)&g_list;
513   addr[3]=(unsigned int)&job;
514   
515   /* default values */
516   port=1025;
517
518   /* parse argv */
519   if(argc==2) port=atoi(argv[1]);
520
521   /* event init */
522   event_init(&event,1);
523   /* 10 sec event timeout - distributing jobs */
524   event_set_timeout(&event,10,0);
525
526   /* list init */
527   list_init(&c_list,1);
528   list_init(&g_list,1);
529   list_init(&job,1);
530
531   /* connect to server */
532   network_init(&net,1);
533   network_set_listen_port(&net,port);
534   if(network_listen(&net)!=N_SUCCESS) {
535     printf("unable to listen on port %d, aborting!\n",port);
536     return -1;
537   }
538
539   /* install sighandler */
540   signal(SIGALRM,destroy_it);
541
542   /* wait for events :) */
543   event_math(net.l_fd,&event,READ,ADD);
544   printf("\nNLSOP_SERVER started!\n\n");
545   event_start(&event,allyouneed,parse_incoming,distribute_jobs);
546
547   return 1;
548 }
549