gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r27374 - in gnunet/src: arm ats-tool include namestore rege


From: gnunet
Subject: [GNUnet-SVN] r27374 - in gnunet/src: arm ats-tool include namestore regex statistics transport util
Date: Wed, 5 Jun 2013 23:33:58 +0200

Author: grothoff
Date: 2013-06-05 23:33:58 +0200 (Wed, 05 Jun 2013)
New Revision: 27374

Modified:
   gnunet/src/arm/arm_api.c
   gnunet/src/ats-tool/gnunet-ats.c
   gnunet/src/include/gnunet_client_lib.h
   gnunet/src/include/gnunet_crypto_lib.h
   gnunet/src/include/gnunet_service_lib.h
   gnunet/src/namestore/gnunet-namestore.c
   gnunet/src/regex/gnunet-regex-simulation-profiler.c
   gnunet/src/regex/regex.c
   gnunet/src/statistics/gnunet-statistics.c
   gnunet/src/transport/gnunet-transport.c
   gnunet/src/util/client.c
   gnunet/src/util/test_service.c
Log:
-cleaning up client api to test for service availability

Modified: gnunet/src/arm/arm_api.c
===================================================================
--- gnunet/src/arm/arm_api.c    2013-06-05 21:33:37 UTC (rev 27373)
+++ gnunet/src/arm/arm_api.c    2013-06-05 21:33:58 UTC (rev 27374)
@@ -743,10 +743,11 @@
  * it is not, start the ARM process.
  *
  * @param cls the context for the request that we will report on (struct 
ARMControlMessage *)
- * @param tc why were we called (reason says if ARM is running)
+ * @param result GNUNET_YES if ARM is running
  */
 static void
-arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+arm_service_report (void *cls,
+                   int result)
 {
   struct ARMControlMessage *cm = cls;
   struct GNUNET_ARM_Handle *h;
@@ -759,12 +760,11 @@
   char *lopostfix;
 
   test_is_active = cm->h->service_test_is_active;
-
-  /* FIXME: shouldn't we check for GNUNET_SCHEDULER_REASON_SHUTDOWN ? */
   if ((GNUNET_YES == test_is_active) &&
-      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)))
+      (GNUNET_YES == result))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, 
+        "Looks like `%s' is already running.\n",
         "gnunet-service-arm");
     /* arm is running! */
     if (cm->result_cont)
@@ -780,7 +780,7 @@
     GNUNET_CONFIGURATION_destroy (cm->h->cfg);
     GNUNET_free (cm->h);
   }
-  if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) ||
+  if ((GNUNET_YES == result) ||
       (GNUNET_NO == test_is_active))
   {
     GNUNET_free (cm);

Modified: gnunet/src/ats-tool/gnunet-ats.c
===================================================================
--- gnunet/src/ats-tool/gnunet-ats.c    2013-06-05 21:33:37 UTC (rev 27373)
+++ gnunet/src/ats-tool/gnunet-ats.c    2013-06-05 21:33:58 UTC (rev 27374)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and 
other contributing authors)
+     (C) 2009--2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -80,15 +80,15 @@
 static int op_monitor;
 
 
-
 static struct GNUNET_ATS_PerformanceHandle *ph;
 
-struct GNUNET_ATS_AddressListHandle *alh;
+static struct GNUNET_ATS_AddressListHandle *alh;
 
 static struct GNUNET_CONFIGURATION_Handle *cfg;
 
-GNUNET_SCHEDULER_TaskIdentifier end_task;
+static GNUNET_SCHEDULER_TaskIdentifier end_task;
 
+
 struct PendingResolutions
 {
   struct PendingResolutions *next;
@@ -104,11 +104,15 @@
   struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
 };
 
-struct PendingResolutions *head;
-struct PendingResolutions *tail;
 
-void end (void *cls,
-          const struct GNUNET_SCHEDULER_TaskContext *tc)
+static struct PendingResolutions *head;
+
+static struct PendingResolutions *tail;
+
+
+static void 
+end (void *cls,
+     const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PendingResolutions * pr;
   struct PendingResolutions * next;
@@ -145,7 +149,8 @@
 }
 
 
