gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r37417 - gnunet/src/arm


From: gnunet
Subject: [GNUnet-SVN] r37417 - gnunet/src/arm
Date: Wed, 29 Jun 2016 19:05:12 +0200

Author: grothoff
Date: 2016-06-29 19:05:12 +0200 (Wed, 29 Jun 2016)
New Revision: 37417

Modified:
   gnunet/src/arm/mockup-service.c
   gnunet/src/arm/test_arm_api_data.conf
   gnunet/src/arm/test_exponential_backoff.c
Log:
-convert backoff test to new MQ API

Modified: gnunet/src/arm/mockup-service.c
===================================================================
--- gnunet/src/arm/mockup-service.c     2016-06-29 16:10:45 UTC (rev 37416)
+++ gnunet/src/arm/mockup-service.c     2016-06-29 17:05:12 UTC (rev 37417)
@@ -34,8 +34,9 @@
  * @param message the actual message
  */
 static void
-handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
-                 const struct GNUNET_MessageHeader *message)
+handle_stop (void *cls,
+             struct GNUNET_SERVER_Client *client,
+             const struct GNUNET_MessageHeader *message)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Initiating shutdown as requested by client.\n"));
@@ -49,7 +50,8 @@
 
 
 static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
+run (void *cls,
+     struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {

Modified: gnunet/src/arm/test_arm_api_data.conf
===================================================================
--- gnunet/src/arm/test_arm_api_data.conf       2016-06-29 16:10:45 UTC (rev 
37416)
+++ gnunet/src/arm/test_arm_api_data.conf       2016-06-29 17:05:12 UTC (rev 
37417)
@@ -10,6 +10,7 @@
 #PREFIX = valgrind --tool=memcheck --leak-check=yes
 
 [resolver]
+AUTOSTART = YES
 PORT = 23355
 # PREFIX = valgrind
 
@@ -32,3 +33,6 @@
 
 [ats]
 AUTOSTART = NO
+
+[peerinfo]
+AUTOSTART = NO

Modified: gnunet/src/arm/test_exponential_backoff.c
===================================================================
--- gnunet/src/arm/test_exponential_backoff.c   2016-06-29 16:10:45 UTC (rev 
37416)
+++ gnunet/src/arm/test_exponential_backoff.c   2016-06-29 17:05:12 UTC (rev 
37417)
@@ -20,6 +20,7 @@
 /**
  * @file arm/test_exponential_backoff.c
  * @brief testcase for gnunet-service-arm.c
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_arm_service.h"
@@ -28,8 +29,6 @@
 
 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
-#define START_ARM GNUNET_YES
-
 #define LOG_BACKOFF GNUNET_NO
 
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
@@ -51,6 +50,8 @@
 
 static struct GNUNET_ARM_MonitorHandle *mon;
 
+static struct GNUNET_SCHEDULER_Task *kt;
+
 static int ok = 1;
 
 static int phase = 0;
@@ -70,10 +71,6 @@
 #endif
 
 
-typedef void
-(*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
-
-
 /**
  * Context for handling the shutdown of a service.
  */
@@ -82,210 +79,94 @@
   /**
    * Connection to the service that is being shutdown.
    */
-  struct GNUNET_CLIENT_Connection *sock;
+  struct GNUNET_MQ_Handle *mq;
 
   /**
-   * Time allowed for shutdown to happen.
-   */
-  struct GNUNET_TIME_Absolute timeout;
-
-  /**
    * Task set up to cancel the shutdown request on timeout.
    */
   struct GNUNET_SCHEDULER_Task *cancel_task;
 
-  /**
-   * Task to call once shutdown complete
-   */
-  GNUNET_CLIENT_ShutdownTask cont;
+};
 
-  /**
-   * Closure for shutdown continuation
-   */
-  void *cont_cls;
 
-  /**
-   * We received a confirmation that the service will shut down.
-   */
-  int confirmed;
+static void
+kill_task (void *cbData);
 
-};
 
 /**
- * Handler receiving response to service shutdown requests.
- * We expect it to be called with NULL, since the service that
- * we are shutting down will just die without replying.
+ * Shutting down took too long, cancel receive and return error.
  *
  * @param cls closure
- * @param msg NULL, indicating socket closure.
  */
 static void
