4d6b6eced8cee4f91c8d847dd67aefca3fbe44a5
[project/ubus.git] / examples / client.c
1 /*
2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <sys/time.h>
15 #include <unistd.h>
16
17 #include <libubox/ustream.h>
18
19 #include "libubus.h"
20
21 static struct ubus_context *ctx;
22 static struct blob_buf b;
23
24 static void test_client_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
25 {
26 fprintf(stderr, "Subscribers active: %d\n", obj->has_subscribers);
27 }
28
29 static struct ubus_object test_client_object = {
30 .subscribe_cb = test_client_subscribe_cb,
31 };
32
33 static void test_client_notify_cb(struct uloop_timeout *timeout)
34 {
35 static int counter = 0;
36 int err;
37 struct timeval tv1, tv2;
38 int max = 1000;
39 long delta;
40 int i = 0;
41
42 blob_buf_init(&b, 0);
43 blobmsg_add_u32(&b, "counter", counter++);
44
45 gettimeofday(&tv1, NULL);
46 for (i = 0; i < max; i++)
47 err = ubus_notify(ctx, &test_client_object, "ping", b.head, 1000);
48 gettimeofday(&tv2, NULL);
49 if (err)
50 fprintf(stderr, "Notify failed: %s\n", ubus_strerror(err));
51
52 delta = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
53 fprintf(stderr, "Avg time per iteration: %ld usec\n", delta / max);
54
55 uloop_timeout_set(timeout, 1000);
56 }
57
58 static struct uloop_timeout notify_timer = {
59 .cb = test_client_notify_cb,
60 };
61
62 static void test_client_fd_data_cb(struct ustream *s, int bytes)
63 {
64 char *data, *sep;
65 int len;
66
67 data = ustream_get_read_buf(s, &len);
68 if (len < 1)
69 return;
70
71 sep = strchr(data, '\n');
72 if (!sep)
73 return;
74
75 *sep = 0;
76 fprintf(stderr, "Got line: %s\n", data);
77 ustream_consume(s, sep + 1 - data);
78 }
79
80 static void test_client_fd_cb(struct ubus_request *req, int fd)
81 {
82 static struct ustream_fd test_fd;
83
84 fprintf(stderr, "Got fd from the server, watching...\n");
85
86 test_fd.stream.notify_read = test_client_fd_data_cb;
87 ustream_fd_init(&test_fd, fd);
88 }
89
90 static void test_client_complete_cb(struct ubus_request *req, int ret)
91 {
92 fprintf(stderr, "completed request, ret: %d\n", ret);
93 }
94
95 static void client_main(void)
96 {
97 static struct ubus_request req;
98 uint32_t id;
99 int ret;
100
101 ret = ubus_add_object(ctx, &test_client_object);
102 if (ret) {
103 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
104 return;
105 }
106
107 if (ubus_lookup_id(ctx, "test", &id)) {
108 fprintf(stderr, "Failed to look up test object\n");
109 return;
110 }
111
112 blob_buf_init(&b, 0);
113 blobmsg_add_u32(&b, "id", test_client_object.id);
114 ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
115 test_client_notify_cb(&notify_timer);
116
117 blob_buf_init(&b, 0);
118 blobmsg_add_string(&b, "msg", "blah");
119 ubus_invoke_async(ctx, id, "hello", b.head, &req);
120 req.fd_cb = test_client_fd_cb;
121 req.complete_cb = test_client_complete_cb;
122 ubus_complete_request_async(ctx, &req);
123
124 uloop_run();
125 }
126
127 int main(int argc, char **argv)
128 {
129 const char *ubus_socket = NULL;
130 int ch;
131
132 while ((ch = getopt(argc, argv, "cs:")) != -1) {
133 switch (ch) {
134 case 's':
135 ubus_socket = optarg;
136 break;
137 default:
138 break;
139 }
140 }
141
142 argc -= optind;
143 argv += optind;
144
145 uloop_init();
146
147 ctx = ubus_connect(ubus_socket);
148 if (!ctx) {
149 fprintf(stderr, "Failed to connect to ubus\n");
150 return -1;
151 }
152
153 ubus_add_uloop(ctx);
154
155 client_main();
156
157 ubus_free(ctx);
158 uloop_done();
159
160 return 0;
161 }