+static void
+remove_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct RunContextOperation *rcop)
+{
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
+ rcop_key (rcop->op),
+ rcop));
+}
+
+/**
+ * Assuming all peers have been destroyed cleanup run handle
+ *
+ * @param rc the run context
+ */
+static void
+cleanup (struct GNUNET_TESTBED_RunHandle *rc)
+{
+ unsigned int hid;
+
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
+ GNUNET_assert (NULL == rc->reg_handle);
+ GNUNET_assert (NULL == rc->peers);
+ GNUNET_assert (NULL == rc->hclist);
+ GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
+ GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
+ GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
+ if (NULL != rc->c)
+ GNUNET_TESTBED_controller_disconnect (rc->c);
+ if (NULL != rc->cproc)
+ GNUNET_TESTBED_controller_stop (rc->cproc);
+ if (NULL != rc->h)
+ GNUNET_TESTBED_host_destroy (rc->h);
+ for (hid = 0; hid < rc->num_hosts; hid++)
+ GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
+ GNUNET_free_non_null (rc->hosts);
+ if (NULL != rc->cfg)
+ GNUNET_CONFIGURATION_destroy (rc->cfg);
+ GNUNET_free_non_null (rc->topo_file);
+ GNUNET_free_non_null (rc->trusted_ip);
+ GNUNET_free (rc);
+}
+
+
+/**
+ * Iterator for cleaning up elements from rcop_map
+ *
+ * @param cls the RunContext
+ * @param key the 32-bit key
+ * @param value the RunContextOperation element
+ * @return always GNUNET_YES
+ */
+static int
+rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
+{
+ struct GNUNET_TESTBED_RunHandle *rc = cls;
+ struct RunContextOperation *rcop = value;
+
+ GNUNET_assert (rc == rcop->rc);
+ remove_rcop (rc, rcop);
+ GNUNET_TESTBED_operation_done (rcop->op);
+ GNUNET_free (rcop);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Cancels operations and tasks which are assigned to the given run context
+ *
+ * @param rc the RunContext
+ */
+static void
+rc_cleanup_operations (struct GNUNET_TESTBED_RunHandle *rc)
+{
+ struct CompatibilityCheckContext *hc;
+ unsigned int nhost;
+
+ if (NULL != rc->hclist)
+ {
+ for (nhost = 0; nhost < rc->num_hosts; nhost++)
+ {
+ hc = &rc->hclist[nhost];
+ if (NULL != hc->h)
+ GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
+ }
+ GNUNET_free (rc->hclist);
+ rc->hclist = NULL;
+ }
+ /* Stop register hosts task if it is running */
+ if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
+ {
+ GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
+ rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (rc->timeout_task);
+ rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (NULL != rc->reg_handle)
+ {
+ GNUNET_TESTBED_cancel_registration (rc->reg_handle);
+ rc->reg_handle = NULL;
+ }
+ if (NULL != rc->topology_operation)
+ {
+ GNUNET_TESTBED_operation_done (rc->topology_operation);
+ rc->topology_operation = NULL;
+ }
+ /* cancel any exiting operations */
+ GNUNET_assert (GNUNET_SYSERR !=
+ GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
+ &rcop_cleanup_iterator,
+ rc));
+}
+
+
+/**
+ * Cancels the scheduled interrupt task
+ *
+ * @param rc the run context
+ */
+static void
+cancel_interrupt_task (struct GNUNET_TESTBED_RunHandle *rc)
+{
+ GNUNET_SCHEDULER_cancel (rc->interrupt_task);
+ rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
+}
+
+
+/**
+ * This callback will be called when all the operations are completed
+ * (done/cancelled)
+ *
+ * @param cls run context
+ */
+static void
+wait_op_completion (void *cls)
+{
+ struct GNUNET_TESTBED_RunHandle *rc = cls;
+ struct RunContextOperation *rcop;
+
+ if ( (NULL == rc->cproc)
+ || (NULL == rc->c)
+ || (GNUNET_YES == rc->shutdown) )
+ {
+ if (NULL != rc->peers)
+ {
+ GNUNET_free (rc->peers);
+ rc->peers = NULL;
+ }
+ goto cleanup_;
+ }
+ if (NULL == rc->peers)
+ goto cleanup_;
+ rc->shutdown = GNUNET_YES;
+ rcop = GNUNET_new (struct RunContextOperation);
+ rcop->rc = rc;
+ rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
+ GNUNET_assert (NULL != rcop->op);
+ DEBUG ("Shutting down peers\n");
+ rc->pstart_time = GNUNET_TIME_absolute_get ();
+ insert_rcop (rc, rcop);
+ return;
+
+ cleanup_:
+ rc->state = RC_PEERS_SHUTDOWN;
+ cancel_interrupt_task (rc);
+ cleanup (rc);
+}
+
+
+/**
+ * Task run upon interrupts (SIGINT, SIGTERM) and upon scheduler shutdown.
+ *
+ * @param cls the RunContext which has to be acted upon
+ * @param tc the scheduler task context
+ */
+static void
+interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)