-void transport_addr_to_str_cb (void *cls, const char *address)
+static void 
+transport_addr_to_str_cb (void *cls, const char *address)
 {
   struct PendingResolutions * pr = cls;
   char *ats_str;
@@ -156,11 +161,10 @@
   uint32_t ats_type;
   uint32_t ats_value;
   uint32_t network;
+
   if (NULL != address)
   {
     ats_str = GNUNET_strdup("");
-
-
     for (c = 0; c < pr->ats_count; c++)
     {
         ats_tmp = ats_str;
@@ -220,17 +224,18 @@
   }
 }
 
-void ats_perf_cb (void *cls,
-                                                                       const 
struct GNUNET_HELLO_Address *address,
-                                                                       int 
active,
-                                                                       struct 
GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
-                                                                       struct 
GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
-                                                                       const 
struct GNUNET_ATS_Information *ats,
-                                                                       
uint32_t ats_count)
+
+static void 
+ats_perf_cb (void *cls,
+            const struct GNUNET_HELLO_Address *address,
+            int active,
+            struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+            struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+            const struct GNUNET_ATS_Information *ats,
+            uint32_t ats_count)
 {
   struct PendingResolutions * pr;
 
-
   if (NULL != address)
   {
     pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
@@ -325,30 +330,30 @@
 }
 
 
-
-void testservice_ats (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+testservice_ats (void *cls,
+                int result)
 {
+  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
   struct GNUNET_PeerIdentity pid;
-  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
   unsigned int c;
   unsigned int type;
 
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
+  if (GNUNET_YES != result)
   {
-      FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
-      return;
+    FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
+    return;
   }
 
   results = 0;
 
   if (NULL != pid_str)
   {
-      if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (pid_str, 
&pid.hashPubKey))
-      {
-        FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
-        return;
-      }
+    if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (pid_str, &pid.hashPubKey))
+    {
+      FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
+      return;
+    }
   }
 
   c = op_list_all + op_list_used + op_monitor + op_set_pref;

Modified: gnunet/src/include/gnunet_client_lib.h
===================================================================
--- gnunet/src/include/gnunet_client_lib.h      2013-06-05 21:33:37 UTC (rev 
27373)
+++ gnunet/src/include/gnunet_client_lib.h      2013-06-05 21:33:58 UTC (rev 
27374)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff 
(and other contributing authors)
+     (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2013 Christian 
Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -190,23 +190,51 @@
 
 
 /**
- * Wait until the service is running.
+ * Handle for a test to check if a service is running.
+ */
+struct GNUNET_CLIENT_TestHandle;
+
+/**
+ * Function called with the result on the service test.
  *
+ * @param cls closure
+ * @param result GNUNET_YES if the service is running,
+ *               GNUNET_NO if the service is not running
+ *               GNUNET_SYSERR if the configuration is invalid
+ */
+typedef void (*GNUNET_CLIENT_TestResultCallback)(void *cls,
+                                                int result);
+
+
+/**
+ * Test if the service is running.  If we are given a UNIXPATH or a
+ * local address, we do this NOT by trying to connect to the service,
+ * but by trying to BIND to the same port.  If the BIND fails, we know
+ * the service is running.
+ *
  * @param service name of the service to wait for
  * @param cfg configuration to use
  * @param timeout how long to wait at most in ms
- * @param task task to run if service is running
- *        (reason will be "PREREQ_DONE" (service running)
- *         or "TIMEOUT" (service not known to be running))
- * @param task_cls closure for task
+ * @param cb function to call with the result
+ * @param cb_cls closure for 'cb'
+ * @return handle to cancel the test
  */
-void
+struct GNUNET_CLIENT_TestHandle *
 GNUNET_CLIENT_service_test (const char *service,
                             const struct GNUNET_CONFIGURATION_Handle *cfg,
                             struct GNUNET_TIME_Relative timeout,
-                            GNUNET_SCHEDULER_Task task, void *task_cls);
+                            GNUNET_CLIENT_TestResultCallback cb, void *cb_cls);
 
 
+/**
+ * Abort testing for service.
+ *
+ * @param th test handle
+ */
+void
+GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th);
+
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif

Modified: gnunet/src/include/gnunet_crypto_lib.h
===================================================================
--- gnunet/src/include/gnunet_crypto_lib.h      2013-06-05 21:33:37 UTC (rev 
27373)
+++ gnunet/src/include/gnunet_crypto_lib.h      2013-06-05 21:33:58 UTC (rev 
27374)
@@ -1106,7 +1106,7 @@
  */
 void
 GNUNET_CRYPTO_rsa_get_public_key_hash (struct GNUNET_CRYPTO_RsaPrivateKey *key,
-    struct GNUNET_HashCode *id);
+                                      struct GNUNET_HashCode *id);
 
 
 /**

Modified: gnunet/src/include/gnunet_service_lib.h
===================================================================
--- gnunet/src/include/gnunet_service_lib.h     2013-06-05 21:33:37 UTC (rev 
27373)
+++ gnunet/src/include/gnunet_service_lib.h     2013-06-05 21:33:58 UTC (rev 
27374)
@@ -119,8 +119,8 @@
  */
 int
 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
