examples: use blocking notify

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
This commit is contained in:
Felix Fietkau 2012-12-15 00:37:26 +01:00
parent f3fabd45a7
commit 54c78ed905
3 changed files with 39 additions and 1 deletions

View file

@ -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)

View file

@ -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(&notify_timer);
uloop_run();
}

View file

@ -13,6 +13,7 @@
#include <unistd.h>
#include <libubox/blobmsg_json.h>
#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;