- relaxed to accommodate overlay linking timeouts
[oweals/gnunet.git] / src / testbed / testbed_api_topology.c
index 45209fb65beb9a31839e6aeee14e57f97441df95..0e237cde3a545e2dd0ebe6a9dc621c2a3f8d1d57 100644 (file)
 #include "testbed_api_peers.h"
 #include "testbed_api_operations.h"
 
+/**
+ * Generic loggins shorthand
+ */
+#define LOG(kind,...)                                           \
+  GNUNET_log_from (kind, "testbed-api-topology", __VA_ARGS__)
+
+
+/**
+ * Context information for topology operations
+ */
+struct TopologyContext;
+
 
 /**
  * Representation of an overlay link
  */
 struct OverlayLink
 {
+
   /**
-   * Peer A
+   * An operation corresponding to this link
    */
-  struct GNUNET_TESTBED_Peer *A;
+  struct GNUNET_TESTBED_Operation *op;
 
   /**
-   * Peer B
+   * The topology context this link is a part of
+   */  
+  struct TopologyContext *tc;
+
+  /**
+   * position of peer A's handle in peers array
    */
-  struct GNUNET_TESTBED_Peer *B;
+  uint32_t A;
+
+  /**
+   * position of peer B's handle in peers array
+   */
+  uint32_t B;
 
 };
 
@@ -54,34 +77,105 @@ struct OverlayLink
 struct TopologyContext
 {
   /**
-   * An array of links
+   * The array of peers
+   */
+  struct GNUNET_TESTBED_Peer **peers;
+
+  /**
+   * An array of links; this array is of size link_array_size
    */
   struct OverlayLink *link_array;
+
+  /**
+   * The operation closure
+   */
+  void *op_cls;
+
+  /**
+   * The size of the link array
+   */
+  unsigned int link_array_size;  
   
 };
 
 
+/**
+ * Callback to be called when an overlay_link operation complete
+ *
+ * @param cls element of the link_op array which points to the corresponding operation
+ * @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 
+overlay_link_completed (void *cls,
+                       struct GNUNET_TESTBED_Operation *op, 
+                       const char *emsg)
+{
+  struct OverlayLink *link = cls;
+  struct TopologyContext *tc;
+
+  GNUNET_assert (op == link->op);
+  GNUNET_TESTBED_operation_done (op);
+  link->op = NULL;  
+  if (NULL != emsg)
+  {
+    tc = link->tc;
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+        "Error while establishing a link: %s -- Retrying\n", emsg);
+    link->op =
+        GNUNET_TESTBED_overlay_connect (tc->op_cls,
+                                        &overlay_link_completed,
+                                        link,
+                                        tc->peers[link->A],
+                                        tc->peers[link->B]);
+    return;
+  }
+}
+
+
+
 /**
  * Function called when a overlay connect operation is ready
  *
- * @param cls the closure from GNUNET_TESTBED_operation_create_()
+ * @param cls the Topology context
  */
 static void
 opstart_overlay_configure_topology (void *cls)
 {
-  GNUNET_break (0);
+  struct TopologyContext *tc = cls;
+  unsigned int p;
+  
+  for (p = 0; p < tc->link_array_size; p++)
+  {
+    tc->link_array[p].op =
+       GNUNET_TESTBED_overlay_connect (tc->op_cls, &overlay_link_completed,
+                                       &tc->link_array[p],
+                                       tc->peers[tc->link_array[p].A],
+                                       tc->peers[tc->link_array[p].B]);                                                  
+  }
 }
 
 
 /**
  * Callback which will be called when overlay connect operation is released
  *
- * @param cls the closure from GNUNET_TESTBED_operation_create_()
+ * @param cls the Topology context
  */
 static void
 oprelease_overlay_configure_topology (void *cls)
 {
-  GNUNET_break (0);
+  struct TopologyContext *tc = cls;
+  unsigned int p;
+  
+  if (NULL != tc->link_array)
+  {
+    for (p = 0; p < tc->link_array_size; p++)
+      if (NULL != tc->link_array[p].op)
+        GNUNET_TESTBED_operation_cancel (tc->link_array[p].op);
+    GNUNET_free (tc->link_array);
+  }
+  GNUNET_free (tc);
 }
 
 
@@ -154,29 +248,54 @@ GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls,
                                               enum GNUNET_TESTBED_TopologyOption
                                               topo, va_list va)
 {
-  struct OverlayLink *link_array;
+  struct TopologyContext *tc;
   struct GNUNET_TESTBED_Operation *op;
   struct GNUNET_TESTBED_Controller *c;
-  unsigned int p;
+  unsigned int cnt;
 
   if (num_peers < 2)
     return NULL;
   c = peers[0]->controller;
+  tc = GNUNET_malloc (sizeof (struct TopologyContext));
+  tc->peers = peers;
+  tc->op_cls = op_cls;
   switch (topo)
   {
   case GNUNET_TESTBED_TOPOLOGY_LINE:
-    link_array = GNUNET_malloc (sizeof (struct OverlayLink) * (num_peers - 1));
-    for (p=1; p < num_peers; p++)
+    tc->link_array_size = num_peers - 1;
+    tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
+                                   tc->link_array_size);
+    for (cnt=1; cnt < num_peers; cnt++)
+    {
+      tc->link_array[cnt-1].A = cnt-1;
+      tc->link_array[cnt-1].B = cnt;
+    }
+    break;
+  case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
+    tc->link_array_size = va_arg (va, unsigned int);
+    tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
+                                    tc->link_array_size);
+    for (cnt = 0; cnt < tc->link_array_size; cnt++)
     {
-      link_array[p-1].A = peers[p-1];
-      link_array[p-1].B = peers[p];
+      uint32_t A_rand;
+      uint32_t B_rand;
+      
+      do {
+        A_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                           num_peers);
+        B_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                           num_peers);
+      } while (A_rand == B_rand);      
+      tc->link_array[cnt].A = A_rand;
+      tc->link_array[cnt].B = B_rand;
+      tc->link_array[cnt].tc = tc;
     }
     break;
   default:
     GNUNET_break (0);
     return NULL;
   }
-  op = GNUNET_TESTBED_operation_create_ (link_array,
+  op = GNUNET_TESTBED_operation_create_ (tc,
                                         &opstart_overlay_configure_topology,
                                         &oprelease_overlay_configure_topology);
   GNUNET_TESTBED_operation_queue_insert_