examples: disable the message on the server example, measure the notify latency on...
[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 "libubus.h"
18
19 static struct ubus_context *ctx;
20 static struct blob_buf b;
21
22 static void test_client_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
23 {
24         fprintf(stderr, "Subscribers active: %d\n", obj->has_subscribers);
25 }
26
27 static struct ubus_object test_client_object = {
28         .subscribe_cb = test_client_subscribe_cb,
29 };
30
31 static void test_client_notify_cb(struct uloop_timeout *timeout)
32 {
33         static int counter = 0;
34         int err;
35         struct timeval tv1, tv2;
36         int max = 1000;
37         long delta;
38         int i = 0;
39
40         blob_buf_init(&b, 0);
41         blobmsg_add_u32(&b, "counter", counter++);
42
43         gettimeofday(&tv1, NULL);
44         for (i = 0; i < max; i++)
45                 err = ubus_notify(ctx, &test_client_object, "ping", b.head, 1000);
46         gettimeofday(&tv2, NULL);
47         if (err)
48                 fprintf(stderr, "Notify failed: %s\n", ubus_strerror(err));
49
50         delta = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
51         fprintf(stderr, "Avg time per iteration: %ld usec\n", delta / max);
52
53         uloop_timeout_set(timeout, 1000);
54 }
55
56 static struct uloop_timeout notify_timer = {
57         .cb = test_client_notify_cb,
58 };
59
60 static void client_main(void)
61 {
62         uint32_t id;
63         int ret;
64
65         ret = ubus_add_object(ctx, &test_client_object);
66         if (ret) {
67                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
68                 return;
69         }
70
71         if (ubus_lookup_id(ctx, "test", &id)) {
72                 fprintf(stderr, "Failed to look up test object\n");
73                 return;
74         }
75
76         blob_buf_init(&b, 0);
77         blobmsg_add_u32(&b, "id", test_client_object.id);
78         ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
79         test_client_notify_cb(&notify_timer);
80         uloop_run();
81 }
82
83 int main(int argc, char **argv)
84 {
85         const char *ubus_socket = NULL;
86         int ch;
87
88         while ((ch = getopt(argc, argv, "cs:")) != -1) {
89                 switch (ch) {
90                 case 's':
91                         ubus_socket = optarg;
92                         break;
93                 default:
94                         break;
95                 }
96         }
97
98         argc -= optind;
99         argv += optind;
100
101         uloop_init();
102
103         ctx = ubus_connect(ubus_socket);
104         if (!ctx) {
105                 fprintf(stderr, "Failed to connect to ubus\n");
106                 return -1;
107         }
108
109         ubus_add_uloop(ctx);
110
111         client_main();
112
113         ubus_free(ctx);
114         uloop_done();
115
116         return 0;
117 }