-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / arm / test_exponential_backoff.c
index bb30f22c648745242c8abfc5e65e8a6e2ad41d66..8135f51579c69f6a332f2d8f59f1579edb10f590 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2016 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file arm/test_exponential_backoff.c
@@ -42,7 +42,7 @@
 
 #define BINARY "mockup-service"
 
-#define CFGFILENAME "test_arm_api_data.conf"
+#define CFGFILENAME "test_arm_api_data2.conf"
 
 
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -70,7 +70,9 @@ static char *killLogFileName;
 #endif
 
 
-typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
+typedef void
+(*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
+
 
 /**
  * Context for handling the shutdown of a service.
@@ -90,7 +92,7 @@ struct ShutdownContext
   /**
    * Task set up to cancel the shutdown request on timeout.
    */
-  GNUNET_SCHEDULER_TaskIdentifier cancel_task;
+  struct GNUNET_SCHEDULER_Task *cancel_task;
 
   /**
    * Task to call once shutdown complete
@@ -118,16 +120,17 @@ struct ShutdownContext
  * @param msg NULL, indicating socket closure.
  */
 static void
-service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+service_shutdown_handler (void *cls,
+                          const struct GNUNET_MessageHeader *msg)
 {
   struct ShutdownContext *shutdown_ctx = cls;
 
-  if (NULL == msg) 
+  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);
@@ -141,15 +144,14 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
  * Shutting down took too long, cancel receive and return error.
  *
  * @param cls closure
- * @param tc context information (why was this task triggered now)
  */
-void
-service_shutdown_cancel (void *cls,
-                        const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+service_shutdown_cancel (void *cls)
 {
   struct ShutdownContext *shutdown_ctx = cls;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
+  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_free (shutdown_ctx);
@@ -209,12 +211,13 @@ write_shutdown (void *cls, size_t size, void *buf)
  */
 static void
 do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
-                     struct GNUNET_TIME_Relative timeout,
-                     GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
+                             struct GNUNET_TIME_Relative timeout,
+                             GNUNET_CLIENT_ShutdownTask cont,
+                             void *cont_cls)
 {
   struct ShutdownContext *shutdown_ctx;
 
-  shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
+  shutdown_ctx = GNUNET_new (struct ShutdownContext);
   shutdown_ctx->cont = cont;
   shutdown_ctx->cont_cls = cont_cls;
   shutdown_ctx->sock = sock;
@@ -227,7 +230,7 @@ do_nothing_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
 
 
 static void
-kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
+kill_task (void *cbData);
 
 
 static void
@@ -237,7 +240,7 @@ shutdown_cont (void *cls, int reason)
   {
     /* Re-try shutdown */
     LOG ("do-nothing didn't die, trying again\n");
-    GNUNET_SCHEDULER_add_now (kill_task, NULL);
+    GNUNET_SCHEDULER_add_now (&kill_task, NULL);
     return;
   }
   startedWaitingAt = GNUNET_TIME_absolute_get ();
@@ -246,14 +249,14 @@ shutdown_cont (void *cls, int reason)
 
 
 static void
-kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
+kill_task (void *cbData)
 {
   static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL;
 
   if (NULL != cbData)
   {
     waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
-    LOG ("Waited for: %s\n", 
+    LOG ("Waited for: %s\n",
         GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
   }
   else
@@ -268,7 +271,10 @@ kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
   else if (trialCount == 13)
   {
     GNUNET_CLIENT_disconnect (doNothingConnection);
-    GNUNET_ARM_request_service_stop (arm, SERVICE, TIMEOUT, NULL, NULL);
+    GNUNET_ARM_request_service_stop (arm,
+                                     SERVICE,
+                                     NULL,
+                                     NULL);
     if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
       ok = 9;
     else
@@ -284,32 +290,39 @@ kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 static void
-trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+trigger_disconnect (void *cls)
 {
-  GNUNET_ARM_disconnect_and_free (arm);
-  GNUNET_ARM_monitor_disconnect_and_free (mon);
+  GNUNET_ARM_disconnect (arm);
+  GNUNET_ARM_monitor_stop (mon);
 }
 
 
 static void
-arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
+arm_stop_cb (void *cls,
+            enum GNUNET_ARM_RequestStatus status,
+            enum GNUNET_ARM_Result result)
 {
   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_add_now (&trigger_disconnect, NULL);
 }
 
 
 static void
-srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
+srv_status (void *cls,
+            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)
   {
     phase++;
-    GNUNET_ARM_request_service_start (arm, SERVICE,
-        GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
+    GNUNET_ARM_request_service_start (arm,
+                                      SERVICE,
+                                      GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                                      NULL,
+                                      NULL);
     return;
   }
   if (phase == 1)
@@ -320,7 +333,9 @@ srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status
     LOG ("do-nothing is starting\n");
     phase++;
     ok = 1;
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                 &kill_task,
+                                  NULL);
   }
   else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
   {
@@ -330,63 +345,62 @@ srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status
     if (status == GNUNET_ARM_SERVICE_STARTING)
     {
       LOG ("do-nothing is starting\n");
-      GNUNET_SCHEDULER_add_now (kill_task, &ok);
+      GNUNET_SCHEDULER_add_now (&kill_task, &ok);
     }
     else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
     {
       phase++;
-      GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
+      GNUNET_ARM_request_service_stop (arm,
+                                       "arm",
+                                       &arm_stop_cb,
+                                       NULL);
     }
   }
 }
 
 
 static void
-arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
+arm_start_cb (void *cls,
+              enum GNUNET_ARM_RequestStatus status,
+              enum GNUNET_ARM_Result result)
 {
   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
   GNUNET_break (phase == 0);
-  LOG ("Sent 'START' request for arm to ARM %s\n", 
+  LOG ("Sent 'START' request for arm to ARM %s\n",
        (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
 }
 
 
 static void
-task (void *cls, char *const *args, const char *cfgfile,
+task (void *cls,
+      char *const *args,
+      const char *cfgfile,
       const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  char *armconfig;
   cfg = c;
-  if (NULL != cfgfile)
-  {
-    if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "arm",
-        "CONFIG", &armconfig))
-    {
-      GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
-                                              *) cfg, "arm", "CONFIG",
-                                             cfgfile);
-    }
-    else
-      GNUNET_free (armconfig);
-  }
-
   arm = GNUNET_ARM_connect (cfg, NULL, NULL);
   if (NULL != arm)
   {
-    mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
+    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, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
+      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, "arm", GNUNET_ARM_SERVICE_STARTING);
+      arm_start_cb (NULL,
+                    arm,
+                    GNUNET_ARM_REQUEST_SENT_OK,
+                    GNUNET_ARM_SERVICE_STARTING);
 #endif
     }
     else
     {
-      GNUNET_ARM_disconnect_and_free (arm);
+      GNUNET_ARM_disconnect (arm);
       arm = NULL;
     }
   }
@@ -416,6 +430,14 @@ check ()
 }
 
 
+#ifndef PATH_MAX
+/**
+ * Assumed maximum path length (for the log file name).
+ */
+#define PATH_MAX 4096
+#endif
+
+
 static int
 init ()
 {
@@ -431,7 +453,7 @@ init ()
     return GNUNET_SYSERR;
   GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
   GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
-  GNUNET_free (binary);  
+  GNUNET_free (binary);
   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
   {
     GNUNET_CONFIGURATION_destroy (cfg);