}
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;
}
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);
/* includes */
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#define MAX_CONTENT 128
+#define LINE_BUFFERED (1<<0)
+#define CONTENT_BUFFER (1<<1)
+
/* input specific variables */
typedef struct s_input {
char *content;
/* 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
/* 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;
event_init(&(ivac.event));
/* input init */
+ ivac.input.mode=CONTENT_BUFFER;
input_init(&(ivac.input));
/* network init */
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);
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;
/* 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;
}
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;
}
#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
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;
-}
#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];
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