summaryrefslogtreecommitdiff
path: root/src/analysis/db/cdb.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-12-02 10:27:31 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-12-02 10:27:31 (GMT)
commitd354abf57454771291d7fd4cba4d288a28b46625 (patch)
treeabd2418022fed4bc776407cd4f2d7e7e0abfc1d1 /src/analysis/db/cdb.c
parent1f564b89687f8c3acdae4858449d0d88cc660563 (diff)
Cleaned the DB code a little bit.
Diffstat (limited to 'src/analysis/db/cdb.c')
-rw-r--r--src/analysis/db/cdb.c145
1 files changed, 83 insertions, 62 deletions
diff --git a/src/analysis/db/cdb.c b/src/analysis/db/cdb.c
index fd1f5cf..829be19 100644
--- a/src/analysis/db/cdb.c
+++ b/src/analysis/db/cdb.c
@@ -38,6 +38,7 @@
#include <sys/stat.h>
+#include <i18n.h>
#include <config.h>
@@ -49,6 +50,7 @@
#include "../../common/pathname.h"
#include "../../common/xml.h"
#include "../../core/collections.h"
+#include "../../core/logs.h"
#include "../../core/params.h"
@@ -150,6 +152,8 @@ static void on_collection_changed(GDbCollection *, DBAction, GDbItem *, GCdbArch
/* Assure le traitement des requêtes de clients. */
static void *g_cdb_archive_process(GCdbArchive *);
+/* Dissocie un utilisateur de l'archive. */
+static void g_cdb_archive_remove_client(GCdbArchive *, size_t);
@@ -779,24 +783,17 @@ static void on_collection_changed(GDbCollection *collec, DBAction action, GDbIte
g_mutex_lock(&archive->clients_access);
for (i = 0; i < archive->count && status; i++)
+ {
status = ssl_send_packed_buffer(&pbuf, archive->clients[i].ssl_fd);
- g_mutex_unlock(&archive->clients_access);
-
- exit_packed_buffer(&pbuf);
-
- if (!status)
- goto occ_error;
-
-
- printf("CHANGED for %d clients !!\n", (int)archive->count);
-
+ if (!status)
+ LOG_ERROR(LMT_ERROR, _("Failed to send some DB update"));
+ }
- occ_error:
+ g_mutex_unlock(&archive->clients_access);
- /* TODO : close() */
- ;
+ exit_packed_buffer(&pbuf);
}
@@ -826,6 +823,7 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
DBError error; /* Bilan d'une opération */
packed_buffer out_pbuf; /* Tampon d'émission */
GDbCollection *collec; /* Collection visée au final */
+ char *msg; /* Erreur à faire remonter */
void interrupt_poll_with_sigusr1(int sig) { };
@@ -845,7 +843,7 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
g_mutex_lock(&archive->clients_access);
nfds = archive->count;
- fds = (struct pollfd *)realloc(fds, nfds * sizeof(struct pollfd));
+ fds = realloc(fds, nfds * sizeof(struct pollfd));
for (i = 0; i < nfds; i++)
{
@@ -940,7 +938,9 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
break;
default:
- printf("bad command :: 0x%08x\n", command);
+ asprintf(&msg, _("Bad protocol command: 0x%08x"), command);
+ LOG_ERROR(LMT_ERROR, msg);
+ free(msg);
goto gcap_bad_exchange;
break;
@@ -956,16 +956,13 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
gcap_bad_exchange:
- printf("Bad exchange...\n");
+ LOG_ERROR(LMT_ERROR, _("Bad exchange"));
exit_packed_buffer(&in_pbuf);
gcap_closed_exchange:
- /* TODO : close conn */
-
- ;
-
+ g_cdb_archive_remove_client(archive, i);
}
@@ -987,13 +984,11 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
if (fds != NULL)
free(fds);
-
return NULL;
}
-
/******************************************************************************
* *
* Paramètres : archive = archive à connecter avec un utilisateur. *
@@ -1002,85 +997,111 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
* *
* Description : Associe un nouvel utilisateur à l'archive. *
* *
-* Retour : Indication d'une éventuelle erreur lors de l'opération. *
+* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
-DBError g_cdb_archive_add_client(GCdbArchive *archive, SSL *fd, const rle_string *user)
+void g_cdb_archive_add_client(GCdbArchive *archive, SSL *fd, const rle_string *user)
{
-
+ packed_buffer out_pbuf; /* Tampon d'émission */
+ bool status; /* Bilan d'un envoi de retour */
GList *iter; /* Boucle de parcours */
GDbCollection *collec; /* Collection visée manipulée */
volatile pthread_t *process_id; /* Identifiant de la procédure */
- packed_buffer out_pbuf; /* Tampon d'émission */
- bool status; /* Bilan d'un envoi de retour */
-
+ /* Envoi des mises à jour au nouveau client... */
+ init_packed_buffer(&out_pbuf);
+ status = true;
+ for (iter = g_list_first(archive->collections);
+ iter != NULL && status;
+ iter = g_list_next(iter))
+ {
+ collec = G_DB_COLLECTION(iter->data);
- g_mutex_lock(&archive->clients_access);
+ status = g_db_collection_pack_all_updates(collec, &out_pbuf);
- /* Ajout dans la liste officielle */
+ }
- archive->clients = (cdb_client *)realloc(archive->clients, ++archive->count * sizeof(cdb_client));
+ if (status && get_packed_buffer_payload_length(&out_pbuf) > 0)
+ status = ssl_send_packed_buffer(&out_pbuf, fd);
- archive->clients[archive->count - 1].ssl_fd = fd;
- dup_into_rle_string(&archive->clients[archive->count - 1].user, get_rle_string(user));
+ exit_packed_buffer(&out_pbuf);
- /* Démarrage ou redémarrage du processus d'écoute */
+ if (!status)
+ LOG_ERROR(LMT_ERROR, _("Failed to add a client"));
- if (archive->process == NULL)
+ else
{
- archive->process = g_thread_new("cdb_process", (GThreadFunc)g_cdb_archive_process, archive);
+ /* Ajout dans la liste officielle */
- /* On attend que le processus parallèle soit prêt */
+ g_mutex_lock(&archive->clients_access);
- process_id = &archive->process_id;
+ archive->clients = realloc(archive->clients, ++archive->count * sizeof(cdb_client));
- g_mutex_lock(&archive->id_access);
- while (process_id == 0)
- g_cond_wait(&archive->id_cond, &archive->id_access);
- g_mutex_unlock(&archive->id_access);
+ archive->clients[archive->count - 1].ssl_fd = fd;
+ dup_into_rle_string(&archive->clients[archive->count - 1].user, get_rle_string(user));
- }
- else
- pthread_kill(archive->process_id, SIGUSR1);
+ /* Démarrage ou redémarrage du processus d'écoute */
- g_mutex_unlock(&archive->clients_access);
+ if (archive->process == NULL)
+ {
+ archive->process = g_thread_new("cdb_process", (GThreadFunc)g_cdb_archive_process, archive);
- /* Envoi des mises à jour au nouveau client... */
+ /* On attend que le processus parallèle soit prêt */
- init_packed_buffer(&out_pbuf);
+ process_id = &archive->process_id;
- status = true;
+ g_mutex_lock(&archive->id_access);
+ while (process_id == 0)
+ g_cond_wait(&archive->id_cond, &archive->id_access);
+ g_mutex_unlock(&archive->id_access);
+ }
+ else
+ pthread_kill(archive->process_id, SIGUSR1);
- /* TODO : lock ? */
+ g_mutex_unlock(&archive->clients_access);
- for (iter = g_list_first(archive->collections);
- iter != NULL && status;
- iter = g_list_next(iter))
- {
- collec = G_DB_COLLECTION(iter->data);
+ }
- status = g_db_collection_pack_all_updates(collec, &out_pbuf);
+}
- }
- if (status && get_packed_buffer_payload_length(&out_pbuf) > 0)
- status = ssl_send_packed_buffer(&out_pbuf, fd);
+/******************************************************************************
+* *
+* Paramètres : archive = archive à connecter avec un utilisateur. *
+* index = indice de l'utilisateur concerné. *
+* *
+* Description : Dissocie un utilisateur de l'archive. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
- exit_packed_buffer(&out_pbuf);
+static void g_cdb_archive_remove_client(GCdbArchive *archive, size_t index)
+{
+ cdb_client *client; /* Client à traiter */
+ client = &archive->clients[index];
+ g_mutex_lock(&archive->clients_access);
- return DBE_NONE; ////
+ SSL_free(client->ssl_fd);
+ exit_rle_string(&client->user);
-}
+ if ((index + 1) < archive->count)
+ memmove(&archive->clients[index], &archive->clients[index + 1],
+ (archive->count - index - 1) * sizeof(cdb_client));
+ archive->clients = realloc(archive->clients, --archive->count * sizeof(cdb_client));
+ g_mutex_unlock(&archive->clients_access);
+}