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