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