+/**
+ * Iterator for searching over the elements matching a given query
+ *
+ * @param cls the SearchContext
+ * @param key the 32-bit key
+ * @param value the RunContextOperation element
+ * @return GNUNET_YES to continue iteration; GNUNET_NO to cancel it
+ */
+static int
+search_iterator (void *cls, uint32_t key, void *value)
+{
+ struct RunContextOperation *rcop = value;
+ struct SearchContext *sc = cls;
+
+ GNUNET_assert (NULL != rcop);
+ if (sc->query == rcop->op)
+ {
+ GNUNET_assert (NULL == sc->result);
+ sc->result = rcop;
+ return GNUNET_NO;
+ }
+ return GNUNET_YES;
+}
+
+
+/**
+ * Initiate a search for the given operation in the rcop_map
+ *
+ * @param rc the RunContext whose rcop_map will be searched for the given
+ * operation
+ * @param op the given operation to search for
+ * @return the matching RunContextOperation if found; NULL if not
+ */
+static struct RunContextOperation *
+search_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct GNUNET_TESTBED_Operation *op)
+{
+ struct SearchContext sc;
+
+ sc.query = op;
+ sc.result = NULL;
+ if (GNUNET_SYSERR ==
+ GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
+ rcop_key (op),
+ &search_iterator,
+ &sc))
+ {
+ GNUNET_assert (NULL != sc.result);
+ return sc.result;
+ }
+ return NULL;
+}
+
+
+/**
+ * Insert an RunContextOperation into the rcop_map of the given RunContext
+ *
+ * @param rc the RunContext into whose map is to be used for insertion
+ * @param rcop the RunContextOperation to insert
+ */
+static void
+insert_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct RunContextOperation *rcop)
+{
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
+ rcop_key (rcop->op), rcop,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+}
+
+
+/**
+ * Remove a RunContextOperation from the rcop_map of the given RunContext
+ *
+ * @param rc the RunContext from whose map the given RunContextOperaton has to
+ * be removed
+ * @param rcop the RunContextOperation
+ */
+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)
+{
+ struct GNUNET_TESTBED_RunHandle *rc = cls;
+ struct GNUNET_TESTBED_Controller *c = rc->c;
+ unsigned int size;
+
+ /* reschedule */
+ rc->interrupt_task = GNUNET_SCHEDULER_add_delayed
+ (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt, rc);
+ rc_cleanup_operations (rc);
+ if ( (GNUNET_NO == rc->shutdown)
+ && (NULL != c)
+ && (0 != (size = GNUNET_CONTAINER_multihashmap32_size (c->opc_map))))
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING, "Shutdown postponed as there are "
+ "%u operations currently active\n", size);
+ c->opcq_empty_cb = &wait_op_completion;
+ c->opcq_empty_cls = rc;
+ return;
+ }
+ wait_op_completion (rc);
+}
+
+
+/**
+ * Function to return the string representation of the duration between current
+ * time and `pstart_time' in `RunContext'
+ *
+ * @param rc the RunContext
+ * @return the representation string; this is NOT reentrant
+ */
+static const char *
+prof_time (struct GNUNET_TESTBED_RunHandle *rc)
+{
+ struct GNUNET_TIME_Relative ptime;
+
+ ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
+ return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
+}
+
+