Skip to content

Commit 056d3d4

Browse files
committed
Unify ggipc_subscribe_to_topic handlers
1 parent 34eb2c6 commit 056d3d4

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
lines changed

cpp/priv_include/ggl/ipc/client_c_api.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,13 @@ GglError ggipc_publish_to_topic_binary_b64(
2727
GglBuffer topic, GglBuffer b64_payload
2828
) noexcept;
2929

30-
typedef struct {
31-
void (*json_handler)(
32-
GglBuffer topic, GglMap payload, GgIpcSubscriptionHandle handle
33-
) noexcept;
34-
void (*binary_handler)(
35-
GglBuffer topic, GglBuffer payload, GgIpcSubscriptionHandle handle
36-
) noexcept;
37-
} GgIpcSubscribeToTopicCallbacks;
30+
typedef void GgIpcSubscribeToTopicCallback(
31+
GglBuffer topic, GglObject payload, GgIpcSubscriptionHandle handle
32+
);
3833

3934
GglError ggipc_subscribe_to_topic(
4035
GglBuffer topic,
41-
const GgIpcSubscribeToTopicCallbacks *callbacks,
36+
GgIpcSubscribeToTopicCallback *callback,
4237
GgIpcSubscriptionHandle *handle
4338
) noexcept;
4439

include/ggl/ipc/client.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,16 @@ GglError ggipc_publish_to_topic_binary_b64(
5959
GglBuffer topic, GglBuffer b64_payload
6060
);
6161

62-
typedef struct {
63-
void (*json_handler)(
64-
GglBuffer topic, GglMap payload, GgIpcSubscriptionHandle handle
65-
);
66-
void (*binary_handler)(
67-
GglBuffer topic, GglBuffer payload, GgIpcSubscriptionHandle handle
68-
);
69-
} GgIpcSubscribeToTopicCallbacks;
62+
typedef void GgIpcSubscribeToTopicCallback(
63+
GglBuffer topic, GglObject payload, GgIpcSubscriptionHandle handle
64+
);
7065

7166
/// Subscribe to messages on a local topic
72-
/// `handlers` must live until the handle is closed.
73-
/// `json_handler` or `binary_handler` may be NULL if that payload type is not
74-
/// expected.
75-
ACCESS(read_only, 2)
67+
/// Payload will be a map for json messages and a buffer for binary messages.
68+
NONNULL(2)
7669
GglError ggipc_subscribe_to_topic(
7770
GglBuffer topic,
78-
const GgIpcSubscribeToTopicCallbacks callbacks[static 1],
71+
GgIpcSubscribeToTopicCallback *callback,
7972
GgIpcSubscriptionHandle *handle
8073
);
8174

src/ipc/client/subscribe_to_topic.c

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static GglError subscribe_to_topic_resp_handler(
2020
GglBuffer service_model_type,
2121
GglMap data
2222
) {
23-
const GgIpcSubscribeToTopicCallbacks *callbacks = ctx;
23+
GgIpcSubscribeToTopicCallback *callback = ctx;
2424

2525
if (!ggl_buffer_eq(
2626
service_model_type,
@@ -88,36 +88,18 @@ static GglError subscribe_to_topic_resp_handler(
8888

8989
GglBuffer topic = ggl_obj_into_buf(*topic_obj);
9090

91-
if (is_json) {
92-
if (callbacks->json_handler == NULL) {
93-
GGL_LOGW(
94-
"Skipping unhandled JSON payload on local topic %.*s.",
95-
(int) topic.len,
96-
topic.data
97-
);
98-
return GGL_ERR_OK;
99-
}
100-
101-
GglMap payload = ggl_obj_into_map(*message_obj);
102-
callbacks->json_handler(topic, payload, handle);
103-
} else {
104-
if (callbacks->json_handler == NULL) {
105-
GGL_LOGW(
106-
"Skipping unhandled binary payload on local topic %.*s.",
107-
(int) topic.len,
108-
topic.data
109-
);
110-
return GGL_ERR_OK;
111-
}
91+
GglObject payload = *message_obj;
11292

113-
GglBuffer payload = ggl_obj_into_buf(*message_obj);
114-
if (!ggl_base64_decode_in_place(&payload)) {
93+
if (!is_json) {
94+
GglBuffer payload_buf = ggl_obj_into_buf(*message_obj);
95+
if (!ggl_base64_decode_in_place(&payload_buf)) {
11596
GGL_LOGE("Failed to decode pubsub subscription response payload.");
11697
return GGL_ERR_INVALID;
11798
}
118-
callbacks->binary_handler(topic, payload, handle);
99+
payload = ggl_obj_buf(payload_buf);
119100
}
120101

102+
callback(topic, payload, handle);
121103
return GGL_ERR_OK;
122104
}
123105

@@ -142,7 +124,7 @@ static GglError error_handler(
142124

143125
GglError ggipc_subscribe_to_topic(
144126
GglBuffer topic,
145-
const GgIpcSubscribeToTopicCallbacks callbacks[static 1],
127+
GgIpcSubscribeToTopicCallback callback,
146128
GgIpcSubscriptionHandle *handle
147129
) {
148130
GglMap args = GGL_MAP(ggl_kv(GGL_STR("topic"), ggl_obj_buf(topic)), );
@@ -155,7 +137,7 @@ GglError ggipc_subscribe_to_topic(
155137
&error_handler,
156138
NULL,
157139
&subscribe_to_topic_resp_handler,
158-
(void *) callbacks,
140+
callback,
159141
handle
160142
);
161143
}

0 commit comments

Comments
 (0)