Changing the logic to check for circle completion
[oweals/gnunet.git] / src / dht / gnunet_dht_profiler.c
index 2f7fdde77dd6b5d2482099c1ce9852f7f7da374e..9fa9f33c471168c205041b6ba98d9c3a3c8bfd6e 100644 (file)
@@ -339,6 +339,17 @@ struct Context **peer_contexts = NULL;
  */
 static int peers_started = 0;
 
+
+/**
+ * Should we do a PUT (mode = 0) or GET (mode = 1);
+ */
+static enum
+{
+  MODE_PUT = 0,
+
+  MODE_GET = 1
+} mode;
+
 /**
  * Task that collects successor statistics from all the peers. 
  * @param cls
@@ -365,7 +376,7 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     for (cnt=0; cnt < num_peers; cnt++)
     {
       if (NULL != a_ctx[cnt].op)
-        GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
+        GNUNET_TESTBED_operation_done (a_ctx[cnt].op); //FIXME: assertion fails.
 
       /* Cleanup active context if this peer is an active peer */
       ac = a_ctx[cnt].ac;
@@ -436,12 +447,8 @@ bandwidth_stats_iterator (void *cls,
      outgoing_bandwidth = outgoing_bandwidth + value;
    else if (0 == strncmp(s_recv, name, strlen (s_recv)))
      incoming_bandwidth = incoming_bandwidth + value;
-   else
-     return GNUNET_OK;
-   DEBUG ("Bandwith - Out: %lu; In: %lu\n",
-          (unsigned long) outgoing_bandwidth,
-          (unsigned long) incoming_bandwidth);
-   return GNUNET_OK;
+   
+    return GNUNET_OK;
 }
 
 
@@ -597,6 +604,13 @@ delayed_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Connect to DHT services of active peers
+ */
+static void
+start_profiling();
+
+
 /**
  * Queue up a delayed task for doing DHT GET
  *
@@ -611,13 +625,18 @@ static void
 put_cont (void *cls, int success)
 {
   struct ActiveContext *ac = cls;
+  struct Context *ctx = ac->ctx;
+  struct GNUNET_TESTBED_Operation *op;
 
   ac->dht_put = NULL;
   if (success)
     n_puts_ok++;
   else
     n_puts_fail++;
-  ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_get, ac);
+  GNUNET_assert (NULL != ctx);
+  op = ctx->op;
+  ctx->op = NULL;
+  GNUNET_TESTBED_operation_done (op);
 }
 
 
@@ -673,7 +692,7 @@ dht_connected (void *cls,
   struct ActiveContext *ac = cls;
   struct Context *ctx = ac->ctx;
 
-  GNUNET_assert (NULL != ctx);
+  GNUNET_assert (NULL != ctx); //FIXME: Fails
   GNUNET_assert (NULL != ctx->op);
   GNUNET_assert (ctx->op == op);
   ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
@@ -684,8 +703,15 @@ dht_connected (void *cls,
     ctx->op = NULL;
     return;
   }
-  
-  ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_put, ac);
+  switch (mode)
+  {
+  case MODE_PUT:
+    ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_put, ac);
+    break;
+  case MODE_GET:
+    ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_get, ac);
+    break;
+  }
 }
 
 
@@ -721,19 +747,26 @@ dht_disconnect (void *cls, void *op_result)
   GNUNET_assert (NULL != ac->dht);
   GNUNET_assert (ac->dht == op_result);
   GNUNET_DHT_disconnect (ac->dht);
+  ac->dht = NULL;
   n_dht--;
-  if (0 == n_dht)
-    GNUNET_SCHEDULER_shutdown ();
+  if (0 != n_dht)
+    return;
+  /* Start GETs if all PUTs have been made */
+  if (MODE_PUT == mode)
+  {
+    mode = MODE_GET;
+    start_profiling ();
+    return;
+  }
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
 /**
- * FIXME:Verify where is n_active used. Should this service be started only
- * for n_active peers?
- * Start testbed service for all the peers. 
+ * Connect to DHT services of active peers
  */
 static void
-start_testbed_service_on_all_peers()
+start_profiling()
 {
   unsigned int i;
   DEBUG("GNUNET_TESTBED_service_connect \n");
@@ -769,36 +802,47 @@ successor_stats_cont (void *cls,
 {
   struct GNUNET_HashCode *val;
   struct GNUNET_HashCode *start_val;
-  int count = 0;
   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_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;
-  while (count < num_peers)
+  for (count = 0; count < num_peers; count++)
   {
-    key = GNUNET_new(struct GNUNET_HashCode);
     key = val;
     val = GNUNET_CONTAINER_multihashmap_get (successor_peer_hashmap,
                                              key);
     GNUNET_assert(NULL != val);
-    count++;
+    /* 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)
+  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_testbed_service_on_all_peers();
-    }
-    
+      start_profiling();
     return;
   }
   else
@@ -809,21 +853,20 @@ successor_stats_cont (void *cls,
                   "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;
-        //FIXME: free hashmap
       }
-      
       if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
       {
-        start_testbed_service_on_all_peers();
+        start_profiling();
       }
       
       return;
     }
     
-    //FIXME: change delay use exponential back off. 
+    flag = 0;
     successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay, &collect_stats, cls);
   }
 }
@@ -877,6 +920,8 @@ successor_stats_iterator (void *cls,
       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);
@@ -898,12 +943,7 @@ collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
-  DEBUG("num_peers = %d", num_peers);
   GNUNET_assert(NULL != testbed_handles);
-         
-  
- /* Check for successor pointer, don't start put till the virtual ring topology
-   is not created. */
   successor_stats_op = 
           GNUNET_TESTBED_get_statistics (num_peers, testbed_handles,
                                          "dht", NULL,
@@ -913,6 +953,30 @@ collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   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
 /**
  * Callback called when DHT service on the peer is started
  *
@@ -935,6 +999,9 @@ service_started (void *cls,
   DEBUG("Peers Started = %d; num_peers = %d \n", peers_started, num_peers);
   if (GNUNET_SCHEDULER_NO_TASK == successor_stats_task && peers_started == num_peers)
   {
+#if ENABLE_MALICIOUS
+    set_malicious();
+#endif
      DEBUG("successor_stats_task \n");
      struct Collect_Stat_Context *collect_stat_cls = GNUNET_new(struct Collect_Stat_Context);
      collect_stat_cls->service_connect_ctx = cls;