fixed nlsop for packaging ..
[physik/nlsop.git] / nlsop_gui.c
1 /*
2  * nlsop gui 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 "display.h"
50 #include "input.h"
51
52 #include "nlsop_general.h"
53
54 #define LOGFILE "nlsop_gui_logfile"
55 #define GOON 2
56
57 int usage(char *prog)
58 {
59  puts("usage:");
60  printf("%s -i <ip> -p <port> -l <logfile>\n",prog);
61  return 1;
62 }
63
64 /*
65  * gui internal functions
66  */
67
68 int display_new_line(t_display *display,t_input *input,char *text) {
69
70   int x,y;
71   int ptr;
72   int i;
73
74   unsigned char *tmp;
75
76   x=display->max_x-1;
77   y=display->max_y-1;
78  
79   tmp=(unsigned char *)malloc(display->max_x);
80   memset(tmp,0x20,display->max_x);
81
82   display_line(display,0,0,x,0,'#');
83   display_string(display,x/2-17,1,"nlsop gui (C) 2004 Frank Zirkelbach",35);
84   display_line(display,0,2,x,2,'#');
85   display_line(display,0,y-1,x,y-1,'#');
86   display_string(display,0,y,"prompt: ",8);
87   memcpy(tmp,input->content,input->c_count);
88   display_string(display,8,y,tmp,x-9);
89
90   if(text!=NULL) {
91     ptr=3*display->max_x;
92     for(i=0;i<display->max_y-6;i++) {
93       memcpy(display->screen+ptr,display->screen+ptr+display->max_x,
94              display->max_x);
95       ptr+=display->max_x;
96     }
97     i=strlen(text);
98     memcpy(display->screen+ptr,text,i);
99     memset(display->screen+ptr+i,0x20,x-i);
100   }
101
102   display_draw(display);
103   display_set_cursor(display,8+input->c_count,y);
104
105   return 1;
106 }
107
108 int send_and_wait_for_answer(t_net *net,t_display *display,t_input *input) {
109
110   int i,count;
111   t_job job;
112   t_client client;
113   char data;
114   char string[MAX_CONTENT];
115
116   data=GUI_INFO;
117
118   /* request information */
119   network_send_chan(net,0,&data,sizeof(unsigned char));
120
121   /* receive information */
122   network_receive_chan(net,0,&data,sizeof(unsigned char));
123   if(data!=GUI_INFO) {
124     display_new_line(display,input,"no gui info answer, ignored");
125     return -1;
126   }
127
128   display_new_line(display,input,"jobs:");
129   network_receive_chan(net,0,(unsigned char *)&count,sizeof(int));
130   for(i=0;i<count;i++) {
131     network_receive_chan(net,0,(unsigned char *)&job,sizeof(t_job));
132     snprintf(string,MAX_CONTENT,
133              "channel %d %c => b=%f c=%f s=%f | d: %d %f | %d",
134              job.channel,job.status&IN_WORK?'a':'q',
135              job.info.b,job.info.c,job.info.s,
136              job.info.diff_rate,job.info.dr_ac,job.progress);
137     display_new_line(display,input,string);
138   }
139   
140   display_new_line(display,input,"clients:");
141   network_receive_chan(net,0,(unsigned char *)&count,sizeof(int));
142   for(i=0;i<count;i++) {
143     network_receive_chan(net,0,(unsigned char *)&client,sizeof(t_client));
144     if(client.status&WORK) snprintf(string,MAX_CONTENT,
145                                     "channel %d: client is busy",
146                                     client.channel);
147     else snprintf(string,MAX_CONTENT,"channel %d: client is idle",
148                                      client.status);
149     display_new_line(display,input,string);
150   }
151
152   display_new_line(display,input,"done");
153
154   return 1;
155 }
156
157 int send_job(t_net *net,t_input *input,t_display *display) {
158
159   char command[MAX_CONTENT];
160   int x,y,z;
161   info info;
162   char *ptr;
163
164   /* default values */
165   x=_X;
166   y=_Y;
167   z=_Z;
168   info.cc=CC;
169   info.steps=STEPS;
170   info.range=RANGE;
171   info.diff_rate=DIFF_RATE;
172   info.cpi=CPI;
173   info.s_rate=S_RATE;
174   info.save_rate=RESAVE;
175   info.s=S_D;
176   info.b=B_D;
177   info.c=C_D;
178   info.dr_ac=DR_AC;
179
180   strncpy(command,input->content,MAX_CONTENT);
181   ptr=command;
182
183   ptr=strtok(command," ");
184   while((ptr=strtok(NULL," "))!=NULL) {
185     switch(ptr[0]) {
186       case 'b':
187         info.b=atof(ptr+1);
188         break;
189       case 'c':
190         info.c=atof(ptr+1);
191         break;
192       case 's':
193         info.s=atof(ptr+1);
194         break;
195       case 'd':
196         info.diff_rate=atoi(ptr+1);
197         break;
198       case 'D':
199         info.dr_ac=atof(ptr+1);
200         break;
201       case 'S':
202         info.steps=atoi(ptr+1);
203         break;
204       case 'C':
205         info.cpi=atoi(ptr+1);
206         break;
207       case 'r':
208         info.range=atoi(ptr+1);
209         break;
210       case 'R':
211         info.s_rate=atoi(ptr+1);
212         break;
213       case 'x':
214         info.save_rate=atoi(ptr+1);
215         break;
216       case 'X':
217         x=atoi(ptr+1);
218         break;
219       case 'Y':
220         y=atoi(ptr+1);
221         break;
222       case 'Z':
223         z=atoi(ptr+1);
224         break;
225       default:
226         display_new_line(display,input,"unknown command");
227         dprintf(display->outfd,"unknown command %s\n",ptr);
228         break;
229     }
230   }
231
232   command[0]=GUI_ADDJOB;
233   network_send_chan(net,0,command,1);
234
235   network_send_chan(net,0,(unsigned char *)&x,sizeof(int));
236   network_send_chan(net,0,(unsigned char *)&y,sizeof(int));
237   network_send_chan(net,0,(unsigned char *)&z,sizeof(int));
238
239   network_send_chan(net,0,(unsigned char *)&info,sizeof(info));
240
241   display_new_line(display,input,"added new job:");
242   snprintf(command,MAX_CONTENT,"b:%f | c:%f | s:%f | d: %d %f | %d",
243            info.b,info.c,info.s,info.diff_rate,info.dr_ac,info.steps);
244   display_new_line(display,input,command);
245
246
247   return 1;
248 }
249
250 int get_whatever(t_input *input,void *ptr) {
251
252   char last;
253   t_display *display;
254
255   display=(t_display *)ptr;
256
257   last=input->content[input->c_count-1];
258
259   if((last=='\n')||(last=='\r')) {
260     input->content[input->c_count-1]='\0';
261     input->c_count=0;
262     return GOON;
263   }
264
265   display_new_line(display,input,NULL);
266
267   return 1;
268 }
269
270 int nothing(t_event *event,void *allineed) {
271
272   return 1;
273 }
274
275 int send_quit(t_net *net) {
276
277   unsigned char data;
278
279   data=GUI_QUIT;
280
281   network_send_chan(net,0,&data,1);
282
283   return 1;
284 }
285
286 int display_help(t_display *display,t_input *input) {
287
288   display_new_line(display,input,"h: show this help");
289   display_new_line(display,input,"i: get job/client info");
290   display_new_line(display,input,"a: add a job");
291   display_new_line(display,input,"  b: ballistic amorphization influence");
292   display_new_line(display,input,"  c: carbon induced amorphization influence");
293   display_new_line(display,input,"  s: stress induced amorphization influence");
294   display_new_line(display,input,"  d: diffusion interval");
295   display_new_line(display,input,"  D: diffusion rate");
296   display_new_line(display,input,"  S: simulation teps / dose");
297   display_new_line(display,input,"  C: collisions per ion");
298   display_new_line(display,input,"  r: stress influence range");
299   display_new_line(display,input,"  R: sputter rate");
300   display_new_line(display,input,"  x: save interval");
301   display_new_line(display,input,"  X: # cells in x direction");
302   display_new_line(display,input,"  Y: # cells in Y direction");
303   display_new_line(display,input,"  Z: # cells in Z direction");
304
305   return 1;
306 }
307
308 int get_command(t_event *event,void *allineed) {
309
310   t_net *net;
311   t_display *display;
312   t_input *input;
313   unsigned int addr[3];
314
315   memcpy(addr,allineed,3*sizeof(unsigned int));
316   net=(t_net *)addr[0];
317   display=(t_display *)addr[1];
318   input=(t_input *)addr[2];
319
320   input_get_event(input,get_whatever,display);
321
322   /* if there was a new line! */
323   if(input->c_count==0) {
324     switch(input->content[0]) {
325       case GUI_HELP:
326         display_help(display,input);
327         break;
328       case GUI_INFO:
329         send_and_wait_for_answer(net,display,input);
330         break;
331       case GUI_ADDJOB:
332         send_job(net,input,display);
333         break;
334       case GUI_QUIT:
335         /* stop everything */
336         event_math(0,event,READ,REMOVE);
337         send_quit(net);
338         network_shutdown(net);
339         input_shutdown(input);
340         display_shutdown(display);
341         event_stop(event);
342         return 2;
343       default:
344         display_new_line(display,input,"unknown gui command");
345         break;
346     }
347   }
348
349   return 1;
350 }
351
352 /*
353  * main program
354  */
355
356 int main(int argc,char **argv)
357 {
358
359   char server_ip[16];
360   char logfile[64];
361   int port;
362
363   t_net net;
364   t_event event;
365   t_display display;
366   t_input input;
367
368   unsigned char data;
369   int i;
370
371   int fd;
372
373   unsigned int addr[3];
374   void *allyouneed;
375
376   allyouneed=(void *)addr;
377
378   addr[0]=(unsigned int)&net;
379   addr[1]=(unsigned int)&display;
380   addr[2]=(unsigned int)&input;
381
382   /* default values */
383   strcpy(logfile,LOGFILE);
384   strcpy(server_ip,"137.250.82.105");
385   port=1025;
386
387   /* parse/check argv */
388   for(i=1;i<argc;i++) {
389     if(argv[i][0]=='-') {
390       switch(argv[i][1]) {
391         case 'h':
392           usage(argv[0]);
393           return -1;
394         case 'i':
395           strncpy(server_ip,argv[++i],16);
396           break;
397         case 'p':
398           port=atoi(argv[++i]);
399           break;
400         case 'l':
401           strncpy(logfile,argv[++i],64);
402           break;
403         default:
404           usage(argv[0]);
405           return -1;
406       }
407     }
408   }
409   if(!strcmp(server_ip,"")) {
410     usage(argv[0]);
411     return -1;
412   }
413
414   if((fd=open(logfile,O_WRONLY|O_CREAT))<0) {
415     printf("unable to open file %s\n",logfile);
416     return -1;
417   }
418
419   /* display init */
420   display_init(&display,fd);
421
422   /* input init */
423   input_init(&input,fd);
424   input.mode=CONTENT_BUFFER|ECHO;
425   input_ios_init(&input);
426
427   /* event init */
428   event_init(&event,fd);
429   event_set_timeout(&event,0,0);
430
431   /* user interaction */
432   event_math(0,&event,READ,ADD);
433
434   display_new_line(&display,&input,"welcome to nlsop gui! :)");
435
436   /* connect to server */
437   network_init(&net,fd);
438   network_set_connection_info(&net,0,server_ip,port);
439   if(network_connect(&net,0)==N_E_CONNECT) {
440     printf("unable to connect to server, aborting ...\n");
441     return -1;
442   }
443   network_select(&net,0);
444
445   display_new_line(&display,&input,"successfully connected to server");
446
447   /* tell server: i am a client, i may work for you */
448   data=NLSOP_GUI;
449   network_send(net.connection[0].fd,&data,1);
450
451   display_new_line(&display,&input,"send data to server, waiting for input");
452
453   /* wait for job */
454   event_start(&event,allyouneed,get_command,nothing);
455
456   close(fd);
457
458   return 1;
459 }