libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit ff5b51e8b00440dcf7ee252451947ce0622fd9c1
parent 8842c8b412921fe9318d2caf0a418257b72cf31d
Author: Jacki <jacki@thejackimonster.de>
Date:   Sat, 10 Feb 2024 05:06:43 +0100

Add dependencies for messages to handle reverse order from requests

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Msrc/gnunet_chat_context.c | 70+++++++++++++++++++++++++++++++++-------------------------------------
Msrc/gnunet_chat_context.h | 1+
Msrc/gnunet_chat_handle_intern.c | 75++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 102 insertions(+), 44 deletions(-)

diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c @@ -32,39 +32,50 @@ static const unsigned int initial_map_size_of_room = 8; static const unsigned int initial_map_size_of_contact = 4; -struct GNUNET_CHAT_Context* -context_create_from_room (struct GNUNET_CHAT_Handle *handle, - struct GNUNET_MESSENGER_Room *room) +static void +init_new_context (struct GNUNET_CHAT_Context *context, + unsigned int initial_map_size) { - GNUNET_assert((handle) && (room)); - - struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); - - context->handle = handle; + GNUNET_assert(context); - context->type = GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN; context->nick[0] = '\0'; context->topic = NULL; context->deleted = GNUNET_NO; context->timestamps = GNUNET_CONTAINER_multishortmap_create( - initial_map_size_of_room, GNUNET_NO); + initial_map_size, GNUNET_NO); + context->dependencies = GNUNET_CONTAINER_multihashmap_create( + initial_map_size, GNUNET_NO); context->messages = GNUNET_CONTAINER_multihashmap_create( - initial_map_size_of_room, GNUNET_NO); + initial_map_size, GNUNET_NO); context->invites = GNUNET_CONTAINER_multihashmap_create( - initial_map_size_of_room, GNUNET_NO); + initial_map_size, GNUNET_NO); context->files = GNUNET_CONTAINER_multihashmap_create( - initial_map_size_of_room, GNUNET_NO); - - context->room = room; - context->contact = NULL; - + initial_map_size, GNUNET_NO); + context->user_pointer = NULL; context->member_pointers = GNUNET_CONTAINER_multishortmap_create( - initial_map_size_of_room, GNUNET_NO); + initial_map_size, GNUNET_NO); context->query = NULL; +} + +struct GNUNET_CHAT_Context* +context_create_from_room (struct GNUNET_CHAT_Handle *handle, + struct GNUNET_MESSENGER_Room *room) +{ + GNUNET_assert((handle) && (room)); + + struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); + + context->handle = handle; + context->type = GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN; + + init_new_context(context, initial_map_size_of_room); + + context->room = room; + context->contact = NULL; return context; } @@ -78,31 +89,13 @@ context_create_from_contact (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); context->handle = handle; - context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT; - context->nick[0] = '\0'; - context->topic = NULL; - context->deleted = GNUNET_NO; - context->timestamps = GNUNET_CONTAINER_multishortmap_create( - initial_map_size_of_contact, GNUNET_NO); - context->messages = GNUNET_CONTAINER_multihashmap_create( - initial_map_size_of_contact, GNUNET_NO); - context->invites = GNUNET_CONTAINER_multihashmap_create( - initial_map_size_of_contact, GNUNET_NO); - context->files = GNUNET_CONTAINER_multihashmap_create( - initial_map_size_of_contact, GNUNET_NO); + init_new_context(context, initial_map_size_of_contact); context->room = NULL; context->contact = contact; - context->user_pointer = NULL; - - context->member_pointers = GNUNET_CONTAINER_multishortmap_create( - initial_map_size_of_contact, GNUNET_NO); - - context->query = NULL; - return context; } @@ -112,6 +105,7 @@ context_destroy (struct GNUNET_CHAT_Context *context) GNUNET_assert( (context) && (context->timestamps) && + (context->dependencies) && (context->messages) && (context->invites) && (context->files) @@ -124,6 +118,7 @@ context_destroy (struct GNUNET_CHAT_Context *context) context->timestamps, it_destroy_context_timestamps, NULL ); + GNUNET_CONTAINER_multihashmap_clear(context->dependencies); GNUNET_CONTAINER_multihashmap_iterate( context->messages, it_destroy_context_messages, NULL ); @@ -135,6 +130,7 @@ context_destroy (struct GNUNET_CHAT_Context *context) GNUNET_CONTAINER_multishortmap_destroy(context->member_pointers); GNUNET_CONTAINER_multishortmap_destroy(context->timestamps); + GNUNET_CONTAINER_multihashmap_destroy(context->dependencies); GNUNET_CONTAINER_multihashmap_destroy(context->messages); GNUNET_CONTAINER_multihashmap_destroy(context->invites); GNUNET_CONTAINER_multihashmap_destroy(context->files); diff --git a/src/gnunet_chat_context.h b/src/gnunet_chat_context.h @@ -43,6 +43,7 @@ struct GNUNET_CHAT_Context int deleted; struct GNUNET_CONTAINER_MultiShortmap *timestamps; + struct GNUNET_CONTAINER_MultiHashMap *dependencies; struct GNUNET_CONTAINER_MultiHashMap *messages; struct GNUNET_CONTAINER_MultiHashMap *invites; struct GNUNET_CONTAINER_MultiHashMap *files; diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c @@ -36,6 +36,7 @@ #include <gnunet/gnunet_identity_service.h> #include <gnunet/gnunet_messenger_service.h> #include <gnunet/gnunet_reclaim_service.h> +#include <gnunet/gnunet_scheduler_lib.h> #include <gnunet/gnunet_util_lib.h> #include <stdio.h> #include <string.h> @@ -748,6 +749,23 @@ on_monitor_namestore_record(void *cls, } void +on_handle_message_callback(void *cls); + +static enum GNUNET_GenericReturnValue +it_context_iterate_dependencies(void *cls, + const struct GNUNET_HashCode *key, + void *value) +{ + struct GNUNET_CHAT_Message *message = (struct GNUNET_CHAT_Message*) value; + + if ((message) && (!message->task)) + message->task = GNUNET_SCHEDULER_add_now( + on_handle_message_callback, message); + + return GNUNET_YES; +} + +void on_handle_message_callback(void *cls) { struct GNUNET_CHAT_Message *message = (struct GNUNET_CHAT_Message*) cls; @@ -765,13 +783,13 @@ on_handle_message_callback(void *cls) struct GNUNET_CHAT_Handle *handle = context->handle; if (!(handle->msg_cb)) - return; + goto clear_dependencies; const struct GNUNET_MESSENGER_Contact *sender; sender = GNUNET_MESSENGER_get_sender(context->room, &(message->hash)); if (!sender) - return; + goto clear_dependencies; struct GNUNET_ShortHashCode shorthash; util_shorthash_from_member(sender, &shorthash); @@ -781,9 +799,17 @@ on_handle_message_callback(void *cls) ); if ((!contact) || (GNUNET_YES == contact->blocked)) - return; + goto clear_dependencies; handle->msg_cb(handle->msg_cls, context, message); + +clear_dependencies: + GNUNET_CONTAINER_multihashmap_get_multiple(context->dependencies, + &(message->hash), + it_context_iterate_dependencies, + NULL); + GNUNET_CONTAINER_multihashmap_remove_all(context->dependencies, + &(message->hash)); } void @@ -859,6 +885,9 @@ on_handle_message (void *cls, *time = timestamp; } + struct GNUNET_SCHEDULER_Task *task = NULL; + const struct GNUNET_HashCode *dependency = NULL; + struct GNUNET_CHAT_Message *message = GNUNET_CONTAINER_multihashmap_get( context->messages, hash ); @@ -883,7 +912,6 @@ on_handle_message (void *cls, return; } - struct GNUNET_SCHEDULER_Task* task = NULL; message = message_create_from_msg(context, hash, flags, msg); switch (msg->header.kind) @@ -1002,10 +1030,43 @@ on_handle_message (void *cls, } handle_callback: - if (!task) + switch (msg->header.kind) + { + case GNUNET_MESSENGER_KIND_DELETE: + { + dependency = &(msg->body.deletion.hash); + break; + } + case GNUNET_MESSENGER_KIND_TRANSCRIPT: + { + dependency = &(msg->body.transcript.hash); + break; + } + case GNUNET_MESSENGER_KIND_TAG: + { + dependency = &(msg->body.tag.hash); + break; + } + default: + break; + } + + if ((dependency) && + (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(context->messages, dependency))) + { + GNUNET_CONTAINER_multihashmap_put( + context->dependencies, + dependency, + message, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE + ); + + GNUNET_MESSENGER_get_message(room, dependency); + } + else if (!task) on_handle_message_callback(message); - else - message->task = task; + + message->task = task; } int