]> hackdaworld.org Git - my-code/ivac.git/commitdiff
more abstraction in network.* and input.*, first display stuff in ivac.*
authorhackbard <hackbard>
Thu, 6 May 2004 13:43:07 +0000 (13:43 +0000)
committerhackbard <hackbard>
Thu, 6 May 2004 13:43:07 +0000 (13:43 +0000)
src/input.c
src/input.h
src/ivac.c
src/ivac.h
src/network.c
src/network.h

index a2886ee0399e6cd53c9113eaeba0fa2ae8623c09..14030175d41e77ef1ec25689fb10116c260b9538 100644 (file)
@@ -18,11 +18,13 @@ int input_init(t_input *input) {
   }
   input->c_count=0;
 
-  tcgetattr(0,&tios);
-  /* switch off canonical mode */
-  tios.c_lflag&=(~ICANON);
-  tios.c_lflag&=(~ECHO);
-  tcsetattr(0,TCSANOW,&tios);
+  if(!(input->mode&LINE_BUFFERED)) {
+    tcgetattr(0,&tios);
+    /* switch off canonical mode */
+    tios.c_lflag&=(~ICANON);
+    tios.c_lflag&=(~ECHO);
+    tcsetattr(0,TCSANOW,&tios);
+  }
 
   return I_SUCCESS;
 }
@@ -43,25 +45,27 @@ int input_shutdown(t_input *input) {
   return I_SUCCESS;
 }
 
