send quit message on gui quit + serer recognizes "fucked up" connection now
[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   return 1;
116 }
117
118 int save_job(t_net *net,int channel,t_job *job,unsigned char dc) {
119
120   char filename[128];
121   int fd;
122   int ret;
123   d3_lattice d3l;
124   info info;
125
126   ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
127   if(ret==N_ERROR) printf("FATAL: getting ac status failed\n");
128   ret=network_receive_chan(net,channel,(unsigned char *)job->cc,
129                            job->size*sizeof(int));
130   if(ret==N_ERROR) printf("FATAL: getting cc failed\n");
131   ret=network_receive_chan(net,channel,(unsigned char *)&(job->step),
132                            sizeof(int));
133   if(ret==N_ERROR) printf("FATAL: getting step number failed\n");
134
135   if(dc!=DC_QUIT) {
136     snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
137              job->info.b,job->info.c,job->info.s,
138              job->info.diff_rate,job->info.dr_ac,
139              job->step,job->info.steps);
140     if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
141       printf("FATAL: unable to open file %s\n",filename);
142       return -1;
143     }
144
145     memset(&d3l,0,sizeof(d3_lattice));
146     d3l.max_x=job->x;
147     d3l.max_y=job->y;
148     d3l.max_z=job->z;
149     if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
150       printf("FATAL: write of d3_lattice failed\n");
151       return -1;
152     }
153
154     if(write(fd,&(job->info),sizeof(info))<sizeof(info)) {
155       printf("FATAL: write of info failed\n");
156       return -1;
157     }
158
159     ret=write(fd,job->ac,job->size*sizeof(unsigned char));
160     if(ret<job->size*sizeof(unsigned char)) {
161       printf("FATAL: write of a/c states failed\n");
162       return -1;
163     }
164    
165     ret=write(fd,job->cc,job->size*sizeof(int));
166     if(ret<job->size*sizeof(int)) {
167       printf("FATAL: write of c.-conc. failed\n");
168       return -1;
169     }
170
171     close(fd);
172     
173   }
174
175   return 1;
176 }
177
178 int add_job(t_net *net,int chan,t_list *jl) {
179
180   t_job job;
181
182   job.channel=-1;
183   job.status=IN_QUEUE;
184   job.progress=0;
185
186   network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
187   network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
188   network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
189   network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
190
191   job.size=job.x*job.y*job.z;
192
193   job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
194   if(job.ac==NULL) {
195     printf("unable to malloc a/c memory\n");
196     return -1;
197   }
198
199   job.cc=(int *)malloc(job.size*sizeof(int));
200   if(job.cc==NULL) {
201     printf("unable to malloc cc memory\n");
202     return -1;
203   }
204
205   job.step=0;
206
207   list_add_element(jl,&job,sizeof(t_job));
208
209   return 1;
210 }
211
212 int send_status(t_net *net,int chan,t_list *jl) {
213
214   unsigned char data;
215   int count;
216   int i;
217
218   data=GUI_INFO;
219   count=list_count(jl);
220
221   network_send_chan(net,chan,&data,sizeof(unsigned char));
222   network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
223
224   list_reset(jl);
225   for(i=0;i<count;i++) {
226     network_send_chan(net,chan,jl->current->data,sizeof(t_job));
227     list_next(jl);
228   }
229
230   return 1;
231 }
232
233 int handle_node(t_net *net,t_event *event,
234                 t_list *c_list,t_list *g_list,t_list *job) {
235
236   int i;
237   unsigned char data;
238   t_client *c;
239   t_job *j;
240
241   gi=&i;
242
243   for(i=0;i<MAX_CONNECTIONS;i++) {
244     if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
245
246       alert=0;
247       alarm(1);
248       network_receive_chan(net,i,&data,1);
249       alarm(0);
250
251       if(alert==1) return -1;
252
253       if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
254         /* it's a client */
255         list_search_data(job,&i,sizeof(int));
256         j=(t_job *)job->current->data;
257         c=(t_client *)c_list->current-data;
258
259         if(data==DC_END) {
260           save_job(net,i,j,DC_END);
261           /* reset client */
262           c->status=IDLE;
263           /* delete job entry */
264           list_del_current(job);
265         }
266
267         if(data==DC_OK) {
268           save_job(net,i,j,DC_OK);
269           /* inc progress state */
270           j->progress+=1;
271         }
272
273         if(data==DC_QUIT) {
274           save_job(net,i,j,DC_QUIT);
275           /* network disconnect */
276           event_math(net->connection[i].fd,event,READ,REMOVE);
277           network_close(net,i);
278           /* del from client list */
279           list_del_current(c_list);
280           /* change job state */
281           j->status=IN_QUEUE;
282         }
283       }
284
285       else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
286         /* its a gui */
287         if(data==GUI_ADDJOB) add_job(net,i,job);
288
289         else if(data==GUI_INFO) send_status(net,i,job);
290
291         else if(data==GUI_QUIT) {
292           printf("disconnecting gui on channel %d\n",i);
293           event_math(net->connection[i].fd,event,READ,REMOVE);
294           network_close(net,i);
295           list_del_current(g_list);
296         }
297
298         else {
299           printf("unknown gui command\n");
300           return -1;
301         }
302       }
303
304       else {
305         printf("this chan is not in client or gui list! i disconnect now!\n");
306         event_math(net->connection[i].fd,event,READ,REMOVE);
307         network_close(net,i);
308       }
309     }
310   }
311    
312   return 1;
313 }
314
315 int distribute_jobs(t_event *event,void *allineed) {
316
317   t_net *net;
318   t_list *c_list,*g_list,*job;
319   int count_j,count_c;
320   t_job *j;
321   t_client *c;
322   unsigned char data;
323   d3_lattice d3l;
324   unsigned int addr[4];
325
326   memcpy(addr,allineed,4*sizeof(unsigned int));
327
328   net=(t_net *)addr[0];
329   c_list=(t_list *)addr[1];
330   g_list=(t_list *)addr[2];
331   job=(t_list *)addr[3];
332
333   count_j=list_count(job);
334   count_c=list_count(c_list);
335
336   list_reset(job);
337   list_reset(c_list);
338   while((count_c!=0)&&(count_j!=0)) {
339     j=(t_job *)job->current->data;
340     c=(t_client *)c_list->current->data;
341     while(c->status!=IDLE) {
342       list_next(c_list);
343       c=(t_client *)c_list->current->data;
344     }
345     while(j->status!=IN_QUEUE) {
346       list_next(job);
347       j=(t_job *)job->current->data;
348     }
349
350     /* direct current job to current client */
351     if(j->step==0) data=NLSOP_NJOB;
352     else data=NLSOP_CJOB;
353
354     c->status=WORK;
355     j->channel=c->channel;
356     j->status=IN_WORK;
357
358     d3l.max_x=j->x;
359     d3l.max_y=j->y;
360     d3l.max_z=j->z;
361
362     network_send_chan(net,c->channel,&data,sizeof(unsigned char));
363     network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
364     network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
365
366     if(data==NLSOP_CJOB) {
367       network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
368       network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
369                         j->size*sizeof(int));
370     }
371
372     --count_c;
373     --count_j;
374     list_next(c_list);
375     list_next(job);
376   }
377
378   return 1;
379 }
380
381 int parse_incoming(t_event *event,void *allineed) {
382
383   t_net *net;
384   t_list *c_list,*g_list,*job;
385   unsigned int addr[4];
386
387   memcpy(addr,allineed,4*sizeof(unsigned int));
388
389   net=(t_net *)addr[0];
390   c_list=(t_list *)addr[1];
391   g_list=(t_list *)addr[2];
392   job=(t_list *)addr[3];
393
394   /* decide what to do */
395   if(FD_ISSET(net->l_fd,&(event->rfds))) {
396     /* new node */
397     printf("new node ...\n");
398     add_node(net,event,c_list,g_list);
399   }
400   else {
401     /* client/gui interaction */
402     printf("node interaction ...\n");
403     handle_node(net,event,c_list,g_list,job);
404   }
405     
406   return 1;
407 }
408
409 void destroy_it(int signum) {
410
411   printf("connection to client (ch %d) fucked up!\n",*gi);
412   event_math(gnet->connection[*gi].fd,gevent,READ,REMOVE);
413   network_close(gnet,*gi);
414   if(list_search_data(gc_list,gi,sizeof(int))==L_SUCCESS) {
415     list_del_current(gc_list);
416     printf("removed client from list\n");
417   }
418   if(list_search_data(gg_list,gi,sizeof(int))==L_SUCCESS) {
419     list_del_current(gg_list);
420     printf("removed gui from list\n");
421   }
422
423   alert=1;
424   alarm(0);
425
426 }
427
428 /*
429  * main program
430  */
431
432 int main(int argc,char **argv)
433 {
434
435   int port;
436   t_net net;
437   t_event event;
438   t_list c_list;
439   t_list g_list;
440   t_list job;
441   void *allyouneed;
442   unsigned int addr[4];
443
444   gnet=&net;
445   gevent=&event;
446   gc_list=&c_list;
447   gg_list=&g_list;
448
449   /* tzzz ... */
450   allyouneed=(void *)addr;
451   addr[0]=(unsigned int)&net;
452   addr[1]=(unsigned int)&c_list;
453   addr[2]=(unsigned int)&g_list;
454   addr[3]=(unsigned int)&job;
455   
456   /* default values */
457   port=1025;
458
459   /* parse argv */
460   if(argc==2) port=atoi(argv[1]);
461
462   /* event init */
463   event_init(&event,1);
464   /* 10 sec event timeout - distributing jobs */
465   event_set_timeout(&event,10,0);
466
467   /* list init */
468   list_init(&c_list,1);
469   list_init(&g_list,1);
470   list_init(&job,1);
471
472   /* connect to server */
473   network_init(&net,1);
474   network_set_listen_port(&net,port);
475   if(network_listen(&net)!=N_SUCCESS) {
476     printf("unable to listen on port %d, aborting!\n",port);
477     return -1;
478   }
479
480   /* install sighandler */
481   signal(SIGALRM,destroy_it);
482
483   /* wait for events :) */
484   event_math(net.l_fd,&event,READ,ADD);
485   printf("\nNLSOP_SERVER started!\n\n");
486   event_start(&event,allyouneed,parse_incoming,distribute_jobs);
487
488   return 1;
489 }
490