afb-stub-ws: improvements
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 11 Aug 2017 14:57:53 +0000 (16:57 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 11 Aug 2017 17:14:54 +0000 (19:14 +0200)
The client stub isn't connecting anymore
automatically to the apiset. The function
'afb_stub_ws_client_add' provides the feature.

The client now returns a afb_api structure.

name and on_hangup features added.

Change-Id: I204cc39adeb782f5d40360c22c79d14f54dd5c63
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api-ws.c
src/afb-stub-ws.c
src/afb-stub-ws.h

index dba8d05..07c8202 100644 (file)
@@ -226,9 +226,14 @@ 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:
index 351b332..ce68b44 100644 (file)
@@ -144,7 +144,7 @@ struct server_describe
        uint32_t descid;
 };
 
-/******************* client description part for server *****************************/
+/******************* stub description for client or servers ******************/
 
 struct afb_stub_ws
 {
@@ -181,6 +181,9 @@ struct afb_stub_ws
        /* apiset */
        struct afb_apiset *apiset;
 
+       /* on hangup callback */
+       void (*on_hangup)(struct afb_stub_ws *);
+
        /* the api name */
        char apiname[1];
 };
@@ -1205,6 +1208,8 @@ static void server_on_hangup(void *closure)
        if (stubws->fd >= 0) {
                close(stubws->fd);
                stubws->fd = -1;
+               if (stubws->on_hangup)
+                       stubws->on_hangup(stubws);
        }
 
        /* release the client */
@@ -1272,19 +1277,7 @@ static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struc
 
 struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset)
 {
-       struct afb_api afb_api;
-       struct afb_stub_ws *stubws;
-
-       stubws = afb_stub_ws_create(fd, apiname, apiset, &stub_ws_client_ws_itf);
-       if (stubws) {
-               afb_api.closure = stubws;
-               afb_api.itf = &ws_api_itf;
-               if (afb_apiset_add(apiset, stubws->apiname, afb_api) >= 0)
-                       return stubws;
-               afb_stub_ws_unref(stubws);
-       }
-       return NULL;
-
+       return afb_stub_ws_create(fd, apiname, apiset, &stub_ws_client_ws_itf);
 }
 
 struct afb_stub_ws *afb_stub_ws_create_server(int fd, const char *apiname, struct afb_apiset *apiset)
@@ -1327,3 +1320,28 @@ void afb_stub_ws_addref(struct afb_stub_ws *stubws)
        __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
 }
 
+void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
+{
+       stubws->on_hangup = on_hangup;
+}
+
+const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
+{
+       return stubws->apiname;
+}
+
+struct afb_api afb_stub_ws_client_api(struct afb_stub_ws *stubws)
+{
+       struct afb_api api;
+
+       assert(!stubws->listener); /* check client */
+       api.closure = stubws;
+       api.itf = &ws_api_itf;
+       return api;
+}
+
+int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
+{
+       return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
+}
+
index 33ab5b2..4e07f98 100644 (file)
@@ -20,6 +20,7 @@
 
 struct afb_stub_ws;
 struct afb_apiset;
+struct afb_api;
 
 extern struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset);
 
@@ -29,3 +30,11 @@ extern void afb_stub_ws_unref(struct afb_stub_ws *stubws);
 
 extern void afb_stub_ws_addref(struct afb_stub_ws *stubws);
 
+extern void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*));
+
+extern const char *afb_stub_ws_name(struct afb_stub_ws *stubws);
+
+extern struct afb_api afb_stub_ws_client_api(struct afb_stub_ws *stubws);
+
+extern int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset);
+