GNUNET_TESTBED_run and test cases
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
index 72a2f8fda29bacd60a0dd189d6121d7f51b338ac..57d9bafa98b85a0209285fa20cc870ff06c14dfe 100644 (file)
@@ -433,7 +433,7 @@ struct OverlayConnectContext
 
 
 /**
- * Context information for operations forward to subcontrollers
+ * Context information for operations forwarded to subcontrollers
  */
 struct ForwardedOperationContext
 {
@@ -447,6 +447,11 @@ struct ForwardedOperationContext
    */
   struct GNUNET_SERVER_Client *client;
 
+  /**
+   * Closure pointer
+   */
+  void *cls;  
+
   /**
    * Task ID for the timeout task
    */
@@ -457,10 +462,6 @@ struct ForwardedOperationContext
    */
   uint64_t operation_id;  
 
-  /**
-   * The ID of the peer we are going to create
-   */
-  uint32_t peer_id;
 };
 
 
@@ -941,8 +942,8 @@ lcf_proc_cc (void *cls, const char *emsg)
  * @param msg the peer create success message
  */
 static void
-forwarded_link_controllers_reply_relay (void *cls,
-                                       const struct GNUNET_MessageHeader *msg)
+forwarded_operation_reply_relay (void *cls,
+                                 const struct GNUNET_MessageHeader *msg)
 {
   struct ForwardedOperationContext *fopc = cls;
   struct GNUNET_MessageHeader *dup_msg;  
@@ -954,7 +955,7 @@ forwarded_link_controllers_reply_relay (void *cls,
   queue_message (fopc->client, dup_msg);
   GNUNET_SERVER_client_drop (fopc->client);
   GNUNET_SCHEDULER_cancel (fopc->timeout_task);  
-  GNUNET_free (fopc);  
+  GNUNET_free (fopc);
 }
 
 
@@ -965,13 +966,14 @@ forwarded_link_controllers_reply_relay (void *cls,
  * @param tc the task context from scheduler
  */
 static void
-forwarded_link_controllers_timeout (void *cls,
+forwarded_operation_timeout (void *cls,
                                    const struct GNUNET_SCHEDULER_TaskContext
                                    *tc)
 {
   struct ForwardedOperationContext *fopc = cls;
   
   GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
+  send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
   GNUNET_SERVER_client_drop (fopc->client);
   GNUNET_free (fopc);  
 }
@@ -1033,11 +1035,11 @@ lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
                                             lcf->operation_id,
                                             &lcf->msg->header,
-                                            &forwarded_link_controllers_reply_relay,
+                                            &forwarded_operation_reply_relay,
                                             fopc);
     fopc->timeout_task = 
       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
-                                   &forwarded_link_controllers_timeout, fopc);    
+                                   &forwarded_operation_timeout, fopc);    
     lcf->state = FINISHED;
     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
     break;
@@ -1533,7 +1535,7 @@ peer_create_forward_timeout (void *cls,
   
   /* send error msg to client */
   send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id,
-                           "Timedout");  
+                           "Timedout");
   GNUNET_SERVER_client_drop (fo_ctxt->client);
   GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
   GNUNET_free (fo_ctxt);  
