+/**
+ * Stats callback. Iterate over the hashmap and check if all th peers form
+ * a virtual ring topology.
+ *
+ * @param cls closure
+ * @param op the operation that has been finished
+ * @param emsg error message in case the operation has failed; will be NULL if
+ * operation has executed successfully.
+ */
+static void
+successor_stats_cont (void *cls,
+ struct GNUNET_TESTBED_Operation *op,
+ const char *emsg)
+{
+ struct GNUNET_HashCode *val;
+ struct GNUNET_HashCode *start_val;
+ struct GNUNET_HashCode *key;
+ int count;
+
+ /* Don't schedule the task till we are looking for circle here. */
+ successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_TESTBED_operation_done (successor_stats_op);
+ successor_stats_op = NULL;
+
+ start_val =
+ (struct GNUNET_HashCode *) GNUNET_CONTAINER_multihashmap_get(successor_peer_hashmap,
+ start_key);
+ val = GNUNET_new(struct GNUNET_HashCode);
+ key = GNUNET_new(struct GNUNET_HashCode);
+ val = start_val;
+ for (count = 0; count < num_peers; count++)
+ {
+ key = val;
+ val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
+ key);
+ GNUNET_assert(NULL != val);
+ /* Remove the entry from hashmap. This is done to take care of loop. */
+ if (GNUNET_NO ==
+ GNUNET_CONTAINER_multihashmap_remove (successor_peer_hashmap,
+ key, val))
+ {
+ DEBUG ("Failed to remove entry from hashmap\n");
+ break;
+ }
+ /* If a peer has its own identity as its successor. */
+ if (0 == memcmp(&key, &val, sizeof (struct GNUNET_HashCode)))
+ {
+ break;
+ }
+ }
+
+ if ((start_val == val) && (count == num_peers))
+ {
+ DEBUG("CIRCLE COMPLETED after %u tries", tries);
+ //FIXME: FREE HASHMAP.
+ if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
+ start_profiling();
+ return;
+ }
+ else
+ {
+ if (max_searches == ++tries)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
+ " cirle formation. Exiting\n",
+ max_searches,tries);
+ //FIXME: FREE HASHMAP
+ if (GNUNET_SCHEDULER_NO_TASK != successor_stats_task)
+ {
+ successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
+ {
+ start_profiling();
+ }
+
+ return;
+ }
+
+ flag = 0;
+ successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay, &collect_stats, cls);
+ }
+}
+
+
+/**
+ * Process successor statistic values.
+ *
+ * @param cls closure
+ * @param peer the peer the statistic belong to
+ * @param subsystem name of subsystem that created the statistic
+ * @param name the name of the datum
+ * @param value the current value
+ * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
+ * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
+ */
+static int
+successor_stats_iterator (void *cls,
+ const struct GNUNET_TESTBED_Peer *peer,
+ const char *subsystem,
+ const char *name,
+ uint64_t value,
+ int is_persistent)
+{
+ static const char *key_string = "XDHT";
+ if (0 == strncmp (key_string, name, strlen (key_string)))
+ {
+ char *my_id_str;
+ char successor_str[13];
+ char truncated_my_id_str[13];
+ char truncated_successor_str[13];
+ struct GNUNET_HashCode *my_id_key;
+ struct GNUNET_HashCode *succ_key;
+
+ strtok((char *)name,":");
+ my_id_str = strtok(NULL,":");
+
+ strncpy(truncated_my_id_str, my_id_str, 12);
+ truncated_my_id_str[12] = '\0';
+ my_id_key = GNUNET_new(struct GNUNET_HashCode);
+ GNUNET_CRYPTO_hash (truncated_my_id_str, sizeof(truncated_my_id_str),my_id_key);
+ GNUNET_STRINGS_data_to_string(&value, sizeof(uint64_t), successor_str, 13);
+ strncpy(truncated_successor_str, successor_str, 12);
+ truncated_successor_str[12] ='\0';
+
+ succ_key = GNUNET_new(struct GNUNET_HashCode);
+ GNUNET_CRYPTO_hash (truncated_successor_str, sizeof(truncated_successor_str),succ_key);
+
+ if (0 == flag)
+ {
+ start_key = my_id_key;
+ flag = 1;
+ }
+ /* FIXME: GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE do not free the value
+ which is replaced, need to free it. */
+ GNUNET_CONTAINER_multihashmap_put (successor_peer_hashmap,
+ my_id_key, (void *)succ_key,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+ }
+ return GNUNET_OK;
+}
+
+
+/*
+ * Task that collects peer and its corresponding successors.
+ *
+ * @param cls Closure (NULL).
+ * @param tc Task Context.
+ */
+static void
+collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
+ return;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
+ GNUNET_assert(NULL != testbed_handles);
+ successor_stats_op =
+ GNUNET_TESTBED_get_statistics (num_peers, testbed_handles,
+ "dht", NULL,
+ successor_stats_iterator,
+ successor_stats_cont, cls);
+
+ GNUNET_assert(NULL != successor_stats_op);
+}
+
+
+#if ENABLE_MALICIOUS
+/**
+ * Set the malicious variable in peer malicious context.
+ */
+static void
+set_malicious()
+{
+ unsigned int i;
+ DEBUG ("Setting %u peers malicious");
+ for(i = 0; i < n_malicious; i++)
+ {
+ struct MaliciousContext *mc = &a_mc[i];
+ mc->ctx->op =
+ GNUNET_TESTBED_service_connect (ac->ctx,
+ ac->ctx->peer,
+ "dht",
+ &dht_set_malicious, mc,
+ &dht_connect,
+ &dht_finish,
+ mc);
+ }
+}
+#endif