From 600ec9fd73d85ff43fe07a40cc4154316fbabb73 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 1 Oct 2012 14:09:37 +0200 Subject: [PATCH 1/1] add deferred reply to the ubus server example --- ubus-example.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/ubus-example.c b/ubus-example.c index 53d275b..a7df06f 100644 --- a/ubus-example.c +++ b/ubus-example.c @@ -17,7 +17,7 @@ static struct ubus_context *ctx; static struct ubus_watch_object test_event; -struct blob_buf b; +static struct blob_buf b; enum { HELLO_ID, @@ -30,24 +30,43 @@ static const struct blobmsg_policy hello_policy[] = { [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING }, }; +struct hello_request { + struct ubus_request_data req; + struct uloop_timeout timeout; + char data[]; +}; + +static void test_hello_reply(struct uloop_timeout *t) +{ + struct hello_request *req = container_of(t, struct hello_request, timeout); + + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "message", req->data); + ubus_send_reply(ctx, &req->req, b.head); + ubus_complete_deferred_request(ctx, &req->req, 0); + free(req); +} + static int test_hello(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { + struct hello_request *hreq; struct blob_attr *tb[__HELLO_MAX]; - char *msgstr = "(unknown)"; - char *strbuf; + const char *format = "%s received a message: %s"; + const char *msgstr = "(unknown)"; blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg)); if (tb[HELLO_MSG]) msgstr = blobmsg_data(tb[HELLO_MSG]); - blob_buf_init(&b, 0); - strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr)); - sprintf(strbuf, "%s received a message: %s", obj->name, msgstr); - blobmsg_add_string_buffer(&b); - ubus_send_reply(ctx, req, b.head); + hreq = calloc(1, sizeof(*hreq) + strlen(format) + strlen(obj->name) + strlen(msgstr) + 1); + sprintf(hreq->data, format, obj->name, msgstr); + ubus_defer_request(ctx, req, &hreq->req); + hreq->timeout.cb = test_hello_reply; + uloop_timeout_set(&hreq->timeout, 1000); + return 0; } -- 2.11.0