summaryrefslogtreecommitdiff
path: root/src/analysis/db/cdb.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-12-17 19:00:29 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-12-17 19:00:29 (GMT)
commit706710aef28a0af4bb8aa343c2631a2139d00955 (patch)
tree08be7ab2f974bdc3dab6ecde426c069adb4e077b /src/analysis/db/cdb.c
parent97fa09113c7988e4b4639190ba9bc51f9ced4d33 (diff)
Updated the connection protocol.
Diffstat (limited to 'src/analysis/db/cdb.c')
-rw-r--r--src/analysis/db/cdb.c111
1 files changed, 58 insertions, 53 deletions
diff --git a/src/analysis/db/cdb.c b/src/analysis/db/cdb.c
index 829be19..c2855f7 100644
--- a/src/analysis/db/cdb.c
+++ b/src/analysis/db/cdb.c
@@ -922,14 +922,33 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
status = g_db_collection_unpack(collec, &in_pbuf, archive->db);
if (!status) goto gcap_bad_exchange;
- printf("## CDB ## Got something for collection %p...\n", collec);
+ break;
+
+ case DBC_GET_ALL_ITEMS:
+
+ init_packed_buffer(&out_pbuf);
+
+ status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SET_ALL_ITEMS },
+ sizeof(uint32_t), true);
+ if (!status) goto gcap_bad_reply;
+
+ status = pack_all_collection_updates(archive->collections, &out_pbuf);
+ if (!status) goto gcap_bad_reply;
- //GDbCollection *find_collection_in_list(GList *, uint32_t);
+ status = ssl_send_packed_buffer(&out_pbuf, archive->clients[i].ssl_fd);
+ if (!status) goto gcap_bad_reply;
- //static GGenConfig *find_collection_in_list(GList *list, uint32_t id)
+ exit_packed_buffer(&out_pbuf);
break;
+ case DBC_SET_ALL_ITEMS:
+ asprintf(&msg, _("This command is not available on this side: 0x%08x"), command);
+ LOG_ERROR(LMT_ERROR, msg);
+ free(msg);
+ goto gcap_bad_exchange;
+ break;
+
case DBC_SET_LAST_ACTIVE:
status = update_activity_in_collections(archive->collections, &in_pbuf, archive->db);
@@ -1005,69 +1024,55 @@ static void *g_cdb_archive_process(GCdbArchive *archive)
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 */
- /* Envoi des mises à jour au nouveau client... */
-
- init_packed_buffer(&out_pbuf);
-
- status = true;
+ /**
+ * La situation est un peu compliquée lors de l'accueil d'un nouveau client :
+ *
+ * - soit on envoie tous les éléments à prendre en compte, mais on doit
+ * bloquer la base de données jusqu'à l'intégration pleine et entière
+ * du client, afin que ce dernier ne loupe pas l'envoi d'un nouvel
+ * élément entre temps.
+ *
+ * - soit on intègre le client et celui ci demande des mises à jour
+ * collection par collection ; c'est également à lui qui revient le rejet
+ * des éléments envoyés en solitaires avant la réception de la base
+ * complète.
+ *
+ * On fait le choix du second scenario ici, du fait de la difficulté
+ * de maîtriser facilement la reconstitution d'une liste de clients dans
+ * g_cdb_archive_process() depuis un autre flot d'exécution.
+ */
+
+ /* Ajout dans la liste officielle */
- 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);
-
- }
+ g_mutex_lock(&archive->clients_access);
- if (status && get_packed_buffer_payload_length(&out_pbuf) > 0)
- status = ssl_send_packed_buffer(&out_pbuf, fd);
+ archive->clients = realloc(archive->clients, ++archive->count * sizeof(cdb_client));
- exit_packed_buffer(&out_pbuf);
+ archive->clients[archive->count - 1].ssl_fd = fd;
+ dup_into_rle_string(&archive->clients[archive->count - 1].user, get_rle_string(user));
- if (!status)
- LOG_ERROR(LMT_ERROR, _("Failed to add a client"));
+ /* Démarrage ou redémarrage du processus d'écoute */
- else
+ if (archive->process == NULL)
{
- /* Ajout dans la liste officielle */
+ archive->process = g_thread_new("cdb_process", (GThreadFunc)g_cdb_archive_process, archive);
- g_mutex_lock(&archive->clients_access);
-
- archive->clients = realloc(archive->clients, ++archive->count * sizeof(cdb_client));
+ /* On attend que le processus parallèle soit prêt */
- archive->clients[archive->count - 1].ssl_fd = fd;
- dup_into_rle_string(&archive->clients[archive->count - 1].user, get_rle_string(user));
+ process_id = &archive->process_id;
- /* Démarrage ou redémarrage du processus d'écoute */
-
- if (archive->process == NULL)
- {
- archive->process = g_thread_new("cdb_process", (GThreadFunc)g_cdb_archive_process, archive);
-
- /* On attend que le processus parallèle soit prêt */
-
- process_id = &archive->process_id;
-
- 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);
-
- g_mutex_unlock(&archive->clients_access);
+ 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);
+
+ g_mutex_unlock(&archive->clients_access);
}