-                    enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main 
task,
-                    void *task_cls);
+                    enum GNUNET_SERVICE_Options options, 
+                   GNUNET_SERVICE_Main task, void *task_cls);
 
 
 /**

Modified: gnunet/src/namestore/gnunet-namestore.c
===================================================================
--- gnunet/src/namestore/gnunet-namestore.c     2013-06-05 21:33:37 UTC (rev 
27373)
+++ gnunet/src/namestore/gnunet-namestore.c     2013-06-05 21:33:58 UTC (rev 
27374)
@@ -326,12 +326,20 @@
   GNUNET_NAMESTORE_zone_iterator_next (list_it);
 }
 
+
+/**
+ * Function called with the result of the ECC key generation.
+ *
+ * @param cls our configuration
+ * @param pk our private key, NULL on failure
+ * @param emsg NULL on success, otherwise error message
+ */
 static void
 key_generation_cb (void *cls,
                    struct GNUNET_CRYPTO_EccPrivateKey *pk,
                    const char *emsg)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
   uint32_t type;
   void *data = NULL;
@@ -599,19 +607,26 @@
 }
 
 
+/**
+ * Function called with the result from the check if the namestore
+ * service is actually running.  If it is, we start the actual
+ * operation.
+ *
+ * @param cls closure with our configuration
+ * @param result GNUNET_YES if the namestore service is running
+ */
 static void
 testservice_task (void *cls,
-                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+                  int result)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
 
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
+  if (GNUNET_YES != result)
   {
-      FPRINTF (stderr, _("Service `%s' is not running\n"), "namestore");
-      return;
+    FPRINTF (stderr, _("Service `%s' is not running\n"), 
+            "namestore");
+    return;
   }
-
-
   if (NULL == keyfile)
   {
     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
@@ -625,7 +640,8 @@
              _("Using default zone file `%s'\n"),
              keyfile);
   }
-  keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile, key_generation_cb, 
cfg);
+  keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
+                                              &key_generation_cb, (void *) 
cfg);
   GNUNET_free (keyfile);
   keyfile = NULL;
   if (NULL == keygen)
@@ -653,9 +669,9 @@
     uri = GNUNET_strdup (args[0]);
 
   GNUNET_CLIENT_service_test ("namestore", cfg,
-      GNUNET_TIME_UNIT_SECONDS,
-      &testservice_task,
-      (void *) cfg);
+                             GNUNET_TIME_UNIT_SECONDS,
+                             &testservice_task,
+                             (void *) cfg);
 }
 
 

Modified: gnunet/src/regex/gnunet-regex-simulation-profiler.c
===================================================================
--- gnunet/src/regex/gnunet-regex-simulation-profiler.c 2013-06-05 21:33:37 UTC 
(rev 27373)
+++ gnunet/src/regex/gnunet-regex-simulation-profiler.c 2013-06-05 21:33:58 UTC 
(rev 27374)
@@ -489,7 +489,7 @@
  * @return GNUNET_OK to continue to iterate,
  *  GNUNET_SYSERR to abort iteration with error!
  */
