added server connection message

This commit is contained in:
Václav Šmejkal 2023-04-20 09:28:39 +02:00
parent 460239b8bc
commit 2d095daef2
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59

View File

@ -65,10 +65,8 @@ char *get_string_from_json_string(char *json, char *string)
void *send_to_all(void *json) void *send_to_all(void *json)
{ {
why2_node_t *head = connection_list.head; why2_node_t _first_node = (why2_node_t) { NULL, connection_list.head };
if (head == NULL) return NULL; why2_node_t *node_buffer = &_first_node;
why2_node_t *node_buffer = head;
connection_node_t connection_buffer; connection_node_t connection_buffer;
//PARSE //PARSE
@ -78,11 +76,11 @@ void *send_to_all(void *json)
while (node_buffer -> next != NULL) //SEND TO ALL CONNECTIONS while (node_buffer -> next != NULL) //SEND TO ALL CONNECTIONS
{ {
node_buffer = node_buffer -> next;
connection_buffer = *(connection_node_t*) node_buffer -> value; connection_buffer = *(connection_node_t*) node_buffer -> value;
why2_send_socket(get_string_from_json(json_obj, "message"), get_string_from_json(json_obj, "username"), connection_buffer.connection); //SEND TO CLIENT why2_send_socket(get_string_from_json(json_obj, "message"), get_string_from_json(json_obj, "username"), connection_buffer.connection); //SEND TO CLIENT
node_buffer = node_buffer -> next;
} }
//DEALLOCATION //DEALLOCATION
@ -289,8 +287,6 @@ void *why2_communicate_thread(void *arg)
{ {
int connection = *(int*) arg; int connection = *(int*) arg;
printf("User connected.\t%d\n", connection);
connection_node_t node = (connection_node_t) connection_node_t node = (connection_node_t)
{ {
connection, connection,
@ -301,12 +297,29 @@ void *why2_communicate_thread(void *arg)
void *buffer; void *buffer;
char *received = NULL; char *received = NULL;
char *raw = NULL; char *raw = why2_strdup("");
void *raw_ptr = NULL; void *raw_ptr = NULL;
char *decoded_buffer; char *decoded_buffer;
pthread_t thread_buffer; pthread_t thread_buffer;
pthread_t thread_deletion_buffer; pthread_t thread_deletion_buffer;
why2_bool exiting = 0; why2_bool exiting = 0;
struct json_object *json = json_tokener_parse("{}");
//SEND CONNECTION MESSAGE
json_object_object_add(json, "message", json_object_new_string("anon connected"));
json_object_object_add(json, "username", json_object_new_string("server")); //TODO: Usernames
json_object_object_foreach(json, key, value) //GENERATE JSON STRING
{
append(&raw, key, (char*) json_object_get_string(value));
}
add_brackets(&raw);
pthread_create(&thread_buffer, NULL, send_to_all, raw); //SEND
pthread_join(thread_buffer, NULL);
why2_deallocate(raw);
raw = NULL;
while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT]
{ {