@@ -1552,13 +1554,24 @@ peer_create_success_cb (void *cls,
                         const struct GNUNET_MessageHeader *msg)
 {
   struct ForwardedOperationContext *fo_ctxt = cls;
-  struct GNUNET_MessageHeader *dup_msg;  
+  const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
+  struct GNUNET_MessageHeader *dup_msg;
+  struct Peer *peer;
   uint16_t msize;
   
-  GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);  
-  GNUNET_assert (ntohs (msg->type) == 
-                 GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
-  msize = ntohs (msg->size);  
+  GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
+  if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
+  {
+    success_msg 
+      = (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
+    peer = GNUNET_malloc (sizeof (struct Peer));
+    peer->is_remote = GNUNET_YES;
+    peer->id = ntohl (success_msg->peer_id);
+    GNUNET_assert (NULL != fo_ctxt->cls);
+    peer->details.remote.controller = fo_ctxt->cls;
+    peer_list_add (peer);    
+  }
+  msize = ntohs (msg->size);
   dup_msg = GNUNET_malloc (msize);
   (void) memcpy (dup_msg, msg, msize);  
   queue_message (fo_ctxt->client, dup_msg);
@@ -1677,8 +1690,8 @@ handle_peer_create (void *cls,
   fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
   GNUNET_SERVER_client_keep (client);
   fo_ctxt->client = client;
-  fo_ctxt->peer_id = ntohl (msg->peer_id);
-  fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);  
+  fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id); 
+  fo_ctxt->cls = slave_list[route->dest]->controller;  
   fo_ctxt->opc = 
     GNUNET_TESTBED_forward_operation_msg_ (slave_list[route->dest]->controller,
                                            fo_ctxt->operation_id,
@@ -1705,6 +1718,7 @@ handle_peer_destroy (void *cls,
                      const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
+  struct ForwardedOperationContext *fopc;
   struct Peer *peer;
   uint32_t peer_id;
   
@@ -1714,8 +1728,8 @@ handle_peer_destroy (void *cls,
              peer_id, GNUNET_ntohll (msg->operation_id));  
   if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
   {
-    GNUNET_break (0);
-    /* FIXME: Reply with failure event message or forward to slave controller */
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Asked to destroy a non existent peer with id: %u\n", peer_id);
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
@@ -1723,7 +1737,19 @@ handle_peer_destroy (void *cls,
   if (GNUNET_YES == peer->is_remote)
   {
     /* Forward the destory message to sub controller */
-    GNUNET_break (0);
+    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;
   }
@@ -1750,6 +1776,8 @@ handle_peer_start (void *cls,
 {
   const struct GNUNET_TESTBED_PeerStartMessage *msg;
   struct GNUNET_TESTBED_PeerEventMessage *reply;
+  struct ForwardedOperationContext *fopc;
+  struct Peer *peer;
   uint32_t peer_id;
 
   msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
@@ -1758,15 +1786,35 @@ handle_peer_start (void *cls,
       || (NULL == peer_list[peer_id]))
   {
     GNUNET_break (0);
-    /* FIXME: reply with failure message or forward to slave controller */
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Asked to start a non existent peer with id: %u\n", peer_id);    
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+  peer = peer_list[peer_id];
+  if (GNUNET_YES == peer->is_remote)
+  {
+    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;
   }
   if (GNUNET_OK != 
-      GNUNET_TESTING_peer_start (peer_list[peer_id]->details.local.peer))
+      GNUNET_TESTING_peer_start (peer->details.local.peer))
   {
-    /* FIXME: return FAILURE message */
-    GNUNET_break (0);
+    send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
+                             "Failed to start");
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
@@ -1796,21 +1844,43 @@ handle_peer_stop (void *cls,
 {
   const struct GNUNET_TESTBED_PeerStopMessage *msg;
   struct GNUNET_TESTBED_PeerEventMessage *reply;
+  struct ForwardedOperationContext *fopc;
+  struct Peer *peer;
   uint32_t peer_id;
 
   msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
   peer_id = ntohl (msg->peer_id);
   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
+  {    
+    send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
+                             "Peer not found");
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+  peer = peer_list[peer_id];
+  if (GNUNET_YES == peer->is_remote)
   {
-    GNUNET_break (0);          /* FIXME: route to slave? */
+    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;
   }
   if (GNUNET_OK != 
-      GNUNET_TESTING_peer_stop (peer_list[peer_id]->details.local.peer))
+      GNUNET_TESTING_peer_stop (peer->details.local.peer))
   {
-    /* FIXME: return FAILURE message */
-    GNUNET_break (0);
+    send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
+                             "Failed to stop peer");
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
@@ -1852,8 +1922,8 @@ handle_peer_get_config (void *cls,
   peer_id = ntohl (msg->peer_id);
   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
   {
-    /* FIXME: return FAILURE message */
-    GNUNET_break (0);
+    send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
+                             "Peer not found");
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
@@ -1916,8 +1986,8 @@ occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Task which will be run when overlay connect request has been timed out
  *
- * @param 
- * @return 
+ * @param cls the OverlayConnectContext
+ * @param tc the TaskContext
  */
 static void
 timeout_overlay_connect (void *cls,
@@ -2242,8 +2312,11 @@ shutdown_task (void *cls,
   for (id = 0; id < peer_list_size; id++)
     if (NULL != peer_list[id])
     {
-      GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
-      GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
+      if (GNUNET_NO == peer_list[id]->is_remote)
+      {        
+        GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
+        GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
+      }      
       GNUNET_free (peer_list[id]);
     }
   GNUNET_free_non_null (peer_list);