-int
+static int
 policy_filename_cb (void *cls, const char *filename)
 {
   char *regex;

Modified: gnunet/src/regex/regex.c
===================================================================
--- gnunet/src/regex/regex.c    2013-06-05 21:33:37 UTC (rev 27373)
+++ gnunet/src/regex/regex.c    2013-06-05 21:33:58 UTC (rev 27374)
@@ -3478,6 +3478,7 @@
                         NULL, a->start, iterator, iterator_cls);
 }
 
+
 /**
  * Create a string with binary IP notation for the given 'addr' in 'str'.
  *

Modified: gnunet/src/statistics/gnunet-statistics.c
===================================================================
--- gnunet/src/statistics/gnunet-statistics.c   2013-06-05 21:33:37 UTC (rev 
27373)
+++ gnunet/src/statistics/gnunet-statistics.c   2013-06-05 21:33:58 UTC (rev 
27374)
@@ -167,42 +167,18 @@
 }
 
 
-
+/**
+ * Main task that does the actual work.
+ *
+ * @param cls closure with our configuration
+ * @param tc schedueler context
+ */
 static void
-resolver_test_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+main_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
   struct GNUNET_STATISTICS_Handle *h;
 
-  if (NULL != remote_host)
-  {
-      if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
-      {
-          FPRINTF (stderr, _("Trying to connect to remote host, but service 
`%s' is not running\n"), "resolver");
-          return;
-      }
-
-      /* connect to a remote host */
-      if (0 == remote_port)
-      {
-        if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, 
"statistics", "PORT", &remote_port))
-        {
-          FPRINTF (stderr, _("A port is required to connect to host `%s'\n"), 
remote_host);
-          return;
-        }
-      }
-      else if (65535 <= remote_port)
-      {
-          FPRINTF (stderr, _("A port has to be between 1 and 65535 to connect 
to host `%s'\n"), remote_host);
-          return;
-      }
-
-      /* Manipulate configuration */
-      GNUNET_CONFIGURATION_set_value_string ((struct 
GNUNET_CONFIGURATION_Handle *) cfg, "statistics", "UNIXPATH", "");
-      GNUNET_CONFIGURATION_set_value_string ((struct 
GNUNET_CONFIGURATION_Handle *) cfg, "statistics", "HOSTNAME", remote_host);
-      GNUNET_CONFIGURATION_set_value_number ((struct 
GNUNET_CONFIGURATION_Handle *) cfg, "statistics", "PORT", remote_port);
-  }
-
   if (set_value)
   {
     if (subsystem == NULL)
@@ -264,6 +240,52 @@
 
 
 /**
+ * Function called with th test result to see if the resolver is
+ * running.
+ *
+ * @param cls closure with our configuration
+ * @param result GNUNET_YES if the resolver is running
+ */ 
+static void
+resolver_test_task (void *cls, 
+                   int result)
+{
+  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+
+  if (GNUNET_YES != result)
+   {
+     FPRINTF (stderr, 
+             _("Trying to connect to remote host, but service `%s' is not 
running\n"), "resolver");
+     return;
+   }
+  /* connect to a remote host */
+  if (0 == remote_port)
+  {
+    if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, 
"statistics", "PORT", &remote_port))
+    {
+      FPRINTF (stderr, _("A port is required to connect to host `%s'\n"), 
remote_host);
+      return;
+    }
+  }
+  else if (65535 <= remote_port)
+  {
+    FPRINTF (stderr, 
+            _("A port has to be between 1 and 65535 to connect to host 
`%s'\n"), remote_host);
+    return;
+  }
+
+  /* Manipulate configuration */
+  GNUNET_CONFIGURATION_set_value_string (cfg, 
+                                        "statistics", "UNIXPATH", "");
+  GNUNET_CONFIGURATION_set_value_string (cfg,
+                                        "statistics", "HOSTNAME", remote_host);
+  GNUNET_CONFIGURATION_set_value_number (cfg,
+                                        "statistics", "PORT", remote_port);
+  GNUNET_SCHEDULER_add_now (&main_task, cfg);
+}
+
+
+/**
  * Main function that will be run by the scheduler.
  *
  * @param cls closure
@@ -278,20 +300,19 @@
   set_value = GNUNET_NO;
   if (NULL != args[0])
   {
-      if (1 != SSCANF (args[0], "%llu", &set_val))
-      {
-          FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
-          ret = 1;
-          return;
-      }
-      set_value = GNUNET_YES;
+    if (1 != SSCANF (args[0], "%llu", &set_val))
+    {
+      FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
+      ret = 1;
+      return;
+    }
+    set_value = GNUNET_YES;
   }
-
   if (NULL != remote_host)
-      GNUNET_CLIENT_service_test ("resolver", cfg, GNUNET_TIME_UNIT_SECONDS, 
&resolver_test_task, (void *) cfg);
+    GNUNET_CLIENT_service_test ("resolver", cfg, GNUNET_TIME_UNIT_SECONDS, 
+                               &resolver_test_task, (void *) cfg);
   else
-      GNUNET_SCHEDULER_add_now (&resolver_test_task, (void *) cfg);
-
+    GNUNET_SCHEDULER_add_now (&main_task, (void *) cfg);
 }
 
 

Modified: gnunet/src/transport/gnunet-transport.c
===================================================================
--- gnunet/src/transport/gnunet-transport.c     2013-06-05 21:33:37 UTC (rev 
27373)
+++ gnunet/src/transport/gnunet-transport.c     2013-06-05 21:33:58 UTC (rev 
27374)
@@ -792,23 +792,32 @@
 }
 
 
+/**
+ * Function called with the result of the check if the 'transport'
+ * service is running.
+ *
+ * @param cls closure with our configuration
+ * @param result GNUNET_YES if transport is running
+ */
 static void
 testservice_task (void *cls,
-                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+                  int result)
 {
   int counter = 0;
   ret = 1;
 
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
+  if (GNUNET_YES != result)
   {
-      FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
-      return;
+    FPRINTF (stderr, 
+            _("Service `%s' is not running\n"), "transport");
+    return;
   }
 
-  if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, 
&pid.hashPubKey)))
+  if ( (NULL != cpid) && 
+       (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
   {
-      FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
-      return;
+    FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
+    return;
   }
 
   counter = benchmark_send + benchmark_receive + iterate_connections +
@@ -970,11 +979,10 @@
     do_test_configuration (cfg);
     return;
   }
-
   GNUNET_CLIENT_service_test ("transport", cfg,
-      GNUNET_TIME_UNIT_SECONDS,
-      &testservice_task,
-      (void *) cfg);
+                             GNUNET_TIME_UNIT_SECONDS,
+                             &testservice_task,
+                             (void *) cfg);
 }
 
 

