-more datacache integration work
[oweals/gnunet.git] / src / testbed / testbed_api_test.c
index 1d49e706d8e79944a1aa612127bd166c6adb8d4b..60fd87b286ff839417963785cdfd16f075d033d2 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2012 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2008--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
@@ -44,57 +44,25 @@ struct TestRunContext
   void *test_master_cls;
 
   /**
-   * Number of peers to start
+   * The controller event callback
    */
-  unsigned int num_peers;
+  GNUNET_TESTBED_ControllerCallback cc;
 
   /**
-   * counter for loading peers
+   * Closure for the above callback
    */
-  unsigned int peer_cnt;
+  void *cc_cls;
 
   /**
-   * Followed by peers list
+   * event mask for the controller callback
    */
-  struct GNUNET_TESTBED_Peer *peers[0];
-};
-
-
-/**
- * Controller event callback
- *
- * @param cls NULL
- * @param event the controller event
- */
-static void
-controller_event_cb (void *cls,
-                     const struct GNUNET_TESTBED_EventInformation *event)
-{
-  struct TestRunContext *rc = cls;
-
-  if (rc->peer_cnt == rc->num_peers)
-    return;
-  GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
-  GNUNET_assert (NULL == rc->peers[rc->peer_cnt]);
-  GNUNET_assert (NULL != event->details.peer_start.peer);
-  rc->peers[rc->peer_cnt++] = event->details.peer_start.peer;
-}
-
+  uint64_t event_mask;
 
-/**
- * Task to be executed when peers are ready
- *
- * @param cls NULL
- * @param tc the task context
- */
-static void
-master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct TestRunContext *rc = cls;
-
-  GNUNET_assert (rc->peer_cnt == rc->num_peers);
-  rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers);
-}
+  /**
+   * Number of peers to start
+   */
+  unsigned int num_peers;
+};
 
 
 /**
@@ -103,7 +71,7 @@ master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cls NULL
  * @param args arguments passed to GNUNET_PROGRAM_run
  * @param cfgfile the path to configuration file
- * @param cfg the configuration file handle
+ * @param config the configuration file handle
  */
 static void
 run (void *cls, char *const *args, const char *cfgfile,
@@ -111,8 +79,8 @@ run (void *cls, char *const *args, const char *cfgfile,
 {
   struct TestRunContext *rc = cls;
 
-  GNUNET_TESTBED_run (NULL, config, rc->num_peers, 0, &controller_event_cb, rc,
-                      &master_task, rc);
+  GNUNET_TESTBED_run (NULL, config, rc->num_peers, rc->event_mask, rc->cc,
+                      rc->cc_cls, rc->test_master, rc->test_master_cls);
 }
 
 
@@ -136,12 +104,23 @@ run (void *cls, char *const *args, const char *cfgfile,
  * @param cfg_filename configuration filename to use
  *              (for testbed, controller and peers)
  * @param num_peers number of peers to start
+ * @param event_mask bit mask with set of events to call 'cc' for;
+ *                   or-ed values of "1LL" shifted by the
+ *                   respective 'enum GNUNET_TESTBED_EventType'
+ *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
+ * @param cc controller callback to invoke on events; This callback is called
+ *        for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
+ *        set in the event_mask as this is the only way get access to the
+ *        handle of each peer
+ * @param cc_cls closure for cc
  * @param test_master task to run once the test is ready
  * @param test_master_cls closure for 'task'.
+ * @return GNUNET_SYSERR on error, GNUNET_OK on success
  */
-void
+int
 GNUNET_TESTBED_test_run (const char *testname, const char *cfg_filename,
-                         unsigned int num_peers,
+                         unsigned int num_peers, uint64_t event_mask,
+                         GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
                          GNUNET_TESTBED_TestMaster test_master,
                          void *test_master_cls)
 {
@@ -155,20 +134,27 @@ GNUNET_TESTBED_test_run (const char *testname, const char *cfg_filename,
     GNUNET_GETOPT_OPTION_END
   };
   struct TestRunContext *rc;
+  int ret;
 
   argv2[0] = GNUNET_strdup (testname);
   argv2[2] = GNUNET_strdup (cfg_filename);
   GNUNET_assert (NULL != test_master);
+  GNUNET_assert (num_peers > 0);
   rc = GNUNET_malloc (sizeof (struct TestRunContext) +
                       (num_peers * sizeof (struct GNUNET_TESTBED_Peer *)));
   rc->test_master = test_master;
   rc->test_master_cls = test_master_cls;
   rc->num_peers = num_peers;
-  (void) GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
-                             testname, "nohelp", options, &run, rc);
+  rc->event_mask = event_mask;
+  rc->cc = cc;
+  rc->cc_cls = cc_cls;
+  ret =
+      GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+                          testname, "nohelp", options, &run, rc);
   GNUNET_free (rc);
   GNUNET_free (argv2[0]);
   GNUNET_free (argv2[2]);
+  return ret;
 }
 
 /* end of testbed_api_test.c */