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