towards handling suboperations during overlay connect
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
index 92d3af32cc8af72635ff8fcb52cf178130cfc294..05d296fee451ef07d68dd7175a5608ce56a30b8c 100644 (file)
  */
 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
 
-/**
- * Timeout of Transport try_connect requests
- */
-#define TRANSPORT_TRY_CONNECT_TIMEOUT                                   \
-  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
-
 /**
  * The main context information associated with the client which started us
  */
@@ -85,7 +79,7 @@ struct Context
    * The TESTING system handle for starting peers locally
    */
   struct GNUNET_TESTING_System *system;
-
+  
   /**
    * Event mask of event to be responded in this context
    */
@@ -186,8 +180,7 @@ struct Slave
   struct GNUNET_TESTBED_Controller *controller;
 
   /**
-   * The configuration of the slave. Will be NULL for slave which we didn't
-   * directly start
+   * The configuration of the slave. Cannot be NULL
    */
   struct GNUNET_CONFIGURATION_Handle *cfg;
 
@@ -302,7 +295,7 @@ struct LCFContextQueue
 
 
 /**
- * A locally started peer
+ * A peer
  */
 struct Peer
 {
@@ -442,6 +435,12 @@ struct OverlayConnectContext
    * The id of peer B
    */
   uint32_t other_peer_id;
+
+  /**
+   * Number of times we tried to send hello; used to increase delay in offering
+   * hellos
+   */
+  uint16_t retries;
 };
 
 
@@ -478,6 +477,12 @@ struct RequestOverlayConnectContext
    */
   GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
   
+  /**
+   * Number of times we tried to send hello; used to increase delay in offering
+   * hellos
+   */
+  uint16_t retries;
+  
 };
 
 
@@ -538,6 +543,11 @@ struct LinkControllersContext
  */
 static struct Context *master_context;
 
+/**
+ * Our hostname; we give this to all the peers we start
+ */
+static char *hostname;
+
 /***********/
 /* Handles */
 /***********/
@@ -587,7 +597,7 @@ static struct Route **route_list;
 static struct Slave **slave_list;
 
 /**
- * A list of peers we own locally
+ * A list of peers we know about
  */
 static struct Peer **peer_list;
 