-service_shutdown_handler (void *cls,
-                          const struct GNUNET_MessageHeader *msg)
+service_shutdown_timeout (void *cls)
 {
-  struct ShutdownContext *shutdown_ctx = cls;
-
-  if (NULL == msg)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
-    if (shutdown_ctx->cont != NULL)
-      shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
-
-    GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
-    GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
-    GNUNET_free (shutdown_ctx);
-    return;
-  }
   GNUNET_assert (0);
 }
 
 
 /**
- * Shutting down took too long, cancel receive and return error.
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
  *
- * @param cls closure
+ * @param cls closure with the `struct ShutdownContext *`
+ * @param error error code
  */
 static void
-service_shutdown_cancel (void *cls)
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
 {
   struct ShutdownContext *shutdown_ctx = cls;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-              "service_shutdown_cancel called!\n");
-  shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
-  GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Service shutdown complete (MQ error).\n");
+  GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
+  GNUNET_MQ_destroy (shutdown_ctx->mq);
   GNUNET_free (shutdown_ctx);
 }
 
 
-/**
- * If possible, write a shutdown message to the target
- * buffer and destroy the client connection.
- *
- * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
- * @param size number of bytes available in buf
- * @param buf NULL on error, otherwise target buffer
- * @return number of bytes written to buf
- */
-static size_t
-write_shutdown (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_MessageHeader *msg;
-  struct ShutdownContext *shutdown_ctx = cls;
-
-  if (size < sizeof (struct GNUNET_MessageHeader))
-  {
-    LOG ("Failed to send a shutdown request\n");
-    shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
-    GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
-    GNUNET_free (shutdown_ctx);
-    return 0;                  /* client disconnected */
-  }
-
-  GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
-                        shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
-  shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (
-      GNUNET_TIME_absolute_get_remaining (shutdown_ctx->timeout),
-      &service_shutdown_cancel, shutdown_ctx);
-  msg = (struct GNUNET_MessageHeader *) buf;
-  msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
-  msg->size = htons (sizeof (struct GNUNET_MessageHeader));
-  strcpy ((char *) &msg[1], SERVICE);
-  LOG ("Sent a shutdown request\n");
-  return sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1;
-}
-
-
-/**
- * Request that the service should shutdown.
- * Afterwards, the connection will automatically be
- * disconnected.  Hence the "sock" should not
- * be used by the caller after this call
- * (calling this function frees "sock" after a while).
- *
- * @param sock the socket connected to the service
- * @param timeout how long to wait before giving up on transmission
- * @param cont continuation to call once the service is really down
- * @param cont_cls closure for continuation
- *
- */
 static void
-do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
-                             struct GNUNET_TIME_Relative timeout,
-                             GNUNET_CLIENT_ShutdownTask cont,
-                             void *cont_cls)
-{
-  struct ShutdownContext *shutdown_ctx;
-
-  shutdown_ctx = GNUNET_new (struct ShutdownContext);
-  shutdown_ctx->cont = cont;
-  shutdown_ctx->cont_cls = cont_cls;
-  shutdown_ctx->sock = sock;
-  shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  GNUNET_CLIENT_notify_transmit_ready (sock,
-                                      sizeof (struct GNUNET_MessageHeader) + 
strlen (SERVICE) + 1,
-                                      timeout, GNUNET_NO, &write_shutdown,
-                                      shutdown_ctx);
-}
-
-
-static void
-kill_task (void *cbData);
-
-
-static void
-shutdown_cont (void *cls, int reason)
-{
-  if (GNUNET_NO != reason)
-  {
-    /* Re-try shutdown */
-    LOG ("do-nothing didn't die, trying again\n");
-    GNUNET_SCHEDULER_add_now (&kill_task, NULL);
-    return;
-  }
-  startedWaitingAt = GNUNET_TIME_absolute_get ();
-  LOG ("do-nothing is dead, starting the countdown\n");
-}
-
-
-static void
 kill_task (void *cbData)
 {
-  static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
+  struct ShutdownContext *shutdown_ctx
+    = GNUNET_new (struct ShutdownContext);
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *msg;
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_handler_end ()
+  };
 
