gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [gnunet] 03/03: TNG testing: Cleanup and Structuring


From: gnunet
Subject: [GNUnet-SVN] [gnunet] 03/03: TNG testing: Cleanup and Structuring
Date: Thu, 18 Apr 2019 15:37:55 +0200

This is an automated email from the git hooks/post-receive script.

julius-buenger pushed a commit to branch master
in repository gnunet.

commit 80588e7d5d142d453ece32cd82b54e36444bf138
Author: Julius Bünger <address@hidden>
AuthorDate: Thu Apr 18 15:35:42 2019 +0200

    TNG testing: Cleanup and Structuring
---
 src/transport/test_communicator_unix.c |   5 +
 src/transport/transport-testing2.c     | 192 +++++++++++++++++++++++++++------
 src/transport/transport-testing2.h     |   3 +
 3 files changed, 168 insertions(+), 32 deletions(-)

diff --git a/src/transport/test_communicator_unix.c 
b/src/transport/test_communicator_unix.c
index ef7ed32c0..fd189659c 100644
--- a/src/transport/test_communicator_unix.c
+++ b/src/transport/test_communicator_unix.c
@@ -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
diff --git a/src/transport/transport-testing2.c 
b/src/transport/transport-testing2.c
index df48e7782..51791e981 100644
--- a/src/transport/transport-testing2.c
+++ b/src/transport/transport-testing2.c
@@ -37,6 +37,57 @@
 #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
diff --git a/src/transport/transport-testing2.h 
b/src/transport/transport-testing2.h
index b4d4fd3be..c5adda4eb 100644
--- a/src/transport/transport-testing2.h
+++ b/src/transport/transport-testing2.h
@@ -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
  *

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]