From 54c78ed9058375e5e548d06499b7a27c5deae81f Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sat, 15 Dec 2012 00:37:26 +0100 Subject: [PATCH] examples: use blocking notify Signed-off-by: Felix Fietkau --- examples/CMakeLists.txt | 2 +- examples/client.c | 20 ++++++++++++++++++++ examples/server.c | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a6ab7bf..5a11a9f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.6) ADD_DEFINITIONS(-I..) ADD_EXECUTABLE(server server.c) -TARGET_LINK_LIBRARIES(server ubus ubox) +TARGET_LINK_LIBRARIES(server ubus ubox blobmsg_json) ADD_EXECUTABLE(client client.c) TARGET_LINK_LIBRARIES(client ubus ubox) diff --git a/examples/client.c b/examples/client.c index 418fb15..75c61a4 100644 --- a/examples/client.c +++ b/examples/client.c @@ -27,6 +27,25 @@ static struct ubus_object test_client_object = { .subscribe_cb = test_client_subscribe_cb, }; +static void test_client_notify_cb(struct uloop_timeout *timeout) +{ + static int counter = 0; + int err; + + blob_buf_init(&b, 0); + blobmsg_add_u32(&b, "counter", counter++); + + err = ubus_notify(ctx, &test_client_object, "ping", b.head, 1000); + if (err) + fprintf(stderr, "Notify failed: %s\n", ubus_strerror(err)); + + uloop_timeout_set(timeout, 1000); +} + +static struct uloop_timeout notify_timer = { + .cb = test_client_notify_cb, +}; + static void client_main(void) { uint32_t id; @@ -46,6 +65,7 @@ static void client_main(void) blob_buf_init(&b, 0); blobmsg_add_u32(&b, "id", test_client_object.id); ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000); + test_client_notify_cb(¬ify_timer); uloop_run(); } diff --git a/examples/server.c b/examples/server.c index 529059c..c5af1aa 100644 --- a/examples/server.c +++ b/examples/server.c @@ -13,6 +13,7 @@ #include +#include #include "libubus.h" static struct ubus_context *ctx; @@ -72,11 +73,13 @@ static int test_hello(struct ubus_context *ctx, struct ubus_object *obj, enum { WATCH_ID, + WATCH_COUNTER, __WATCH_MAX }; static const struct blobmsg_policy watch_policy[__WATCH_MAX] = { [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 }, + [WATCH_COUNTER] = { .name = "counter", .type = BLOBMSG_TYPE_INT32 }, }; static void @@ -86,6 +89,20 @@ test_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, fprintf(stderr, "Object %08x went away\n", id); } +static int +test_notify(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + char *str; + + str = blobmsg_format_json(msg, true); + fprintf(stderr, "Received notification '%s': %s\n", method, str); + free(str); + + return 0; +} + static int test_watch(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) @@ -98,6 +115,7 @@ static int test_watch(struct ubus_context *ctx, struct ubus_object *obj, return UBUS_STATUS_INVALID_ARGUMENT; test_event.remove_cb = test_handle_remove; + test_event.cb = test_notify; ret = ubus_subscribe(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID])); fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret)); return ret; -- 2.30.2