some more bugfixes + testing
[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   char data;
113   char string[MAX_CONTENT];
114
115   data=GUI_INFO;
116
117   /* request information */
118   network_send_chan(net,0,&data,sizeof(unsigned char));
119
120   /* receive information */
121   network_receive_chan(net,0,&data,sizeof(unsigned char));
122   if(data!=GUI_INFO) {
123     display_new_line(display,input,"no gui info answer, ignored");
124     return -1;
125   }
126
127   network_receive_chan(net,0,(unsigned char *)&count,sizeof(int));
128   for(i=0;i<count;i++) {
129     network_receive_chan(net,0,(unsigned char *)&job,sizeof(t_job));
130     snprintf(string,MAX_CONTENT,
131              "channel %d %c => b=%f c=%f s=%f | d: %d %f | %d",
132              job.channel,job.status&IN_WORK?'a':'q',
133              job.info.b,job.info.c,job.info.s,
134              job.info.diff_rate,job.info.dr_ac,job.progress);
135     display_new_line(display,input,string);
136   }
137
138   display_new_line(display,input,"done");
139
140   return 1;
141 }
142
143 int send_job(t_net *net,t_input *input,t_display *display) {
144
145   char command[MAX_CONTENT];
146   int x,y,z;
147   info info;
148   char *ptr;
149
150   /* default values */
151   x=_X;
152   y=_Y;
153   z=_Z;
154   info.cc=CC;
155   info.steps=STEPS;
156   info.range=RANGE;
157   info.diff_rate=DIFF_RATE;
158   info.cpi=CPI;
159   info.s_rate=S_RATE;
160   info.save_rate=RESAVE;
161   info.s=S_D;
162   info.b=B_D;
163   info.c=C_D;
164   info.dr_ac=DR_AC;
165
166   strncpy(command,input->content,MAX_CONTENT);
167   ptr=command;
168
169   ptr=strtok(command," ");
170   while((ptr=strtok(NULL," "))!=NULL) {
171     switch(ptr[0]) {
172       case 'b':
173         info.b=atof(ptr+1);
174         break;
175       case 'c':
176         info.c=atof(ptr+1);
177         break;
178       case 's':
179         info.s=atof(ptr+1);
180         break;
181       case 'd':
182         info.diff_rate=atoi(ptr+1);
183         break;
184       case 'D':
185         info.dr_ac=atof(ptr+1);
186         break;
187       case 'S':
188         info.steps=atoi(ptr+1);
189         break;
190       case 'C':
191         info.cpi=atoi(ptr+1);
192         break;
193       case 'r':
194         info.range=atoi(ptr+1);
195         break;
196       case 'R':
197         info.s_rate=atoi(ptr+1);
198         break;
199       case 'x':
200         info.save_rate=atoi(ptr+1);
201         break;
202       case 'X':
203         x=atoi(ptr+1);
204         break;
205       case 'Y':
206         y=atoi(ptr+1);
207         break;
208       case 'Z':
209         z=atoi(ptr+1);
210         break;
211       default:
212         display_new_line(display,input,"unknown command");
213         dprintf(display->outfd,"unknown command %s\n",ptr);
214         break;
215     }
216   }
217
218   command[0]=GUI_ADDJOB;
219   network_send_chan(net,0,command,1);
220
221   network_send_chan(net,0,(unsigned char *)&x,sizeof(int));
222   network_send_chan(net,0,(unsigned char *)&y,sizeof(int));
223   network_send_chan(net,0,(unsigned char *)&z,sizeof(int));
224
225   network_send_chan(net,0,(unsigned char *)&info,sizeof(info));
226
227   display_new_line(display,input,"added new job:");
228   snprintf(command,MAX_CONTENT,"b:%f | c:%f | s:%f | d: %d %f | %d",
229            info.b,info.c,info.s,info.diff_rate,info.dr_ac,info.steps);
230   display_new_line(display,input,command);
231
232
233   return 1;
234 }
235
236 int get_whatever(t_input *input,void *ptr) {
237
238   char last;
239   t_display *display;
240
241   display=(t_display *)ptr;
242
243   last=input->content[input->c_count-1];
244
245   if((last=='\n')||(last=='\r')) {
246     input->content[input->c_count-1]='\0';
247     input->c_count=0;
248     return GOON;
249   }
250
251   display_new_line(display,input,NULL);
252
253   return 1;
254 }
255
256 int nothing(t_event *event,void *allineed) {
257
258   return 1;
259 }
260
261 int send_quit(t_net *net) {
262
263   unsigned char data;
264
265   data=GUI_QUIT;
266
267   network_send_chan(net,0,&data,1);
268
269   return 1;
270 }
271
272 int get_command(t_event *event,void *allineed) {
273
274   t_net *net;
275   t_display *display;
276   t_input *input;
277   unsigned int addr[3];
278
279   memcpy(addr,allineed,3*sizeof(unsigned int));
280   net=(t_net *)addr[0];
281   display=(t_display *)addr[1];
282   input=(t_input *)addr[2];
283
284   input_get_event(input,get_whatever,display);
285
286   /* if there was a new line! */
287   if(input->c_count==0) {
288     switch(input->content[0]) {
289       case GUI_INFO:
290         send_and_wait_for_answer(net,display,input);
291         break;
292       case GUI_ADDJOB:
293         send_job(net,input,display);
294         break;
295       case GUI_QUIT:
296         /* stop everything */
297         event_math(0,event,READ,REMOVE);
298         send_quit(net);
299         network_shutdown(net);
300         input_shutdown(input);
301         display_shutdown(display);
302         event_stop(event);
303         return 2;
304       default:
305         display_new_line(display,input,"unknown gui command");
306         break;
307     }
308   }
309
310   return 1;
311 }
312
313 /*
314  * main program
315  */
316
317 int main(int argc,char **argv)
318 {
319
320   char server_ip[16];
321   char logfile[64];
322   int port;
323
324   t_net net;
325   t_event event;
326   t_display display;
327   t_input input;
328
329   unsigned char data;
330   int i;
331
332   int fd;
333
334   unsigned int addr[3];
335   void *allyouneed;
336
337   allyouneed=(void *)addr;
338
339   addr[0]=(unsigned int)&net;
340   addr[1]=(unsigned int)&display;
341   addr[2]=(unsigned int)&input;
342
343   /* default values */
344   strcpy(logfile,LOGFILE);
345   strcpy(server_ip,"");
346   port=1025;
347
348   /* parse/check argv */
349   for(i=1;i<argc;i++) {
350     if(argv[i][0]=='-') {
351       switch(argv[i][1]) {
352         case 'h':
353           usage(argv[0]);
354           return -1;
355         case 'i':
356           strncpy(server_ip,argv[++i],16);
357           break;
358         case 'p':
359           port=atoi(argv[++i]);
360           break;
361         case 'l':
362           strncpy(logfile,argv[++i],64);
363           break;
364         default:
365           usage(argv[0]);
366           return -1;
367       }
368     }
369   }
370   if(!strcmp(server_ip,"")) {
371     usage(argv[0]);
372     return -1;
373   }
374
375   if((fd=open(logfile,O_WRONLY|O_CREAT))<0) {
376     printf("unable to open file %s\n",logfile);
377     return -1;
378   }
379
380   /* display init */
381   display_init(&display,fd);
382
383   /* input init */
384   input_init(&input,fd);
385   input.mode=CONTENT_BUFFER|ECHO;
386   input_ios_init(&input);
387
388   /* event init */
389   event_init(&event,fd);
390   event_set_timeout(&event,0,0);
391
392   /* user interaction */
393   event_math(0,&event,READ,ADD);
394
395   display_new_line(&display,&input,"welcome to nlsop gui! :)");
396
397   /* connect to server */
398   network_init(&net,fd);
399   network_set_connection_info(&net,0,server_ip,port);
400   if(network_connect(&net,0)==N_E_CONNECT) {
401     printf("unable to connect to server, aborting ...\n");
402     return -1;
403   }
404   network_select(&net,0);
405
406   display_new_line(&display,&input,"successfully connected to server");
407
408   /* tell server: i am a client, i may work for you */
409   data=NLSOP_GUI;
410   network_send(net.connection[0].fd,&data,1);
411
412   display_new_line(&display,&input,"send data to server, waiting for input");
413
414   /* wait for job */
415   event_start(&event,allyouneed,get_command,nothing);
416
417   close(fd);
418
419   return 1;
420 }