From bd40ddc2660dff7c809fc4ffe39dca8d01e71454 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sat, 1 Dec 2018 19:18:05 +0100
Subject: Cleaned the client code.

---
 src/analysis/db/client.c | 41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c
index 2256a21..f97335c 100644
--- a/src/analysis/db/client.c
+++ b/src/analysis/db/client.c
@@ -65,6 +65,7 @@ struct _GDbClient
 
     int fd;                                 /* Canal de communication      */
     SSL *tls_fd;                            /* Même canal, mais sécurisé   */
+    char *desc;                             /* Description du lien         */
 
     GMutex sending_lock;                    /* Concurrence des envois      */
     GThread *update;                        /* Procédure de traitement     */
@@ -89,7 +90,7 @@ static void g_db_client_init(GDbClient *);
 static void g_db_client_finalize(GDbClient *);
 
 /* Démarre réellement la connexion à la base de données. */
-static bool g_db_client_start_common(GDbClient *, const char *);
+static bool g_db_client_start_common(GDbClient *, char *);
 
 /* Assure l'accueil des nouvelles mises à jour. */
 static void *g_db_client_update(GDbClient *);
@@ -143,6 +144,7 @@ static void g_db_client_init(GDbClient *client)
 
     client->fd = -1;
     client->tls_fd = NULL;
+    client->desc = NULL;
 
 }
 
@@ -176,6 +178,9 @@ static void g_db_client_finalize(GDbClient *client)
 
     assert(client->tls_fd == NULL);
 
+    if (client->desc != NULL)
+        free(client->desc);
+
     G_OBJECT_CLASS(g_db_client_parent_class)->finalize(G_OBJECT(client));
 
 }
@@ -260,8 +265,6 @@ bool g_db_client_start_internal(GDbClient *client)
 
     status = g_db_client_start_common(client, desc);
 
-    free(desc);
-
     if (!status)
         goto gdcsi_no_listening;
 
@@ -332,8 +335,6 @@ bool g_db_client_start_remote(GDbClient *client, const char *host, unsigned shor
 
     status = g_db_client_start_common(client, desc);
 
-    free(desc);
-
     if (!status)
         goto gdcsr_no_listening;
 
@@ -363,7 +364,7 @@ bool g_db_client_start_remote(GDbClient *client, const char *host, unsigned shor
 *                                                                             *
 ******************************************************************************/
 
-static bool g_db_client_start_common(GDbClient *client, const char *desc)
+static bool g_db_client_start_common(GDbClient *client, char *desc)
 {
     const SSL_METHOD *method;               /* Mode du canal sécurisé      */
     int ret;                                /* Bilan d'un appel            */
@@ -378,6 +379,8 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)
     uint32_t data;                          /* Mot de données lues         */
     DBError error;                          /* Validation de la connexion  */
 
+    client->desc = desc;
+
     /**
      * Mise en place d'un environnement sécurisé.
      */
@@ -432,7 +435,7 @@ static bool g_db_client_start_common(GDbClient *client, const char *desc)
     status = pack_rle_string(&client->hash, &out_pbuf);
     if (!status) goto setup_error;
 
-    dup_into_rle_string(&user, client->author); /* FIXME : src ? */
+    dup_into_rle_string(&user, client->author);
 
     status = pack_rle_string(&user, &out_pbuf);
     if (!status) goto setup_error;
@@ -577,6 +580,7 @@ static void *g_db_client_update(GDbClient *client)
     uint32_t command;                       /* Commande de la requête      */
     DBError error;                          /* Bilan d'une commande passée */
     GDbCollection *collec;                  /* Collection visée au final   */
+    char *msg;                              /* Message d'erreur à imprimer */
 
     fds.fd = client->fd;
     fds.events = POLLIN | POLLPRI;
@@ -639,19 +643,19 @@ static void *g_db_client_update(GDbClient *client)
 
  gdcu_bad_exchange:
 
-                printf("Bad reception...\n");
-
-                /* TODO : close conn */
+            asprintf(&msg, _("Bad reception from %s"), client->desc);
 
-                ;
+            LOG_ERROR(LMT_ERROR, msg);
 
+            free(msg);
 
+            break;
 
         }
 
     }
 
-    //g_db_client_stop(client);
+    g_db_client_stop(client);
 
     exit_packed_buffer(&in_pbuf);
 
@@ -680,17 +684,24 @@ void g_db_client_stop(GDbClient *client)
 
     if (client->fd != -1)
     {
-        assert(client->tls_ctx == NULL);
+        /**
+         * Si la fermture est forcée, le thread de traitement va terminer en erreur.
+         * Donc cette fonction sera appelée deux fois. Seule la première va affecter
+         * le contexte, donc on le peut pas s'assurer de la condition suivante dans
+         * tous les cas.
+         */
+
+        /*assert(client->tls_ctx == NULL);*/
         return;
     }
 
     ret = close(client->fd);
     if (ret == -1) perror("close");
 
-    g_thread_join(client->update);
-
     client->fd = -1;
 
+    g_thread_join(client->update);
+
     /* Environnement TLS */
 
     SSL_CTX_free(client->tls_ctx);
-- 
cgit v0.11.2-87-g4458