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