-simplify
[oweals/gnunet.git] / src / consensus / gnunet-consensus.c
index c8a5593f17c26134c9738fe8025785815d7fcc3d..d8c1b14eee8173d739219d0307230f786a8ef18e 100644 (file)
@@ -39,6 +39,8 @@ static struct GNUNET_TIME_Relative conclude_timeout;
 
 static struct GNUNET_CONSENSUS_Handle **consensus_handles;
 
+static struct GNUNET_TESTBED_Operation **testbed_operations;
+
 static unsigned int num_connected_handles;
 
 static struct GNUNET_TESTBED_Peer **peers;
@@ -49,6 +51,8 @@ static unsigned int num_retrieved_peer_ids;
 
 static struct GNUNET_HashCode session_id;
 
+static unsigned int peers_done = 0;
+
 
 /**
  * Signature of the event handler function called by the
@@ -64,22 +68,37 @@ controller_cb(void *cls,
   GNUNET_assert (0);
 }
 
+static void
+destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *ctx)
+{
+  struct GNUNET_CONSENSUS_Handle *consensus;
+  consensus = cls;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying consensus\n");
+  GNUNET_CONSENSUS_destroy (consensus);
+  peers_done++;
+  if (peers_done == num_peers)
+  {
+    int i;
+    for (i = 0; i < num_peers; i++)
+      GNUNET_TESTBED_operation_done (testbed_operations[i]);
+    GNUNET_SCHEDULER_shutdown ();
+  }
+}
+
 
 /**
  * Called when a conclusion was successful.
  *
- * @param cls
- * @param group
+ * @param cls closure, the consensus handle
  * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
  */
-static int
-conclude_cb (void *cls, const struct GNUNET_CONSENSUS_Group *group)
+static void
+conclude_cb (void *cls)
 {
-  return GNUNET_NO;
+  GNUNET_SCHEDULER_add_now (destroy, cls);
 }
 
 
-
 static void
 generate_indices (int *indices)
 {
@@ -133,7 +152,7 @@ do_consensus ()
   }
 
   for (i = 0; i < num_peers; i++)
-    GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, 0, conclude_cb, consensus_handles[i]);
+    GNUNET_CONSENSUS_conclude (consensus_handles[i], conclude_timeout, conclude_cb, consensus_handles[i]);
 }
 
 
@@ -173,11 +192,11 @@ connect_complete (void *cls,
 }
 
 
-static int
+static void
 new_element_cb (void *cls,
-                struct GNUNET_CONSENSUS_Element *element)
+                const struct GNUNET_CONSENSUS_Element *element)
 {
-  return GNUNET_YES;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received new element\n");
 }
 
 
@@ -245,8 +264,9 @@ peer_info_cb (void *cb_cls,
     num_retrieved_peer_ids++;
     if (num_retrieved_peer_ids == num_peers)
       for (i = 0; i < num_peers; i++)
-        GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, &consensus_handles[i],
-                                        connect_adapter, disconnect_adapter, NULL);
+        testbed_operations[i] =
+            GNUNET_TESTBED_service_connect (NULL, peers[i], "consensus", connect_complete, &consensus_handles[i],
+                                            connect_adapter, disconnect_adapter, NULL);
   }
   else
   {
@@ -255,21 +275,38 @@ peer_info_cb (void *cb_cls,
 }
 
 
+/**
+ * Signature of a main function for a testcase.
+ *
+ * @param cls closure
+ * @param num_peers number of peers in 'peers'
+ * @param started_peers handle to peers run in the testbed.  NULL upon timeout (see
+ *          GNUNET_TESTBED_test_run()).
+ * @param links_succeeded the number of overlay link connection attempts that
+ *          succeeded
+ * @param links_failed the number of overlay link connection attempts that
+ *          failed
+ */
 static void
 test_master (void *cls,
              unsigned int num_peers,
-             struct GNUNET_TESTBED_Peer **started_peers)
+             struct GNUNET_TESTBED_Peer **started_peers,
+             unsigned int links_succeeded,
+             unsigned int links_failed)
 {
   int i;
 
+  GNUNET_log_setup ("gnunet-consensus", "INFO", NULL);
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
 
+
   peers = started_peers;
 
   peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
 
   consensus_handles = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
+  testbed_operations = GNUNET_malloc (num_peers * sizeof (struct ConsensusHandle *));
 
   for (i = 0; i < num_peers; i++)
     GNUNET_TESTBED_peer_get_information (peers[i],
@@ -284,6 +321,11 @@ run (void *cls, char *const *args, const char *cfgfile,
 {
   static char *session_str = "gnunet-consensus/test";
 
+  if (num_peers < replication)
+  {
+    fprintf (stderr, "k must be <=n\n");
+    return;
+  }
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "running gnunet-consensus\n");