add auto (re)connect logic to libubus
[project/ubus.git] / examples / server.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 <unistd.h>
15
16 #include <libubox/blobmsg_json.h>
17 #include "libubus.h"
18
19 static struct ubus_context *ctx;
20 static struct ubus_subscriber test_event;
21 static struct blob_buf b;
22
23 enum {
24         HELLO_ID,
25         HELLO_MSG,
26         __HELLO_MAX
27 };
28
29 static const struct blobmsg_policy hello_policy[] = {
30         [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
31         [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
32 };
33
34 struct hello_request {
35         struct ubus_request_data req;
36         struct uloop_timeout timeout;
37         char data[];
38 };
39
40 static void test_hello_reply(struct uloop_timeout *t)
41 {
42         struct hello_request *req = container_of(t, struct hello_request, timeout);
43
44         blob_buf_init(&b, 0);
45         blobmsg_add_string(&b, "message", req->data);
46         ubus_send_reply(ctx, &req->req, b.head);
47         ubus_complete_deferred_request(ctx, &req->req, 0);
48         free(req);
49 }
50
51 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
52                       struct ubus_request_data *req, const char *method,
53                       struct blob_attr *msg)
54 {
55         struct hello_request *hreq;
56         struct blob_attr *tb[__HELLO_MAX];
57         const char *format = "%s received a message: %s";
58         const char *msgstr = "(unknown)";
59
60         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
61
62         if (tb[HELLO_MSG])
63                 msgstr = blobmsg_data(tb[HELLO_MSG]);
64
65         hreq = calloc(1, sizeof(*hreq) + strlen(format) + strlen(obj->name) + strlen(msgstr) + 1);
66         sprintf(hreq->data, format, obj->name, msgstr);
67         ubus_defer_request(ctx, req, &hreq->req);
68         hreq->timeout.cb = test_hello_reply;
69         uloop_timeout_set(&hreq->timeout, 1000);
70
71         return 0;
72 }
73
74 enum {
75         WATCH_ID,
76         WATCH_COUNTER,
77         __WATCH_MAX
78 };
79
80 static const struct blobmsg_policy watch_policy[__WATCH_MAX] = {
81         [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
82         [WATCH_COUNTER] = { .name = "counter", .type = BLOBMSG_TYPE_INT32 },
83 };
84
85 static void
86 test_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
87                    uint32_t id)
88 {
89         fprintf(stderr, "Object %08x went away\n", id);
90 }
91
92 static int
93 test_notify(struct ubus_context *ctx, struct ubus_object *obj,
94             struct ubus_request_data *req, const char *method,
95             struct blob_attr *msg)
96 {
97 #if 0
98         char *str;
99
100         str = blobmsg_format_json(msg, true);
101         fprintf(stderr, "Received notification '%s': %s\n", method, str);
102         free(str);
103 #endif
104
105         return 0;
106 }
107
108 static int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
109                       struct ubus_request_data *req, const char *method,
110                       struct blob_attr *msg)
111 {
112         struct blob_attr *tb[__WATCH_MAX];
113         int ret;
114
115         blobmsg_parse(watch_policy, __WATCH_MAX, tb, blob_data(msg), blob_len(msg));
116         if (!tb[WATCH_ID])
117                 return UBUS_STATUS_INVALID_ARGUMENT;
118
119         test_event.remove_cb = test_handle_remove;
120         test_event.cb = test_notify;
121         ret = ubus_subscribe(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID]));
122         fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret));
123         return ret;
124 }
125
126 static const struct ubus_method test_methods[] = {
127         UBUS_METHOD("hello", test_hello, hello_policy),
128         UBUS_METHOD("watch", test_watch, watch_policy),
129 };
130
131 static struct ubus_object_type test_object_type =
132         UBUS_OBJECT_TYPE("test", test_methods);
133
134 static struct ubus_object test_object = {
135         .name = "test",
136         .type = &test_object_type,
137         .methods = test_methods,
138         .n_methods = ARRAY_SIZE(test_methods),
139 };
140
141 static void server_main(void)
142 {
143         int ret;
144
145         ret = ubus_add_object(ctx, &test_object);
146         if (ret)
147                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
148
149         ret = ubus_register_subscriber(ctx, &test_event);
150         if (ret)
151                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
152
153         uloop_run();
154 }
155
156 int main(int argc, char **argv)
157 {
158         const char *ubus_socket = NULL;
159         int ch;
160
161         while ((ch = getopt(argc, argv, "cs:")) != -1) {
162                 switch (ch) {
163                 case 's':
164                         ubus_socket = optarg;
165                         break;
166                 default:
167                         break;
168                 }
169         }
170
171         argc -= optind;
172         argv += optind;
173
174         uloop_init();
175
176         ctx = ubus_connect(ubus_socket);
177         if (!ctx) {
178                 fprintf(stderr, "Failed to connect to ubus\n");
179                 return -1;
180         }
181
182         ubus_add_uloop(ctx);
183
184         server_main();
185
186         ubus_free(ctx);
187         uloop_done();
188
189         return 0;
190 }