From d75e6e4273b1bb43b5ca9f6b207c10fc824efbae Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 12 May 2011 14:01:29 +0000 Subject: [PATCH] fixes --- src/mesh/gnunet-service-mesh.c | 26 +++++++++++++++++++++ src/mesh/mesh_api_new.c | 42 +++++++++++++++++++++------------- src/mesh/test_mesh_api.c | 34 +++++++++++++++++++-------- 3 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c index d545d8dd3..f07465ae2 100644 --- a/src/mesh/gnunet-service-mesh.c +++ b/src/mesh/gnunet-service-mesh.c @@ -1296,6 +1296,27 @@ core_disconnect (void *cls, /************************ MAIN FUNCTIONS ****************************/ /******************************************************************************/ +/** + * Task run during shutdown. + * + * @param cls unused + * @param tc unused + */ +static void +shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (core_handle != NULL) + { + GNUNET_CORE_disconnect (core_handle); + core_handle = NULL; + } + if (dht_handle != NULL) + { + GNUNET_DHT_disconnect (dht_handle); + dht_handle = NULL; + } +} + /** * Process mesh requests. * @@ -1331,6 +1352,11 @@ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "******* MESH DEBUG MESSAGE 4 ********\n"); if (dht_handle == NULL) { GNUNET_break(0); } + + /* Scheduled the task to clean up when shutdown is called */ + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, + &shutdown_task, NULL); + } /** diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c index ca68279ed..989b526ae 100644 --- a/src/mesh/mesh_api_new.c +++ b/src/mesh/mesh_api_new.c @@ -76,6 +76,9 @@ struct GNUNET_MESH_Handle { */ GNUNET_MESH_TunnelEndHandler *cleaner; + + struct GNUNET_CLIENT_TransmitHandle *th; + /** * Closure for all the handlers given by the client */ @@ -127,26 +130,28 @@ struct GNUNET_MESH_TransmitHandle { * @param buf where the callee should write the message * @return number of bytes written to buf */ -size_t +static size_t send_connect_packet (void *cls, size_t size, void *buf) { - struct GNUNET_MESH_Handle *h; + struct GNUNET_MESH_Handle *h = cls; struct GNUNET_MESH_ClientConnect *msg; uint16_t *types; int ntypes; GNUNET_MESH_ApplicationType *apps; int napps; - if(0 == size || buf == NULL) { + h->th = NULL; + if (0 == size || buf == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size 0 or buffer invalid\n"); + // FIXME: disconnect, reconnect, retry! return 0; } - if(sizeof(struct GNUNET_MessageHeader) > size) { + if (sizeof(struct GNUNET_MessageHeader) > size) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size too small\n"); + // FIXME: disconnect, reconnect, retry! return 0; } msg = (struct GNUNET_MESH_ClientConnect *) buf; - h = (struct GNUNET_MESH_Handle *) cls; msg->header.type = GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT; for(ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) { @@ -219,7 +224,8 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, GNUNET_MESH_TunnelEndHandler cleaner, const struct GNUNET_MESH_MessageHandler *handlers, - const GNUNET_MESH_ApplicationType *stypes) { + const GNUNET_MESH_ApplicationType *stypes) +{ struct GNUNET_MESH_Handle *h; size_t size; @@ -247,12 +253,12 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, size += h->n_handlers * sizeof(uint16_t); size += h->n_applications * sizeof(GNUNET_MESH_ApplicationType); - GNUNET_CLIENT_notify_transmit_ready(h->mesh, - size, - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), - GNUNET_NO, - &send_connect_packet, - (void *)h); + h->th = GNUNET_CLIENT_notify_transmit_ready(h->mesh, + size, + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_YES, + &send_connect_packet, + (void *)h); return h; } @@ -263,10 +269,14 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, * * @param handle connection to mesh to disconnect */ -void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) { - - GNUNET_free(handle); - return; +void +GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) +{ + if (NULL != handle->th) + GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th); + if (NULL != handle->mesh) + GNUNET_CLIENT_disconnect (handle->mesh, GNUNET_NO); + GNUNET_free(handle); } diff --git a/src/mesh/test_mesh_api.c b/src/mesh/test_mesh_api.c index 00212eb49..5f988f555 100644 --- a/src/mesh/test_mesh_api.c +++ b/src/mesh/test_mesh_api.c @@ -7,16 +7,31 @@ static struct GNUNET_MESH_MessageHandler handlers[] = { {NULL, 0, 0} }; +static struct GNUNET_OS_Process *arm_pid; + +static struct GNUNET_MESH_Handle *mesh; + +static struct GNUNET_DHT_Handle *dht; + +static void + do_shutdown (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (NULL != mesh) + GNUNET_MESH_disconnect (mesh); + if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); + GNUNET_OS_process_close (arm_pid); +} + static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct GNUNET_OS_Process *arm_pid; - struct GNUNET_MESH_Handle *mesh; - struct GNUNET_DHT_Handle *dht; GNUNET_MESH_ApplicationType app; - char buffer[2048]; + // char buffer[2048]; arm_pid = GNUNET_OS_start_process (NULL, NULL, @@ -41,16 +56,15 @@ run (void *cls, } /* do real test work here */ - if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); - GNUNET_OS_process_close (arm_pid); - - return; + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, + &do_shutdown, + NULL); } + + int main (int argc, char *argv[]) { int ret; char *const argv2[] = {"test-mesh-api", -- 2.25.1