nearly finished gui code. adaptions / bugfixes.
[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
51 #include "nlsop_general.h"
52
53 #define LOGFILE "~/.nlsop_logfile"
54
55 int usage(char *prog)
56 {
57  puts("usage:");
58  printf("%s -i <ip> -p <port> -l <logfile>\n",prog);
59  return 1;
60 }
61
62 /*
63  * gui internal functions
64  */
65
66 int display_new_line(t_display *display,char *text) {
67
68   return 1;
69 }
70
71 int send_and_wait_for_answer(t_net *net,t_display *display) {
72
73   return 1;
74 }
75
76 int send_job(t_net *net,t_input *input,t_display *display) {
77
78   char command[MAX_CONTENT];
79   int x,y,z;
80   info info;
81
82   /* default values */
83   x=X;
84   y=Y;
85   z=Z;
86   info.cc=CC;
87   info.steps=STEPS;
88   info.range=RANGE;
89   info.diff_rate=DIFF_RATE;
90   info.cpi=CPI;
91   info.s_rate=S_RATE;
92   info.save_rate=RESAVE;
93   info.s=S_D;
94   info.b=B_D;
95   info.c=C_D;
96   info.dr_ac=DR_AC;
97
98   strncpy(command,input->content,MAX_CONTENT);
99
100   strtok(command," ");
101   while(strtok(NULL," ")!=NULL) {
102     switch(command[0]) {
103       case 'b':
104         info.b=atof(command+1);
105         break;
106       case 'c':
107         info.c=atof(command+1);
108         break;
109       case 's':
110         info.s=atof(command+1);
111         break;
112       case 'd':
113         info.diff_rate=atoi(command+1);
114         break;
115       case 'D':
116         info.dr_ac=atof(command+1);
117         break;
118       case 'S':
119         info.steps=atoi(command+1);
120         break;
121       case 'C':
122         info.cpi=atoi(command+1);
123         break;
124       case 'r':
125         info.range=atoi(command+1);
126         break;
127       case 'R':
128         info.s_rate=atoi(command+1);
129         break;
130       case 'x':
131         info.save_rate=atoi(command+1);
132         break;
133       case 'X':
134         x=atoi(command+1);
135         break;
136       case 'Y':
137         y=atoi(command+1);
138         break;
139       case 'Z':
140         z=atoi(command+1);
141         break;
142       default:
143         display_new_line(display,"unknown command");
144         break;
145     }
146   }
147
148   command[0]=GUI_ADDJOB;
149   network_send_chan(net,0,data,1);
150
151   network_send_chan(net,0,(unsigned char *)&x,sizeof(int));
152   network_send_chan(net,0,(unsigned char *)&y,sizeof(int));
153   network_send_chan(net,0,(unsigned char *)&z,sizeof(int));
154
155   network_send_chan(net,0,(unsigned char *)&info,sizeof(info));
156
157   return 1;
158 }
159
160 int get_command(t_event *event,void *allineed) {
161
162   t_net *net;
163   t_display *display;
164   t_input *input;
165
166   unsigned char data[64];
167
168   net=(t_net *)allineed;
169   display=(t_display *)(allineed+sizeof(t_net));
170   input=(t_input *)(allineed+sizeof(t_net)+sizeof(t_display));
171
172   switch(input->content[0]) {
173     case GUI_INFO:
174       send_and_wait_for_answer(net,display);
175       break;
176     case GUI_ADDJOB:
177       send_job(net,input,display)
178       break;
179     case GUI_QUIT:
180       /* stop everything */
181       event_math(0,event,READ,REMOVE);
182       network_shutdown(net);
183       display_shutdown(display);
184       input_shutdown(input);
185       event_shutdown(event);
186       return 2;
187     default:
188       display_new_line(display,"unknown gui command");
189       break;
190   }
191
192   return 1;
193 }
194
195 /*
196  * main program
197  */
198
199 int main(int argc,char **argv)
200 {
201
202   char server_ip[16];
203   char logfile[64];
204   int port;
205
206   t_net net;
207   t_event event;
208   t_display display;
209   t_input input;
210
211   unsigned char data;
212   int i;
213
214   int fd;
215
216   void *allyouneed;
217
218   /* default values */
219   strcpy(logfile,LOGFILE);
220   strcpy(server_ip,"");
221   port=1025;
222
223   /* parse/check argv */
224   for(i=1;i<argc;i++) {
225     if(argv[i][0]=='-') {
226       switch(argv[i][1]) {
227         case 'h':
228           usage(argv[0]);
229           return -1;
230         case 'i':
231           strncpy(server_ip,argv[++i],16);
232           break;
233         case 'p':
234           port=atoi(argv[++i]);
235           break;
236         case 'l':
237           strncpy(logfile,argv[++i],64);
238           break;
239         default:
240           usage(argv[0]);
241           return -1;
242       }
243     }
244   }
245   if(!strcmp(server_ip,"")) {
246     usage(argv[0]);
247     return -1;
248   }
249
250   if((fd=open(logfile,O_WRONLY,O_CREAT))<0) {
251     printf("unable to open file %s\n",logfile);
252     return -1;
253   }
254
255   allyouneed=malloc(sizeof(t_net)+sizeof(t_display)+sizeof(t_input));
256   memcpy(allyouneed,&net,sizeof(t_net));
257   memcpy(allyouneed+sizeof(t_net),&display,sizeof(t_display));
258   memcpy(allyouneed+sizeof(t_net)+sizeof(t_display),sizeof(t_input));
259
260   /* input init */
261   input_init(&input,fd);
262   input.mode=CONTENT_BUFFER|LINE_BUFFERED|INPUT_ECHO;
263   input_ios_init(&input);
264
265   /* event init */
266   event_init(&event,fd);
267   event_set_timeout(&event,0,0);
268
269   /* display init */
270   display_init(&display,fd);
271
272   /* user interaction */
273   event_math(0,&event,READ,ADD);
274
275   /* connect to server */
276   network_init(&net,fd);
277   network_set_connection_info(&net,0,server_ip,port);
278   if(network_connect(&net,0)==N_E_CONNECT) {
279     printf("unable to connect to server, aborting ...\n");
280     return -1;
281   }
282   network_select(&net,0);
283
284   /* tell server: i am a client, i may work for you */
285   data=NLSOP_GUI;
286   network_send(net.connection[0].fd,&data,1);
287
288   /* wait for job */
289   event_start(&event,allyouneed,get_command,NULL);
290
291   free(allyouneed);
292   close(fd);
293
294   return 1;
295 }