clean shutdown in case address is in use + include ncurses functions (todo)
[my-code/ivac.git] / src / ivac.c
1 /* ivac.c -- main ivac file
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *         frank.zirkelbach@physik.uni-augsburg.de
5  *
6  * Copyright (C) 2004 Frank Zirkelbach
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "ivac.h"
25
26 int main(int argc,char **argv) {
27
28   /* TESTING BY NOW */
29
30   t_ivac ivac;
31
32   /* set username (futur: read from config or entered later) */
33   strcpy(ivac.username,"hackbard");
34
35   /* set capabilities (futur: set by check routines) */
36   ivac.g_cap=NETWORK;
37   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
38
39   /* set event timeout */
40   ivac.event.timeout.tv_sec=IVAC_S_SEC;
41   ivac.event.timeout.tv_usec=IVAC_S_USEC;
42
43   /* set listen port (futur: read from config or entered later) */
44   network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
45
46   /* event init */
47   event_init(&(ivac.event));
48
49   /* input init */
50   ivac.input.mode=CONTENT_BUFFER;
51   input_init(&(ivac.input));
52
53   /* network init */
54   if(network_init(&(ivac.net))==N_ERROR) {
55     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
56            ivac.net.l_port);
57     ivac_shutdown(&ivac);
58     return ERROR;
59   }
60
61   /* add listening port + stdin to (read) event system */
62   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
63   event_math(0,&(ivac.event),READ,ADD);
64
65   /* display */
66   ivac_display(&(ivac));
67
68   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
69   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
70
71   ivac_shutdown(&ivac);
72
73   return SUCCESS;
74 }
75
76 int ivac_shutdown(t_ivac *ivac) {
77
78   network_shutdown(&(ivac->net));
79   input_shutdown(&(ivac->input));
80   event_stop(&(ivac->event));
81
82   return SUCCESS;
83 }
84
85 int ivac_send_info(int channel,t_ivac *ivac) {
86
87   char data[SEND_N_MAX];
88   int size;
89
90   size=strlen(ivac->username);
91
92   data[0]=IVAC_SEND_NAME;
93   data[1]=size;
94   strncpy(data+2,ivac->username,size);
95   size+=2;
96
97   data[size]=IVAC_SEND_G_CAP;
98   data[size+1]=1;
99   data[size+2]=ivac->g_cap;
100   size+=3;
101
102   data[size]=IVAC_SEND_AV_CAP;
103   data[size+1]=2;
104   data[size+2]=(ivac->av_cap)>>8;
105   data[size+3]=(ivac->av_cap)&0xff;
106   size+=4;
107
108   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
109     puts("[ivac] ivac_send_info failed");
110     return ERROR;
111   }
112
113   return SUCCESS;
114 }
115
116 int ivac_receive_info(int channel,t_ivac *ivac) {
117
118   char data[SEND_N_MAX];
119   int count,length;
120
121   count=0;
122
123   if((length=network_receive(ivac->net.connection[channel].fd,
124                              data,SEND_N_MAX))==N_ERROR) {
125     puts("[ivac] ivac_receive_info failed");
126     return ERROR;
127   }
128
129   while(length-count) {
130     switch(data[count]) {
131       case IVAC_SEND_NAME:
132         strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
133         ivac->challenger[channel].name[data[count+1]]='\0';
134         count+=(data[count+1]+2);
135         break;
136       case IVAC_SEND_G_CAP:
137         ivac->challenger[channel].g_cap=data[count+2];
138         count+=3;
139         break;
140       case IVAC_SEND_AV_CAP:
141         ivac->challenger[channel].av_cap=data[count+2]<<8;
142         ivac->challenger[channel].av_cap|=data[count+3];
143         count+=4;
144         break;
145       default:
146         puts("[ivac] ivac_receive_info, unknown character");
147         return ERROR;
148         break;
149     }
150   }
151
152   return SUCCESS;
153 }
154
155 int ivac_event_cb(t_event *event,void *ptr) {
156  
157   t_ivac *ivac;
158   int channel;
159
160   ivac=(t_ivac *)ptr;
161
162   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
163     /* manage incoming + send info */
164     channel=network_manage_incoming(&(ivac->net));
165     event_math(ivac->net.connection[channel].fd,event,READ,ADD);
166     ivac_send_info(channel,ivac);
167   }
168
169   /* receive info */
170   for(channel=0;channel<MAX_CONNECTIONS;channel++)
171     if(ivac->net.connection[channel].status&C_ESTABL)
172       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
173         ivac_receive_info(channel,ivac);
174
175   /* user interaction */
176   if(FD_ISSET(0,&(event->rfds)))
177     input_get_event(&(ivac->input),ivac_parse_command,ivac);
178
179   /* display ivac gui */
180   ivac_display(ivac);
181     
182   return SUCCESS;
183 }
184
185 int ivac_regular_cb(t_event *event,void *ptr) {
186
187   /* usual jobs like audio & video transmit ... */
188
189   return SUCCESS;
190 }
191
192 int ivac_parse_command(t_input *input,void *ptr) {
193
194   t_ivac *ivac;
195   int channel;
196
197   ivac=(t_ivac *)ptr;
198
199   /* parse command routines */
200
201   if(input->content[input->c_count-1]=='\n') {
202     /* delete content buffer + reset counter */
203     memset(input->content,0,input->c_count);
204     input->c_count=0;
205   }
206
207   return SUCCESS;
208 }
209
210 int ivac_display_head(void) {
211
212   /* 23 x 80 */
213   int column,line;
214
215   for(column=0;column<COLUMN;column++) printf("#");
216   printf("\n");
217
218   printf("##");
219   for(column=2;column<(COLUMN-8)/2;column++) printf(" ");
220   printf("- ivac -");
221   for(column=2;column<(COLUMN-8)/2;column++) printf(" ");
222   printf("##\n");
223
224   for(column=0;column<COLUMN;column++) printf("#");
225   printf("\n");
226
227   return SUCCESS;
228 }
229
230 int ivac_display_prompt(t_ivac *ivac) {
231
232   int column,line;
233
234   for(column=0;column<COLUMN;column++) printf("#");
235   printf("\n");
236   printf("## command: ");
237   for(column=12;column<12+ivac->input.c_count;column++)
238     printf("%c",ivac->input.content[column-12]);
239   for(column=12+ivac->input.c_count;column<COLUMN-2;column++) printf(" ");
240   printf("##");
241   printf("\n");
242   for(column=0;column<COLUMN;column++) printf("#");
243
244   return SUCCESS;
245 }
246
247 int ivac_display(t_ivac *ivac) {
248
249   int line,column;
250
251   /* display head */
252   ivac_display_head();
253
254   /* build content of middle part + display */
255   for(line=3;line<LINE-3;line++) {
256     printf("#");
257     for(column=1;column<COLUMN-1;column++) printf(" ");
258     printf("#\n");
259   }
260
261   /* display command prompt */
262   ivac_display_prompt(ivac);
263
264   return SUCCESS;
265 }