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");
+ memset(&ivac.console[0][0],0,IVAC_CONSOLE_LEN*IVAC_CONSOLE_STRING_LEN);
+
+ /* set capabilities (futur: set by check routines) */
+ ivac.g_cap=NETWORK;
+ ivac.av_cap=AUDIO|VIDEO|DUPLEX;
+
+ /* 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 */
+ ivac.input.mode=CONTENT_BUFFER;
+ input_init(&(ivac.input));
+
+ /* network init */
+ if(network_init(&(ivac.net))==N_ERROR) {
+ printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
+ ivac.net.l_port);
+ ivac_shutdown(&ivac);
+ return ERROR;
+ }
+
+ /* 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);
+
+ /* display init */
+ display_init(&(ivac.display));
+
+ /* display */
+ ivac_display(&(ivac.display));
+
+ /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
+ event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
+
+ ivac_shutdown(&ivac);
+
+ return SUCCESS;
+}
+
+int ivac_shutdown(t_ivac *ivac) {
+
+ network_shutdown(&(ivac->net));
+ input_shutdown(&(ivac->input));
+ event_stop(&(ivac->event));
+ display_shutdown(&(ivac->display));
+
+ return SUCCESS;
+}
+
+int ivac_send_info(int channel,t_ivac *ivac) {
+
+ char data[SEND_N_MAX];
+ int size;
+
+ size=strlen(ivac->username);
+
+ data[0]=IVAC_SEND_NAME;
+ data[1]=size;
+ strncpy(data+2,ivac->username,size);
+ size+=2;
+
+ data[size]=IVAC_SEND_G_CAP;
+ data[size+1]=1;
+ data[size+2]=ivac->g_cap;
+ size+=3;
+
+ data[size]=IVAC_SEND_AV_CAP;
+ data[size+1]=2;
+ data[size+2]=(ivac->av_cap)>>8;
+ data[size+3]=(ivac->av_cap)&0xff;
+ size+=4;
+
+ if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
+ puts("[ivac] ivac_send_info failed");
+ return ERROR;
+ }
+
+ return SUCCESS;
+}
+
+int ivac_receive_info(int channel,t_ivac *ivac) {
+
+ char data[SEND_N_MAX];
+ int count,length;
+
+ count=0;
+
+ if((length=network_receive(ivac->net.connection[channel].fd,
+ data,SEND_N_MAX))==N_ERROR) {
+ puts("[ivac] ivac_receive_info failed");
+ return ERROR;
+ }
+
+ while(length-count) {
+ switch(data[count]) {
+ case IVAC_SEND_NAME:
+ strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
+ ivac->challenger[channel].name[data[count+1]]='\0';
+ count+=(data[count+1]+2);
+ break;
+ case IVAC_SEND_G_CAP:
+ ivac->challenger[channel].g_cap=data[count+2];
+ count+=3;
+ break;
+ case IVAC_SEND_AV_CAP:
+ ivac->challenger[channel].av_cap=data[count+2]<<8;
+ ivac->challenger[channel].av_cap|=data[count+3];
+ count+=4;
+ break;
+ default:
+ puts("[ivac] ivac_receive_info, unknown character");
+ return ERROR;
+ break;
+ }
+ }
+
+ 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);
+ ivac_send_info(channel,ivac);
+ }
+
+ /* 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)))
+ ivac_receive_info(channel,ivac);
+
+ /* user interaction */
+ if(FD_ISSET(0,&(event->rfds)))
+ input_get_event(&(ivac->input),ivac_parse_command,ivac);
+
+ /* display ivac gui */
+ ivac_display_content(ivac);
+
+ return SUCCESS;
+}
+
+int ivac_regular_cb(t_event *event,void *ptr) {
+
+ /* usual jobs like audio & video transmit ... */
+
+ return SUCCESS;
+}
+
+int ivac_parse_command(t_input *input,void *ptr) {
+
+ t_ivac *ivac;
+ int channel,i;
+ int len;
+ char *data,valid=0;
+ char c_str[IVAC_CONSOLE_STRING_LEN];
+
+ ivac=(t_ivac *)ptr;
+ data=input->content;
+ i=input->c_count-1;
+ memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
+
+ /* parse command routines */
+
+ if((data[i]=='\r') || (data[i]=='\n')) {
+
+ /* parse commands */
+ switch(data[0]) {
+ case '/':
+ len=strlen(data+1);
+ if(len==4) {
+ /* len 4 commands */
+ if(!strncmp(data+1,"quit",4)) {
+ valid=1;
+ sprintf(c_str,"ivac shutdown\n");
+ ivac_add_to_monitor(ivac,c_str);
+ ivac_shutdown(ivac);
+ }
+ }
+ break;
+ default:
+ break;
+ }
+
+ if(!valid) {
+ snprintf(c_str,IVAC_CONSOLE_STRING_LEN-1,"unknown command: '%s...'\n",
+ data);
+ ivac_add_to_monitor(ivac,c_str);
+ }
+
+ /* delete content buffer + reset counter */
+ memset(input->content,0,input->c_count);
+ input->c_count=0;
+
+ }
+
+ return SUCCESS;
+}
+
+int ivac_display_head(t_display *display) {
+
+ int x,y;
+
+ move(0,0);
+ for(x=0;x<display->max_x;x++) addch('#');
+ mvaddstr(1,0,"##");
+ mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
+ mvaddstr(1,(display->max_x-2),"##");
+ move(2,0);
+ for(x=0;x<display->max_x;x++) addch('#');
+ refresh();
+
+ return SUCCESS;
+}
+
+int ivac_display_box(t_display *display) {
+
+ int x,y;
+
+ for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
+ mvaddch(y,0,'#');
+ mvaddch(y,display->max_x-1,'#');
+ }
+
+ return SUCCESS;
+}
+
+int ivac_display_box_content(t_ivac *ivac) {
+
+
+ return SUCCESS;
+}
+
+int ivac_display_console(t_display *display) {
+
+ int x,y;
+
+ move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
+ for(x=0;x<display->max_x;x++) addch('#');
+
+ return SUCCESS;
+}
+
+int ivac_display_console_content(t_ivac *ivac) {
+
+ int x,y,len;
+ for(y=0;y<IVAC_CONSOLE_LEN;y++) {
+ len=strlen(ivac->console[y]);
+ move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
+ for(x=0;x<IVAC_CONSOLE_STRING_LEN;x++) addch(ivac->console[y][x]);
+ }
+ refresh();
+
+ return SUCCESS;
+}
+
+int ivac_display_prompt(t_display *display) {
+
+ int x,y;
+
+ move(display->max_y-3,0);
+ for(x=0;x<display->max_x;x++) addch('#');
+ mvaddstr(display->max_y-2,0,"## command: ");
+ mvaddstr(display->max_y-2,display->max_x-2,"##");
+ move(display->max_y-1,0);
+ for(x=0;x<display->max_x;x++) addch('#');
+ refresh();
+
+ return SUCCESS;
+}
+
+int ivac_display_prompt_content(t_ivac *ivac) {
+
+ int x,y;
+
+ /* delete old command */
+ if(ivac->input.c_count==0) {
+ move(ivac->display.max_y-2,12);
+ for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
+ }
+
+ for(x=0;x<ivac->input.c_count;x++)
+ mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
+ refresh();
+
+ return SUCCESS;
+}
+
+int ivac_display(t_display *display) {
+
+ int x,y;
+
+ /* display head */
+ ivac_display_head(display);
+
+ /* display box */
+ ivac_display_box(display);
+
+ /* display console */
+ ivac_display_console(display);
+
+ /* display command prompt */
+ ivac_display_prompt(display);
+
+ return SUCCESS;
+}
+
+int ivac_display_content(t_ivac *ivac) {
+
+ /* display box content */
+ ivac_display_box_content(ivac);
+
+ /* display console content */
+ ivac_display_console_content(ivac);
+
+ /* display prompt content */
+ ivac_display_prompt_content(ivac);
+
+ return SUCCESS;
+}
+
+int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
+
+ int i;
+
+ for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
+ memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
+ memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
+ ivac_display_console_content(ivac);
+
+ return SUCCESS;
}