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