@@ -729,16 +739,19 @@ static int
 host_list_add (struct GNUNET_TESTBED_Host *host)
 {
   uint32_t host_id;
+  uint32_t orig_size;
 
   host_id = GNUNET_TESTBED_host_get_id_ (host);
+  orig_size = host_list_size;  
   if (host_list_size <= host_id)
   {
+    while (host_list_size <= host_id)
+      host_list_size += LIST_GROW_STEP;
     host_list =
         TESTBED_realloc (host_list,
-                         sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
-                         sizeof (struct GNUNET_TESTBED_Host *) *
-                         (host_list_size + LIST_GROW_STEP));
-    host_list_size += LIST_GROW_STEP;
+                         sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
+                         sizeof (struct GNUNET_TESTBED_Host *)
+                        * host_list_size);
   }
   if (NULL != host_list[host_id])
   {
@@ -758,13 +771,17 @@ host_list_add (struct GNUNET_TESTBED_Host *host)
 static void
 route_list_add (struct Route *route)
 {
+  uint32_t orig_size;
+
+  orig_size = route_list_size;  
   if (route->dest >= route_list_size)
   {
+    while (route->dest >= route_list_size)
+      route_list_size += LIST_GROW_STEP;
     route_list =
-        TESTBED_realloc (route_list, sizeof (struct Route *) * route_list_size,
-                         sizeof (struct Route *) * (route_list_size +
-                                                    LIST_GROW_STEP));
-    route_list_size += LIST_GROW_STEP;
+        TESTBED_realloc (route_list,
+                        sizeof (struct Route *) * orig_size,
+                         sizeof (struct Route *) * route_list_size);
   }
   GNUNET_assert (NULL == route_list[route->dest]);
   route_list[route->dest] = route;
@@ -1204,8 +1221,8 @@ handle_init (void *cls, struct GNUNET_SERVER_Client *client,
 
   if (NULL != master_context)
   {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    LOG_DEBUG ("We are being connected to laterally\n");
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
   msg = (const struct GNUNET_TESTBED_InitMessage *) message;
@@ -1230,7 +1247,7 @@ handle_init (void *cls, struct GNUNET_SERVER_Client *client,
   master_context->master_ip = GNUNET_strdup (controller_hostname);
   LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
   master_context->system =
-      GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
+      GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
   host =
       GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
                                           0);
@@ -1528,7 +1545,7 @@ handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
           GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
                                              master_context->event_mask,
                                              &slave_event_callback, slave);
-      GNUNET_CONFIGURATION_destroy (cfg);
+      slave->cfg = cfg;
       if (NULL != slave->controller)
         send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
       else
@@ -1586,11 +1603,24 @@ handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
   /* FIXME: Adding a new route should happen after the controllers are linked
    * successfully */
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  if (1 != msg->is_subordinate)
+  {
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+  if ((delegated_host_id < route_list_size)
+      && (NULL != route_list[delegated_host_id]))
+  {
+    GNUNET_break_op (0);       /* Are you trying to link delegated host twice
+                                  with is subordinate flag set to GNUNET_YES? */
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
   new_route = GNUNET_malloc (sizeof (struct Route));
   new_route->dest = delegated_host_id;
   new_route->thru = route->dest;
   route_list_add (new_route);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
@@ -2011,8 +2041,20 @@ handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
   peer = peer_list[peer_id];
   if (GNUNET_YES == peer->is_remote)
   {
-    /* FIXME: forward to sub controller */
-    GNUNET_break (0);
+    struct ForwardedOperationContext *fopc;
+    
+    fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
+    GNUNET_SERVER_client_keep (client);
+    fopc->client = client;
+    fopc->operation_id = GNUNET_ntohll (msg->operation_id);
+    fopc->opc =
+        GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
+                                               fopc->operation_id, &msg->header,
+                                               &forwarded_operation_reply_relay,
+                                               fopc);
+    fopc->timeout_task =
+        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
+                                      fopc);    
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
@@ -2200,8 +2242,10 @@ send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
     GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
     occ->send_hello_task =
-        GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
-                                      &send_hello, occ);
+        GNUNET_SCHEDULER_add_delayed
+        (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                        100 * (pow (2, occ->retries++))),
+         &send_hello, occ);
   }
   GNUNET_free (other_peer_str);  
 }
@@ -2402,6 +2446,7 @@ handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_client_keep (client);
     fopc->client = client;
     fopc->operation_id = operation_id;
+    LOG_DEBUG ("Forwarding overlay connect\n");
     fopc->opc = 
        GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
                                               operation_id, message,
@@ -2419,7 +2464,7 @@ handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
   occ->peer_id = p1;
   occ->other_peer_id = p2;
   occ->peer = peer_list[p1];
-  occ->op_id = GNUNET_ntohll (msg->operation_id);
+  occ->op_id = GNUNET_ntohll (msg->operation_id);  
   if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
   {
     uint32_t peer2_host_id;
@@ -2428,21 +2473,32 @@ handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
     if ((peer2_host_id >= slave_list_size)
        || (NULL ==slave_list[peer2_host_id]))
     {
-      GNUNET_break (0);
-      GNUNET_SERVER_client_drop (client);
+      struct GNUNET_TESTBED_NeedControllerConfig *reply;
+
       GNUNET_free (occ);
-      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+      reply = GNUNET_malloc (sizeof (struct
+                                     GNUNET_TESTBED_NeedControllerConfig)); 
+      reply->header.size = htons (sizeof (struct
+                                          GNUNET_TESTBED_NeedControllerConfig));
+      reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG);
+      reply->controller_host_id = msg->peer2_host_id;
+      reply->operation_id = msg->operation_id;
+      queue_message (client, &reply->header);      
+      GNUNET_SERVER_receive_done (client, GNUNET_OK);
       return;
     }
