/**
* Notification context, simplifies client broadcasts.
*/
-static struct GNUNET_SERVER_NotificationContext *nc;
+static struct GNUNET_NotificationContext *nc;
/**
* The next major time.
*/
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
static void
setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
{
- unsigned int i;
- unsigned int j;
double mean;
double sum;
double std_dev;
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;
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;
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);
+ }
}
* 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);
}
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);
}
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;
}
if (NULL != nc)
{
- GNUNET_SERVER_notification_context_destroy (nc);
+ GNUNET_notification_context_destroy (nc);
nc = NULL;
}
if (NULL != core_api)
* 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,
struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
cfg = c;
- srv = server;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_time (cfg,
"NSE",
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 */
/**
- * 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 <malloc.h>
}
+/**
+ * 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);
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);
}
-/**
- * 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.