Modified: gnunet/src/util/client.c
===================================================================
--- gnunet/src/util/client.c    2013-06-05 21:33:37 UTC (rev 27373)
+++ gnunet/src/util/client.c    2013-06-05 21:33:58 UTC (rev 27374)
@@ -179,27 +179,12 @@
   struct GNUNET_CLIENT_TransmitHandle *th;
 
   /**
-   * Handler for service test completion (NULL unless in service_test)
-   */
-  GNUNET_SCHEDULER_Task test_cb;
-
-  /**
-   * Deadline for calling 'test_cb'.
-   */
-  struct GNUNET_TIME_Absolute test_deadline;
-
-  /**
    * If we are re-trying and are delaying to do so,
    * handle to the scheduled task managing the delay.
    */
   GNUNET_SCHEDULER_TaskIdentifier receive_task;
 
   /**
-   * Closure for test_cb (NULL unless in service_test)
-   */
-  void *test_cb_cls;
-
-  /**
    * Buffer for received message.
    */
   char *received_buf;
@@ -639,26 +624,119 @@
 
 
 /**
- * Report service unavailable.
+ * Handle for a test to check if a service is running.
  */
+struct GNUNET_CLIENT_TestHandle 
+{
+  /**
+   * Function to call with the result of the test.
+   */
+  GNUNET_CLIENT_TestResultCallback cb;
+
+  /**
+   * Closure for 'cb'.
+   */
+  void *cb_cls;
+
+  /**
+   * Client connection we are using for the test, if any.
+   */ 
+  struct GNUNET_CLIENT_Connection *client;
+
+  /**
+   * Handle for the transmission request, if any.
+   */
+  struct GNUNET_CLIENT_TransmitHandle *th;
+
+  /**
+   * Deadline for calling 'cb'.
+   */
+  struct GNUNET_TIME_Absolute test_deadline;
+
+  /**
+   * ID of task used for asynchronous operations.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier task;
+
+  /**
+   * Final result to report back (once known).
+   */
+  int result;
+};
+
+
+/**
+ * Abort testing for service.
+ *
+ * @param th test handle
+ */
+void
+GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th)
+{
+  if (NULL != th->th)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel (th->th);
+    th->th = NULL;
+  }
+  if (NULL != th->client)
+  {
+    GNUNET_CLIENT_disconnect (th->client);
+    th->client = NULL;
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != th->task)
+  {
+    GNUNET_SCHEDULER_cancel (th->task);
+    th->task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  GNUNET_free (th);
+}
+
+
+/**
+ * Task that reports back the result by calling the callback
+ * and then cleans up.
+ *
+ * @param cls the 'struct GNUNET_CLIENT_TestHandle'
+ * @param tc scheduler context
+ */
 static void
