more display + 1st network connection tries
[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 // #define USE_NCURSES
25
26 #include "ivac.h"
27
28 int main(int argc,char **argv) {
29
30   /* TESTING BY NOW */
31
32   t_ivac ivac;
33   int i;
34
35   if(argc!=2) {
36     printf("\nusage: %s <username>\n\n",argv[0]);
37     return ERROR;
38   }
39
40   /* set username */
41   strncpy(ivac.username,argv[1],CHAR_USERNAME);
42   /* clear challenger struct */
43   for(i=0;i<MAX_CONNECTIONS;i++)
44     memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
45   /* zero console buffer */
46   for(i=0;i<IVAC_CONSOLE_LEN;i++)
47     memset(ivac.console[i],0,IVAC_CONSOLE_STRING_LEN);
48
49   /* set capabilities (futur: set by check routines) */
50   ivac.g_cap=NETWORK;
51   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
52
53   /* set event timeout */
54   ivac.event.timeout.tv_sec=IVAC_S_SEC;
55   ivac.event.timeout.tv_usec=IVAC_S_USEC;
56   /* event init */
57   event_init(&(ivac.event));
58
59   /* input init */
60   ivac.input.mode=CONTENT_BUFFER;
61   input_init(&(ivac.input));
62
63   /* set listen port (futur: read from config or entered later) */
64   network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
65   /* network init */
66   if(network_init(&(ivac.net))==N_ERROR) {
67     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
68            ivac.net.l_port);
69     ivac_shutdown(&ivac);
70     return ERROR;
71   }
72
73   /* add listening port + stdin to (read) event system */
74   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
75   event_math(0,&(ivac.event),READ,ADD);
76
77   /* display init */
78   // display_init(&(ivac.display));
79
80   /* display */
81   ivac_display(&(ivac.display));
82
83   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
84   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
85
86   // ivac_shutdown(&ivac);
87
88   return SUCCESS;
89 }
90
91 int ivac_shutdown(t_ivac *ivac) {
92
93   network_shutdown(&(ivac->net));
94   input_shutdown(&(ivac->input));
95   event_stop(&(ivac->event));
96   // display_shutdown(&(ivac->display));
97
98   return SUCCESS;
99 }
100
101 int ivac_send_info(int channel,t_ivac *ivac) {
102
103   char data[SEND_N_MAX];
104   int size;
105
106   size=strlen(ivac->username);
107
108   data[0]=IVAC_SEND_NAME;
109   data[1]=size;
110   strncpy(data+2,ivac->username,size);
111   size+=2;
112
113   data[size]=IVAC_SEND_G_CAP;
114   data[size+1]=1;
115   data[size+2]=ivac->g_cap;
116   size+=3;
117
118   data[size]=IVAC_SEND_AV_CAP;
119   data[size+1]=2;
120   data[size+2]=(ivac->av_cap)>>8;
121   data[size+3]=(ivac->av_cap)&0xff;
122   size+=4;
123
124   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
125     puts("[ivac] ivac_send_info failed");
126     return ERROR;
127   }
128
129   return SUCCESS;
130 }
131
132 int ivac_receive_info(int channel,t_ivac *ivac) {
133
134   char data[SEND_N_MAX];
135   int count,length;
136
137   count=0;
138
139   if((length=network_receive(ivac->net.connection[channel].fd,
140                              data,SEND_N_MAX))==N_ERROR) {
141     puts("[ivac] ivac_receive_info failed");
142     return ERROR;
143   }
144
145   while(length-count) {
146     switch(data[count]) {
147       case IVAC_SEND_NAME:
148         strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
149         ivac->challenger[channel].name[data[count+1]]='\0';
150         count+=(data[count+1]+2);
151         break;
152       case IVAC_SEND_G_CAP:
153         ivac->challenger[channel].g_cap=data[count+2];
154         count+=3;
155         break;
156       case IVAC_SEND_AV_CAP:
157         ivac->challenger[channel].av_cap=data[count+2]<<8;
158         ivac->challenger[channel].av_cap|=data[count+3];
159         count+=4;
160         break;
161       default:
162         puts("[ivac] ivac_receive_info, unknown character");
163         return ERROR;
164         break;
165     }
166   }
167
168   return SUCCESS;
169 }
170
171 int ivac_event_cb(t_event *event,void *ptr) {
172  
173   t_ivac *ivac;
174   int channel;
175
176   ivac=(t_ivac *)ptr;
177
178   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
179     /* manage incoming + send info */
180     if((channel=network_manage_incoming(&(ivac->net)))!=N_ERROR) {
181       event_math(ivac->net.connection[channel].fd,event,READ,ADD);
182       ivac_send_info(channel,ivac);
183     }
184   }
185
186   /* receive info */
187   for(channel=0;channel<MAX_CONNECTIONS;channel++)
188     if(ivac->net.connection[channel].status&C_ESTABL)
189       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
190         ivac_receive_info(channel,ivac);
191
192   /* user interaction */
193   if(FD_ISSET(0,&(event->rfds)))
194     input_get_event(&(ivac->input),ivac_parse_command,ivac);
195
196   /* display ivac gui */
197   // ivac_display_content(ivac);
198     
199   return SUCCESS;
200 }
201
202 int ivac_regular_cb(t_event *event,void *ptr) {
203
204   /* usual jobs like audio & video transmit ... */
205
206   return SUCCESS;
207 }
208
209 int ivac_parse_command(t_input *input,void *ptr) {
210
211   t_ivac *ivac;
212   int i,j,k;
213   int len;
214   int channel;
215   char *data,valid;
216   char c_str[IVAC_CONSOLE_STRING_LEN];
217   char arg[IVAC_ARG_COUNT][IVAC_ARG_LEN];
218
219   ivac=(t_ivac *)ptr;
220   data=input->content;
221   valid=0;
222
223   /* refresh prompt content only! */
224   ivac_display_prompt_content(ivac);
225
226   /* parse command routines */
227   if(data[input->c_count-1]=='\n') {
228
229     /* delete console string + args */
230     memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
231     for(j=0;j<IVAC_ARG_COUNT;j++) memset(arg[j],0,IVAC_ARG_LEN);
232
233     /* get args */
234     len=0;
235     while(data[len]!='\n') len++;
236     i=0; j=0;
237     while((i<len) && (j<IVAC_ARG_COUNT)) {
238       k=0;
239       while((data[i+k]!=' ')&&(data[i+k]!='\n')) {
240         arg[j][k]=data[i+k];
241         k++;
242       }
243       arg[j][k]='\0';
244       j++;
245       /* skip all ' ' */
246       while(data[i+k]==' ') k++;
247       i+=k;
248     }
249
250     /* parse command  aka arg[0] */
251     if(!(strncmp(arg[0],"quit",4))) {
252       valid=1;
253       sprintf(c_str,"ivac shutdown ...");
254       ivac_shutdown(ivac);
255     }
256     if(!(strncmp(arg[0],"set",3))) {
257       valid=1;
258       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
259       else channel=-1;
260       if(!(strncmp(arg[1],"name",4))) {
261         strncpy(ivac->username,arg[2],CHAR_USERNAME);
262         sprintf(c_str,"changed username to %s",ivac->username);
263       }
264       else if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
265         if(ivac->net.connection[channel].status&C_IN_USE) {
266           sprintf(c_str,"channel %02d: connection in use",channel);
267         }
268         else {
269           strncpy(ivac->net.connection[channel].ip,arg[2],IP_DIGITS);
270           ivac->net.connection[channel].port=atoi(arg[3]);
271           sprintf(c_str,"channel %02d: set connection info",channel);
272           ivac->net.connection[channel].status|=C_INFO_A;
273         }
274       } else snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown argument: '%s'",
275                       arg[1]);
276     }
277     if(!(strncmp(arg[0],"connect",7))) {
278       valid=1;
279       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
280       else channel=-1;
281       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
282         if(!(ivac->net.connection[channel].status&C_INFO_A))
283           sprintf(c_str,"channel %02d: channel not configured",channel);
284         else {
285           if(ivac->net.connection[channel].status&C_IN_USE)
286             sprintf(c_str,"channel %02d: connection in use",channel);
287           else {
288             sprintf(c_str,"channel %02d: trying to connect to %s:%d",channel,
289                     ivac->net.connection[channel].ip,
290                     ivac->net.connection[channel].port);
291             ivac->net.connection[channel].status|=C_IN_USE;
292           }
293         }
294       }
295       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
296     } 
297     if(!(strncmp(arg[0],"close",5))) {
298       valid=1;
299       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
300       else channel=-1;
301       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
302         if(!(ivac->net.connection[channel].status&C_ESTABL))
303           sprintf(c_str,"channel %02d: no active connection",channel);
304         else {
305           ivac->net.connection[channel].status|=C_HANGUP;
306           sprintf(c_str,"channel %02d: connection closed",channel);
307         }
308       }
309       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
310     }
311     if(!(strncmp(arg[0],"select",6))) {
312       valid=1;
313       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
314       else channel=-1;
315       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
316         ivac->net.sendmask|=(1<<channel);
317         sprintf(c_str,"selected channel %d",channel);
318       }
319       else if(arg[1][0]='*') {
320         ivac->net.sendmask=0xff;
321         strcpy(c_str,"selected all channels");
322       }
323       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
324     }
325     if(!(strncmp(arg[0],"deselect",8))) {
326       valid=1;
327       if((arg[1][0]>='0')&&(arg[1][0]<='9')) channel=atoi(arg[1]);
328       else channel=-1;
329       if((channel>=0)&&(channel<MAX_CONNECTIONS)) {
330         ivac->net.sendmask&=(~(1<<channel));
331         sprintf(c_str,"deselected channel %d",channel);
332       }
333       else if(arg[1][0]='*') {
334         ivac->net.sendmask=0;
335         strcpy(c_str,"deselected all channels");
336       }
337       else sprintf(c_str,"invalid argument: '%s'",arg[1]);
338     }
339
340     if(!valid)
341       snprintf(c_str,IVAC_CONSOLE_STRING_LEN,"unknown command: '%s'",arg[0]);
342
343     /* call network functions */
344     network_manage_connection(&(ivac->net));
345
346     /* add console string to console buffer */
347     ivac_add_to_monitor(ivac,c_str);
348
349     /* refresh whole display content */
350     ivac_display_content(ivac);
351
352     /* delete content buffer + reset counter */
353     memset(input->content,0,input->c_count-1);
354     input->c_count=0;
355
356   }
357
358   return SUCCESS;
359 }
360
361 int ivac_display_head(t_display *display) {
362
363 #ifdef USE_NCURSES
364   int x,y;
365
366   move(0,0);
367   for(x=0;x<display->max_x;x++) addch('#');
368   mvaddstr(1,0,"##");
369   mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
370   mvaddstr(1,(display->max_x-2),"##");
371   move(2,0);
372   for(x=0;x<display->max_x;x++) addch('#');
373   refresh();
374 #else
375   puts("#########################################################");
376   puts("##### ivac - -  Copyright (C) 2004 Frank Zirkelbach #####");
377   puts("#########################################################");
378 #endif
379
380   return SUCCESS;
381 }
382
383 int ivac_display_box(t_display *display) {
384  
385 #ifdef USE_NCURSES 
386   int x,y;
387
388   for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
389     mvaddch(y,0,'#');
390     mvaddch(y,display->max_x-1,'#');
391   }
392 #endif
393
394   return SUCCESS;
395 }
396
397 int ivac_display_box_content(t_ivac *ivac) {
398
399   int channel;
400
401   /* prepare challenger names */
402   for(channel=0;channel<MAX_CONNECTIONS;channel++)
403     if(ivac->challenger[channel].name[0]==0)
404       strcpy(ivac->challenger[channel].name,"<empty>");
405
406 #ifdef USE_NCURSES
407 #else
408   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
409     if(ivac->net.connection[channel].status&C_INFO_A)
410       printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel,
411              ivac->net.connection[channel].ip,
412              ivac->net.connection[channel].port,
413              ivac->net.connection[channel].status,
414              ivac->challenger[channel].name);
415   }
416 #endif
417
418   return SUCCESS;
419 }
420
421 int ivac_display_console(t_display *display) {
422
423 #ifdef USE_NCURSES                                             
424   int x,y;
425          
426   move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
427   for(x=0;x<display->max_x;x++) addch('#');
428 #endif
429                                           
430   return SUCCESS;
431 }
432
433 int ivac_display_console_content(t_ivac *ivac) {
434
435 #ifdef USE_NCURSES
436   int x,y;
437   int len;
438
439   for(y=0;y<IVAC_CONSOLE_LEN;y++) {
440     len=strlen(ivac->console[y]);
441     move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
442     for(x=0;x<len;x++)
443        addch(((ivac->console[y][x]>' ')||(ivac->console[y][x]<='~'))
444                ?ivac->console[y][x]:' ');
445     for(x=len;x<IVAC_CONSOLE_STRING_LEN-4;x++) addch(' ');
446   }
447   refresh();
448 #else
449   int i;
450
451   for(i=0;i<IVAC_CONSOLE_LEN;i++)
452     printf("[ivac] console line %d: %s\n",i,ivac->console[i]);
453 #endif
454
455   return SUCCESS;
456 }
457
458 int ivac_display_prompt(t_display *display) {
459
460 #ifdef USE_NCURSES
461   int x,y;
462
463   move(display->max_y-3,0);
464   for(x=0;x<display->max_x;x++) addch('#');
465   mvaddstr(display->max_y-2,0,"## command: ");
466   mvaddstr(display->max_y-2,display->max_x-2,"##");
467   move(display->max_y-1,0);
468   for(x=0;x<display->max_x;x++) addch('#');
469   refresh();
470 #endif
471
472   return SUCCESS;
473 }
474
475 int ivac_display_prompt_content(t_ivac *ivac) {
476
477 #ifdef USE_NCURSES
478   int x,y;
479
480   /* delete old command */
481   if(ivac->input.c_count==0) {
482     move(ivac->display.max_y-2,12);
483     for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
484   }
485
486   for(x=0;x<ivac->input.c_count;x++)
487     mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
488   refresh();
489 #else
490   printf("%c",ivac->input.content[ivac->input.c_count-1]);
491   fflush(NULL);
492 #endif
493
494   return SUCCESS;
495 }
496
497 int ivac_display(t_display *display) {
498
499   int x,y;
500
501   /* display head */
502   ivac_display_head(display);
503
504   /* display box */
505   ivac_display_box(display);
506
507   /* display console */
508   ivac_display_console(display);
509
510   /* display command prompt */
511   ivac_display_prompt(display);
512
513   return SUCCESS;
514 }
515
516 int ivac_display_content(t_ivac *ivac) {
517
518   /* display box content */
519   ivac_display_box_content(ivac);
520
521   /* display console content */
522   ivac_display_console_content(ivac);
523
524   /* display prompt content */
525   ivac_display_prompt_content(ivac);
526
527   return SUCCESS;
528 }
529
530 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
531
532   int i;
533
534   for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
535     memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
536   memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
537
538   return SUCCESS;
539 }