libgnunetchat

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

commit 5abb9dc0d5a65ab193a0febfc4ce5a3f4692d6ff
parent 9b2d42f4c528539a3b1ad4effdd457d8df9f47bc
Author: Jacki <jacki@thejackimonster.de>
Date:   Tue,  4 Feb 2025 05:18:45 +0100

Add shutdown hook to ping tool

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

Diffstat:
Mtools/gnunet_messenger_ping.c | 41+++++++++++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/tools/gnunet_messenger_ping.c b/tools/gnunet_messenger_ping.c @@ -37,6 +37,7 @@ struct GNUNET_MESSENGER_PingTool const struct GNUNET_CONFIGURATION_Handle *cfg; struct GNUNET_IDENTITY_EgoLookup *lookup; struct GNUNET_MESSENGER_Handle *handle; + struct GNUNET_SCHEDULER_Task *hook; struct GNUNET_SCHEDULER_Task *task; struct GNUNET_CONTAINER_MultiHashMap *map; @@ -51,6 +52,7 @@ struct GNUNET_MESSENGER_PingTool int require_pong; bool quit; + size_t counter; }; struct GNUNET_MESSENGER_Ping @@ -96,6 +98,12 @@ idle (void *cls) tool->task = NULL; tool->quit = true; + if (tool->hook) + { + GNUNET_SCHEDULER_cancel (tool->hook); + tool->hook = NULL; + } + if (tool->handle) GNUNET_MESSENGER_disconnect(tool->handle); @@ -103,6 +111,23 @@ idle (void *cls) GNUNET_IDENTITY_ego_lookup_cancel(tool->lookup); } +static void +shutdown_hook (void *cls) +{ + struct GNUNET_MESSENGER_PingTool *tool = cls; + + tool->hook = NULL; + tool->quit = true; + + if (tool->task) + { + GNUNET_SCHEDULER_cancel(tool->task); + tool->task = NULL; + } + + idle(cls); +} + static enum GNUNET_GenericReturnValue member_callback (void *cls, struct GNUNET_MESSENGER_Room *room, @@ -114,7 +139,7 @@ member_callback (void *cls, return GNUNET_YES; GNUNET_CONTAINER_multishortmap_put(ping->pong_map, hash_contact (contact), NULL, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); return GNUNET_YES; } @@ -153,6 +178,7 @@ send_ping (struct GNUNET_MESSENGER_PingTool *tool, message.body.text.text = NULL; GNUNET_MESSENGER_send_message(room, &message, NULL); + tool->counter++; if (tool->count) tool->count--; @@ -179,6 +205,7 @@ send_pong (struct GNUNET_MESSENGER_PingTool *tool, ((float) difference.rel_value_us) / GNUNET_TIME_relative_get_millisecond_().rel_value_us); GNUNET_MESSENGER_send_message(room, &message, NULL); + tool->counter++; if (tool->count) tool->count--; @@ -261,7 +288,7 @@ finish_ping (struct GNUNET_MESSENGER_PingTool *tool, ping->traffic, recipients, loss_rate, ((float) delta.rel_value_us) / GNUNET_TIME_relative_get_millisecond_().rel_value_us); if (recipients > 0) - printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n", + printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n\n", ((float) min.rel_value_us) / GNUNET_TIME_relative_get_millisecond_().rel_value_us, ((float) avg.rel_value_us) / GNUNET_TIME_relative_get_millisecond_().rel_value_us, ((float) max.rel_value_us) / GNUNET_TIME_relative_get_millisecond_().rel_value_us, @@ -270,11 +297,7 @@ finish_ping (struct GNUNET_MESSENGER_PingTool *tool, if (!(tool->quit)) { if (tool->count) - { - printf("\n"); - send_ping(tool, room); - } else tool->quit = true; } @@ -370,6 +393,9 @@ message_callback (void *cls, ((GNUNET_MESSENGER_KIND_TAG != message->header.kind) || (0 != GNUNET_CRYPTO_hash_cmp(&(message->body.tag.hash), &key)))) continue; + + if (!sender) + continue; if (NULL != GNUNET_CONTAINER_multishortmap_get(ping->pong_map, hash_contact (sender))) continue; @@ -499,6 +525,7 @@ run (void *cls, struct GNUNET_MESSENGER_PingTool *tool = cls; tool->cfg = cfg; + tool->hook = GNUNET_SCHEDULER_add_shutdown(shutdown_hook, tool); if (!(tool->ego_name)) { @@ -621,6 +648,8 @@ main (int argc, &tool ); + printf("--- %lu iteration%s done ---\n", tool.counter, tool.counter == 1? "" : "s"); + GNUNET_CONTAINER_multihashmap_iterate(tool.ping_map, free_map_ping, NULL); GNUNET_CONTAINER_multihashmap_iterate(tool.map, free_map_hashes, NULL);