clean shutdown in case address is in use + include ncurses functions (todo)
[my-code/ivac.git] / src / ivac.c
index 610ac1c..70d1a35 100644 (file)
 
 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 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 */
+  ivac_display(&(ivac));
+
+  /* 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));
+
+  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(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;
+
+  ivac=(t_ivac *)ptr;
+
+  /* parse command routines */
+
+  if(input->content[input->c_count-1]=='\n') {
+    /* delete content buffer + reset counter */
+    memset(input->content,0,input->c_count);
+    input->c_count=0;
+  }
+
+  return SUCCESS;
+}
+
+int ivac_display_head(void) {
+
+  /* 23 x 80 */
+  int column,line;
+
+  for(column=0;column<COLUMN;column++) printf("#");
+  printf("\n");
+
+  printf("##");
+  for(column=2;column<(COLUMN-8)/2;column++) printf(" ");
+  printf("- ivac -");
+  for(column=2;column<(COLUMN-8)/2;column++) printf(" ");
+  printf("##\n");
+
+  for(column=0;column<COLUMN;column++) printf("#");
+  printf("\n");
+
+  return SUCCESS;
+}
+
+int ivac_display_prompt(t_ivac *ivac) {
+
+  int column,line;
+
+  for(column=0;column<COLUMN;column++) printf("#");
+  printf("\n");
+  printf("## command: ");
+  for(column=12;column<12+ivac->input.c_count;column++)
+    printf("%c",ivac->input.content[column-12]);
+  for(column=12+ivac->input.c_count;column<COLUMN-2;column++) printf(" ");
+  printf("##");
+  printf("\n");
+  for(column=0;column<COLUMN;column++) printf("#");
+
+  return SUCCESS;
+}
+
+int ivac_display(t_ivac *ivac) {
+
+  int line,column;
+
+  /* display head */
+  ivac_display_head();
+
+  /* build content of middle part + display */
+  for(line=3;line<LINE-3;line++) {
+    printf("#");
+    for(column=1;column<COLUMN-1;column++) printf(" ");
+    printf("#\n");
+  }
+
+  /* display command prompt */
+  ivac_display_prompt(ivac);
+
+  return SUCCESS;
 }