afb-api-ws: Make weak ws-client by default (to be fixed)
[src/app-framework-binder.git] / src / afb-api-ws.c
index dba8d05..1a46358 100644 (file)
@@ -169,7 +169,6 @@ static int api_ws_socket_inet(const char *path, int server)
        }
        freeaddrinfo(rai);
        return -1;
-       
 }
 
 static int api_ws_socket(const char *path, int server)
@@ -204,7 +203,7 @@ static int api_ws_socket(const char *path, int server)
 
 /**********************************************************************************/
 
-int afb_api_ws_add_client(const char *path, struct afb_apiset *apiset)
+int afb_api_ws_add_client(const char *path, struct afb_apiset *apiset, int strong)
 {
        struct api_ws *apiws;
        struct afb_stub_ws *stubws;
@@ -226,15 +225,30 @@ int afb_api_ws_add_client(const char *path, struct afb_apiset *apiset)
                ERROR("can't setup client ws service to %s", apiws->path);
                goto error3;
        }
+       if (afb_stub_ws_client_add(stubws, apiset) < 0) {
+               ERROR("can't add the client to the apiset for service %s", apiws->path);
+               goto error4;
+       }
        free(apiws);
        return 0;
-
+error4:
+       afb_stub_ws_unref(stubws);
 error3:
        close(apiws->fd);
 error2:
        free(apiws);
 error:
-       return -1;
+       return -!!strong;
+}
+
+int afb_api_ws_add_client_strong(const char *path, struct afb_apiset *apiset)
+{
+       return afb_api_ws_add_client(path, apiset, 1);
+}
+
+int afb_api_ws_add_client_weak(const char *path, struct afb_apiset *apiset)
+{
+       return afb_api_ws_add_client(path, apiset, 0);
 }
 
 static int api_ws_server_accept_client(struct api_ws *apiws, int fd)
@@ -319,6 +333,12 @@ int afb_api_ws_add_server(const char *path, struct afb_apiset *apiset)
        if (apiws == NULL)
                goto error;
 
+       /* check api name */
+       if (!afb_apiset_lookup(apiset, apiws->api, 1)) {
+               ERROR("Can't provide ws-server for %s: API %s doesn't exist", path, apiws->api);
+               goto error2;
+       }
+
        /* connect for serving */
        rc = api_ws_server_connect(apiws);
        if (rc < 0)