some bugfixes + new tests
[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   network_send_chan(net,channel,&data,sizeof(unsigned char));
133
134   network_receive_chan(net,channel,(unsigned char *)&d3l,
135                            sizeof(d3_lattice));
136   network_send_chan(net,channel,&data,sizeof(unsigned char));
137   printf("debug: got d3_lattice\n");
138
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");
142
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");
146
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");
151
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");
155
156   if(dc!=DC_QUIT) {
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);
163       return -1;
164     }
165
166     if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
167       printf("FATAL: write of d3_lattice failed\n");
168       return -1;
169     }
170
171     if(write(fd,&info,sizeof(info))<sizeof(info)) {
172       printf("FATAL: write of info failed\n");
173       return -1;
174     }
175
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");
179       return -1;
180     }
181    
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");
185       return -1;
186     }
187
188     close(fd);
189     
190   }
191
192   return 1;
193 }
194
195 int add_job(t_net *net,int chan,t_list *jl) {
196
197   t_job job;
198
199   job.channel=-1;
200   job.status=IN_QUEUE;
201   job.progress=0;
202
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));
207
208   job.size=job.x*job.y*job.z;
209
210   job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
211   if(job.ac==NULL) {
212     printf("unable to malloc a/c memory\n");
213     return -1;
214   }
215
216   job.cc=(int *)malloc(job.size*sizeof(int));
217   if(job.cc==NULL) {
218     printf("unable to malloc cc memory\n");
219     return -1;
220   }
221
222   job.step=0;
223
224   list_add_element(jl,&job,sizeof(t_job));
225   
226   printf("job added: b=%f | c=%f | s=%f ...\n",
227          job.info.b,job.info.c,job.info.s);
228
229   return 1;
230 }
231
232 int send_status(t_net *net,int chan,t_list *jl) {
233
234   unsigned char data;
235   int count;
236   int i;
237
238   data=GUI_INFO;
239
240   count=list_count(jl);
241
242   printf("sending job info\n");
243
244   network_send_chan(net,chan,&data,sizeof(unsigned char));
245   network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
246
247   list_reset(jl);
248   for(i=0;i<count;i++) {
249     network_send_chan(net,chan,jl->current->data,sizeof(t_job));
250     list_next(jl);
251   }
252
253   return 1;
254 }
255
256 int handle_node(t_net *net,t_event *event,
257                 t_list *c_list,t_list *g_list,t_list *job) {
258
259   int i;
260   unsigned char data;
261   t_client *c;
262   t_job *j;
263
264   gi=&i;
265
266   for(i=0;i<MAX_CONNECTIONS;i++) {
267     if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
268
269       alert=0;
270       alarm(1);
271       network_receive_chan(net,i,&data,1);
272       alarm(0);
273
274       if(alert==1) return -1;
275
276       if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
277         /* it's a client */
278         list_search_data(job,&i,sizeof(int));
279         j=(t_job *)job->current->data;
280         c=(t_client *)c_list->current-data;
281
282         if(data==DC_END) {
283           save_job(net,i,j,DC_END);
284           /* reset client */
285           printf("client in state %c now\n",c->status&IDLE?'i':'a');
286           c->status=IDLE;
287           printf("now: %c\n",c->status&IDLE?'i':'a');
288           printf("it is channel %d\n",c->channel);
289           /* free job memory */
290           free(j->ac);
291           free(j->cc);
292           /* delete job entry */
293           list_del_current(job);
294           printf("job ended, saved and removed from list.\n");
295         }
296
297         if(data==DC_OK) {
298           save_job(net,i,j,DC_OK);
299           /* inc progress state */
300           j->progress+=1;
301           printf("job at next level, saved.\n");
302         }
303
304         if(data==DC_QUIT) {
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 */
312           j->status=IN_QUEUE;
313           printf("client terminating, job queued, client removed.\n");
314         }
315       }
316
317       else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
318         /* its a gui */
319         if(data==GUI_ADDJOB) add_job(net,i,job);
320
321         else if(data==GUI_INFO) send_status(net,i,job);
322
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);
328         }
329
330         else {
331           printf("unknown gui command\n");
332           return -1;
333         }
334       }
335
336       else {
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);
340       }
341     }
342   }
343
344   printf("\n");
345    
346   return 1;
347 }
348
349 int distribute_jobs(t_event *event,void *allineed) {
350
351   t_net *net;
352   t_list *c_list,*g_list,*job;
353   int count_j,count_c,min;
354   t_job *j;
355   t_client *c;
356   unsigned char data;
357   d3_lattice d3l;
358   unsigned int addr[4];
359
360   memcpy(addr,allineed,4*sizeof(unsigned int));
361
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];
366
367   list_reset(job);
368   list_reset(c_list);
369
370   count_j=0;
371   count_c=0;
372
373   if((c_list->current==NULL)||(job->current==NULL)) return 2;
374
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++;
379
380   while(list_next(job)!=L_NO_NEXT_ELEMENT) {
381     j=(t_job *)job->current->data;
382     if(j->status==IN_QUEUE) count_j++;
383   }
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++;
387   }
388  
389   min=(count_c<count_j)?count_c:count_j;
390
391   if(min!=0) {
392     printf("d: distributing jobs ...\n");
393     printf("%d queued jobs, %d idle clients\n\n",count_j,count_c);
394   }
395
396   list_reset(job);
397   list_reset(c_list);
398
399   while(min) {
400     j=(t_job *)job->current->data;
401     c=(t_client *)c_list->current->data;
402     while(c->status!=IDLE) {
403       list_next(c_list);
404       c=(t_client *)c_list->current->data;
405     }
406     while(j->status!=IN_QUEUE) {
407       list_next(job);
408       j=(t_job *)job->current->data;
409     }
410
411     /* direct current job to current client */
412     if(j->step==0) data=NLSOP_NJOB;
413     else data=NLSOP_CJOB;
414
415     c->status=WORK;
416     j->channel=c->channel;
417     j->status=IN_WORK;
418
419     d3l.max_x=j->x;
420     d3l.max_y=j->y;
421     d3l.max_z=j->z;
422
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));
426
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));
435     }
436
437     --min;
438     list_next(c_list);
439     list_next(job);
440   }
441
442   return 1;
443 }
444
445 int parse_incoming(t_event *event,void *allineed) {
446
447   t_net *net;
448   t_list *c_list,*g_list,*job;
449   unsigned int addr[4];
450
451   memcpy(addr,allineed,4*sizeof(unsigned int));
452
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];
457
458   /* decide what to do */
459   if(FD_ISSET(net->l_fd,&(event->rfds))) {
460     /* new node */
461     printf("new node ...\n");
462     add_node(net,event,c_list,g_list);
463   }
464   else {
465     /* client/gui interaction */
466     printf("node interaction ...\n");
467     handle_node(net,event,c_list,g_list,job);
468   }
469     
470   return 1;
471 }
472
473 void destroy_it(int signum) {
474
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");
481   }
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");
485   }
486
487   alert=1;
488   alarm(0);
489
490 }
491
492 /*
493  * main program
494  */
495
496 int main(int argc,char **argv)
497 {
498
499   int port;
500   t_net net;
501   t_event event;
502   t_list c_list;
503   t_list g_list;
504   t_list job;
505   void *allyouneed;
506   unsigned int addr[4];
507
508   gnet=&net;
509   gevent=&event;
510   gc_list=&c_list;
511   gg_list=&g_list;
512
513   /* tzzz ... */
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;
519   
520   /* default values */
521   port=1025;
522
523   /* parse argv */
524   if(argc==2) port=atoi(argv[1]);
525
526   /* event init */
527   event_init(&event,1);
528   /* 10 sec event timeout - distributing jobs */
529   event_set_timeout(&event,10,0);
530
531   /* list init */
532   list_init(&c_list,1);
533   list_init(&g_list,1);
534   list_init(&job,1);
535
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);
541     return -1;
542   }
543
544   /* install sighandler */
545   signal(SIGALRM,destroy_it);
546
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);
551
552   return 1;
553 }
554