diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2019-10-19 12:50:37 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2019-10-19 12:50:37 (GMT) |
commit | d7c69bcb27a7d06932cd25021144b6cbbe4eb82f (patch) | |
tree | b014aa31a1c9d9ce4afddf505b727cabacf15e44 /src/analysis/db/client.c | |
parent | bcdf953ef6616c404d013f3473fb12a7bf43440b (diff) |
Exchanged the list of all snapshots.
Diffstat (limited to 'src/analysis/db/client.c')
-rw-r--r-- | src/analysis/db/client.c | 185 |
1 files changed, 182 insertions, 3 deletions
diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c index 3372c74..2cab655 100644 --- a/src/analysis/db/client.c +++ b/src/analysis/db/client.c @@ -25,6 +25,7 @@ #include <assert.h> +#include <malloc.h> #include <netdb.h> #include <poll.h> #include <stdio.h> @@ -76,11 +77,13 @@ struct _GHubClient bool can_get_updates; /* Réception de maj possibles ?*/ GThread *update; /* Procédure de traitement */ - - + snapshot_info_t *snapshots; /* Liste des instantanés */ + size_t snap_count; /* Taille de cette liste */ + GMutex snap_lock; /* Concurrence des accès */ snapshot_id_t current; /* Instantané courant */ bool has_current; /* Validité de l'identifiant */ + GMutex cur_lock; /* Concurrence des accès */ }; @@ -110,6 +113,9 @@ static bool g_hub_client_start_common(GHubClient *, char *); /* Assure l'accueil des nouvelles mises à jour. */ static void *g_hub_client_update(GHubClient *); +/* Met à jour la liste des instantanés courants. */ +static bool g_hub_client_update_snapshots(GHubClient *, packed_buffer *); + /* Identifie le canal de communication pour envois au serveur. */ static SSL *g_hub_client_get_ssl_fd(GHubClient *); @@ -160,6 +166,9 @@ static void g_hub_client_class_init(GHubClientClass *klass) static void g_hub_client_init(GHubClient *client) { + init_static_rle_string(&client->hash, NULL); + client->collections = NULL; + client->working = NULL; client->tls_ctx = NULL; @@ -168,7 +177,16 @@ static void g_hub_client_init(GHubClient *client) client->tls_fd = NULL; client->desc = NULL; + g_mutex_init(&client->sending_lock); + client->can_get_updates = false; + client->update = NULL; + + client->snapshots = NULL; + client->snap_count = 0; + g_mutex_init(&client->snap_lock); + client->has_current = false; + g_mutex_init(&client->cur_lock); } @@ -189,6 +207,12 @@ static void g_hub_client_dispose(GHubClient *client) { g_hub_client_stop(client); + g_mutex_clear(&client->cur_lock); + + g_mutex_clear(&client->snap_lock); + + g_mutex_clear(&client->sending_lock); + G_OBJECT_CLASS(g_hub_client_parent_class)->dispose(G_OBJECT(client)); } @@ -208,18 +232,28 @@ static void g_hub_client_dispose(GHubClient *client) static void g_hub_client_finalize(GHubClient *client) { + size_t i; /* Boucle de parcours */ + unset_rle_string(&client->hash); if (client->working != NULL) free(client->working); assert(client->tls_ctx == NULL); - assert(client->tls_fd == NULL); if (client->desc != NULL) free(client->desc); + if (client->snapshots != NULL) + { + for (i = 0; i < client->snap_count; i++) + exit_snapshot_info(&client->snapshots[i]); + + free(client->snapshots); + + } + G_OBJECT_CLASS(g_hub_client_parent_class)->finalize(G_OBJECT(client)); } @@ -703,6 +737,13 @@ static void *g_hub_client_update(GHubClient *client) init_packed_buffer(&out_pbuf); + status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_GET_SNAPSHOTS }, sizeof(uint32_t), true); + if (!status) + { + exit_packed_buffer(&out_pbuf); + goto exit; + } + status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_GET_CUR_SNAPSHOT }, sizeof(uint32_t), true); if (!status) { @@ -814,6 +855,13 @@ static void *g_hub_client_update(GHubClient *client) client->can_get_updates = (tmp8 == 0x1); break; + case DBC_SNAPSHOTS_UPDATED: + + status = g_hub_client_update_snapshots(client, &in_pbuf); + if (!status) goto gdcu_bad_exchange; + + break; + case DBC_GET_CUR_SNAPSHOT: log_variadic_message(LMT_INFO, _("This command is not available on this side: 0x%08x"), command); @@ -825,9 +873,13 @@ static void *g_hub_client_update(GHubClient *client) status = unpack_snapshot_id(&id, &in_pbuf); if (!status) goto gdcu_bad_exchange; + g_mutex_lock(&client->cur_lock); + copy_snapshot_id(&client->current, &id); client->has_current = true; + g_mutex_unlock(&client->cur_lock); + break; case DBC_SET_CUR_SNAPSHOT: @@ -872,6 +924,82 @@ static void *g_hub_client_update(GHubClient *client) /****************************************************************************** * * * Paramètres : client = client pour les accès distants à manipuler. * +* pbuf = données présentes à traiter. * +* * +* Description : Met à jour la liste des instantanés courants. * +* * +* Retour : true si la liste retournée est valide, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_hub_client_update_snapshots(GHubClient *client, packed_buffer *pbuf) +{ + bool result; /* Validité à retourner */ + size_t i; /* Boucle de parcours */ + char id[SNAP_ID_HEX_SZ]; /* Caractères hexadécimaux */ + snapshot_info_t info; /* Description d'instantané */ + snapshot_info_t *dest; /* Destination de description */ + + result = true; + + g_mutex_lock(&client->snap_lock); + + if (client->snapshots != NULL) + { + for (i = 0; i < client->snap_count; i++) + exit_snapshot_info(&client->snapshots[i]); + + free(client->snapshots); + + client->snapshots = NULL; + client->snap_count = 0; + + } + + do + { + result = peek_packed_buffer(pbuf, id, SNAP_ID_HEX_SZ, false); + if (!result) break; + + if (strncmp(id, SNAPSHOT_END_MARK, SNAP_ID_HEX_SZ) == 0) + { + advance_packed_buffer(pbuf, SNAP_ID_HEX_SZ); + break; + } + + else + { + setup_empty_snapshot_info(&info); + + result = unpack_snapshot_info(&info, pbuf); + if (!result) break; + + client->snapshots = realloc(client->snapshots, ++client->snap_count * sizeof(snapshot_info_t)); + + dest = &client->snapshots[client->snap_count - 1]; + + setup_empty_snapshot_info(dest); + copy_snapshot_info(dest, &info); + + exit_snapshot_info(&info); + + } + + } + while (true); + + g_mutex_unlock(&client->snap_lock); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : client = client pour les accès distants à manipuler. * * * * Description : Arrête la connexion à la base de données. * * * @@ -1135,6 +1263,53 @@ bool g_hub_client_set_last_active(GHubClient *client, timestamp_t timestamp) /****************************************************************************** * * * Paramètres : client = client pour les accès distants à manipuler. * +* info = description des instantanés présents. [OUT] * +* count = taille de la liste retournée. [OUT] * +* * +* Description : Fournit la liste des instantanés existants. * +* * +* Retour : true si la liste retournée est valide, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_hub_client_get_snapshots(GHubClient *client, snapshot_info_t **info, size_t *count) +{ + bool result; /* Validité à retourner */ + size_t i; /* Boucle de parcours */ + snapshot_info_t *dest; /* Destination de description */ + + g_mutex_lock(&client->snap_lock); + + result = (client->snap_count > 0); + + if (result) + { + *info = malloc(client->snap_count * sizeof(snapshot_info_t)); + *count = client->snap_count; + + for (i = 0; i < client->snap_count; i++) + { + dest = &(*info)[i]; + + setup_empty_snapshot_info(dest); + copy_snapshot_info(dest, &client->snapshots[i]); + + } + + } + + g_mutex_unlock(&client->snap_lock); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : client = client pour les accès distants à manipuler. * * id = identifiant d'instantané à renseigner. [OUT] * * * * Description : Fournit l'identifiant de l'instantané courant. * @@ -1149,11 +1324,15 @@ bool g_hub_client_get_current_snapshot(GHubClient *client, snapshot_id_t *id) { bool result; /* Validité à retourner */ + g_mutex_lock(&client->cur_lock); + result = client->has_current; if (result) copy_snapshot_id(id, &client->current); + g_mutex_unlock(&client->cur_lock); + return result; } |