TNG testing: Cleanup and Structuring
authorJulius Bünger <buenger@mytum.de>
Thu, 18 Apr 2019 13:35:42 +0000 (15:35 +0200)
committerJulius Bünger <buenger@mytum.de>
Thu, 18 Apr 2019 13:37:29 +0000 (15:37 +0200)
src/transport/test_communicator_unix.c
src/transport/transport-testing2.c
src/transport/transport-testing2.h

index ef7ed32c04406605503a836e596bde2b7f406a82..fd189659cdfc970ac63af56eba3a2787da28af26 100644 (file)
@@ -63,6 +63,11 @@ run (void *cls)
       "test_communicator_1.conf",
       &communicator_available,
       NULL); /* cls */
+  GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
+      "transport",
+      "test_communicator_2.conf",
+      &communicator_available,
+      NULL); /* cls */
 }
 
 int
index df48e778297853e655c877b6fdf9931fb1122847..51791e9810f9f169a343a7e2d742bb8110a3e00d 100644 (file)
 #define LOG(kind,...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
 
 
+struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
+{
+  /**
+   * @brief Handle to the configuration
+   */
+  struct GNUNET_CONFIGURATION_Handle *cfg;
+
+  /**
+   * @brief File name of configuration file
+   */
+  char *cfg_filename;
+
+  /**
+   * @brief Handle to the transport service
+   */
+  struct GNUNET_SERVICE_Handle *tsh;
+
+  /**
+   * @brief Task that will be run on shutdown to stop and clean transport
+   * service
+   */
+  struct GNUNET_SCHEDULER_Task *ts_shutdown_task;
+
+  /**
+   * @brief Handle to the client
+   */
+  struct GNUNET_SERVICE_Client *client;
+
+  /**
+   * @brief Process of the communicator
+   */
+  struct GNUNET_OS_Process *c_proc;
+
+  /**
+   * @brief Task that will be run on shutdown to stop and clean communicator
+   */
+  struct GNUNET_SCHEDULER_Task *c_shutdown_task;
+
+  /* Callbacks + Closures */
+  /**
+   * @brief Callback called when a new communicator connects
+   */
+  GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available;
+
+  /**
+   * @brief Closure to the callback
+   */
+  void *communicator_available_cls;
+};
+
+
 /**
  * @brief Check whether incoming msg indicating available communicator is
  * correct
@@ -50,9 +101,13 @@ static int
 check_communicator_available (void *cls,
     const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-      "check_communicator_available()\n");
-  return GNUNET_YES;
+  uint16_t size;
+
+  size = ntohs (msg->header.size) - sizeof (*msg);
+  if (0 == size)
+    return GNUNET_OK; /* receive-only communicator */
+  GNUNET_MQ_check_zero_termination (msg);
+  return GNUNET_OK;
 }
 
 
@@ -66,16 +121,19 @@ static void
 handle_communicator_available (void *cls,
     const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
 {
-  GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available = cls;
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-      "handle_communicator_available()\n");
-  if (NULL != communicator_available)
+  struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
+  uint16_t size;
+
+  size = ntohs (msg->header.size) - sizeof (*msg);
+  if (0 == size)
+    return; /* receive-only communicator */
+  if (NULL != tc_h->communicator_available)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
         "calling communicator_available()\n");
-    communicator_available (NULL, msg);
+    tc_h->communicator_available (tc_h->communicator_available_cls, msg);
   }
-  //GNUNET_SERVICE_client_continue (client);
+  GNUNET_SERVICE_client_continue (tc_h->client);
 }
 
 
@@ -93,6 +151,49 @@ shutdown_service (void *cls)
 }
 
 
+/**
+ * @brief Callback called when new Client (Communicator) connects
+ *
+ * @param cls Closure - TransporCommmunicator Handle
+ * @param client Client
+ * @param mq Messagequeue
+ *
+ * @return TransportCommunicator Handle
+ */
+static void *
+connect_cb (void *cls,
+            struct GNUNET_SERVICE_Client *client,
+            struct GNUNET_MQ_Handle *mq)
+{
+  struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+      "Client connected.\n");
+  tc_h->client = client;
+  return tc_h;
+}
+
+
+/**
+ * @brief Callback called when Client disconnects
+ *
+ * @param cls Closure - TransportCommunicator Handle
+ * @param client Client
+ * @param internal_cls TransporCommmunicator Handle
+ */
+static void
+disconnect_cb (void *cls,
+               struct GNUNET_SERVICE_Client *client,
+               void *internal_cls)
+{
+  struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+      "Client disconnected.\n");
+  tc_h->client = NULL;
+}
+
+
 /**
  * @brief Start the communicator part of the transport service
  *
@@ -101,14 +202,13 @@ shutdown_service (void *cls)
  * @param cfg Configuration
  */
 static void
-transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available,
-                              struct GNUNET_CONFIGURATION_Handle *cfg)
+transport_communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
 {
   struct GNUNET_MQ_MessageHandler mh[] = {
     GNUNET_MQ_hd_var_size (communicator_available,
         GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
         struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
-        &communicator_available),
+        &tc_h),
     //GNUNET_MQ_hd_var_size (communicator_backchannel,
     //    GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
     //    struct GNUNET_TRANSPORT_CommunicatorBackchannel,
@@ -149,10 +249,10 @@ transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCall
   struct GNUNET_SERVICE_Handle *h;
 
   h = GNUNET_SERVICE_start ("transport",
-                            cfg,
-                            NULL,
-                            NULL,
-                            NULL,
+                            tc_h->cfg,
+                            &connect_cb,
+                            &disconnect_cb,
+                            tc_h,
                             mh);
   if (NULL == h)
     LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -161,48 +261,70 @@ transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCall
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Started service\n");
-    GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
+    /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
   }
 }
 
 
+/**
+ * @brief Task run at shutdown to kill communicator and clean up
+ *
+ * @param cls Closure - Process of communicator
+ */
+static void
+shutdown_communicator (void *cls)
+{
+  struct GNUNET_OS_Process *proc = cls;
+
+  if (GNUNET_OK != GNUNET_OS_process_kill (proc,
+                                           SIGTERM))
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+        "Error shutting down communicator with SIGERM, trying SIGKILL\n");
+    if (GNUNET_OK != GNUNET_OS_process_kill (proc,
+                                             SIGKILL))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+          "Error shutting down communicator with SIGERM and SIGKILL\n");
+    }
+  }
+  GNUNET_OS_process_destroy (proc);
+}
+
+
 /**
  * @brief Start the communicator
  *
  * @param cfgname Name of the communicator
  */
 static void
-communicator_start (const char *cfgname)
+communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
 {
   char *binary;
-  struct GNUNET_CONFIGURATION_Handle *cfg;
-  struct GNUNET_OS_Process *proc;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
       "communicator_start\n");
   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-communicator-unix");
-  cfg = GNUNET_CONFIGURATION_create ();
-  proc =
+  tc_h->c_proc =
     GNUNET_OS_start_process (GNUNET_YES,
                              GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
                              NULL, NULL, NULL,
                              binary,
                              "./gnunet-communicator-unix",
                              "-c",
-                             cfgname,
+                             tc_h->cfg_filename,
                              NULL);
-  if (NULL == proc)
+  if (NULL == tc_h->c_proc)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Failed to start communicator!");
     return;
   }
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONFIGURATION_load (cfg,
-                                            cfgname));
   LOG (GNUNET_ERROR_TYPE_DEBUG,
       "started communicator\n");
   GNUNET_free (binary);
+  /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator,
+                                            tc_h->c_proc);
 }
 
 
@@ -227,11 +349,13 @@ GNUNET_TRANSPORT_TESTING_transport_communicator_service_start
    //GNUNET_TRANSPORT_TESTING_Callback4 cb4,
    void *cb_cls)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg;
+  struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
 
-  cfg = GNUNET_CONFIGURATION_create ();
+  tc_h = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
+  tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
+  tc_h->cfg = GNUNET_CONFIGURATION_create ();
   if ( (GNUNET_SYSERR ==
-        GNUNET_CONFIGURATION_load (cfg,
+        GNUNET_CONFIGURATION_load (tc_h->cfg,
                                    cfg_filename)) )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -239,11 +363,15 @@ GNUNET_TRANSPORT_TESTING_transport_communicator_service_start
                   cfg_filename);
     return NULL;
   }
+  tc_h->communicator_available = communicator_available;
+  tc_h->communicator_available_cls = cb_cls;
+
   /* Start communicator part of service */
-  transport_communicator_start (communicator_available, cfg);
+  transport_communicator_start (tc_h);
 
   /* Schedule start communicator */
-  communicator_start ("test_communicator_1.conf");
+  communicator_start (tc_h);
+  return tc_h;
 }
 
 //void
index b4d4fd3be418d0bfdaba7215360ed8697ca559c6..c5adda4ebb050747f6de38000b1da8d220b077e9 100644 (file)
@@ -29,6 +29,9 @@
 #include "gnunet_ats_transport_service.h"
 #include "transport.h"
 
+
+struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle;
+
 /**
  * @brief Function signature for callbacks that are called when new communicators become available
  *