return sd_event_ref(self->loop);
}
+collecty_graphs* collecty_daemon_get_graphs(collecty_daemon* self) {
+ if (self->graphs)
+ return collecty_graphs_ref(self->graphs);
+
+ return NULL;
+}
+
int collecty_daemon_run(collecty_daemon* self) {
int r;
#include "bus.h"
#include "ctx.h"
+#include "graphs.h"
#include "module.h"
int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx);
collecty_daemon* collecty_daemon_unref(collecty_daemon* daemon);
sd_event* collecty_daemon_loop(collecty_daemon* self);
+collecty_graphs* collecty_daemon_get_graphs(collecty_daemon* self);
int collecty_daemon_run(collecty_daemon* self);
static int collecty_graph_node_enumerator(sd_bus* bus,
const char* path, void* data, char*** nodes, sd_bus_error* error) {
- //collecty_daemon* daemon = data;
+ collecty_daemon* daemon = data;
+ collecty_graphs* graphs = NULL;
+ int r = 1;
+
+ // Fetch all graphs
+ graphs = collecty_daemon_get_graphs(daemon);
+ if (!graphs)
+ goto ERROR;
+
+ // Fetch the bus paths
+ *nodes = collecty_graphs_get_bus_paths(graphs);
+ if (*nodes)
+ goto ERROR;
+
+ // Success
+ r = 0;
- // XXX TODO
+ERROR:
+ if (graphs)
+ collecty_graphs_unref(graphs);
- return 0;
+ return r;
}
static int collecty_graph_object_find(sd_bus* bus, const char* path,
const char* interface, void* data, void** found, sd_bus_error* error) {
collecty_daemon* daemon = data;
+ collecty_graphs* graphs = NULL;
collecty_graph* graph = NULL;
char* name = NULL;
int r;
// Decode the path of the requested object
r = sd_bus_path_decode(path, "/org/ipfire/collecty1/graph", &name);
- if (r <= 0)
- return 0;
+ if (r <= 0) {
+ r = 0;
+ goto ERROR;
+ }
+
+ // Fetch all graphs
+ graphs = collecty_daemon_get_graphs(daemon);
+ if (!graphs) {
+ r = 0;
+ goto ERROR;
+ }
-#if 0
// Find the graph
- graph = collecty_daemon_get_graph_by_name(daemon, name);
- if (!graph)
- return 0;
-#endif
+ graph = collecty_graphs_get_by_name(graphs, name);
+ if (!graph) {
+ r = 0;
+ goto ERROR;
+ }
// Match!
- *found = collecty_graph_unref(graph);
+ *found = graph;
+
+ // Success
+ r = 1;
+
+ERROR:
+ if (graphs)
+ collecty_graphs_unref(graphs);
+ if (graph)
+ collecty_graph_unref(graph);
- return 1;
+ return r;
}
static const sd_bus_vtable collecty_graph_vtable[] = {
collecty_graph_free(self);
return NULL;
}
+
+const char* collecty_graph_get_name(collecty_graph* self) {
+ return self->impl->name;
+}
+
+char* collecty_graph_get_bus_path(collecty_graph* self) {
+ const char* name = NULL;
+ char* path = NULL;
+ int r;
+
+ // Fetch the name
+ name = collecty_graph_get_name(self);
+ if (!name)
+ return NULL;
+
+ // Format the bus path
+ r = sd_bus_path_encode("/org/ipfire/collecty1/graph", name, &path);
+ if (r < 0)
+ return NULL;
+
+ return path;
+}
collecty_graph* collecty_graph_ref(collecty_graph* self);
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);
+
#endif /* COLLECTY_GRAPH_H */
collecty_graphs_free(self);
return NULL;
}
+
+collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* name) {
+ const char* n = NULL;
+
+ // Iterate over all graphs to find a match
+ for (unsigned int i = 0; i < self->num_graphs; i++) {
+ // Fetch the name
+ n = collecty_graph_get_name(self->graphs[i]);
+ if (!n)
+ continue;
+
+ // Return the object if the name matches
+ if (strcmp(name, n) == 0)
+ return collecty_graph_ref(self->graphs[i]);
+ }
+
+ return NULL;
+}
+
+char** collecty_graphs_get_bus_paths(collecty_graphs* self) {
+ char** paths = NULL;
+ char* path = NULL;
+
+ // Allocate an array to store all the strings
+ paths = calloc(self->num_graphs + 1, sizeof(*path));
+ if (!paths)
+ goto ERROR;
+
+ // Add all paths for all graphs
+ for (unsigned int i = 0; i < self->num_graphs; i++) {
+ // Fetch the bus path
+ path = collecty_graph_get_bus_path(self->graphs[i]);
+ if (!path)
+ continue;
+
+ // Assign to the output array
+ paths[i] = path;
+ }
+
+ return paths;
+
+ERROR:
+ if (paths) {
+ for (unsigned int i = 0; i < self->num_graphs; i++)
+ if (paths[i])
+ free(paths[i]);
+ free(paths);
+ }
+
+ return NULL;
+}
#include "ctx.h"
#include "daemon.h"
+#include "graph.h"
int collecty_graphs_create(collecty_graphs** graphs, collecty_ctx* ctx, collecty_daemon* daemon);
collecty_graphs* collecty_graphs_ref(collecty_graphs* self);
collecty_graphs* collecty_graphs_unref(collecty_graphs* self);
+collecty_graph* collecty_graphs_get_by_name(collecty_graphs* self, const char* name);
+char** collecty_graphs_get_bus_paths(collecty_graphs* self);
+
#endif /* COLLECTY_GRAPHS_H */