fixed input system (callback added), more ivac tests
[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 event timeout */
36   ivac.event.timeout.tv_sec=IVAC_S_SEC;
37   ivac.event.timeout.tv_usec=IVAC_S_USEC;
38
39   /* set listen port (futur: read from config or entered later) */
40   network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
41
42   /* event init */
43   event_init(&(ivac.event));
44
45   /* input init */
46   input_init(&(ivac.input));
47
48   /* network init */
49   if(network_init(&(ivac.net))==N_ERROR) {
50     printf("[ivac] use 'fuser -n tcp %d' to kill that process",ivac.net.l_port);
51     return ERROR;
52   }
53
54   /* add listening port + stdin to (read) event system */
55   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
56   event_math(0,&(ivac.event),READ,ADD);
57
58   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
59   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
60
61   network_shutdown(&(ivac.net));
62
63   input_shutdown(&(ivac.input));
64
65   return SUCCESS;
66 }
67
68 int ivac_event_cb(t_event *event,void *ptr) {
69  
70   t_ivac *ivac;
71   int channel;
72
73   ivac=(t_ivac *)ptr;
74
75   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
76     /* manage incoming + send info */
77     channel=network_manage_incoming(&(ivac->net));
78     event_math(ivac->net.connection[channel].fd,event,READ,ADD);
79     send_info(channel,&(ivac->net),ivac->username);
80   }
81
82   /* receive info */
83   for(channel=0;channel<MAX_CONNECTIONS;channel++)
84     if(ivac->net.connection[channel].status&C_ESTABL)
85       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
86         receive_info(channel,&(ivac->net));
87
88   /* user interaction */
89   if(FD_ISSET(0,&(event->rfds)))
90     input_get_char(&(ivac->input),ivac_display,ivac);
91     
92   return SUCCESS;
93 }
94
95 int ivac_regular_cb(t_event *event,void *ptr) {
96
97   /* usual jobs like audio & video transmit ... */
98
99   return SUCCESS;
100 }
101
102 int ivac_display(t_input *input,void *ptr) {
103
104   t_ivac *ivac;
105
106   ivac=(t_ivac *)ptr;
107
108   if(input->content[input->c_count-1]=='q') ivac->event.status=DISABLED;
109
110    return SUCCESS;
111 }