+ if (0 != n_dht)
+ return;
+ if (GNUNET_YES == in_shutdown)
+ return;
+ switch (mode)
+ {
+ case MODE_PUT:
+ if ((n_puts_ok + n_puts_fail) != n_active)
+ return;
+ /* Start GETs if all PUTs have been made */
+ mode = MODE_GET;
+ //(void) GNUNET_SCHEDULER_add_now (&call_start_profiling, NULL);
+ start_profiling ();
+ return;
+ case MODE_GET:
+ if ((n_gets_ok + n_gets_fail) != n_active)
+ return;
+ break;
+ }
+}
+
+/**
+ * Connect to DHT services of active peers
+ */
+static void
+start_profiling()
+{
+ struct Context *ctx;
+ unsigned int i;
+
+ DEBUG("GNUNET_TESTBED_service_connect \n");
+ GNUNET_break (GNUNET_YES != in_shutdown);
+ for(i = 0; i < n_active; i++)
+ {
+ struct ActiveContext *ac = &a_ac[i];
+ GNUNET_assert (NULL != (ctx = ac->ctx));
+ GNUNET_assert (NULL == ctx->op);
+ ctx->op =
+ GNUNET_TESTBED_service_connect (ctx,
+ ctx->peer,
+ "dht",
+ &dht_connected, ac,
+ &dht_connect,
+ &dht_disconnect,
+ ac);
+ }
+}
+
+#if ENABLE_MALICIOUS
+/**
+ * Count of total number of malicious peers.
+ */
+static unsigned int count_malicious;
+
+/**
+ * Continuation of GNUNET_DHT_act_malicious
+ * @param cls Malicious context
+ * @param success #GNUNET_OK if the ACT_MALICIOUS was transmitted,
+ * #GNUNET_NO on timeout,
+ * #GNUNET_SYSERR on disconnect from service
+ * after the ACT_MALICIOUS message was transmitted
+ * (so we don't know if it was received or not)
+ */
+static void
+act_malicious_cont (void *cls, int success)
+{
+ struct MaliciousContext *mc = cls;
+ struct Context *ctx = mc->ctx;
+
+ GNUNET_TESTBED_operation_done (ctx->op);
+ ctx->op = NULL;
+ return;
+}
+
+
+/**
+ * Call malicious API for all the malicious peers.
+ * @param cls the malicious context.
+ * @param op the operation that has been finished
+ * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
+ * @param emsg error message in case the operation has failed; will be NULL if
+ * operation has executed successfully.
+ */
+static void
+dht_set_malicious(void *cls,
+ struct GNUNET_TESTBED_Operation *op,
+ void *ca_result,
+ const char *emsg)
+{
+ struct MaliciousContext *mc = cls;
+ struct Context *ctx = mc->ctx;
+
+ GNUNET_assert (NULL != ctx);
+ GNUNET_assert (NULL != ctx->op);
+ GNUNET_assert (ctx->op == op);
+ mc->dht = (struct GNUNET_DHT_Handle *) ca_result;
+ if (NULL != emsg)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
+ GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect_malicious() */
+ ctx->op = NULL;
+ return;
+ }
+ mc->dht_malicious = GNUNET_DHT_act_malicious(mc->dht, 1, act_malicious_cont, mc);
+}
+
+
+/**
+ * Adapter function called to destroy a connection to
+ * a service.
+ *
+ * @param cls the active context
+ * @param op_result service handle returned from the connect adapter
+ */
+static void
+dht_disconnect_malicious (void *cls, void *op_result)
+{
+ struct MaliciousContext *mc = cls;
+ count_malicious++;
+ GNUNET_assert (NULL != mc->dht);
+ GNUNET_assert (mc->dht == op_result);
+ GNUNET_DHT_disconnect (mc->dht);
+ mc->dht = NULL;
+ mc->ctx->op = NULL;
+ n_dht--;
+
+ if (0 != n_dht)
+ return;
+
+ if(n_malicious == count_malicious)
+ {
+ DEBUG("\n Call start_profiling()");
+ start_profiling();
+ }
+}
+
+
+/**
+ * Set the malicious variable in peer malicious context.
+ */
+static void
+set_malicious()
+{
+ unsigned int i;
+
+ DEBUG ("Setting %u peers malicious",
+ n_malicious);
+ for(i = 0; i < n_malicious; i++)
+ {
+ struct MaliciousContext *mc = &a_mc[i];
+ mc->ctx->op =
+ GNUNET_TESTBED_service_connect (mc->ctx,
+ mc->ctx->peer,
+ "dht",
+ &dht_set_malicious, mc,
+ &dht_connect,
+ &dht_disconnect_malicious,
+ mc);
+ }
+}
+
+#endif
+
+
+/**
+ * Start collecting relevant statistics. If ENABLE_MALICIOUS set, first
+ * set the malicious peers. If not, then start with PUT operation on active
+ * peers.
+ */
+static void
+start_func()
+{
+#if ENABLE_MALICIOUS
+ set_malicious();
+#else
+ start_profiling();
+#endif
+}
+
+
+/**
+ * Remove entry from successor peer hashmap.
+ * @param cls closure
+ * @param key current public key
+ * @param value value in the hash map
+ * @return #GNUNET_YES if we should continue to iterate,
+ * #GNUNET_NO if not.
+ */
+static int
+hashmap_iterate_remove(void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
+{
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove(successor_peer_hashmap, key, value));
+ return GNUNET_YES;
+}
+
+
+/**
+ * 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 = NULL;
+ GNUNET_TESTBED_operation_done (successor_stats_op);
+ successor_stats_op = NULL;
+ if (0 == max_searches)
+ {
+ start_func();
+ return;
+ }
+
+ GNUNET_assert (NULL != start_key);
+ start_val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
+ start_key);
+ GNUNET_assert (NULL != start_val);
+ val = start_val;
+ for (count = 0; count < num_peers; count++)
+ {
+ key = val;
+ val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
+ key);
+ if (NULL == val)
+ break;
+ /* 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;
+ }
+
+ GNUNET_assert (GNUNET_SYSERR !=
+ GNUNET_CONTAINER_multihashmap_iterate (successor_peer_hashmap,
+ &hashmap_iterate_remove,
+ NULL));
+
+ successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
+ GNUNET_NO);
+ if ((start_val == val) && (count == num_peers))
+ {
+ DEBUG("CIRCLE COMPLETED after %u tries", tries);
+ if(NULL == successor_stats_task)
+ {
+ start_func();
+ }
+ return;
+ }
+ else
+ {
+ if (max_searches == ++tries)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
+ " circle formation. Exiting\n",
+ max_searches,tries);
+ if (NULL != successor_stats_task)
+ {
+ successor_stats_task = NULL;
+ }
+ if(NULL == successor_stats_task)
+ {
+ start_func();
+ }
+
+ return;
+ }
+ else
+ {
+ flag = 0;
+ successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
+ &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 == max_searches)
+ return GNUNET_OK;
+
+ 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)
+ {
+ GNUNET_assert(NULL != my_id_key);
+ start_key = my_id_key;
+ GNUNET_assert(NULL != start_key);
+ flag = 1;
+ }
+ 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);
+
+ if (0 != max_searches)
+ successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
+ GNUNET_NO);
+ 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);