ubus实现订阅

ubus支持以订阅的方式进行进程间通信,通信方式如下图:

ubus实现订阅_第1张图片

被订阅者(server)又称为notifier,订阅者(client)又称为subscriber。

主要过程为:

server进程:

  1. 连接ubusd。
  2. 注册一个object,用于client订阅。
  3. server可以向订阅者广播消息。
  4. 等待消息。

client进程:

  1. 连接ubusd。
  2. 向server订阅object,并定义收到server的消息时的处理函数。
  3. 等待消息。

代码示例:

client:

#include <unistd.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubus.h>

static struct ubus_context *ctx;

static int counter = 0;
static uint32_t obj_id;
static struct ubus_subscriber test_event;

static int test_notify(struct ubus_context *ctx, struct ubus_object *obj,
			      struct ubus_request_data *req,
			      const char *method, struct blob_attr *msg)
{
	printf("notify handler...\n");
	counter++;
	if (counter > 3)
		ubus_unsubscribe(ctx, &test_event, obj_id);
	return 0;
}

static void test_handle_remove(struct ubus_context *ctx,
				      struct ubus_subscriber *obj, uint32_t id)
{
	printf("remove handler...\n");
}

static void subscriber_main(void)
{
	int ret;
	
	/* 通知到来时的处理函数。 */
	test_event.cb = test_notify;
	test_event.remove_cb = test_handle_remove; //server主动发起删除该client的订阅的cb函数(如server退出的时候)

	/* 注册test_event */
	ret = ubus_register_subscriber(ctx, &test_event);
	if (ret)
		fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
    
	/* 得到要订阅的object的id */
	ret = ubus_lookup_id(ctx, "test", &obj_id);

	/* 订阅object */
	ret = ubus_subscribe(ctx, &test_event, obj_id);
}

int main(int argc, char **argv)
{
	const char *ubus_socket = NULL;
	int ch;

	while ((ch = getopt(argc, argv, "cs:")) != -1) {
		switch (ch) {
		case 's':
			ubus_socket = optarg;
			break;
		default:
			break;
		}
	}

	argc -= optind;
	argv += optind;

	uloop_init();

	ctx = ubus_connect(ubus_socket);
	if (!ctx) {
		fprintf(stderr, "Failed to connect to ubus\n");
		return -1;
	}

	ubus_add_uloop(ctx);

	subscriber_main();
	
	uloop_run();

	ubus_free(ctx);
	uloop_done();

	return 0;
}

server:

#include <unistd.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubus.h>

static struct ubus_context *ctx;

enum {
	HELLO_ID,
	HELLO_MSG,
	__HELLO_MAX
};

static const struct blobmsg_policy hello_policy[] = {
	[HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
	[HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
};

static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
		      struct ubus_request_data *req, const char *method,
		      struct blob_attr *msg)
{
	printf("test hello be called\n");
	return 0;
}

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 int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
		      struct ubus_request_data *req, const char *method,
		      struct blob_attr *msg)
{
	int ret = 0;
	printf("test watch be called\n");
	return ret;
}

/* 注意,这些method和订阅无关,只是server供外部ubus call的method。 */
static const struct ubus_method test_methods[] = {
	UBUS_METHOD("hello", test_hello, hello_policy),
	UBUS_METHOD("watch", test_watch, watch_policy),
};

static struct ubus_object_type test_object_type =
	UBUS_OBJECT_TYPE("test", test_methods);

static struct ubus_object test_object = {
	.name = "test",
	.type = &test_object_type,
	.methods = test_methods,
	.n_methods = ARRAY_SIZE(test_methods),
};

static void notifier_main(void)
{
	int ret;

	/* 注册一个object,client可以订阅这个object */
	ret = ubus_add_object(ctx, &test_object);
	if (ret)
		fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));

	//TODO: prepare event argument if necessary

        /* 在需要的时候发送订阅更新消息 */
	/* 
	int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj, const char *type, struct blob_attr *msg, int timeout);
	type是一个字符串,自定义的。msg是json消息,需要等待回复时timeout需设置为>=0。
	*/
	while (1) {
		sleep(2);
		/* Broadcast the event notification to bus */
		ubus_notify(ctx,  &test_object, "hahaha", NULL, -1);
	}
}

int main(int argc, char **argv)
{
	const char *ubus_socket = NULL;
	int ch;

	while ((ch = getopt(argc, argv, "cs:")) != -1) {
		switch (ch) {
		case 's':
			ubus_socket = optarg;
			break;
		default:
			break;
		}
	}

	argc -= optind;
	argv += optind;

	uloop_init();

	ctx = ubus_connect(ubus_socket);
	if (!ctx) {
		fprintf(stderr, "Failed to connect to ubus\n");
		return -1;
	}

	ubus_add_uloop(ctx);

	notifier_main();

	ubus_free(ctx);
	uloop_done();

	return 0;
}




你可能感兴趣的:(openwrt,ubus)