From 0b8a02cc156081ad9a087ea91952a5cfc4270475 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 5 Jul 2016 11:51:32 +0000 Subject: [PATCH] update tests to use new MQ API --- src/util/test_client.c | 2 +- src/util/test_server.c | 130 ++++++++++++++++------------------------ src/util/test_service.c | 90 +++++++++++++--------------- src/util/test_socks.c | 2 +- 4 files changed, 96 insertions(+), 128 deletions(-) diff --git a/src/util/test_client.c b/src/util/test_client.c index 97fe10110..89adf41a3 100644 --- a/src/util/test_client.c +++ b/src/util/test_client.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2009 GNUnet e.V. + Copyright (C) 2009, 2016 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published diff --git a/src/util/test_server.c b/src/util/test_server.c index 189c13e18..f82ecb2b1 100644 --- a/src/util/test_server.c +++ b/src/util/test_server.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2009, 2010, 2014 GNUnet e.V. + Copyright (C) 2009, 2010, 2014, 2016 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -52,7 +52,7 @@ static struct GNUNET_SERVER_Handle *server; /** * Handle for the client. */ -static struct GNUNET_CLIENT_Connection *cc; +static struct GNUNET_MQ_Handle *mq; /** * Handle of the server for the client. @@ -81,7 +81,7 @@ finish_up (void *cls) GNUNET_assert (7 == ok); ok = 0; GNUNET_SERVER_destroy (server); - GNUNET_CLIENT_disconnect (cc); + GNUNET_MQ_destroy (mq); GNUNET_CONFIGURATION_destroy (cfg); } @@ -105,32 +105,6 @@ recv_fin_cb (void *cls, } -/** - * The client connected to the server and is now allowed - * to send a second message. We send one. - * - * @param cls NULL - * @param size number of bytes that can be transmitted - * @param buf where to copy the message - * @return number of bytes copied to @a buf - */ -static size_t -transmit_second_message (void *cls, - size_t size, - void *buf) -{ - struct GNUNET_MessageHeader msg; - - GNUNET_assert (5 == ok); - ok = 6; - GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); - msg.type = htons (MY_TYPE2); - msg.size = htons (sizeof (struct GNUNET_MessageHeader)); - memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader)); - return sizeof (struct GNUNET_MessageHeader); -} - - /** * We have received the reply from the server, check that we are at * the right stage and queue the next message to the server. Cleans @@ -140,18 +114,18 @@ transmit_second_message (void *cls, * @param msg message we got from the server */ static void -first_reply_handler (void *cls, - const struct GNUNET_MessageHeader *msg) +handle_reply (void *cls, + const struct GNUNET_MessageHeader *msg) { + struct GNUNET_MQ_Envelope *env; + struct GNUNET_MessageHeader *m; + GNUNET_assert (4 == ok); - ok = 5; - GNUNET_assert (NULL != - GNUNET_CLIENT_notify_transmit_ready (cc, - sizeof (struct GNUNET_MessageHeader), - TIMEOUT, - GNUNET_YES, - &transmit_second_message, - NULL)); + ok = 6; + env = GNUNET_MQ_msg (m, + MY_TYPE2); + GNUNET_MQ_send (mq, + env); } @@ -210,40 +184,12 @@ recv_cb (void *cls, GNUNET_assert (NULL != GNUNET_SERVER_notify_transmit_ready (client, ntohs (message->size), - TIMEOUT, &reply_msg, + TIMEOUT, + &reply_msg, NULL)); } -/** - * The client connected to the server and is now allowed - * to send a first message. We transmit a simple message, - * ask for a second transmission and get ready to receive - * a response. - * - * @param cls NULL - * @param size number of bytes that can be transmitted - * @param buf where to copy the message - * @return number of bytes copied to @a buf - */ -static size_t -transmit_initial_message (void *cls, - size_t size, - void *buf) -{ - struct GNUNET_MessageHeader msg; - - GNUNET_assert (1 == ok); - ok = 2; - GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); - msg.type = htons (MY_TYPE); - msg.size = htons (sizeof (struct GNUNET_MessageHeader)); - memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader)); - GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT); - return sizeof (struct GNUNET_MessageHeader); -} - - /** * Message handlers for the server. */ @@ -254,6 +200,22 @@ static struct GNUNET_SERVER_MessageHandler handlers[] = { }; +/** + * Generic error handler, called with the appropriate error code and + * the same closure specified at the creation of the message queue. + * Not every message queue implementation supports an error handler. + * + * @param cls closure with the `struct GNUNET_STATISTICS_Handle *` + * @param error error code + */ +static void +mq_error_handler (void *cls, + enum GNUNET_MQ_Error error) +{ + GNUNET_assert (0); /* should never happen */ +} + + /** * First task run by the scheduler. Initializes the server and * a client and asks for a transmission from the client to the @@ -267,6 +229,16 @@ task (void *cls) struct sockaddr_in sa; struct sockaddr *sap[2]; socklen_t slens[2]; + struct GNUNET_MQ_Envelope *env; + struct GNUNET_MessageHeader *msg; + GNUNET_MQ_hd_fixed_size (reply, + MY_TYPE, + struct GNUNET_MessageHeader); + struct GNUNET_MQ_MessageHandler chandlers[] = { + make_reply_handler (cls), + GNUNET_MQ_handler_end () + }; + sap[0] = (struct sockaddr *) &sa; slens[0] = sizeof (sa); @@ -296,15 +268,17 @@ task (void *cls) "resolver", "HOSTNAME", "localhost"); - cc = GNUNET_CLIENT_connect ("test-server", cfg); - GNUNET_assert (cc != NULL); - GNUNET_assert (NULL != - GNUNET_CLIENT_notify_transmit_ready (cc, - sizeof (struct - GNUNET_MessageHeader), - TIMEOUT, GNUNET_YES, - &transmit_initial_message, - NULL)); + mq = GNUNET_CLIENT_connecT (cfg, + "test-server", + chandlers, + &mq_error_handler, + NULL); + GNUNET_assert (NULL != mq); + ok = 2; + env = GNUNET_MQ_msg (msg, + MY_TYPE); + GNUNET_MQ_send (mq, + env); } diff --git a/src/util/test_service.c b/src/util/test_service.c index f340b724d..707f8658b 100644 --- a/src/util/test_service.c +++ b/src/util/test_service.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2009, 2013 GNUnet e.V. + Copyright (C) 2009, 2013, 2016 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -37,16 +37,16 @@ static struct GNUNET_SERVICE_Context *sctx; static int ok = 1; -static struct GNUNET_CLIENT_Connection *client; +static struct GNUNET_MQ_Handle *mq; static void do_stop (void *cls) { - if (NULL != client) + if (NULL != mq) { - GNUNET_CLIENT_disconnect (client); - client = NULL; + GNUNET_MQ_destroy (mq); + mq = NULL; } if (NULL != sctx) { @@ -60,49 +60,33 @@ do_stop (void *cls) } -static size_t -build_msg (void *cls, size_t size, void *buf) -{ - struct GNUNET_MessageHeader *msg = buf; - - if (size < sizeof (struct GNUNET_MessageHeader)) - { - /* timeout */ - GNUNET_break (0); - GNUNET_SCHEDULER_add_now (&do_stop, NULL); - ok = 1; - return 0; - } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n"); - GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); - msg->type = htons (MY_TYPE); - msg->size = htons (sizeof (struct GNUNET_MessageHeader)); - return sizeof (struct GNUNET_MessageHeader); -} - - static void ready (void *cls, int result) { const struct GNUNET_CONFIGURATION_Handle *cfg = cls; + struct GNUNET_MQ_Envelope *env; + struct GNUNET_MessageHeader *msg; GNUNET_assert (GNUNET_YES == result); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n"); - client = GNUNET_CLIENT_connect ("test_service", cfg); - GNUNET_assert (client != NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client connecting, waiting to transmit\n"); - GNUNET_CLIENT_notify_transmit_ready (client, - sizeof (struct GNUNET_MessageHeader), - GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, - &build_msg, NULL); + "Service confirmed running\n"); + mq = GNUNET_CLIENT_connecT (cfg, + "test_service", + NULL, + NULL, + NULL); + GNUNET_assert (NULL != mq); + env = GNUNET_MQ_msg (msg, + MY_TYPE); + GNUNET_MQ_send (mq, + env); } static void -recv_cb (void *cls, struct GNUNET_SERVER_Client *sc, +recv_cb (void *cls, + struct GNUNET_SERVER_Client *sc, const struct GNUNET_MessageHeader *message) { GNUNET_assert (NULL != message); @@ -120,11 +104,14 @@ static struct GNUNET_SERVER_MessageHandler myhandlers[] = { static void -runner (void *cls, struct GNUNET_SERVER_Handle *server, +runner (void *cls, + struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *cfg) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n"); - GNUNET_SERVER_add_handlers (server, myhandlers); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Service initializing\n"); + GNUNET_SERVER_add_handlers (server, + myhandlers); GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS, &ready, (void *) cfg); } @@ -158,21 +145,28 @@ ready6 (void *cls, int result) { const struct GNUNET_CONFIGURATION_Handle *cfg = cls; + struct GNUNET_MQ_Envelope *env; + struct GNUNET_MessageHeader *msg; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n"); GNUNET_assert (GNUNET_YES == result); - client = GNUNET_CLIENT_connect ("test_service6", cfg); - GNUNET_assert (client != NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n"); - GNUNET_CLIENT_notify_transmit_ready (client, - sizeof (struct GNUNET_MessageHeader), - GNUNET_TIME_UNIT_SECONDS, GNUNET_NO, - &build_msg, NULL); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "V6 ready\n"); + mq = GNUNET_CLIENT_connecT (cfg, + "test_service6", + NULL, + NULL, + NULL); + GNUNET_assert (NULL != mq); + env = GNUNET_MQ_msg (msg, + MY_TYPE); + GNUNET_MQ_send (mq, + env); } static void -runner6 (void *cls, struct GNUNET_SERVER_Handle *server, +runner6 (void *cls, + struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *cfg) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, diff --git a/src/util/test_socks.c b/src/util/test_socks.c index d63cce571..4b86d25dc 100644 --- a/src/util/test_socks.c +++ b/src/util/test_socks.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2015 GNUnet e.V. + Copyright (C) 2015, 2016 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published -- 2.25.1