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