Smallers steps to keep plugin running
[oweals/gnunet.git] / src / arm / arm_api.c
index f15c9ab535ddbe70b2baa63f388f03c0417f0628..66747a624acde3d7190926e1044ad00df255301d 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2010 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
 #include "arm.h"
 
 
-/**
- * How long are we willing to wait for a service operation during the multi-operation
- * request processing?
- */
-#define MULTI_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
-
-
 /**
  * Handle for interacting with ARM.
  */
@@ -121,21 +114,23 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
 {
   struct ShutdownContext *shutdown_ctx = cls;
 
-  if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))   
+  if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
     {
       /* Means the other side closed the connection and never confirmed a shutdown */
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Service handle shutdown before ACK!\n");
       if (shutdown_ctx->cont != NULL)
-        shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);      
+        shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
       GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
       GNUNET_free(shutdown_ctx);
     }
   else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
+#if DEBUG_ARM
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                 "Service shutdown complete.\n");
+#endif
       if (shutdown_ctx->cont != NULL)
         shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
 
@@ -149,20 +144,24 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
       switch (ntohs(msg->type))
        {
        case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
-         GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+#if DEBUG_ARM
+         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                     "Received confirmation for service shutdown.\n");
+#endif
          shutdown_ctx->confirmed = GNUNET_YES;
-         GNUNET_CLIENT_receive (shutdown_ctx->sock, 
-                                &service_shutdown_handler, 
-                                shutdown_ctx, 
+         GNUNET_CLIENT_receive (shutdown_ctx->sock,
+                                &service_shutdown_handler,
+                                shutdown_ctx,
                                 GNUNET_TIME_UNIT_FOREVER_REL);
          break;
        default: /* Fall through */
-         GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
+#if DEBUG_ARM
+         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                     "Service shutdown refused!\n");
+#endif
          if (shutdown_ctx->cont != NULL)
            shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
-         
+
          GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
          GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
          GNUNET_free(shutdown_ctx);
@@ -214,11 +213,11 @@ write_shutdown (void *cls, size_t size, void *buf)
     }
 
   GNUNET_CLIENT_receive (shutdown_ctx->sock,
-                        &service_shutdown_handler, shutdown_ctx, 
+                        &service_shutdown_handler, shutdown_ctx,
                         GNUNET_TIME_UNIT_FOREVER_REL);
-  shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched, 
-                                                           GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), 
-                                                           &service_shutdown_cancel, 
+  shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched,
+                                                           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_SHUTDOWN);
@@ -259,7 +258,7 @@ arm_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched,
                                       sizeof (struct
                                               GNUNET_MessageHeader),
                                       timeout,
-                                      GNUNET_NO,
+                                      GNUNET_YES,
                                       &write_shutdown, shutdown_ctx);
 }
 
@@ -435,15 +434,42 @@ arm_service_report (void *cls,
       GNUNET_free (lopostfix);
       return;
     }
-  pid = do_start_process (loprefix,
-                         binary,
-                         "-c", config,
+  if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
+                                                      "TESTING",
+                                                      "WEAKRANDOM")) &&
+      (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg,
+                                                           "TESTING",
+                                                           "WEAKRANDOM")) &&
+      (GNUNET_NO == GNUNET_CONFIGURATION_have_value (pos->h->cfg,
+                                                     "TESTING",
+                                                     "HOSTFILE")) /* Means we are ONLY running locally */
+                                                           )
+    {
+      /* we're clearly running a test, don't daemonize */
+      pid = do_start_process (NULL,
+                             loprefix,
+                             binary,
+                             "-c", config,
 #if DEBUG_ARM
-                         "-L", "DEBUG",
+                             "-L", "DEBUG",
 #endif
-                         "-d",
-                         lopostfix,
-                         NULL);
+                             /* no daemonization! */
+                             lopostfix,
+                             NULL);
+    }
+  else
+    {
+      pid = do_start_process (NULL,
+                             loprefix,
+                             binary,
+                             "-c", config,
+#if DEBUG_ARM
+                             "-L", "DEBUG",
+#endif
+                             "-d",
+                             lopostfix,
+                             NULL);
+    }
   GNUNET_free (binary);
   GNUNET_free (config);
   GNUNET_free (loprefix);
