gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r37950 - in gnunet/src: include nse util


From: gnunet
Subject: [GNUnet-SVN] r37950 - in gnunet/src: include nse util
Date: Mon, 19 Sep 2016 20:12:49 +0200

Author: grothoff
Date: 2016-09-19 20:12:49 +0200 (Mon, 19 Sep 2016)
New Revision: 37950

Modified:
   gnunet/src/include/gnunet_mq_lib.h
   gnunet/src/nse/gnunet-service-nse.c
   gnunet/src/util/mq.c
Log:
converting nse to new service API

Modified: gnunet/src/include/gnunet_mq_lib.h
===================================================================
--- gnunet/src/include/gnunet_mq_lib.h  2016-09-19 14:37:07 UTC (rev 37949)
+++ gnunet/src/include/gnunet_mq_lib.h  2016-09-19 18:12:49 UTC (rev 37950)
@@ -619,12 +619,12 @@
  *
  * @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);
 
 
 /**

Modified: gnunet/src/nse/gnunet-service-nse.c
===================================================================
--- gnunet/src/nse/gnunet-service-nse.c 2016-09-19 14:37:07 UTC (rev 37949)
+++ gnunet/src/nse/gnunet-service-nse.c 2016-09-19 18:12:49 UTC (rev 37950)
@@ -286,7 +286,7 @@
 /**
  * Notification context, simplifies client broadcasts.
  */
-static struct GNUNET_SERVER_NotificationContext *nc;
+static struct GNUNET_NotificationContext *nc;
 
 /**
  * The next major time.
@@ -313,12 +313,7 @@
  */
 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
  * size estimate.
@@ -328,8 +323,6 @@
 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 @@
   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 @@
   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 @@
   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 @@
  * 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 @@
   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 @@
                  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 @@
   }
   if (NULL != nc)
   {
-    GNUNET_SERVER_notification_context_destroy (nc);
+    GNUNET_notification_context_destroy (nc);
     nc = NULL;
   }
   if (NULL != core_api)
@@ -1476,19 +1473,14 @@
  * 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 @@
   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
 
   cfg = c;
-  srv = server;
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (cfg,
                                           "NSE",
@@ -1614,9 +1605,7 @@
 
   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 @@
 
 
 /**
- * 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>
 

Modified: gnunet/src/util/mq.c
===================================================================
--- gnunet/src/util/mq.c        2016-09-19 14:37:07 UTC (rev 37949)
+++ gnunet/src/util/mq.c        2016-09-19 18:12:49 UTC (rev 37950)
@@ -952,19 +952,68 @@
 }
 
 
+/**
+ * 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 @@
     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);
@@ -1184,39 +1235,6 @@
 
 
 /**
- * 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.
  *




reply via email to

[Prev in Thread] Current Thread [Next in Thread]