-  if (NULL != cbData)
+  kt = NULL;
+  if (trialCount == 13)
   {
-    waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
-    LOG ("Waited for: %s\n",
-        GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
-  }
-  else
-  {
-    waitedFor.rel_value_us = 0;
-  }
-  /* Connect to the doNothing task */
-  doNothingConnection = GNUNET_CLIENT_connect (SERVICE, cfg);
-  GNUNET_assert (doNothingConnection != NULL);
-  if (trialCount == 12)
-    waitedFor_prev = waitedFor;
-  else if (trialCount == 13)
-  {
-    GNUNET_CLIENT_disconnect (doNothingConnection);
+    LOG ("Saw enough kills, asking ARM to stop mock service for good\n");
     GNUNET_ARM_request_service_stop (arm,
                                      SERVICE,
                                      NULL,
                                      NULL);
-    if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
-      ok = 9;
-    else
-      ok = 0;
-    trialCount += 1;
+    ok = 0;
+    trialCount++;
+    GNUNET_free (shutdown_ctx);
     return;
   }
-  trialCount += 1;
-  /* Use the created connection to kill the doNothingTask */
-  do_nothing_service_shutdown (doNothingConnection,
-      TIMEOUT, &shutdown_cont, NULL);
+  shutdown_ctx->mq = GNUNET_CLIENT_connecT (cfg,
+                                            SERVICE,
+                                            handlers,
+                                            &mq_error_handler,
+                                            shutdown_ctx);
+  GNUNET_assert (NULL != shutdown_ctx->mq);
+  trialCount++;
+  LOG ("Sending a shutdown request to the mock service\n");
+  env = GNUNET_MQ_msg (msg,
+                       GNUNET_MESSAGE_TYPE_ARM_STOP); /* FIXME: abuse of 
message type */
+  GNUNET_MQ_send (shutdown_ctx->mq,
+                  env);
+  shutdown_ctx->cancel_task
+    = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                    &service_shutdown_timeout,
+                                    shutdown_ctx);
 }
 
 
@@ -294,6 +175,11 @@
 {
   GNUNET_ARM_disconnect (arm);
   GNUNET_ARM_monitor_stop (mon);
+  if (NULL != kt)
+  {
+    GNUNET_SCHEDULER_cancel (kt);
+    kt = NULL;
+  }
 }
 
 
@@ -305,7 +191,7 @@
   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
   LOG ("ARM service stopped\n");
-  GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -314,9 +200,9 @@
             const char *service,
             enum GNUNET_ARM_ServiceStatus status)
 {
-  LOG ("Service %s is %u, phase %u\n", service, status, phase);
   if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
   {
+    LOG ("ARM monitor started, starting mock service\n");
     phase++;
     GNUNET_ARM_request_service_start (arm,
                                       SERVICE,
@@ -325,19 +211,22 @@
                                       NULL);
     return;
   }
+  if (0 != strcasecmp (service, SERVICE))
+    return; /* not what we care about */
   if (phase == 1)
   {
     GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
-    GNUNET_break (0 == strcasecmp (service, SERVICE));
     GNUNET_break (phase == 1);
     LOG ("do-nothing is starting\n");
     phase++;
     ok = 1;
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                 &kill_task,
-                                  NULL);
+    GNUNET_assert (NULL == kt);
+    startedWaitingAt = GNUNET_TIME_absolute_get ();
+    kt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                       &kill_task,
+                                       NULL);
   }
