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