added general header file
[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   return 1;
79 }
80
81 int get_command(t_event *event,void *allineed) {
82
83   t_net *net;
84   t_display *display;
85   t_input *input;
86
87   unsigned char data[64];
88
89   net=(t_net *)allineed;
90   display=(t_display *)(allineed+sizeof(t_net));
91   input=(t_input *)(allineed+sizeof(t_net)+sizeof(t_display));
92
93   switch(input->content[0]) {
94     case GUI_INFO:
95       send_and_wait_for_answer(net,display);
96       break;
97     case GUI_ADDJOB:
98       send_job(net,input,display)
99       break;
100     case GUI_QUIT:
101       /* stop everything */
102       event_math(0,event,READ,REMOVE);
103       network_shutdown(net);
104       display_shutdown(display);
105       input_shutdown(input);
106       event_shutdown(event);
107       return 2;
108     default:
109       display_new_line(display,"unknown gui command");
110       break;
111   }
112
113   return 1;
114 }
115
116 /*
117  * main program
118  */
119
120 int main(int argc,char **argv)
121 {
122
123   char server_ip[16];
124   char logfile[64];
125   int port;
126
127   t_net net;
128   t_event event;
129   t_display display;
130   t_input input;
131
132   unsigned char data;
133   int i;
134
135   int fd;
136
137   void *allyouneed;
138
139   /* default values */
140   strcpy(logfile,LOGFILE);
141   strcpy(server_ip,"");
142   port=1025;
143
144   /* parse/check argv */
145   for(i=1;i<argc;i++) {
146     if(argv[i][0]=='-') {
147       switch(argv[i][1]) {
148         case 'h':
149           usage(argv[0]);
150           return -1;
151         case 'i':
152           strncpy(server_ip,argv[++i],16);
153           break;
154         case 'p':
155           port=atoi(argv[++i]);
156           break;
157         case 'l':
158           strncpy(logfile,argv[++i],64);
159           break;
160         default:
161           usage(argv[0]);
162           return -1;
163       }
164     }
165   }
166   if(!strcmp(server_ip,"")) {
167     usage(argv[0]);
168     return -1;
169   }
170
171   if((fd=open(logfile,O_WRONLY,O_CREAT))<0) {
172     printf("unable to open file %s\n",logfile);
173     return -1;
174   }
175
176   allyouneed=malloc(sizeof(t_net)+sizeof(t_display)+sizeof(t_input));
177   memcpy(allyouneed,&net,sizeof(t_net));
178   memcpy(allyouneed+sizeof(t_net),&display,sizeof(t_display));
179   memcpy(allyouneed+sizeof(t_net)+sizeof(t_display),sizeof(t_input));
180
181   /* input init */
182   input_init(&input,fd);
183   input.mode=CONTENT_BUFFER|LINE_BUFFERED|INPUT_ECHO;
184   input_ios_init(&input);
185
186   /* event init */
187   event_init(&event,fd);
188   event_set_timeout(&event,0,0);
189
190   /* display init */
191   display_init(&display,fd);
192
193   /* user interaction */
194   event_math(0,&event,READ,ADD);
195
196   /* connect to server */
197   network_init(&net,fd);
198   network_set_connection_info(&net,0,server_ip,port);
199   if(network_connect(&net,0)==N_E_CONNECT) {
200     printf("unable to connect to server, aborting ...\n");
201     return -1;
202   }
203   network_select(&net,0);
204
205   /* tell server: i am a client, i may work for you */
206   data=NLSOP_GUI;
207   network_send(net.connection[0].fd,&data,1);
208
209   /* wait for job */
210   event_start(&event,allyouneed,get_command,NULL);
211
212   free(allyouneed);
213   close(fd);
214
215   return 1;
216 }