-int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr),
+int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr),
                    void *ptr) {
 
-  char data[1];
+  char data[MAX_CONTENT];
+  int count;
 
-  if(read(0,data,1)==-1) {
+  /* delete char counter if not buffered */
+  if(!(input->mode&CONTENT_BUFFER)) input->c_count=0;
+
+  if((count=read(0,data,MAX_CONTENT))==-1) {
     perror("[input] read call");
     return I_ERROR;
   }
 
-  if(input->c_count==MAX_CONTENT) {
+  if(input->c_count>=MAX_CONTENT) {
     puts("[input] max input length reached");
     return I_ERROR;
   }
 
-  input->content[input->c_count]=data[0];
-  input->c_count++;
-
-  if(data[0]=='\n') input->c_count=0;
+  strncpy(&(input->content[input->c_count]),data,count);
+  input->c_count+=count;
 
   callback(input,ptr);
 
index e31ebe2475f96b584fc91d507eb5c51ff6874290..a3d8dd4cfb7e26a46b458e37e19418feeca9b8c0 100644 (file)
@@ -6,6 +6,7 @@
 /* includes */
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <errno.h>
 #include <termios.h>
@@ -19,6 +20,9 @@
 
 #define MAX_CONTENT 128
 
+#define LINE_BUFFERED (1<<0)
+#define CONTENT_BUFFER (1<<1)
+
 /* input specific variables */
 typedef struct s_input {
   char *content;
@@ -29,7 +33,7 @@ typedef struct s_input {
 /* function prototypes */
 int input_init(t_input *input);
 int input_shutdown(t_input *input);
-int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr),
+int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr),
                    void *ptr);
 
 #endif
index f4995a791a4f5304090456064ce64cf4cd5e819f..062fc56eb08f26ef05f335f7bc199b6ee37900b5 100644 (file)
@@ -32,6 +32,10 @@ int main(int argc,char **argv) {
   /* 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;
@@ -43,6 +47,7 @@ int main(int argc,char **argv) {
   event_init(&(ivac.event));
 
   /* input init */
+  ivac.input.mode=CONTENT_BUFFER;
   input_init(&(ivac.input));
 
   /* network init */
@@ -55,6 +60,9 @@ int main(int argc,char **argv) {
   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);
 
@@ -65,6 +73,76 @@ int main(int argc,char **argv) {
   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;
@@ -76,18 +154,21 @@ int ivac_event_cb(t_event *event,void *ptr) {
     /* 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);
+    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)))
-        receive_info(channel,&(ivac->net));
+        ivac_receive_info(channel,ivac);
 
   /* user interaction */
   if(FD_ISSET(0,&(event->rfds)))
-    input_get_char(&(ivac->input),ivac_display,ivac);
+    input_get_event(&(ivac->input),ivac_parse_command,ivac);
+
+  /* display ivac gui */
+  ivac_display(ivac);
     
   return SUCCESS;
 }
@@ -99,13 +180,77 @@ int ivac_regular_cb(t_event *event,void *ptr) {
   return SUCCESS;
 }
 
-int ivac_display(t_input *input,void *ptr) {
+int ivac_parse_command(t_input *input,void *ptr) {
 
   t_ivac *ivac;
+  int channel;
 
   ivac=(t_ivac *)ptr;
 
-  if(input->content[input->c_count-1]=='q') ivac->event.status=DISABLED;
+  /* parse command routines */
 
-   return SUCCESS;
+  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;
 }
index 9cbced91134e8946f949d2d25877f149694f17cb..a4365f4ff063708cf0739f7e012d93d35d8650c0 100644 (file)
 #define IVAC_S_SEC 2
 #define IVAC_S_USEC 500
 
+#define IVAC_SEND_NAME 'n'
+#define IVAC_SEND_G_CAP 'g'
+#define IVAC_SEND_AV_CAP 'c'
+
+#define NETWORK (1<<0)
+
+#define AUDIO (1<<0)
+#define VIDEO (1<<1)
+#define DUPLEX (1<<2)
+
+#define COLUMN 80
+#define LINE 24
+
 /* variables */
+typedef struct s_challenger {
+  char name[CHAR_USERNAME];
+  unsigned char g_cap;
+  unsigned short av_cap;
+} t_challenger;
 
 typedef struct s_ivac {
   char username[CHAR_USERNAME];
+  unsigned char g_cap;
+  unsigned short av_cap;
   t_net net;
   t_event event;
   t_input input;
+  t_challenger challenger[MAX_CONNECTIONS];
 } t_ivac;
 
 /* function prototypes */
+int ivac_send_info(int channel,t_ivac *ivac);
+int ivac_receive_info(int channel,t_ivac *ivac);
 int ivac_event_cb(t_event *event,void *ptr);
 int ivac_regular_cb(t_event *event,void *ptr);
-int ivac_display(t_input *input,void *ptr);
+int ivac_parse_command(t_input *input,void *ptr);
+int ivac_display_head(void);
+int ivac_display_prompt(t_ivac *ivac);
+int ivac_display(t_ivac *ivac);
 
 #endif
index c7425efb5f55c0c89894fdbf179f2a89d676347c..3e9fe39b8176dfe10a4d0ea38478f9b0936c9105 100644 (file)
@@ -183,71 +183,3 @@ int network_receive(int fd,unsigned char *data,int datasize) {
 
   return count;
 }
-
-int send_info(int channel,t_net *net,char *name) {
-
-  char data[SEND_N_MAX];
-  int size;
-
-  size=strlen(name);
-
-  data[0]=SEND_N_NAME;
-  data[1]=size;
-  strncpy(data+2,name,size);
-  size+=2;
-
-  data[size]=SEND_N_G_CAP;
-  data[size+1]=sizeof(unsigned char);
-  data[size+1+sizeof(unsigned char)]=net->cap;
-  size+=(sizeof(unsigned char)+2);
-
-  data[size]=SEND_N_AV_CAP;
-  data[size+1]=sizeof(unsigned short);
-  data[size+1+sizeof(unsigned short)]=net->avcap;
-  size+=(sizeof(unsigned short)+2);
-
-  if(network_send(net->connection[channel].fd,data,size)==N_ERROR) {
-    puts("[network] send_info failed");
-    return N_ERROR;
-  }
-
-  return N_SUCCESS;
-}
-
-int receive_info(int channel,t_net *net) {
-
-  char data[CHAR_N_UNAME+2];
-  int count,length;
-
-  count=0;
-
-  if((length=network_receive(net->connection[channel].fd,
-                             data,SEND_N_MAX))==N_ERROR) {
-    puts("[network] receive_info failed");
-    return N_ERROR;
-  }
-
-  while(length-count) {
-    switch(data[count]) {
-      case SEND_N_NAME:
-        strncpy(net->connection[channel].name,&data[count+2],data[count+1]);
-        net->connection[channel].name[(int)data[count+2]]='\0';
-        count+=(data[count+2]+2);
-        break;
-      case SEND_N_G_CAP:
-        net->connection[channel].cap=data[count+4];
-        count+=(sizeof(unsigned char)+2);
-        break;
-      case SEND_N_AV_CAP:
-        net->connection[channel].avcap=data[count+3]<<8;
-        net->connection[channel].avcap|=data[count+4];
-        count+=(sizeof(unsigned short)+2);
-        break;
-      default:
-        puts("[network] receive_info, unknown character");
-        return N_ERROR;
-    }
-  }
-    
-  return N_SUCCESS;
-}
index 274811ebfbc9286296130b4fa8ceb882a67fa39b..31ca335148e9237a2b3eaf62980528b15ce73908 100644 (file)
 #define C_ESTABL (1<<3)
 #define C_HANGUP (1<<4)
 
-#define CHAR_N_UNAME 32
-
 #define SEND_N_MAX 128
-#define SEND_N_NAME 'n'
-#define SEND_N_G_CAP 'g'
-#define SEND_N_AV_CAP 'c'
 
 #define N_SUCCESS 1
 #define N_ERROR -1
 /* net specific variables */
 typedef struct s_connection {
   int fd;
-  char name[CHAR_N_UNAME];
   char ip[IP_DIGITS];
   in_port_t port;
   unsigned char status;
-  unsigned char cap; /* general capabilities */
-  unsigned short avcap; /* audio/video capabilities */
+  unsigned short cap; /* capabilities */
 } t_connection;
 
 typedef struct s_net {
   int l_fd; /* listen file descriptor */
   in_port_t l_port;
-  unsigned char cap;
-  unsigned short avcap;
+  unsigned short cap;
   /* limited connections by now -- replaced by list management later */
   int c_count;
   t_connection connection[MAX_CONNECTIONS];
@@ -67,7 +59,5 @@ int network_manage_connection(t_net *net);
 int network_manage_incoming(t_net *net);
 int network_send(int fd,unsigned char *data,int datasize);
 int network_receive(int fd,unsigned char *data,int datasize);
-int send_info(int channel,t_net *net,char *name);
-int receive_info(int channel,t_net *net);
 
 #endif