fixes for controller_start() API changes
authorSree Harsha Totakura <totakura@in.tum.de>
Wed, 11 Jul 2012 15:06:03 +0000 (15:06 +0000)
committerSree Harsha Totakura <totakura@in.tum.de>
Wed, 11 Jul 2012 15:06:03 +0000 (15:06 +0000)
src/include/gnunet_testbed_service.h
src/testbed/Makefile.am
src/testbed/gnunet-service-testbed.c
src/testbed/test_testbed_api.c
src/testbed/test_testbed_api_hosts.c
src/testbed/testbed_api.c

index 46be98b79c2c398409879efc3524bfe1c4bbbd3a..736a0c03c5da81b907da4343e227c079f3626f37 100644 (file)
@@ -29,6 +29,7 @@
 #define GNUNET_TESTBED_SERVICE_H
 
 #include "gnunet_util_lib.h"
+#include "gnunet_testing_lib-new.h"
 
 #ifdef __cplusplus
 extern "C"
index a4e1b2a01b90c8ec0f46a506847b94c566094db4..b6e42276ee29171a006897d5d703243db09abd15 100644 (file)
@@ -46,6 +46,7 @@ libgnunettestbed_la_LIBADD = $(XLIB) \
  $(top_builddir)/src/hello/libgnunethello.la \
  -lm \
  $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la \
  $(LTLIBINTL)
 libgnunettestbed_la_LDFLAGS = \
  $(GN_LIB_LDFLAGS) \
index 9ba145abed8fc77ad007852a066ff9c8217d0611..0619a67cb77ccb3ed59a5451f9c8ac155b2098fc 100644 (file)
@@ -385,6 +385,11 @@ static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
  */
 static struct GNUNET_TESTING_System *test_system;
 
+/**
+ * Our configuration; we also use this as template for starting other controllers
+ */
+static struct GNUNET_CONFIGURATION_Handle *config;
+
 
 /**
  * Function called to notify a client about the connection begin ready to queue
@@ -692,6 +697,27 @@ slave_event_callback(void *cls,
 }
 
 
+/**
+ * Callback for unexpected slave shutdowns
+ *
+ * @param cls closure
+ * @param emsg error message if available; can be NULL, which does NOT mean
+ *             that there was no error
+ */
+static void 
+slave_shutdown_handler (void *cls, const char *emsg)
+{
+  struct Slave *slave;
+
+  slave = (struct Slave *) cls;
+  slave->controller_proc = NULL;
+  LOG (GNUNET_ERROR_TYPE_WARNING,
+       "Unexpected slave shutdown\n");
+  if (NULL != emsg)
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Error: %s\n", emsg);
+  GNUNET_SCHEDULER_shutdown ();        /* We too shutdown */
+}
+
 
 /**
  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
@@ -1011,7 +1037,10 @@ handle_link_controllers (void *cls,
     if (1 == msg->is_subordinate)
     {
       slave->controller_proc =
-        GNUNET_TESTBED_controller_start (host_list[delegated_host_id]);
+        GNUNET_TESTBED_controller_start (test_system,
+                                        host_list[delegated_host_id],
+                                        cfg, &slave_shutdown_handler,
+                                        slave);
     }
     slave->controller =
       GNUNET_TESTBED_controller_connect (cfg, host_list[delegated_host_id],
@@ -1378,13 +1407,14 @@ testbed_run (void *cls,
       {NULL}
     };
 
+  config = GNUNET_CONFIGURATION_dup (cfg);
   GNUNET_SERVER_add_handlers (server,
                               message_handlers);
   GNUNET_SERVER_disconnect_notify (server,
                                    &client_disconnect_cb,
                                    NULL);
   ss_map = GNUNET_CONTAINER_multihashmap_create (5);
-  test_system = GNUNET_TESTING_system_create ("testbed_peers", NULL);
+  test_system = GNUNET_TESTING_system_create ("testbed", NULL);
   
   fh = GNUNET_DISK_get_handle_from_native (stdin);
   if (NULL == fh)
index 1fdb7169a58782d551bc80d3db7d901c5f68dd0d..849f5ddd435f1beb693389e314c1c631af4d9d1c 100644 (file)
 #define TIME_REL_SECS(sec) \
   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
 
+/**
+ * The testing system we work with
+ */
+struct GNUNET_TESTING_System *test_system;
+
 /**
  * Our localhost
  */
@@ -75,7 +80,7 @@ static struct GNUNET_TESTBED_Peer *peer;
 /**
  * Handle to configuration
  */
-static const struct GNUNET_CONFIGURATION_Handle *cfg;
+static struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
  * Handle to operation
@@ -107,10 +112,12 @@ do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (NULL != reg_handle)
     GNUNET_TESTBED_cancel_registration (reg_handle);
   GNUNET_TESTBED_controller_disconnect (controller);
+  GNUNET_CONFIGURATION_destroy (cfg);
   if (NULL != cp)
     GNUNET_TESTBED_controller_stop (cp);
   GNUNET_TESTBED_host_destroy (neighbour);
   GNUNET_TESTBED_host_destroy (host);
