From df05e2a9ec96696931644ec3ca82478d80516afd Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 29 Sep 2025 18:05:27 +0000 Subject: [PATCH] daemon: Initialize all graphs when the daemon starts Signed-off-by: Michael Tremer --- src/daemon/graph.c | 8 +++++- src/daemon/graph.h | 10 ++++++- src/daemon/graphs.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/daemon/graph.c b/src/daemon/graph.c index ea0049b..8a2065c 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -31,6 +31,9 @@ struct collecty_graph { // Daemon collecty_daemon* daemon; + + // Implementation + const collecty_graph_impl* impl; }; static void collecty_graph_free(collecty_graph* self) { @@ -42,7 +45,7 @@ static void collecty_graph_free(collecty_graph* self) { } int collecty_graph_create(collecty_graph** graph, - collecty_ctx* ctx, collecty_daemon* daemon) { + collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl) { collecty_graph* self = NULL; // Allocate some memory @@ -59,6 +62,9 @@ int collecty_graph_create(collecty_graph** graph, // Store a reference to the daemon self->daemon = collecty_daemon_ref(daemon); + // Store the implementation + self->impl = impl; + // Return the pointer *graph = self; return 0; diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 0cf37b0..f0828d8 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -26,7 +26,15 @@ typedef struct collecty_graph collecty_graph; #include "ctx.h" #include "daemon.h" -int collecty_graph_create(collecty_graph** graph, collecty_ctx* ctx, collecty_daemon* daemon); +typedef struct collecty_graph_impl { + const char* name; + + // Available + int (*available)(collecty_ctx* ctx, collecty_daemon* daemon); +} collecty_graph_impl; + +int collecty_graph_create(collecty_graph** graph, + collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl); collecty_graph* collecty_graph_ref(collecty_graph* self); collecty_graph* collecty_graph_unref(collecty_graph* self); diff --git a/src/daemon/graphs.c b/src/daemon/graphs.c index 9daf43a..137849e 100644 --- a/src/daemon/graphs.c +++ b/src/daemon/graphs.c @@ -23,17 +23,70 @@ #include "ctx.h" #include "daemon.h" +#include "graph.h" #include "graphs.h" +// Register all graphs +static const collecty_graph_impl* graph_impls[] = { + NULL, +}; + struct collecty_graphs { collecty_ctx* ctx; int nrefs; // Daemon collecty_daemon* daemon; + + // Graphs + collecty_graph** graphs; + unsigned int num_graphs; }; +static int collecty_graphs_init(collecty_graphs* self) { + collecty_graph** graphs = NULL; + collecty_graph* graph = NULL; + int r; + + // Initialize all graphs + for (const collecty_graph_impl** impl = graph_impls; *impl; impl++) { + r = collecty_graph_create(&graph, self->ctx, self->daemon, *impl); + if (r < 0) + return r; + + // Make space to store the graph + graphs = reallocarray(self->graphs, self->num_graphs + 1, sizeof(*self->graphs)); + if (!graphs) { + r = -errno; + goto ERROR; + } + + // Store a reference to the graph in the array + graphs[self->num_graphs++] = collecty_graph_ref(graph); + + // Replace the array + self->graphs = graphs; + + // Unref the graph + collecty_graph_unref(graph); + graph = NULL; + } + + return 0; + +ERROR: + if (graph) + collecty_graph_unref(graph); + + return r; +} + static void collecty_graphs_free(collecty_graphs* self) { + if (self->graphs) { + for (unsigned int i = 0; i < self->num_graphs; i++) + collecty_graph_unref(self->graphs[i]); + free(self->graphs); + } if (self->daemon) collecty_daemon_unref(self->daemon); if (self->ctx) @@ -44,6 +97,7 @@ static void collecty_graphs_free(collecty_graphs* self) { int collecty_graphs_create(collecty_graphs** graphs, collecty_ctx* ctx, collecty_daemon* daemon) { collecty_graphs* self = NULL; + int r; // Allocate some memory self = calloc(1, sizeof(*self)); @@ -59,9 +113,20 @@ int collecty_graphs_create(collecty_graphs** graphs, // Store a reference to the daemon self->daemon = collecty_daemon_ref(daemon); + // Setup all graphs + r = collecty_graphs_init(self); + if (r < 0) + goto ERROR; + // Return the pointer *graphs = self; return 0; + +ERROR: + if (self) + collecty_graphs_unref(self); + + return r; } collecty_graphs* collecty_graphs_ref(collecty_graphs* self) { -- 2.47.3