2 * Copyright (C) 2015-2018 "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 char *uri; /* uri of the object for the API */
44 char *api; /* api name of the interface */
45 struct fdev *fdev; /* fdev handler */
46 struct afb_apiset *apiset;
49 /******************************************************************************/
52 * create a structure api_ws not connected to the 'uri'.
54 static struct api_ws *api_ws_make(const char *uri)
59 /* allocates the structure */
61 api = calloc(1, sizeof *api + 1 + length);
67 /* uri is copied after the struct */
68 api->uri = (char*)(api+1);
69 memcpy(api->uri, uri, length + 1);
71 /* api name is at the end of the uri */
72 while (length && uri[length - 1] != '/' && uri[length - 1] != ':')
74 api->api = &api->uri[length];
75 if (api->api == NULL || !afb_api_is_valid_name(api->api)) {
88 /**********************************************************************************/
90 int afb_api_ws_add_client(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set, int strong)
93 struct afb_stub_ws *stubws;
95 /* create the ws client api */
96 apiws = api_ws_make(uri);
100 /* connect to the service */
101 apiws->fdev = afb_socket_open(apiws->uri, 0);
105 stubws = afb_stub_ws_create_client(apiws->fdev, apiws->api, call_set);
107 ERROR("can't setup client ws service to %s", apiws->uri);
110 if (afb_stub_ws_client_add(stubws, declare_set) < 0) {
111 ERROR("can't add the client to the apiset for service %s", apiws->uri);
117 afb_stub_ws_unref(stubws);
124 int afb_api_ws_add_client_strong(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set)
126 return afb_api_ws_add_client(uri, declare_set, call_set, 1);
129 int afb_api_ws_add_client_weak(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set)
131 return afb_api_ws_add_client(uri, declare_set, call_set, 0);
134 static int api_ws_server_accept_client(struct api_ws *apiws, struct fdev *fdev)
136 return -!afb_stub_ws_create_server(fdev, apiws->api, apiws->apiset);
139 static void api_ws_server_accept(struct api_ws *apiws)
142 struct sockaddr addr;
146 lenaddr = (socklen_t)sizeof addr;
147 fd = accept(fdev_fd(apiws->fdev), &addr, &lenaddr);
149 ERROR("can't accept connection to %s: %m", apiws->uri);
151 fdev = afb_fdev_create(fd);
153 ERROR("can't hold accepted connection to %s: %m", apiws->uri);
156 rc = api_ws_server_accept_client(apiws, fdev);
158 ERROR("can't serve accepted connection to %s: %m", apiws->uri);
163 static int api_ws_server_connect(struct api_ws *apiws);
165 static void api_ws_server_listen_callback(void *closure, uint32_t revents, struct fdev *fdev)
167 struct api_ws *apiws = closure;
169 if ((revents & EPOLLIN) != 0)
170 api_ws_server_accept(apiws);
171 if ((revents & EPOLLHUP) != 0)
172 api_ws_server_connect(apiws);
175 static void api_ws_server_disconnect(struct api_ws *apiws)
177 fdev_unref(apiws->fdev);
181 static int api_ws_server_connect(struct api_ws *apiws)
183 /* ensure disconnected */
184 api_ws_server_disconnect(apiws);
186 /* request the service object name */
187 apiws->fdev = afb_socket_open(apiws->uri, 1);
189 ERROR("can't create socket %s", apiws->uri);
191 /* listen for service */
192 fdev_set_events(apiws->fdev, EPOLLIN);
193 fdev_set_callback(apiws->fdev, api_ws_server_listen_callback, apiws);
199 /* create the service */
200 int afb_api_ws_add_server(const char *uri, struct afb_apiset *declare_set, struct afb_apiset *call_set)
203 struct api_ws *apiws;
205 /* creates the ws api object */
206 apiws = api_ws_make(uri);
211 if (!afb_apiset_lookup(call_set, apiws->api, 1)) {
212 ERROR("Can't provide ws-server for %s: API %s doesn't exist", uri, apiws->api);
216 /* connect for serving */
217 rc = api_ws_server_connect(apiws);
221 apiws->apiset = afb_apiset_addref(call_set);