-service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
+report_result (void *cls,
+              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_SCHEDULER_add_continuation (task, task_cls,
-                                     GNUNET_SCHEDULER_REASON_TIMEOUT);
+  struct GNUNET_CLIENT_TestHandle *th = cls;
+  
+  th->task = GNUNET_SCHEDULER_NO_TASK;
+  th->cb (th->cb_cls, th->result);
+  GNUNET_CLIENT_service_test_cancel (th);
 }
 
 
 /**
+ * Report service test result asynchronously back to callback.
+ *
+ * @param th test handle with the result and the callback
+ * @param result result to report
+ */
+static void
+service_test_report (struct GNUNET_CLIENT_TestHandle *th,
+                    int result)
+{
+  th->result = result;
+  th->task = GNUNET_SCHEDULER_add_now (&report_result,
+                                      th);                                    
+}
+
+
+/**
  * Receive confirmation from test, service is up.
  *
- * @param cls closure
+ * @param cls closure with the 'struct GNUNET_CLIENT_TestHandle'
  * @param msg message received, NULL on timeout or fatal error
  */
 static void
 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
 {
-  struct GNUNET_CLIENT_Connection *client = cls;
+  struct GNUNET_CLIENT_TestHandle *th = cls;
 
   /* We may want to consider looking at the reply in more
    * detail in the future, for example, is this the
@@ -667,14 +745,12 @@
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Received confirmation that service is running.\n");
-    GNUNET_SCHEDULER_add_continuation (client->test_cb, client->test_cb_cls,
-                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+    service_test_report (th, GNUNET_YES);
   }
   else
   {
-    service_test_error (client->test_cb, client->test_cb_cls);
+    service_test_report (th, GNUNET_NO);
   }
-  GNUNET_CLIENT_disconnect (client);
 }
 
 
@@ -682,7 +758,7 @@
  * Send the 'TEST' message to the service.  If successful, prepare to
  * receive the reply.
  *
- * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
+ * @param cls the 'struct GNUNET_CLIENT_TestHandle' of the test
  * @param size number of bytes available in buf
  * @param buf where to write the message
  * @return number of bytes written to buf
@@ -690,52 +766,61 @@
 static size_t
 write_test (void *cls, size_t size, void *buf)
 {
-  struct GNUNET_CLIENT_Connection *client = cls;
+  struct GNUNET_CLIENT_TestHandle *th = cls;
   struct GNUNET_MessageHeader *msg;
 
+  th->th = NULL;
   if (size < sizeof (struct GNUNET_MessageHeader))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
-    service_test_error (client->test_cb, client->test_cb_cls);
-    GNUNET_CLIENT_disconnect (client);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, 
+        "Failed to transmit TEST request.\n");
+    service_test_report (th, GNUNET_NO);
     return 0;                   /* client disconnected */
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+       "Transmitting `%s' request.\n", 
+       "TEST");
   msg = (struct GNUNET_MessageHeader *) buf;
   msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