-    occ->peer2_controller = slave_list[peer2_host_id]->controller;
-    if (NULL == occ->peer2_controller)
+    else
     {
-      GNUNET_break (0);
-      GNUNET_SERVER_client_drop (client);
-      GNUNET_free (occ);
-      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-      return;
-    }   
+      occ->peer2_controller = slave_list[peer2_host_id]->controller;
+      if (NULL == occ->peer2_controller)
+      {
+        GNUNET_break (0);       /* What's going on? */
+        GNUNET_SERVER_client_drop (client);
+        GNUNET_free (occ);
+        GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+        return;
+      }
+    }
   }
   else
   {
@@ -2568,8 +2624,10 @@ attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
   GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
   rocc->attempt_connect_task_id = 
-      GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
-                                    &attempt_connect_task, rocc);
+      GNUNET_SCHEDULER_add_delayed 
+      (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                      100 * (pow (2, rocc->retries++))),
+       &attempt_connect_task, rocc);
 }
 
 
@@ -2620,10 +2678,14 @@ handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  if (GNUNET_NO != peer->is_remote)
+  if (GNUNET_YES == peer->is_remote)
   {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    struct GNUNET_MessageHeader *msg2;
+    
+    msg2 = GNUNET_malloc (msize);
+    (void) memcpy (msg2, message, msize);
+    GNUNET_TESTBED_queue_message_ (peer->details.remote.controller, msg2);
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
   rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
@@ -2640,8 +2702,6 @@ handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
           sizeof (struct GNUNET_PeerIdentity));
   rocc->hello = GNUNET_malloc (hsize);
   memcpy (rocc->hello, msg->hello, hsize);
-  /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
-  /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
   rocc->attempt_connect_task_id =
       GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
   rocc->timeout_rocc_task_id =
@@ -2662,6 +2722,13 @@ handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
                         const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
+  struct Slave *slave;  
+  struct GNUNET_TESTBED_SlaveConfiguration *reply;
+  char *config;
+  char *xconfig;
+  size_t config_size;
+  size_t xconfig_size;
+  size_t reply_size;
   uint64_t op_id;
   uint32_t slave_id;
 
@@ -2674,7 +2741,30 @@ handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
-  GNUNET_break (0);
+  slave = slave_list[slave_id];
+  if (NULL == slave->cfg)
+  {
+    send_operation_fail_msg (client, op_id,
+                            "Configuration not found (slave not started by me)");
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+  config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
+  xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size, 
+                                                 &xconfig);
+  GNUNET_free (config);  
+  reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
+  GNUNET_break (reply_size <= UINT16_MAX);
+  GNUNET_break (config_size <= UINT16_MAX);
+  reply = GNUNET_realloc (xconfig, reply_size);
+  (void) memmove (&reply[1], reply, xconfig_size);
+  reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
+  reply->header.size = htons ((uint16_t) reply_size);
+  reply->slave_id = msg->slave_id;
+  reply->operation_id = msg->operation_id;
+  reply->config_size = htons ((uint16_t) config_size);
+  queue_message (client, &reply->header);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
@@ -2781,6 +2871,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_free (master_context);
     master_context = NULL;
   }
+  GNUNET_free_non_null (hostname);
 }
 
 
@@ -2845,9 +2936,11 @@ testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
     {NULL}
   };
 
+  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string 
+                (cfg, "testbed", "HOSTNAME", &hostname));
   GNUNET_SERVER_add_handlers (server, message_handlers);
   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
-  ss_map = GNUNET_CONTAINER_multihashmap_create (5);
+  ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
   shutdown_task_id =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                     &shutdown_task, NULL);