From 506899aa2be2b4d5dc09c1740969c28ddf43c82d Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 19 Sep 2016 18:12:49 +0000 Subject: [PATCH] converting nse to new service API --- src/include/gnunet_mq_lib.h | 4 +- src/nse/gnunet-service-nse.c | 158 +++++++++++++++++++---------------- src/util/mq.c | 88 +++++++++++-------- 3 files changed, 143 insertions(+), 107 deletions(-) diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h index ff8c9ba1b..567624985 100644 --- a/src/include/gnunet_mq_lib.h +++ b/src/include/gnunet_mq_lib.h @@ -619,12 +619,12 @@ GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq, * * @param ev message to call the notify callback for * @param cb the notify callback - * @param cls closure for the callback + * @param cb_cls closure for the callback */ void GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev, GNUNET_MQ_NotifyCallback cb, - void *cls); + void *cb_cls); /** diff --git a/src/nse/gnunet-service-nse.c b/src/nse/gnunet-service-nse.c index bf7c79fcb..511f95514 100644 --- a/src/nse/gnunet-service-nse.c +++ b/src/nse/gnunet-service-nse.c @@ -286,7 +286,7 @@ static struct GNUNET_SCHEDULER_Task *proof_task; /** * Notification context, simplifies client broadcasts. */ -static struct GNUNET_SERVER_NotificationContext *nc; +static struct GNUNET_NotificationContext *nc; /** * The next major time. @@ -313,11 +313,6 @@ static struct GNUNET_PeerIdentity my_identity; */ static uint64_t my_proof; -/** - * Handle to this serivce's server. - */ -static struct GNUNET_SERVER_Handle *srv; - /** * Initialize a message to clients with the current network @@ -328,8 +323,6 @@ static struct GNUNET_SERVER_Handle *srv; static void setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) { - unsigned int i; - unsigned int j; double mean; double sum; double std_dev; @@ -350,9 +343,10 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) sum = 0.0; sumweight = 0.0; variance = 0.0; - for (i = 0; i < estimate_count; i++) + for (unsigned int i = 0; i < estimate_count; i++) { - j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; + unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; + val = htonl (size_estimate_messages[j].matching_bits); weight = estimate_count + 1 - i; @@ -375,9 +369,10 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) variance = 0.0; mean = 0.0; - for (i = 0; i < estimate_count; i++) + for (unsigned int i = 0; i < estimate_count; i++) { - j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; + unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; + val = htonl (size_estimate_messages[j].matching_bits); sum += val; vsq += val * val; @@ -399,20 +394,22 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE); em->reserved = htonl (0); em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); - double se = mean - 0.332747; - j = GNUNET_CONTAINER_multipeermap_size (peers); - if (0 == j) - j = 1; /* Avoid log2(0); can only happen if CORE didn't report - connection to self yet */ - nsize = log2 (j); - em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, - nsize)); - em->std_deviation = GNUNET_hton_double (std_dev); - GNUNET_STATISTICS_set (stats, - "# nodes in the network (estimate)", - (uint64_t) pow (2, GNUNET_MAX (se, - nsize)), - GNUNET_NO); + { + double se = mean - 0.332747; + unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers); + if (0 == j) + j = 1; /* Avoid log2(0); can only happen if CORE didn't report + connection to self yet */ + nsize = log2 (j); + em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, + nsize)); + em->std_deviation = GNUNET_hton_double (std_dev); + GNUNET_STATISTICS_set (stats, + "# nodes in the network (estimate)", + (uint64_t) pow (2, GNUNET_MAX (se, + nsize)), + GNUNET_NO); + } } @@ -422,28 +419,28 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) * Also, we remember the client for updates upon future * estimate measurements. * - * @param cls unused - * @param client who sent the message + * @param cls client who sent the message * @param message the message received */ static void -handle_start_message (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +handle_start (void *cls, + const struct GNUNET_MessageHeader *message) { + struct GNUNET_SERVICE_Client *client = cls; + struct GNUNET_MQ_Handle *mq; struct GNUNET_NSE_ClientMessage em; + struct GNUNET_MQ_Envelope *env; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n"); - GNUNET_SERVER_notification_context_add (nc, - client); + mq = GNUNET_SERVICE_client_get_mq (client); + GNUNET_notification_context_add (nc, + mq); setup_estimate_message (&em); - GNUNET_SERVER_notification_context_unicast (nc, - client, - &em.header, - GNUNET_YES); - GNUNET_SERVER_receive_done (client, - GNUNET_OK); + env = GNUNET_MQ_msg_copy (&em.header); + GNUNET_MQ_send (mq, + env); + GNUNET_SERVICE_client_continue (client); } @@ -676,9 +673,9 @@ update_network_size_estimate () struct GNUNET_NSE_ClientMessage em; setup_estimate_message (&em); - GNUNET_SERVER_notification_context_broadcast (nc, - &em.header, - GNUNET_YES); + GNUNET_notification_context_broadcast (nc, + &em.header, + GNUNET_YES); } @@ -1292,7 +1289,7 @@ handle_core_disconnect (void *cls, GNUNET_CONTAINER_multipeermap_remove (peers, peer, pos)); - if (pos->transmit_task != NULL) + if (NULL != pos->transmit_task) { GNUNET_SCHEDULER_cancel (pos->transmit_task); pos->transmit_task = NULL; @@ -1344,7 +1341,7 @@ shutdown_task (void *cls) } if (NULL != nc) { - GNUNET_SERVER_notification_context_destroy (nc); + GNUNET_notification_context_destroy (nc); nc = NULL; } if (NULL != core_api) @@ -1476,19 +1473,14 @@ status_cb (void *cls, * Handle network size estimate clients. * * @param cls closure - * @param server the initialized server * @param c configuration to use + * @param service the initialized service */ static void run (void *cls, - struct GNUNET_SERVER_Handle *server, - const struct GNUNET_CONFIGURATION_Handle *c) + const struct GNUNET_CONFIGURATION_Handle *c, + struct GNUNET_SERVICE_Handle *service) { - static const struct GNUNET_SERVER_MessageHandler handlers[] = { - {&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START, - sizeof (struct GNUNET_MessageHeader)}, - {NULL, NULL, 0, 0} - }; struct GNUNET_MQ_MessageHandler core_handlers[] = { GNUNET_MQ_hd_fixed_size (p2p_estimate, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, @@ -1500,7 +1492,6 @@ run (void *cls, struct GNUNET_CRYPTO_EddsaPrivateKey *pk; cfg = c; - srv = server; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", @@ -1614,9 +1605,7 @@ run (void *cls, peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES); - GNUNET_SERVER_add_handlers (srv, - handlers); - nc = GNUNET_SERVER_notification_context_create (srv, 1); + nc = GNUNET_notification_context_create (1); /* Connect to core service and register core handlers */ core_api = GNUNET_CORE_connecT (cfg, /* Main configuration */ NULL, /* Closure passed to functions */ @@ -1635,26 +1624,55 @@ run (void *cls, /** - * The main function for the network size estimation service. + * Callback called when a client connects to the service. * - * @param argc number of arguments from the command line - * @param argv command line arguments - * @return 0 ok, 1 on error + * @param cls closure for the service + * @param c the new client that connected to the service + * @param mq the message queue used to send messages to the client + * @return @a c */ -int -main (int argc, - char *const *argv) +static void * +client_connect_cb (void *cls, + struct GNUNET_SERVICE_Client *c, + struct GNUNET_MQ_Handle *mq) { - return (GNUNET_OK == - GNUNET_SERVICE_run (argc, - argv, - "nse", - GNUNET_SERVICE_OPTION_NONE, - &run, - NULL)) ? 0 : 1; + return c; } +/** + * Callback called when a client disconnected from the service + * + * @param cls closure for the service + * @param c the client that disconnected + * @param internal_cls should be equal to @a c + */ +static void +client_disconnect_cb (void *cls, + struct GNUNET_SERVICE_Client *c, + void *internal_cls) +{ + GNUNET_assert (c == internal_cls); +} + + +/** + * Define "main" method using service macro. + */ +GNUNET_SERVICE_MAIN +("nse", + GNUNET_SERVICE_OPTION_NONE, + &run, + &client_connect_cb, + &client_disconnect_cb, + NULL, + GNUNET_MQ_hd_fixed_size (start, + GNUNET_MESSAGE_TYPE_NSE_START, + struct GNUNET_MessageHeader, + NULL), + GNUNET_MQ_handler_end ()); + + #if defined(LINUX) && defined(__GLIBC__) #include diff --git a/src/util/mq.c b/src/util/mq.c index 01cdf764b..e69d1c483 100644 --- a/src/util/mq.c +++ b/src/util/mq.c @@ -952,19 +952,68 @@ GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, } +/** + * Call a callback once the envelope has been sent, that is, + * sending it can not be canceled anymore. + * There can be only one notify sent callback per envelope. + * + * @param ev message to call the notify callback for + * @param cb the notify callback + * @param cb_cls closure for the callback + */ void GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm, GNUNET_MQ_NotifyCallback cb, - void *cls) + void *cb_cls) { mqm->sent_cb = cb; - mqm->sent_cls = cls; + mqm->sent_cls = cb_cls; } +/** + * Handle we return for callbacks registered to be + * notified when #GNUNET_MQ_destroy() is called on a queue. + */ +struct GNUNET_MQ_DestroyNotificationHandle +{ + /** + * Kept in a DLL. + */ + struct GNUNET_MQ_DestroyNotificationHandle *prev; + + /** + * Kept in a DLL. + */ + struct GNUNET_MQ_DestroyNotificationHandle *next; + + /** + * Queue to notify about. + */ + struct GNUNET_MQ_Handle *mq; + + /** + * Function to call. + */ + GNUNET_SCHEDULER_TaskCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; +}; + + +/** + * Destroy the message queue. + * + * @param mq message queue to destroy + */ void GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq) { + struct GNUNET_MQ_DestroyNotificationHandle *dnh; + if (NULL != mq->destroy_impl) { mq->destroy_impl (mq, mq->impl_state); @@ -996,6 +1045,8 @@ GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq) mq->queue_length--; } GNUNET_assert (0 == mq->queue_length); + while (NULL != (dnh = mq->dnh_head)) + dnh->cb (dnh->cb_cls); if (NULL != mq->assoc_map) { GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map); @@ -1183,39 +1234,6 @@ GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq, } -/** - * Handle we return for callbacks registered to be - * notified when #GNUNET_MQ_destroy() is called on a queue. - */ -struct GNUNET_MQ_DestroyNotificationHandle -{ - /** - * Kept in a DLL. - */ - struct GNUNET_MQ_DestroyNotificationHandle *prev; - - /** - * Kept in a DLL. - */ - struct GNUNET_MQ_DestroyNotificationHandle *next; - - /** - * Queue to notify about. - */ - struct GNUNET_MQ_Handle *mq; - - /** - * Function to call. - */ - GNUNET_SCHEDULER_TaskCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; -}; - - /** * Register function to be called whenever @a mq is being * destroyed. -- 2.25.1