From 1087e40cfcf5e46a28c84203973374dd20268887 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sat, 25 Mar 2023 17:39:52 +0100 Subject: [PATCH] sending json format in send_to_all --- include/chat/misc.h | 2 +- src/chat/main/client.c | 2 +- src/chat/misc.c | 60 ++++++++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/include/chat/misc.h b/include/chat/misc.h index 61466ab..ae5db8f 100644 --- a/include/chat/misc.h +++ b/include/chat/misc.h @@ -19,7 +19,7 @@ along with this program. If not, see . #ifndef WHY2_CHAT_MISC_H #define WHY2_CHAT_MISC_H -void why2_send_socket(char *text, int socket); //send socket.... wtf did you expect +void why2_send_socket(char *text, char *username, int socket); //send socket.... wtf did you expect char *why2_read_socket(int socket); //read lol void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS diff --git a/src/chat/main/client.c b/src/chat/main/client.c index 6edf75d..b05547d 100644 --- a/src/chat/main/client.c +++ b/src/chat/main/client.c @@ -67,7 +67,7 @@ int main(void) getline(&line, &line_length, stdin); printf(WHY2_CLEAR_AND_GO_UP); - why2_send_socket(line, listen_socket); + why2_send_socket(line, "anon", listen_socket); if (strcmp(line, "!exit\n") == 0) //USER REQUESTED PROGRAM EXIT { diff --git a/src/chat/misc.c b/src/chat/misc.c index 89a0374..833c557 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -115,19 +115,47 @@ node_t *get_node(int connection) return buffer; } -void *send_to_all(void *message) + + +char *get_string_from_json(struct json_object *json, char *string) +{ + struct json_object *object; + json_object_object_get_ex(json, string, &object); + + return (char*) json_object_get_string(object); +} + +char *get_string_from_json_string(char *json, char *string) +{ + struct json_object *json_obj = json_tokener_parse(json); + char *returning = get_string_from_json(json_obj, string); + + //DEALLOCATION + json_object_put(json_obj); + + //GET STRINGS + return returning; +} + +void *send_to_all(void *json) { if (head == NULL) return NULL; node_t *node_buffer = head; + //PARSE + struct json_object *json_obj = json_tokener_parse((char*) json); + do //SEND TO ALL CONNECTIONS { - why2_send_socket((char*) message, node_buffer -> connection); //SEND TO CLIENT + why2_send_socket(get_string_from_json(json_obj, "message"), get_string_from_json(json_obj, "username"), node_buffer -> connection); //SEND TO CLIENT node_buffer = node_buffer -> next; } while (node_buffer -> next != NULL); + //DEALLOCATION + json_object_put(json_obj); + return NULL; } @@ -153,26 +181,6 @@ void add_brackets(char **json) *json = output; } -char *get_string_from_json(struct json_object *json, char *string) -{ - struct json_object *object; - json_object_object_get_ex(json, string, &object); - - return (char*) json_object_get_string(object); -} - -char *get_string_from_json_string(char *json, char *string) -{ - struct json_object *json_obj = json_tokener_parse(json); - char *returning = get_string_from_json(json_obj, string); - - //DEALLOCATION - json_object_put(json_obj); - - //GET STRINGS - return returning; -} - char *read_socket_raw(int socket) { if (socket == -1) @@ -227,7 +235,7 @@ char *read_socket_from_raw(char *raw) } //GLOBAL -void why2_send_socket(char *text, int socket) +void why2_send_socket(char *text, char *username, int socket) { char *output = why2_strdup(""); size_t length_buffer = strlen(text); @@ -239,7 +247,7 @@ void why2_send_socket(char *text, int socket) //ADD OBJECTS json_object_object_add(json, "message", json_object_new_string(text_copy)); - json_object_object_add(json, "username", json_object_new_string("anon")); + json_object_object_add(json, "username", json_object_new_string(username)); //GENERATE JSON STRING json_object_object_foreach(json, key, value) @@ -305,7 +313,7 @@ void *why2_communicate_thread(void *arg) printf("Received:\n%s\n\n", received); - pthread_create(&thread_buffer, NULL, send_to_all, received); //TODO: Send only json format, not json format "squared" + pthread_create(&thread_buffer, NULL, send_to_all, raw); pthread_join(thread_buffer, NULL); why2_deallocate(received); @@ -336,6 +344,8 @@ char *why2_read_socket(int socket) //ALLOCATE final_message final_message = why2_calloc(strlen(message) + strlen(username) + 3, sizeof(char)); + printf("\n\nGIGANIGGA: %s\n\n", raw_socket); + //BUILD final_message sprintf(final_message, "%s: %s", username, message);