From d096b7ac4a2435339f7a7e3de68c51efc07422a0 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 1 May 2013 15:39:02 -0400 Subject: It's beautiful. But actually. Oh my god. --- chat_acceptor.h | 38 +++++++++++++++++++++++--------------- chat_request.h | 25 +++++-------------------- localchat.c | 16 ++++------------ user_command.h | 8 ++++---- 4 files changed, 36 insertions(+), 51 deletions(-) diff --git a/chat_acceptor.h b/chat_acceptor.h index 674d920..816e6c3 100644 --- a/chat_acceptor.h +++ b/chat_acceptor.h @@ -22,34 +22,41 @@ void *receive_chat_messages(void *arg) { char *message; message = strtok(in_buf, separators); if (strcmp(message, "SESMSG") == 0) { - printf("\n > %s", strtok(NULL, separators)); + printf("\n > %s", strtok(NULL, separators)); + printf("%s ", prompt); + fflush(stdout); } } retcode = recv(client_s, in_buf, sizeof(in_buf), 0); } - printf("Peer closed the session. >\r\n > "); - printf(" > "); + printf("\n*** Peer closed the session. *** \n"); close(client_s); strcpy(in_chat, "N"); strcpy(prompt, ">"); + printf("%s ", prompt); + fflush(stdout); + return; } void *chat_acceptor(void *arg) { - accepted_client = (int)arg; + accepted_client = (int) arg; + int retcode; + char buf_i[GLOBAL_MSG_LENGTH]; + char buf_o[GLOBAL_MSG_LENGTH]; - retcode = recv(accepted_client, accept_in, sizeof(accept_in), 0); + retcode = recv(accepted_client, buf_i, sizeof(buf_i), 0); if (strcmp(in_chat, "Y") == 0) { // if already in a chat, reply no and close - strcpy(accept_out, "SESANS:N"); - send(accepted_client, accept_out, strlen(accept_out)+1, 0); + strcpy(buf_o, "SESANS:N"); + send(accepted_client, buf_o, strlen(buf_o)+1, 0); close(accepted_client); } else { @@ -86,9 +93,11 @@ void accept_callback_accept() { void accept_callback_decline() { - strcpy(out_buf, "SESANS:N"); - send(client_s, out_buf, strlen(out_buf)+1, 0); - close(client_s); + char buf[GLOBAL_MSG_LENGTH]; + + strcpy(buf, "SESANS:N"); + send(accepted_client, buf, strlen(buf)+1, 0); + close(accepted_client); return; } @@ -96,7 +105,6 @@ void accept_callback_decline() { void *server_loop(void *arg) { int server_s; struct sockaddr_in server_addr; - int client_s; struct sockaddr_in client_addr; struct in_addr client_ip_addr; socklen_t addr_len; @@ -117,16 +125,16 @@ void *server_loop(void *arg) { listen(server_s, 100); while (1) { - addr_len = sizeof(client_addr); + addr_len = sizeof(accepted_addr); - client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len); + accepted_client = accept(server_s, (struct sockaddr *)&accepted_addr, &addr_len); - if (client_s == -1) { + if (accepted_client == -1) { printf("Network error, quitting... \n"); exit(-1); } - pthread_create(&thread_id, NULL, chat_acceptor, (void *)client_s); + pthread_create(&thread_id, NULL, chat_acceptor, (void *)accepted_client); } return; diff --git a/chat_request.h b/chat_request.h index d57d7e1..6b4db03 100644 --- a/chat_request.h +++ b/chat_request.h @@ -6,11 +6,10 @@ */ -void *send_chat_request(void * arg) { +void send_chat_request(char * username) { int i; char ip[16]; - char * username = (char *) arg; pthread_mutex_lock(&peer_table_lock); @@ -22,10 +21,10 @@ void *send_chat_request(void * arg) { pthread_mutex_unlock(&peer_table_lock); - struct sockaddr_in server_addr; - char out_buf[GLOBAL_MSG_LENGTH]; - char in_buf[GLOBAL_MSG_LENGTH]; - int retcode; + struct sockaddr_in server_addr; + char out_buf[GLOBAL_MSG_LENGTH]; + char in_buf[GLOBAL_MSG_LENGTH]; + int retcode; // create the socket client_s = socket(AF_INET, SOCK_STREAM, 0); @@ -51,8 +50,6 @@ void *send_chat_request(void * arg) { strcpy(out_buf, "SESREQ"); send(client_s, out_buf, strlen(out_buf), 0); - - retcode = recv(client_s, in_buf, sizeof(in_buf), 0); if (strcmp(in_buf, "SESANS:Y") == 0) { @@ -74,15 +71,3 @@ void *send_chat_request(void * arg) { return; } -void start_chat_request_thread(char * user) { - - pthread_create(&requester_t, NULL, send_chat_request, (void *)user); - - sleep(10); - - printf("Request timed out. \n"); - - pthread_cancel(requester_t); - - return; -} \ No newline at end of file diff --git a/localchat.c b/localchat.c index 4c9d671..ddc73e1 100644 --- a/localchat.c +++ b/localchat.c @@ -22,8 +22,8 @@ // define constants #define DEBUG 0 #define MAX_NUM_PEERS 100 -#define CMD_PORT 6062 -#define CHAT_PORT 6063 +#define CMD_PORT 6064 +#define CHAT_PORT 6065 #define BROADCAST_IP "192.168.130.255" #define GLOBAL_MSG_LENGTH 1024 @@ -45,21 +45,13 @@ char prompt[16] = ">"; int client_s; char in_buf[GLOBAL_MSG_LENGTH]; char out_buf[GLOBAL_MSG_LENGTH]; -int retcode; -int i; -char * token; struct sockaddr_in client_addr; - int accepted_client; -char accept_out[16]; -char accept_in[128]; +struct sockaddr_in accepted_addr; int respond_to_chat_request = 0; -pthread_t requester_t; -char user_name_request[32]; - // include functions #include "clean_table.h" #include "online.h" @@ -112,7 +104,7 @@ int main(int argc, char const *argv[]) { // enter the user input loop while (1) { - printf("\n %s ", prompt); + printf("%s ", prompt); fgets(command, 256, stdin); process_user_command(); } diff --git a/user_command.h b/user_command.h index 0854a70..803fada 100644 --- a/user_command.h +++ b/user_command.h @@ -41,7 +41,6 @@ void process_user_command() { // reset prompt strcpy(prompt, ">"); - printf("\n > "); } else { @@ -55,9 +54,9 @@ void process_user_command() { } - if (strcmp(command, "quit\n") == 0) { + if (strcmp(command, "quit\n") == 0 || strcmp(command, "q\n") == 0) { exit(0); - } else if (strcmp(command, "who\n") == 0) { + } else if (strcmp(command, "who\n") == 0 || strcmp(command, "w\n") == 0) { // print a list of online peers pthread_mutex_lock(&peer_table_lock); @@ -74,7 +73,7 @@ void process_user_command() { pthread_mutex_unlock(&peer_table_lock); - } else if (strcmp(command, "chat\n") == 0) { + } else if (strcmp(command, "chat\n") == 0 || strcmp(command, "c\n") == 0) { char user[32]; int exists; @@ -99,6 +98,7 @@ void process_user_command() { printf("Command Summary: \n"); printf(" who: display a list of online peers \n"); printf(" chat: initiate a chat session \n"); + printf(" quit: exit the localchat program \n"); } else if (strcmp(command, "\n") == 0) { // no command, do nothing. -- cgit v1.2.3