aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-05-01 12:20:30 -0400
committerBen Burwell <bburwell1@gmail.com>2013-05-01 12:20:30 -0400
commit79230604a20409ab12c2cb9620df81078ff7fae2 (patch)
tree23fa78c1ccb4bae1a7d67102c84b62d369a7e4be
parent0322fefa7f5f465909ccd95c85a088d9cf83812e (diff)
Better?
-rw-r--r--chat_acceptor.h26
-rw-r--r--chat_request.h22
-rw-r--r--localchat.c16
-rw-r--r--user_command.h1
4 files changed, 48 insertions, 17 deletions
diff --git a/chat_acceptor.h b/chat_acceptor.h
index 8a805a7..674d920 100644
--- a/chat_acceptor.h
+++ b/chat_acceptor.h
@@ -22,15 +22,15 @@ 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));
}
}
-
retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
}
- printf("Peer closed the session. \n");
+ printf("Peer closed the session. >\r\n > ");
+ printf(" > ");
close(client_s);
strcpy(in_chat, "N");
@@ -41,16 +41,16 @@ void *receive_chat_messages(void *arg) {
void *chat_acceptor(void *arg) {
- client_s = (int) arg;
+ accepted_client = (int)arg;
- retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
+ retcode = recv(accepted_client, accept_in, sizeof(accept_in), 0);
if (strcmp(in_chat, "Y") == 0) {
// if already in a chat, reply no and close
- strcpy(out_buf, "SESANS:N");
- send(client_s, out_buf, strlen(out_buf)+1, 0);
- close(client_s);
+ strcpy(accept_out, "SESANS:N");
+ send(accepted_client, accept_out, strlen(accept_out)+1, 0);
+ close(accepted_client);
} else {
@@ -68,17 +68,19 @@ void *chat_acceptor(void *arg) {
void accept_callback_accept() {
+ client_s = accepted_client;
+
strcpy(out_buf, "SESANS:Y");
- send(client_s, out_buf, strlen(out_buf), 0);
+ send(client_s, out_buf, strlen(out_buf)+1, 0);
char inp[256];
- pthread_t recv_thread;
- pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
-
strcpy(prompt, "chat>");
strcpy(in_chat, "Y");
+ pthread_t recv_thread;
+ pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
+
return;
}
diff --git a/chat_request.h b/chat_request.h
index 337b822..d57d7e1 100644
--- a/chat_request.h
+++ b/chat_request.h
@@ -5,10 +5,12 @@
* Send chat request
*/
-void send_chat_request(char * username) {
+
+void *send_chat_request(void * arg) {
int i;
char ip[16];
+ char * username = (char *) arg;
pthread_mutex_lock(&peer_table_lock);
@@ -49,6 +51,8 @@ void send_chat_request(char * username) {
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) {
@@ -57,6 +61,9 @@ void send_chat_request(char * username) {
// set my in_chat flag
strcpy(in_chat, "Y");
+ pthread_t recv_thread;
+ pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
+
// set the user prompt
strcpy(prompt, "chat>");
@@ -65,4 +72,17 @@ void send_chat_request(char * username) {
}
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 8c1c57a..4c9d671 100644
--- a/localchat.c
+++ b/localchat.c
@@ -22,8 +22,8 @@
// define constants
#define DEBUG 0
#define MAX_NUM_PEERS 100
-#define CMD_PORT 6060
-#define CHAT_PORT 6061
+#define CMD_PORT 6062
+#define CHAT_PORT 6063
#define BROADCAST_IP "192.168.130.255"
#define GLOBAL_MSG_LENGTH 1024
@@ -50,8 +50,16 @@ int i;
char * token;
struct sockaddr_in client_addr;
+
+int accepted_client;
+char accept_out[16];
+char accept_in[128];
+
int respond_to_chat_request = 0;
+pthread_t requester_t;
+char user_name_request[32];
+
// include functions
#include "clean_table.h"
#include "online.h"
@@ -61,8 +69,8 @@ int respond_to_chat_request = 0;
#include "receive.h"
#include "check_user_name.h"
#include "loading.h"
-#include "chat_request.h"
#include "chat_acceptor.h"
+#include "chat_request.h"
#include "user_command.h"
int main(int argc, char const *argv[]) {
@@ -104,7 +112,7 @@ int main(int argc, char const *argv[]) {
// enter the user input loop
while (1) {
- printf("%s ", prompt);
+ printf("\n %s ", prompt);
fgets(command, 256, stdin);
process_user_command();
}
diff --git a/user_command.h b/user_command.h
index 1dac4a9..0854a70 100644
--- a/user_command.h
+++ b/user_command.h
@@ -41,6 +41,7 @@ void process_user_command() {
// reset prompt
strcpy(prompt, ">");
+ printf("\n > ");
} else {