cli_webSock.c

Go to the documentation of this file.
00001 
00021 #include "cli_webSock.h"
00022 #include "../uart/uart.h"
00023 #include <string.h>
00024 #include <stdio.h>
00025 #include "uip.h"
00026 #include "psock.h"
00027 //#include "uip-conf.h"
00028 
00029 char cabec_or[20];      
00030 char cabec_dest[20];    
00035 void chat_end(struct chat_state *s);
00036 
00040 void chat_init(){
00041         uip_listen(HTONS(CHAT_PORT));
00042 }
00043 
00050 void chat_start(){
00051         //conv com a UART. main do chat.
00052 
00053         sprintf(cabec_or, "%d-CHAT>", CHAT_PORT);
00054         sprintf(cabec_dest, ":%d->", CHAT_PORT);
00055 
00056         chat_init();
00057         //uart_puts("\nBem Vindo ao chat.\n");
00058         //uart_puts(cabec_or);
00059 
00060 }
00061 
00067 void chat_appcall(void){
00068         char * dest;
00069         int tambuff;
00070 
00071         struct chat_state *s = (struct chat_state *)&(uip_conn->appstate.chat_st);
00072 
00073 
00074         //quando uma nova conexão é estabelecida.
00075         if(uip_connected()){
00076                 DEBUG_SOCK_INFO("try connection\n");
00077                 PSOCK_INIT(&prot, s->inbuff, sizeof(s->inbuff));
00078                 DEBUG_SOCK_INFO("connection .. ok");
00079                 s->state = SEND_CHAT;
00080         }
00081 
00082 //      DEBUG_SOCK_INFO("Iniciando comunicacao.\n");
00083         chat_communication(s);
00084 
00085 }
00086 
00087 
00096 int chat_communication(struct chat_state *s){
00097         char *dest;
00098         char estado[20];
00099         int a;
00100         sprintf(estado,"Estado: %d\n", s->state);
00101         DEBUG_INFO(estado);
00102 
00103         PSOCK_BEGIN(&prot);
00104         if(s->state==RECV_CHAT){
00105 //                      PSOCK_WAIT_UNTIL(&prot,PSOCK_NEWDATA(&prot));//{        //verifica se tem dados armazenado.
00106                                 DEBUG_SOCK_INFO("recebendo dados.\n");
00107                                 strcpy(s->inbuff,"\n");
00108                                 uart_puts(cabec_dest);  //monta cabecalho para origem (placa) escrever.
00109                                 PSOCK_READTO(&prot, '\n');//fica bloqueado esperando informação até que venha o caracter \n
00110 //                              strncpy(dest, s->inbuff, strlen(s->inbuff));
00111                                 dest = strtok(s->inbuff, "\n");
00112                                 uart_puts(dest);        //envia para a UART os dados que vieram.
00113                                 uart_putc('\n');
00114 
00115                                 if(!strcmp(dest,"quit")){
00116                                         s->state = QUIT_CHAT;
00117                                 }
00118                                 else {
00119                                         s->state = SEND_CHAT;
00120                                         memset(s->inbuff,'\0',strlen(s->inbuff));
00121                                 }
00122         //              }
00123         }
00124         //envia dados por sockets;
00125         if(s->state == SEND_CHAT){
00126                 uart_puts(cabec_or);    //monta cabecalho para origem (placa) escrever.
00127                 //do{
00128                         UARTTakeString(s->buff, UART_CH_0);     //pega dados da uart
00129                 //}while(strlen(s->buff)<2);//consistencia simples.
00130                 if(!strcmp(s->buff,"quit")){
00131                         //PSOCK_SEND_STR(&prot, "bye!!!");      //envia para a socket.
00132                         global_chat_flag = 0;   //verifica se é para sair do chat
00133                         s->state = QUIT_CHAT;
00134                 }
00135                 else {
00136                         DEBUG_SOCK_INFO("enviando dados\n");
00137                         PSOCK_SEND_STR(&prot, s->buff); //envia para a socket.
00138                         DEBUG_SOCK_INFO("dados enviados\n");
00139                         s->state = RECV_CHAT;
00140                 }
00141         }
00142 
00143         if(s->state == QUIT_CHAT){
00144                 uart_puts("bye.\n");
00145                 PSOCK_SEND_STR(&prot, "bye.");  //envia para a socket.
00146                 global_chat_flag = 0;   //verifica se é para sair do chat
00147                 DEBUG_SOCK_INFO("socket finalizada.\n");
00148                 PSOCK_CLOSE(&prot);
00149                 PSOCK_END(&prot);
00150                 PSOCK_CLOSE_EXIT(&prot);
00151                 s->state = 10;//estado nao tratado.
00152                 fflush(stdout);
00153                 return 0;
00154         }
00155 
00156         return 0;
00157 }
00158 
00159 
00160