From acc52e700121120726b42a6dadd8c12f030ba349 Mon Sep 17 00:00:00 2001 From: Russell King Date: Sat, 11 Sep 2021 12:25:36 +0100 Subject: resource: split resource operations from resource object structure Move the resource operations out of the resource object structure and make them const. Signed-off-by: Russell King --- event-httpd.c | 17 +++++++++++------ resource.c | 10 ++++++---- resource.h | 9 +++++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/event-httpd.c b/event-httpd.c index 1492fe4..27590dd 100644 --- a/event-httpd.c +++ b/event-httpd.c @@ -83,8 +83,8 @@ static void finish(GObject *source, GAsyncResult *res, gpointer user_data) g_data_input_stream_read_line_finish(c->data, res, &len, &error); - if (c->resource->close) { - c->resource->close(c, c->resource); + if (c->resource->ops->close) { + c->resource->ops->close(c, c->resource); c->resource = NULL; } @@ -106,7 +106,7 @@ static void update(GObject *source, GAsyncResult *res, gpointer user_data) return; } - c->resource->update(c, c->resource, line); + c->resource->ops->update(c, c->resource, line); g_free(line); @@ -227,17 +227,22 @@ static void receive(GObject *source, GAsyncResult *res, gpointer user_data) } c->resource = resource; + if (!resource->ops) { + respond_error(c, 204, "No Content"); + close_client(c); + return; + } // We have a valid resource, start the response switch (method) { case GET: - if (!resource->get) { + if (!resource->ops->get) { respond_error(c, 204, "No Content"); close_client(c); return; } - if (resource->get(c, resource)) { + if (resource->ops->get(c, resource)) { g_data_input_stream_read_line_async(c->data, 0, NULL, finish, c); } else { @@ -246,7 +251,7 @@ static void receive(GObject *source, GAsyncResult *res, gpointer user_data) break; case UPDATE: - if (!resource->update) { + if (!resource->ops->update) { respond_error(c, 204, "No Content"); close_client(c); return; diff --git a/resource.c b/resource.c index 249029e..bc5dfdd 100644 --- a/resource.c +++ b/resource.c @@ -105,17 +105,19 @@ static void object_v1_close(struct client *c, struct resource *r) resource_obj_del_client(obj, c); } -static struct resource resource_position1 = { +static const struct resource_ops resource_v1_ops = { .get = object_v1_get, .update = object_v1_update, .close = object_v1_close, +}; + +static struct resource resource_position1 = { + .ops = &resource_v1_ops, .data = &position_object, }; static struct resource resource_signal1 = { - .get = object_v1_get, - .update = object_v1_update, - .close = object_v1_close, + .ops = &resource_v1_ops, .data = &signal_object, }; diff --git a/resource.h b/resource.h index 44d0d74..c3a1907 100644 --- a/resource.h +++ b/resource.h @@ -4,11 +4,16 @@ #define RESOURCE_h struct client; +struct resource; -struct resource { +struct resource_ops { int (*get)(struct client *c, struct resource *r); - int (*update)(struct client *c, struct resource *r, const char *m); void (*close)(struct client *c, struct resource *r); + int (*update)(struct client *c, struct resource *r, const char *m); +}; + +struct resource { + const struct resource_ops *ops; void *data; }; -- cgit