From 9a599d96798a737c223cdbab99ba8cabf3f3ea64 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 30 Sep 2025 16:34:25 +0000 Subject: [PATCH] graph: Add a Render() method to the bus to render graphs Signed-off-by: Michael Tremer --- src/daemon/graph-bus.c | 46 +++++++++++++++++++++++++++++++++++++++++- src/daemon/graph.c | 15 ++++++++++++++ src/daemon/graph.h | 3 +++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/daemon/graph-bus.c b/src/daemon/graph-bus.c index f2820ec..b26ef6d 100644 --- a/src/daemon/graph-bus.c +++ b/src/daemon/graph-bus.c @@ -18,6 +18,8 @@ # # #############################################################################*/ +#include + #include #include "graph.h" @@ -93,11 +95,53 @@ ERROR: return r; } +static int collecty_graph_bus_render(sd_bus_message* m, void* data, sd_bus_error* error) { + collecty_graph* graph = data; + sd_bus_message* reply = NULL; + const char* format = NULL; + char* buffer = NULL; + size_t length = 0; + int r; + + // Parse the arguments + r = sd_bus_message_read(m, "s", &format); + if (r < 0) + goto ERROR; + + // Render the graph + r = collecty_graph_render(graph, format, &buffer, &length); + if (r < 0) + goto ERROR; + + // Make the reply message + r = sd_bus_message_new_method_return(m, &reply); + if (r < 0) + goto ERROR; + + // Append the graph + r = sd_bus_message_append_array(reply, 'y', buffer, length); + if (r < 0) + goto ERROR; + + // Send the reply + r = sd_bus_send(NULL, reply, NULL); + +ERROR: + if (reply) + sd_bus_message_unref(reply); + if (buffer) + free(buffer); + + return r; +} + + static const sd_bus_vtable collecty_graph_vtable[] = { SD_BUS_VTABLE_START(0), // Operations - //XXX TODO + SD_BUS_METHOD_WITH_ARGS("Render", SD_BUS_ARGS("s", format), SD_BUS_RESULT("ay", graph), + collecty_graph_bus_render, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END }; diff --git a/src/daemon/graph.c b/src/daemon/graph.c index d03f8f1..888a45e 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -104,3 +104,18 @@ char* collecty_graph_get_bus_path(collecty_graph* self) { return path; } + +int collecty_graph_render(collecty_graph* self, + const char* format, char** buffer, size_t* length) { + char* p = NULL; + int r; + + r = asprintf(&p, "GRAPH"); + if (r < 0) + return r; + + *buffer = p; + *length = r; + + return 0; +} diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 8330c1e..01509be 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -42,4 +42,7 @@ collecty_graph* collecty_graph_unref(collecty_graph* self); const char* collecty_graph_get_name(collecty_graph* self); char* collecty_graph_get_bus_path(collecty_graph* self); +int collecty_graph_render(collecty_graph* self, + const char* format, char** buffer, size_t* length); + #endif /* COLLECTY_GRAPH_H */ -- 2.47.3