aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-04-30 10:52:41 -0400
committerBen Burwell <bburwell1@gmail.com>2013-04-30 10:52:41 -0400
commit0322fefa7f5f465909ccd95c85a088d9cf83812e (patch)
tree862ac466912289d8adb4bcf58331ce7861848400
parentca9e46c937e75df43d2f80f0d957fabb07892c29 (diff)
Changes
-rw-r--r--chat_acceptor.h88
-rw-r--r--chat_request.h46
-rw-r--r--localchat.c43
-rw-r--r--status.h4
-rw-r--r--user_command.h45
5 files changed, 122 insertions, 104 deletions
diff --git a/chat_acceptor.h b/chat_acceptor.h
index dd94ef5..8a805a7 100644
--- a/chat_acceptor.h
+++ b/chat_acceptor.h
@@ -7,37 +7,43 @@
void *receive_chat_messages(void *arg) {
- char in_buf[1024];
- int length = 1024;
- int retcode;
+ char in_buf[GLOBAL_MSG_LENGTH];
+ int retcode;
+ int i;
+ char separators[4] = ":";
+ char * tok;
- retcode = recv(client_s, in_buf, length, 0);
- printf("%s \n", in_buf);
+ retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
+
+ while (strcmp(in_buf, "SESQ") != 0) {
- while (strcmp(in_buf, "SESQ")) {
- retcode = recv(client_s, in_buf, length, 0);
if (retcode > 0) {
- printf("%s \n", in_buf);
+
+ char *message;
+ message = strtok(in_buf, separators);
+ if (strcmp(message, "SESMSG") == 0) {
+ printf("\n > %s", strtok(NULL, separators));
+ }
+
}
+
+ retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
}
+ printf("Peer closed the session. \n");
+
+ close(client_s);
+ strcpy(in_chat, "N");
+ strcpy(prompt, ">");
+
+ return;
}
void *chat_acceptor(void *arg) {
- int client_s;
- char in_buf[4096];
- char out_buf[4096];
- int length = 4096;
- char command[4096];
- int retcode;
- int i;
- char * token;
- struct sockaddr_in client_addr;
-
client_s = (int) arg;
- retcode = recv(client_s, in_buf, length, 0);
+ retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
if (strcmp(in_chat, "Y") == 0) {
@@ -50,42 +56,36 @@ void *chat_acceptor(void *arg) {
// not in a chat, ask the user
- char ans[4];
printf("\n*** CHAT REQUEST *** \n");
printf("Accept [y/n]? ");
fflush(stdout);
- fgets(ans, 2, stdin);
+
+ respond_to_chat_request = 1;
+ }
- if (strcmp(ans, "y") == 0) {
- strcpy(out_buf, "SESANS:Y");
- send(client_s, out_buf, strlen(out_buf)+1, 0);
+ return;
+}
- char inp[256];
+void accept_callback_accept() {
- pthread_t recv_thread;
- pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
+ strcpy(out_buf, "SESANS:Y");
+ send(client_s, out_buf, strlen(out_buf), 0);
- while (strcmp(inp, "/q\n") != 0) {
- printf("chat> ");
- fgets(inp, 256, stdin);
+ char inp[256];
- if (strcmp(inp, "/q\n") != 0) {
- strcpy(out_buf, "SESMSG:");
- strcat(out_buf, inp);
- send(client_s, out_buf, strlen(out_buf)+1, 0);
- } else {
- strcpy(out_buf, "SESQ");
- send(client_s, out_buf, strlen(out_buf)+1, 0);
- }
+ pthread_t recv_thread;
+ pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
- }
+ strcpy(prompt, "chat>");
+ strcpy(in_chat, "Y");
- } else {
- strcpy(out_buf, "SESANS:N");
- send(client_s, out_buf, strlen(out_buf)+1, 0);
- }
- }
+ return;
+}
+
+void accept_callback_decline() {
+ strcpy(out_buf, "SESANS:N");
+ send(client_s, out_buf, strlen(out_buf)+1, 0);
close(client_s);
return;
diff --git a/chat_request.h b/chat_request.h
index 8b29886..337b822 100644
--- a/chat_request.h
+++ b/chat_request.h
@@ -21,8 +21,8 @@ void send_chat_request(char * username) {
pthread_mutex_unlock(&peer_table_lock);
struct sockaddr_in server_addr;
- char out_buf[4096];
- char in_buf[4096];
+ char out_buf[GLOBAL_MSG_LENGTH];
+ char in_buf[GLOBAL_MSG_LENGTH];
int retcode;
// create the socket
@@ -57,50 +57,12 @@ void send_chat_request(char * username) {
// set my in_chat flag
strcpy(in_chat, "Y");
- // keep looping until no more input
- int keep_going = 1;
-
- while (keep_going) {
-
- char inp[256];
- printf("chat> ");
- fgets(inp, 140, stdin);
-
- if (strcmp(inp, "/q\n") == 0) {
-
- // send quit message to peer
- strcpy(out_buf, "SESQ");
- send(client_s, out_buf, sizeof(out_buf), 0);
- close(client_s);
-
- // exit the loop
- keep_going = 0;
-
- // print message to user
- printf("Session closed. \n");
-
- // set my in_chat flag
- strcpy(in_chat, "N");
-
- } else {
-
- // send the message
- strcpy(out_buf, "SESMSG:");
- strcat(out_buf, inp);
- send(client_s, out_buf, sizeof(out_buf), 0);
-
- // testing...
- printf("%s \n", out_buf);
-
- }
-
- }
+ // set the user prompt
+ strcpy(prompt, "chat>");
} else {
printf("Session declined. \n");
}
- close(client_s);
-
return;
} \ No newline at end of file
diff --git a/localchat.c b/localchat.c
index 60c41d2..8c1c57a 100644
--- a/localchat.c
+++ b/localchat.c
@@ -20,26 +20,37 @@
#include <netdb.h>
// define constants
-#define DEBUG 0
-#define MAX_NUM_PEERS 100
-#define CMD_PORT 6060
-#define CHAT_PORT 6061
-#define BROADCAST_IP "192.168.130.255"
+#define DEBUG 0
+#define MAX_NUM_PEERS 100
+#define CMD_PORT 6060
+#define CHAT_PORT 6061
+#define BROADCAST_IP "192.168.130.255"
+#define GLOBAL_MSG_LENGTH 1024
// structs
#include "peer.h"
// global variables
-struct peer peers[MAX_NUM_PEERS];
-int num_peers_in_table = 0;
-pthread_mutex_t peer_table_lock;
-
-char my_ip[64];
-char in_chat[4] = "N";
-const char * username;
-
-char command[256];
-int client_s;
+struct peer peers[MAX_NUM_PEERS];
+int num_peers_in_table = 0;
+pthread_mutex_t peer_table_lock;
+
+char my_ip[64];
+char in_chat[4] = "N";
+const char * username;
+
+char command[256];
+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 respond_to_chat_request = 0;
// include functions
#include "clean_table.h"
@@ -93,7 +104,7 @@ int main(int argc, char const *argv[]) {
// enter the user input loop
while (1) {
- printf("> ");
+ printf("%s ", prompt);
fgets(command, 256, stdin);
process_user_command();
}
diff --git a/status.h b/status.h
index 8c3df4b..7df363a 100644
--- a/status.h
+++ b/status.h
@@ -14,10 +14,10 @@ void status_broadcast_once() {
int client_sock; // Client socket descriptor
struct sockaddr_in server_addr; // Server Internet address
struct in_addr server_ip_addr; // Server IP Address
- int addr_leng; // Internet address length
+ int addr_leng; // Internet address sizeof(in_buf)
int retrncode; // Return code
int sOptVal; // Socket option value
- int sOptLen; // Socket option length
+ int sOptLen; // Socket option sizeof(in_buf)
char out_buf[4096]; // Output buffer for data
char in_buf[4096]; // Input buffer for data
char out_temp[4096];
diff --git a/user_command.h b/user_command.h
index 1c554e6..1dac4a9 100644
--- a/user_command.h
+++ b/user_command.h
@@ -9,6 +9,51 @@ void process_user_command() {
int i;
+ if (respond_to_chat_request) {
+
+ // reset the global variable
+ respond_to_chat_request = 0;
+
+ if (strcmp(command, "y\n") == 0) {
+ accept_callback_accept();
+ } else {
+ accept_callback_decline();
+ }
+
+ return;
+ }
+
+ if (strcmp(in_chat, "Y") == 0) {
+
+ // in chat, send input to the peer
+
+ if (strcmp(command, "/q\n") == 0) {
+
+ // send the quit message
+ strcpy(out_buf, "SESQ");
+ send(client_s, out_buf, strlen(out_buf)+1, 0);
+
+ // close the socket
+ close(client_s);
+
+ // set my flag
+ strcpy(in_chat, "N");
+
+ // reset prompt
+ strcpy(prompt, ">");
+
+ } else {
+
+ strcpy(out_buf, "SESMSG:");
+ strcat(out_buf, command);
+ send(client_s, out_buf, strlen(out_buf)+1, 0);
+
+ }
+
+ return;
+
+ }
+
if (strcmp(command, "quit\n") == 0) {
exit(0);
} else if (strcmp(command, "who\n") == 0) {