#include "event.h"
-int event_init(t_ivac *ivac) {
+int event_init(t_event *event) {
- puts("[ivac] event: initializing event system ...");
+ puts("[event] initializing event system ...");
- ivac->event.maxfd=0;
+ event->maxfd=0;
- FD_ZERO(ivac->event.rfds);
- FD_ZERO(ivac->event.rfds_c);
- FD_ZERO(ivac->event.wfds);
- FD_ZERO(ivac->event.wfds_c);
+ FD_ZERO(&(event->rfds));
+ FD_ZERO(&(event->rfds_o));
+ FD_ZERO(&(event->wfds));
+ FD_ZERO(&(event->wfds_o));
- return SUCCESS;
+ return E_SUCCESS;
+}
+
+int event_math(int fd,t_event *event,char rw,char ar) {
+
+ if(ar==ADD) {
+ if(rw==READ) FD_SET(fd,&(event->rfds_o));
+ else FD_SET(fd,&(event->wfds_o));
+ if(fd>event->maxfd) event->maxfd=fd;
+ } else {
+ if(rw==READ) FD_CLR(fd,&(event->rfds_o));
+ else FD_CLR(fd,&(event->wfds_o));
+ if(fd==event->maxfd) event->maxfd-=1;
+ }
+
+ printf("[event] %c fd %d to %c file descriptor set\n",ar==ADD?'+':'-',fd,
+ rw==READ?'r':'w');
+
+ return E_SUCCESS;
+}
+
+int event_start(t_event *event,void *ptr,
+ int (*callback0)(t_event *event,void *ptr),
+ int (*callback1)(t_event *event,void *ptr)) {
+
+ int s_ret;
+
+ /* switch on event system */
+ event->status=ENABLED;
+
+ while(event->status&ENABLED) {
+
+ event->rfds=event->rfds_o;
+ event->wfds=event->wfds_o;
+
+ if((s_ret=select(event->maxfd+1,&(event->rfds),&(event->wfds),NULL,
+ &(event->timeout)))==-1) {
+ perror("[event] select call");
+ return E_ERROR;
+ }
+
+ if(s_ret) {
+ /* 1+ fd(s) available for reading/writing */
+ if(callback0(event,ptr)==CALLBACK_ERROR) {
+ puts("[event] callback0 returned error");
+ return E_ERROR;
+ }
+ }
+
+ /* usual stuff to be executed */
+ if(callback1(event,ptr)==CALLBACK_ERROR) {
+ puts("[event] callback returned error");
+ return E_ERROR;
+ }
+
+ }
+
+ return E_SUCCESS;
+}
+
+int event_stop(t_event *event) {
+
+ event->status=DISABLED;
+
+ return E_SUCCESS;
}
#define EVENT_H
/* includes */
-#include "ivac.h"
+#include <stdio.h>
#include <sys/select.h>
+/* defines */
+
+/* return values */
+#define CALLBACK_ERROR -1
+#define E_ERROR -1
+#define E_SUCCESS 1
+
+/* event status */
+#define DISABLED 0
+#define ENABLED 1
+
+/* read write mode */
+#define READ 'r'
+#define WRITE 'w'
+/* add remove mode */
+#define ADD 'a'
+#define REMOVE 'r'
+
/* event specific variables */
-typedef s_event {
+typedef struct s_event {
int maxfd;
- fd_set rfds,rfds_c;
- fd_set wfds,wfds_c;
+ fd_set rfds,rfds_o;
+ fd_set wfds,wfds_o;
+ struct timeval timeout;
+ unsigned char status;
} t_event;
+/* function prototypes */
+int event_init(t_event *event);
+int event_math(int fd,t_event *event,char rw,char ar);
+int event_start(t_event *event,void *ptr,
+ int (*callback0)(t_event *event,void *ptr),
+ int (*callback1)(t_event *event,void *ptr));
+int event_stop(t_event *event);
+
#endif
int main(int argc,char **argv) {
+ /* TESTING BY NOW */
-
- return 1;
+ t_ivac ivac;
+
+ /* set username (futur: read from config or entered later) */
+ strcpy(ivac.username,"hackbard");
+
+ /* set event timeout */
+ ivac.event.timeout.tv_sec=IVAC_S_SEC;
+ ivac.event.timeout.tv_usec=IVAC_S_USEC;
+
+ /* set listen port (futur: read from config or entered later) */
+ network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
+
+ /* event init */
+ event_init(&(ivac.event));
+
+ /* input init */
+ input_init(&(ivac.input));
+
+ /* network init */
+ network_init(&(ivac.net));
+
+ /* add listening port + stdin to (read) event system */
+ event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
+ event_math(0,&(ivac.event),READ,ADD);
+
+ /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
+ event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
+
+ return SUCCESS;
+}
+
+int ivac_event_cb(t_event *event,void *ptr) {
+
+ t_ivac *ivac;
+ int channel;
+
+ ivac=(t_ivac *)ptr;
+
+ if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
+ /* manage incoming + send info */
+ channel=network_manage_incoming(&(ivac->net));
+ event_math(ivac->net.connection[channel].fd,event,READ,ADD);
+ send_info(channel,&(ivac->net),ivac->username);
+ }
+
+ /* receive info */
+ for(channel=0;channel<MAX_CONNECTIONS;channel++)
+ if(ivac->net.connection[channel].status&C_ESTABL)
+ if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
+ receive_info(channel,&(ivac->net));
+
+ /* user interaction */
+ if(FD_ISSET(0,&(event->rfds))) input_get_char(ivac);
+
+ return SUCCESS;
+}
+
+int ivac_regular_cb(t_event *event,void *ptr) {
+
+ /* usual jobs like audio & video transmit ... */
+
+ return SUCCESS;
}
#define _GNU_SOURCE
#include <stdio.h>
+#include "network.h"
+#include "event.h"
+#include "input.h"
+
/* defines */
#define CHAR_USERNAME 32
#define ERROR -1
#define SUCCESS 1
+#define IVAC_LISTEN_PORT 1025
+
+#define IVAC_S_SEC 2
+#define IVAC_S_USEC 500
+
/* variables */
-typedef s_ivac {
+typedef struct s_ivac {
char username[CHAR_USERNAME];
t_net net;
t_event event;
+ t_input input;
} t_ivac;
+/* function prototypes */
+int ivac_event_cb(t_event *event,void *ptr);
+int ivac_regular_cb(t_event *event,void *ptr);
+
#endif