2 * Copyright (C) 2015-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 #include <sys/types.h>
29 #include <sys/socket.h>
33 #include "afb-apiset.h"
34 #include "afb-api-ws.h"
36 #include "afb-socket.h"
37 #include "afb-stub-ws.h"
43 struct afb_apiset *apiset; /* the apiset for calling */
44 struct fdev *fdev; /* fdev handler */
45 uint16_t offapi; /* api name of the interface */
46 char uri[]; /* the uri of the server socket */
49 /******************************************************************************/
51 /******************************************************************************/
53 static struct fdev *reopen_client(void *closure)
55 const char *uri = closure;
56 return afb_socket_open_fdev(uri, 0);
59 int afb_api_ws_add_client(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set, int strong)
61 struct afb_stub_ws *stubws;
65 /* check the api name */
66 api = afb_socket_api(uri);
67 if (api == NULL || !afb_api_is_valid_name(api)) {
68 ERROR("invalid (too long) ws client uri %s", uri);
74 fdev = afb_socket_open_fdev(uri, 0);
76 /* create the client stub */
77 stubws = afb_stub_ws_create_client(fdev, api, call_set);
79 ERROR("can't setup client ws service to %s", uri);
82 if (afb_stub_ws_client_add(stubws, declare_set) >= 0) {
84 /* it is asserted here that uri is never released */
85 afb_stub_ws_client_robustify(stubws, reopen_client, (void*)uri, NULL);
87 /* it is asserted here that uri is released, so use a copy */
88 afb_stub_ws_client_robustify(stubws, reopen_client, strdup(uri), free);
92 ERROR("can't add the client to the apiset for service %s", uri);
93 afb_stub_ws_unref(stubws);
100 int afb_api_ws_add_client_strong(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set)
102 return afb_api_ws_add_client(uri, declare_set, call_set, 1);
105 int afb_api_ws_add_client_weak(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set)
107 return afb_api_ws_add_client(uri, declare_set, call_set, 0);
110 /*****************************************************************************/
111 /*** S E R V E R ***/
112 /******************************************************************************/
114 static void api_ws_server_accept(struct api_ws_server *apiws)
117 struct sockaddr addr;
120 struct afb_stub_ws *server;
122 lenaddr = (socklen_t)sizeof addr;
123 fd = accept(fdev_fd(apiws->fdev), &addr, &lenaddr);
125 ERROR("can't accept connection to %s: %m", apiws->uri);
127 fdev = afb_fdev_create(fd);
129 ERROR("can't hold accepted connection to %s: %m", apiws->uri);
132 server = afb_stub_ws_create_server(fdev, &apiws->uri[apiws->offapi], apiws->apiset);
134 afb_stub_ws_set_on_hangup(server, afb_stub_ws_unref);
136 ERROR("can't serve accepted connection to %s: %m", apiws->uri);
141 static int api_ws_server_connect(struct api_ws_server *apiws);
143 static void api_ws_server_listen_callback(void *closure, uint32_t revents, struct fdev *fdev)
145 struct api_ws_server *apiws = closure;
147 if ((revents & EPOLLHUP) != 0)
148 api_ws_server_connect(apiws);
149 else if ((revents & EPOLLIN) != 0)
150 api_ws_server_accept(apiws);
153 static void api_ws_server_disconnect(struct api_ws_server *apiws)
155 fdev_unref(apiws->fdev);
159 static int api_ws_server_connect(struct api_ws_server *apiws)
161 /* ensure disconnected */
162 api_ws_server_disconnect(apiws);
164 /* request the service object name */
165 apiws->fdev = afb_socket_open_fdev(apiws->uri, 1);
167 ERROR("can't create socket %s", apiws->uri);
169 /* listen for service */
170 fdev_set_events(apiws->fdev, EPOLLIN);
171 fdev_set_callback(apiws->fdev, api_ws_server_listen_callback, apiws);
177 /* create the service */
178 int afb_api_ws_add_server(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set)
182 struct api_ws_server *apiws;
183 size_t luri, lapi, extra;
188 ERROR("can't create socket %s", uri);
193 /* check the api name */
194 api = afb_socket_api(uri);
195 if (api == NULL || !afb_api_is_valid_name(api)) {
196 ERROR("invalid api name in ws uri %s", uri);
202 if (!afb_apiset_lookup(call_set, api, 1)) {
203 ERROR("Can't provide ws-server for URI %s: API %s doesn't exist", uri, api);
208 /* make the structure */
210 extra = luri == (api - uri) + lapi ? 0 : lapi + 1;
211 apiws = malloc(sizeof * apiws + 1 + luri + extra);
213 ERROR("out of memory");
218 apiws->apiset = afb_apiset_addref(call_set);
220 strcpy(apiws->uri, uri);
222 apiws->offapi = (uint16_t)(api - uri);
224 apiws->offapi = (uint16_t)(luri + 1);
225 strcpy(&apiws->uri[apiws->offapi], api);
228 /* connect for serving */
229 rc = api_ws_server_connect(apiws);
233 afb_apiset_unref(apiws->apiset);