-  else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
+  else if (phase == 2)
   {
     /* We passively monitor ARM for status updates. ARM should tell us
      * when do-nothing dies (no need to run a service upness test ourselves).
@@ -344,12 +233,20 @@
      */
     if (status == GNUNET_ARM_SERVICE_STARTING)
     {
-      LOG ("do-nothing is starting\n");
-      GNUNET_SCHEDULER_add_now (&kill_task, &ok);
+      waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
+      LOG ("Waited for: %s\n",
+           GNUNET_STRINGS_relative_time_to_string (waitedFor,
+                                                   GNUNET_YES));
+
+      LOG ("do-nothing is starting, killing it...\n");
+      GNUNET_assert (NULL == kt);
+      kt = GNUNET_SCHEDULER_add_now (&kill_task, &ok);
     }
     else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
     {
       phase++;
+      LOG ("do-nothing stopped working %u times, we are done here\n",
+           (unsigned int) trialCount);
       GNUNET_ARM_request_service_stop (arm,
                                        "arm",
                                        &arm_stop_cb,
@@ -380,30 +277,28 @@
 {
   cfg = c;
   arm = GNUNET_ARM_connect (cfg, NULL, NULL);
-  if (NULL != arm)
+  if (NULL == arm)
   {
-    mon = GNUNET_ARM_monitor_start (cfg, &srv_status, NULL);
-    if (NULL != mon)
-    {
-#if START_ARM
-      GNUNET_ARM_request_service_start (arm,
-                                        "arm",
-                                        GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
-                                        &arm_start_cb,
-                                        NULL);
-#else
-      arm_start_cb (NULL,
-                    arm,
-                    GNUNET_ARM_REQUEST_SENT_OK,
-                    GNUNET_ARM_SERVICE_STARTING);
-#endif
-    }
-    else
-    {
-      GNUNET_ARM_disconnect (arm);
-      arm = NULL;
-    }
+    GNUNET_break (0);
+    return;
   }
+  mon = GNUNET_ARM_monitor_start (cfg,
+                                  &srv_status,
+                                  NULL);
+  if (NULL == mon)
+  {
+    GNUNET_break (0);
+    GNUNET_ARM_disconnect (arm);
+    arm = NULL;
+    return;
+  }
+  GNUNET_ARM_request_service_start (arm,
+                                    "arm",
+                                    GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                                    &arm_start_cb,
+                                    NULL);
+  GNUNET_SCHEDULER_add_shutdown (&trigger_disconnect,
+                                 NULL);
 }
 
 
@@ -422,10 +317,12 @@
   /* Running ARM  and running the do_nothing task */
   GNUNET_assert (GNUNET_OK ==
                 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
-                                    argv, "test-exponential-backoff",
-                                    "nohelp", options, &task, NULL));
-
-
+                                    argv,
+                                     "test-exponential-backoff",
+                                    "nohelp",
+                                     options,
+                                     &task,
+                                     NULL));
   return ok;
 }
 
@@ -451,10 +348,17 @@
     return GNUNET_SYSERR;
   if (NULL == getcwd (pwd, PATH_MAX))
     return GNUNET_SYSERR;
-  GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
-  GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
+  GNUNET_assert (0 < GNUNET_asprintf (&binary,
+                                      "%s/%s",
+                                      pwd,
+                                      BINARY));
+  GNUNET_CONFIGURATION_set_value_string (cfg,
+                                         SERVICE,
+                                         "BINARY",
+                                         binary);
   GNUNET_free (binary);
-  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
+  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg,
+                                               CFGFILENAME))
   {
     GNUNET_CONFIGURATION_destroy (cfg);
     return GNUNET_SYSERR;
@@ -463,9 +367,11 @@
 
 #if LOG_BACKOFF
   killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
-  if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
+  if (NULL == (killLogFilePtr = FOPEN (killLogFileName,
+                                       "w")))
     {
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen",
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                "fopen",
                                killLogFileName);
       GNUNET_free (killLogFileName);
       return GNUNET_SYSERR;




reply via email to

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