-  GNUNET_CLIENT_receive (client, &confirm_handler, client,
+  GNUNET_CLIENT_receive (th->client, 
+                        &confirm_handler, th,
                          GNUNET_TIME_absolute_get_remaining
-                         (client->test_deadline));
+                         (th->test_deadline));
   return sizeof (struct GNUNET_MessageHeader);
 }
 
 
 /**
- * Test if the service is running.  If we are given a UNIXPATH or a local 
address,
- * we do this NOT by trying to connect to the service, but by trying to BIND to
- * the same port.  If the BIND fails, we know the service is running.
+ * Test if the service is running.  If we are given a UNIXPATH or a
+ * local address, we do this NOT by trying to connect to the service,
+ * but by trying to BIND to the same port.  If the BIND fails, we know
+ * the service is running.
  *
  * @param service name of the service to wait for
  * @param cfg configuration to use
  * @param timeout how long to wait at most
- * @param task task to run if service is running
- *        (reason will be "PREREQ_DONE" (service running)
- *         or "TIMEOUT" (service not known to be running))
- * @param task_cls closure for task
+ * @param cb function to call with the result
+ * @param cb_cls closure for 'cb'
+ * @return handle to cancel the test
  */
-void
+struct GNUNET_CLIENT_TestHandle *
 GNUNET_CLIENT_service_test (const char *service,
                             const struct GNUNET_CONFIGURATION_Handle *cfg,
                             struct GNUNET_TIME_Relative timeout,
-                            GNUNET_SCHEDULER_Task task, void *task_cls)
+                            GNUNET_CLIENT_TestResultCallback cb, void *cb_cls)
 {
+  struct GNUNET_CLIENT_TestHandle *th;
   char *hostname;
   unsigned long long port;
   struct GNUNET_NETWORK_Handle *sock;
-  struct GNUNET_CLIENT_Connection *client;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
+  th = GNUNET_new (struct GNUNET_CLIENT_TestHandle);
+  th->cb = cb;
+  th->cb_cls = cb_cls;
+  th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+       "Testing if service `%s' is running.\n",
        service);
 #ifdef AF_UNIX
   {
@@ -783,9 +868,8 @@
          /* failed to bind => service must be running */
          GNUNET_free (unixpath);
          (void) GNUNET_NETWORK_socket_close (sock);
-         GNUNET_SCHEDULER_add_continuation (task, task_cls,
-                                            
GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-         return;
+         service_test_report (th, GNUNET_YES);
+         return th;
        }
        (void) GNUNET_NETWORK_socket_close (sock);        
         /* let's try IP */
@@ -804,8 +888,8 @@
                                               &hostname)))
   {
     /* UNIXPATH failed (if possible) AND IP failed => error */
-    service_test_error (task, task_cls);
-    return;
+    service_test_report (th, GNUNET_SYSERR);
+    return th;
   }
 
   if (0 == strcmp ("localhost", hostname)
@@ -834,9 +918,8 @@
         /* failed to bind => service must be running */
         GNUNET_free (hostname);
         (void) GNUNET_NETWORK_socket_close (sock);
-        GNUNET_SCHEDULER_add_continuation (task, task_cls,
-                                           
GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-        return;
+        service_test_report (th, GNUNET_YES);
+        return th;
       }
       (void) GNUNET_NETWORK_socket_close (sock);
     }
@@ -868,9 +951,8 @@
         /* failed to bind => service must be running */
         GNUNET_free (hostname);
         (void) GNUNET_NETWORK_socket_close (sock);
-        GNUNET_SCHEDULER_add_continuation (task, task_cls,
-                                           
GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-        return;
+        service_test_report (th, GNUNET_YES);
+        return th;
       }
       (void) GNUNET_NETWORK_socket_close (sock);
     }
