summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell King <rmk@armlinux.org.uk>2021-09-11 11:59:10 +0100
committerRussell King <rmk@armlinux.org.uk>2021-09-11 11:59:10 +0100
commit42ccceaac9eef9a6a5387a4ffcf994e279e69573 (patch)
treea4eadef37f82f7ce769b0da2fdc2000b1d19a381
parent88416fdda7f6c3ab3a324c7faeb5bb94d997f0b7 (diff)
resource: split resource object handling
Move the low-level resource object handling out of the interface functions. Signed-off-by: Russell King <rmk@armlinux.org.uk>
-rw-r--r--resource.c57
1 files changed, 39 insertions, 18 deletions
diff --git a/resource.c b/resource.c
index 085a66a..249029e 100644
--- a/resource.c
+++ b/resource.c
@@ -12,7 +12,7 @@ struct resource_object {
static struct resource_object position_object;
static struct resource_object signal_object;
-static void object_v1_send(struct client *c, struct resource_object *obj)
+static void resource_obj_send(struct client *c, struct resource_object *obj)
{
GString *string = obj->string;
@@ -22,6 +22,39 @@ static void object_v1_send(struct client *c, struct resource_object *obj)
respond_chunk(c, string);
}
+// Update this client with the new resource object data
+static void resource_obj_update(gpointer data, gpointer user_data)
+{
+ struct resource_object *obj = user_data;
+ struct client *c = data;
+
+ resource_obj_send(c, obj);
+}
+
+static void resource_obj_set_string(struct resource_object *obj, GString *str)
+{
+ GString *old;
+
+ old = obj->string;
+ obj->string = str;
+ g_string_free(old, TRUE);
+
+ g_list_foreach(obj->client_list, resource_obj_update, obj);
+}
+
+static void resource_obj_add_client(struct resource_object *obj,
+ struct client *c)
+{
+ obj->client_list = g_list_append(obj->client_list, c);
+}
+
+static void resource_obj_del_client(struct resource_object *obj,
+ struct client *c)
+{
+ obj->client_list = g_list_remove(obj->client_list, c);
+}
+
+
static int object_v1_get(struct client *c, struct resource *r)
{
struct resource_object *obj = r->data;
@@ -32,28 +65,19 @@ static int object_v1_get(struct client *c, struct resource *r)
"Content-Type: text/event-stream; charset=UTF-8\r\n"
"Transfer-Encoding: chunked\r\n");
- object_v1_send(c, obj);
+ resource_obj_send(c, obj);
// Add this client to the object list so it receives updates
- obj->client_list = g_list_append(obj->client_list, c);
+ resource_obj_add_client(obj, c);
return 1;
}
-// Update this client with the new resource object data
-static void object_v1_client_update(gpointer data, gpointer user_data)
-{
- struct resource_object *obj = user_data;
- struct client *c = data;
-
- object_v1_send(c, obj);
-}
-
// Update all attached clients with the new resource object data
static int object_v1_update(struct client *c, struct resource *r, const char *m)
{
struct resource_object *obj = r->data;
- GString *string, *old;
+ GString *string;
char *n;
// Remove any trailing whitespace.
@@ -66,12 +90,9 @@ static int object_v1_update(struct client *c, struct resource *r, const char *m)
// See https://www.html5rocks.com/en/tutorials/eventsource/basics/
string = g_string_new(NULL);
g_string_printf(string, "data:%s\n\n", n);
- old = obj->string;
- obj->string = string;
- g_string_free(old, TRUE);
g_free(n);
- g_list_foreach(obj->client_list, object_v1_client_update, obj);
+ resource_obj_set_string(obj, string);
return 0;
}
@@ -81,7 +102,7 @@ static void object_v1_close(struct client *c, struct resource *r)
struct resource_object *obj = r->data;
// Remove this client from the object list
- obj->client_list = g_list_remove(obj->client_list, c);
+ resource_obj_del_client(obj, c);
}
static struct resource resource_position1 = {