2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
17 #include <libubox/ustream.h>
21 static struct ubus_context
*ctx
;
22 static struct blob_buf b
;
24 static void test_client_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
26 fprintf(stderr
, "Subscribers active: %d\n", obj
->has_subscribers
);
29 static struct ubus_object test_client_object
= {
30 .subscribe_cb
= test_client_subscribe_cb
,
33 static void test_client_notify_cb(struct uloop_timeout
*timeout
)
35 static int counter
= 0;
37 struct timeval tv1
, tv2
;
43 blobmsg_add_u32(&b
, "counter", counter
++);
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
);
50 fprintf(stderr
, "Notify failed: %s\n", ubus_strerror(err
));
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
);
55 uloop_timeout_set(timeout
, 1000);
58 static struct uloop_timeout notify_timer
= {
59 .cb
= test_client_notify_cb
,
62 static void test_client_fd_data_cb(struct ustream
*s
, int bytes
)
67 data
= ustream_get_read_buf(s
, &len
);
71 sep
= strchr(data
, '\n');
76 fprintf(stderr
, "Got line: %s\n", data
);
77 ustream_consume(s
, sep
+ 1 - data
);
80 static void test_client_fd_cb(struct ubus_request
*req
, int fd
)
82 static struct ustream_fd test_fd
;
84 fprintf(stderr
, "Got fd from the server, watching...\n");
86 test_fd
.stream
.notify_read
= test_client_fd_data_cb
;
87 ustream_fd_init(&test_fd
, fd
);
90 static void test_client_complete_cb(struct ubus_request
*req
, int ret
)
92 fprintf(stderr
, "completed request, ret: %d\n", ret
);
95 static void client_main(void)
97 static struct ubus_request req
;
101 ret
= ubus_add_object(ctx
, &test_client_object
);
103 fprintf(stderr
, "Failed to add_object object: %s\n", ubus_strerror(ret
));
107 if (ubus_lookup_id(ctx
, "test", &id
)) {
108 fprintf(stderr
, "Failed to look up test object\n");
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(¬ify_timer
);
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
);
127 int main(int argc
, char **argv
)
129 const char *ubus_socket
= NULL
;
132 while ((ch
= getopt(argc
, argv
, "cs:")) != -1) {
135 ubus_socket
= optarg
;
147 ctx
= ubus_connect(ubus_socket
);
149 fprintf(stderr
, "Failed to connect to ubus\n");