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
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
00052
00053 sprintf(cabec_or, "%d-CHAT>", CHAT_PORT);
00054 sprintf(cabec_dest, ":%d->", CHAT_PORT);
00055
00056 chat_init();
00057
00058
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
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
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
00106 DEBUG_SOCK_INFO("recebendo dados.\n");
00107 strcpy(s->inbuff,"\n");
00108 uart_puts(cabec_dest);
00109 PSOCK_READTO(&prot, '\n');
00110
00111 dest = strtok(s->inbuff, "\n");
00112 uart_puts(dest);
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
00125 if(s->state == SEND_CHAT){
00126 uart_puts(cabec_or);
00127
00128 UARTTakeString(s->buff, UART_CH_0);
00129
00130 if(!strcmp(s->buff,"quit")){
00131
00132 global_chat_flag = 0;
00133 s->state = QUIT_CHAT;
00134 }
00135 else {
00136 DEBUG_SOCK_INFO("enviando dados\n");
00137 PSOCK_SEND_STR(&prot, s->buff);
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.");
00146 global_chat_flag = 0;
00147 DEBUG_SOCK_INFO("socket finalizada.\n");
00148 PSOCK_CLOSE(&prot);
00149 PSOCK_END(&prot);
00150 PSOCK_CLOSE_EXIT(&prot);
00151 s->state = 10;
00152 fflush(stdout);
00153 return 0;
00154 }
00155
00156 return 0;
00157 }
00158
00159
00160