+  GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
 }
 
 
@@ -182,19 +189,19 @@ run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *config)
 {
   uint64_t event_mask;
-  struct GNUNET_CONFIGURATION_Handle *cdup;
 
-  cfg = config;
+  test_system = GNUNET_TESTING_system_create ("test_testbed", 
+                                             "127.0.0.1");
   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
   GNUNET_assert (NULL != host);
-  cdup = GNUNET_CONFIGURATION_dup (config);
-  cp = GNUNET_TESTBED_controller_start (system, host, cdup, NULL, NULL);
+  cfg = GNUNET_CONFIGURATION_dup (config);
+  cp = GNUNET_TESTBED_controller_start (test_system, host, cfg, NULL, NULL);
   event_mask = 0;
   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
-  controller = GNUNET_TESTBED_controller_connect (config, host, event_mask,
+  controller = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
                                                   &controller_cb, NULL);
   GNUNET_assert (NULL != controller);
   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
index 2528e7aff1a5914772092536e65cfdb22c6fdd9c..340e2491a6f7f7abcf2e6865aace086d83c86102 100644 (file)
@@ -70,7 +70,8 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  char *const binary_args[] = {NULL};
+  char *const binary_args[] = {"gnunet-service-testbed",
+                              NULL};
 
   host = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
   GNUNET_assert (NULL != host);
@@ -82,7 +83,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
   helper_handle = GNUNET_TESTBED_host_run_ (host, binary_args);
   GNUNET_assert (NULL != helper_handle);
-  GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (2), &do_shutdown, NULL);
+  GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (1), &do_shutdown, NULL);
 }
 
 
index dfec11f94a823ac5ada1e5f0409626ab6c22d392..5070d4f29c19b1667e129c91da56ccc6add9b434 100644 (file)
@@ -329,6 +329,7 @@ message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
   }
 }
 
+
 /**
  * Function called to notify a client about the connection begin ready to queue
  * more data.  "buf" will be NULL and "size" zero if the connection was closed
@@ -432,26 +433,86 @@ struct GNUNET_TESTBED_ControllerProc
 };
 
 
+/**
+ * Context to use while starting a controller
+ */
+struct ControllerStartContext
+{
+  /**
+   * The error callback
+   */
+  GNUNET_TESTBED_ControllerErrorCallback cec;
+
+  /**
+   * Closure for the above callback
+   */
+  void *cec_cls;
+
+  /**
+   * error message if any
+   */
+  char *emsg;
+};
+
+
 /**
  * Starts a controller process at the host
  *
+ * @param system used for reserving ports if host is NULL and to determine
+ *               which 'host' to set as TRUSTED ('controller') when starting testbed remotely
  * @param host the host where the controller has to be started; NULL for localhost
- * @return the controller process handle
+ * @param cfg template configuration to use for the remote controller; will
+ *            be modified to contain the actual host/port/unixpath used for
+ *            the testbed service
+ * @param cec function called if the contoller dies unexpectedly; will not be 
+ *            invoked after GNUNET_TESTBED_controller_stop, if 'cec' was called,
+ *            GNUNET_TESTBED_controller_stop must no longer be called; will
+ *            never be called in the same task as 'GNUNET_TESTBED_controller_start'
+ *            (synchronous errors will be signalled by returning NULL)
+ * @param cec_cls closure for 'cec'
+ * @return the controller process handle, NULL on errors
  */
 struct GNUNET_TESTBED_ControllerProc *
-GNUNET_TESTBED_controller_start (struct GNUNET_TESTBED_Host *host)
+GNUNET_TESTBED_controller_start (struct GNUNET_TESTING_System *system,
+                                struct GNUNET_TESTBED_Host *host,
+                                struct GNUNET_CONFIGURATION_Handle *cfg,
+                                GNUNET_TESTBED_ControllerErrorCallback cec,
+                                void *cec_cls)
 {
   struct GNUNET_TESTBED_ControllerProc *cproc;
-  char * const binary_argv[] = {
-    "gnunet-service-testbed",
-    NULL
-  };
-
-  cproc = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
-  cproc->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
-  if (NULL == cproc->helper)
+  //struct ControllerStartContext *csc;
+  char *cfg_filename;
+
+  if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
+  {
+    if (GNUNET_OK != GNUNET_TESTING_configuration_create (system, cfg))
+      return NULL;
+    GNUNET_assert 
+      (GNUNET_OK == 
+       GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "DEFAULTCONFIG",
+                                             &cfg_filename));
+    if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, cfg_filename))
+    {
+      GNUNET_break (0);
+      return NULL;
+    }
+    char * const binary_argv[] = {
+      "gnunet-service-testbed",
+      "-c", cfg_filename,
+      NULL
+    };
+    cproc = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
+    cproc->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
+    GNUNET_free (cfg_filename);
+    if (NULL == cproc->helper)
+    {
+      GNUNET_free (cproc);
+      return NULL;
+    }
+  }
+  else
   {
-    GNUNET_free (cproc);
+    GNUNET_break (0);
     return NULL;
   }
   return cproc;