@@ -541,8 +567,7 @@ change_service (struct GNUNET_ARM_Handle *h,
   struct GNUNET_MessageHeader *msg;
 
   slen = strlen (service_name) + 1;
-  if (slen + sizeof (struct GNUNET_MessageHeader) >
-      GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  if (slen + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
     {
       GNUNET_break (0);
       if (cb != NULL)
@@ -706,171 +731,4 @@ GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
 }
 
 
-/**
- * Function to call for each service.
- *
- * @param h handle to ARM
- * @param service_name name of the service
- * @param timeout how long to wait before failing for good
- * @param cb callback to invoke when service is ready
- * @param cb_cls closure for callback
- */
-typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
-                                 const char *service_name,
-                                 struct GNUNET_TIME_Relative timeout,
-                                 GNUNET_ARM_Callback cb, void *cb_cls);
-
-
-/**
- * Context for starting or stopping multiple services.
- */
-struct MultiContext
-{
-  /**
-   * NULL-terminated array of services to start or stop.
-   */
-  char **services;
-
-  /**
-   * Our handle to ARM.
-   */
-  struct GNUNET_ARM_Handle *h;
-
-  /**
-   * Identifies the operation (start or stop).
-   */
-  ServiceOperation op;
-
-  /**
-   * Current position in "services".
-   */
-  unsigned int pos;
-};
-
-
-/**
- * Run the operation for the next service in the multi-service
- * request.
- *
- * @param cls the "struct MultiContext" that is being processed
- * @param success status of the previous operation (ignored)
- */
-static void
-next_operation (void *cls,
-               int success)
-{
-  struct MultiContext *mc = cls;
-  char *pos;
-
-  if (NULL == (pos = mc->services[mc->pos]))
-    {
-      GNUNET_free (mc->services);
-      GNUNET_ARM_disconnect (mc->h);
-      GNUNET_free (mc);
-      return;
-    }
-  mc->pos++;
-  mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
-  GNUNET_free (pos);
-}
-
-
-/**
- * Run a multi-service request.
- *
- * @param cfg configuration to use (needed to contact ARM;
- *        the ARM service may internally use a different
- *        configuration to determine how to start the service).
- * @param sched scheduler to use
- * @param op the operation to perform for each service
- * @param va NULL-terminated list of services
- */
-static void
-run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                  struct GNUNET_SCHEDULER_Handle *sched,
-                  ServiceOperation op,
-                  va_list va)
-{
-  va_list cp;
-  unsigned int total;
-  struct MultiContext *mc;
-  struct GNUNET_ARM_Handle *h;
-  const char *c;
-
-  h = GNUNET_ARM_connect (cfg, sched, NULL);
-  if (NULL == h)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Error while trying to transmit to ARM service\n"));
-      return;
-    }
-  total = 1;
-  va_copy (cp, va);
-  while (NULL != (va_arg (cp, const char*))) total++;
-  va_end (cp);
-  mc = GNUNET_malloc (sizeof(struct MultiContext));
-  mc->services = GNUNET_malloc (total * sizeof (char*));
-  mc->h = h;
-  mc->op = op;
-  total = 0;
-  va_copy (cp, va);
-  while (NULL != (c = va_arg (cp, const char*)))
-    mc->services[total++] = GNUNET_strdup (c);
-  va_end (cp);
-  next_operation (mc, GNUNET_YES);
-}
-
-
-/**
- * Start multiple services in the specified order.  Convenience
- * function.  Works asynchronously, failures are not reported.
- *
- * @param cfg configuration to use (needed to contact ARM;
- *        the ARM service may internally use a different
- *        configuration to determine how to start the service).
- * @param sched scheduler to use
- * @param ... NULL-terminated list of service names (const char*)
- */
-void
-GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                          struct GNUNET_SCHEDULER_Handle *sched,
-                          ...)
-{
-  va_list ap;
-
-  va_start (ap, sched);
-  run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
-  va_end (ap);
-}
-
-
-/**
- * Stop multiple services in the specified order.  Convenience
- * function.  Works asynchronously, failures are not reported.
- * Should normally only be called from gnunet-arm or testcases,
- * stopping a service is generally otherwise a bad idea.
- *
- * @param cfg configuration to use (needed to contact ARM;
- *        the ARM service may internally use a different
- *        configuration to determine how to start the service).
- * @param sched scheduler to use
- * @param ... NULL-terminated list of service names (const char*)
- */
-void
-GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                         struct GNUNET_SCHEDULER_Handle *sched,
-                         ...)
-{
-  va_list ap;
-
-  va_start (ap, sched);
-  run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
-  va_end (ap);
-}
-
-
-
-
-
-
 /* end of arm_api.c */