@@ -885,35 +967,33 @@
   {
     /* all binds succeeded => claim service not running right now */
     GNUNET_free_non_null (hostname);
-    service_test_error (task, task_cls);
-    return;
+    service_test_report (th, GNUNET_NO);
+    return th;
   }
   GNUNET_free_non_null (hostname);
 
   /* non-localhost, try 'connect' method */
-  client = GNUNET_CLIENT_connect (service, cfg);
-  if (NULL == client)
+  th->client = GNUNET_CLIENT_connect (service, cfg);
+  if (NULL == th->client)
   {
     LOG (GNUNET_ERROR_TYPE_INFO,
-         _("Could not connect to service `%s', must not be running.\n"),
+         _("Could not connect to service `%s', configuration broken.\n"),
          service);
-    service_test_error (task, task_cls);
-    return;
+    service_test_report (th, GNUNET_SYSERR);
+    return th;
   }
-  client->test_cb = task;
-  client->test_cb_cls = task_cls;
-  client->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
-  if (NULL == GNUNET_CLIENT_notify_transmit_ready (client,
-                                                  sizeof (struct 
GNUNET_MessageHeader),
-                                                  timeout, GNUNET_YES, 
&write_test,
-                                                  client))
+  th->th = GNUNET_CLIENT_notify_transmit_ready (th->client,
+                                               sizeof (struct 
GNUNET_MessageHeader),
+                                               timeout, GNUNET_YES,
+                                               &write_test, th);
+  if (NULL == th->th)
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
          _("Failure to transmit request to service `%s'\n"), service);
-    service_test_error (task, task_cls);
-    GNUNET_CLIENT_disconnect (client);
-    return;
+    service_test_report (th, GNUNET_SYSERR);
+    return th;
   }
+  return th;
 }
 
 
@@ -1246,5 +1326,4 @@
 }
 
 
-
 /*  end of client.c */

Modified: gnunet/src/util/test_service.c
===================================================================
--- gnunet/src/util/test_service.c      2013-06-05 21:33:37 UTC (rev 27373)
+++ gnunet/src/util/test_service.c      2013-06-05 21:33:58 UTC (rev 27374)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -20,6 +20,7 @@
 /**
  * @file util/test_service.c
  * @brief tests for service.c
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_common.h"
@@ -33,6 +34,9 @@
 
 #define PORT 12435
 
+/**
+ * Message type we use for testing.
+ */
 #define MY_TYPE 256
 
 static struct GNUNET_SERVICE_Context *sctx;
@@ -42,8 +46,6 @@
 static struct GNUNET_CLIENT_Connection *client;
 
 
-
-
 static void
 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
@@ -87,11 +89,12 @@
 
 
 static void
-ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ready (void *cls,
+       int result)
 {
   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
 
-  GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
+  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);
@@ -108,8 +111,7 @@
 recv_cb (void *cls, struct GNUNET_SERVER_Client *sc,
          const struct GNUNET_MessageHeader *message)
 {
-  if (NULL == message)
-    return;
+  GNUNET_assert (NULL != message);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
   GNUNET_SERVER_receive_done (sc, GNUNET_OK);
   GNUNET_SCHEDULER_add_now (&do_stop, NULL);
@@ -156,13 +158,15 @@
   return ok;
 }
 
+
 static void
-ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+ready6 (void *cls, 
+       int result)
 {
   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
-  GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
+  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");
@@ -172,6 +176,7 @@
                                        &build_msg, NULL);
 }
 
+
 static void
 runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
          const struct GNUNET_CONFIGURATION_Handle *cfg)
@@ -182,6 +187,7 @@
                               &ready6, (void *) cfg);
 }
 
+
 /**
  * Main method, starts scheduler with task1,
  * checks that "ok" is correct at the end.
@@ -205,7 +211,6 @@
 }
 
 
-
 static void
 start_stop_main (void *cls, char *const *args, const char *cfgfile,
                  const struct GNUNET_CONFIGURATION_Handle *cfg)




reply via email to

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