/*
- * Copyright (C) 2015-2018 "IoT.bzh"
+ * Copyright (C) 2015-2019 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
struct {
/* event replica */
struct client_event *events;
+
+ /* robustify */
+ struct {
+ struct fdev *(*reopen)(void*);
+ void *closure;
+ void (*release)(void*);
+ } robust;
};
};
char apiname[1];
};
+static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t server);
+
/******************* ws request part for server *****************/
/* decrement the reference count of the request and free/release it on falling to null */
while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
ev = ev->next;
+ DEBUG("searching event %s[%d]: %s", name, eventid, ev ? "found" : "not found");
return ev;
}
+static struct afb_proto_ws *client_get_proto(struct afb_stub_ws *stubws)
+{
+ struct fdev *fdev;
+ struct afb_proto_ws *proto;
+
+ proto = stubws->proto;
+ if (proto == NULL && stubws->robust.reopen) {
+ fdev = stubws->robust.reopen(stubws->robust.closure);
+ if (fdev != NULL)
+ proto = afb_stub_ws_create_proto(stubws, fdev, 0);
+ }
+ return proto;
+}
+
/* on call, propagate it to the ws service */
static void client_api_call_cb(void * closure, struct afb_xreq *xreq)
{
int rc;
struct afb_stub_ws *stubws = closure;
+ struct afb_proto_ws *proto;
- if (stubws->proto == NULL) {
+ proto = client_get_proto(stubws);
+ if (proto == NULL) {
afb_xreq_reply(xreq, NULL, "disconnected", "server hung up");
return;
}
+ afb_xreq_unhooked_addref(xreq);
rc = afb_proto_ws_client_call(
- stubws->proto,
+ proto,
xreq->request.called_verb,
afb_xreq_json(xreq),
afb_session_uuid(xreq->context.session),
xreq,
xreq_on_behalf_cred_export(xreq));
- if (rc >= 0)
- afb_xreq_unhooked_addref(xreq);
- else
+ if (rc < 0) {
afb_xreq_reply(xreq, NULL, "internal", "can't send message");
+ afb_xreq_unhooked_unref(xreq);
+ }
}
static void client_on_description_cb(void *closure, struct json_object *data)
static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
{
struct client_describe *desc = closure;
+ struct afb_proto_ws *proto;
- if (signum || desc->stubws->proto == NULL)
+ proto = client_get_proto(desc->stubws);
+ if (signum || proto == NULL)
jobs_leave(jobloop);
else {
desc->jobloop = jobloop;
- afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
+ afb_proto_ws_client_describe(proto, client_on_description_cb, desc);
}
}
}
}
-static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
+static int enqueue_processing(struct afb_proto_ws *proto, void (*callback)(int signum, void* arg), void *arg)
{
- return jobs_queue(NULL, 0, callback, arg);
+ return jobs_queue(NULL /* proto */, 0, callback, arg);
}
/*****************************************************/
{
if (stubws && !__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
+ if (stubws->is_client) {
+ stubws->robust.reopen = NULL;
+ if (stubws->robust.release)
+ stubws->robust.release(stubws->robust.closure);
+ }
+
disconnect(stubws);
afb_apiset_unref(stubws->apiset);
free(stubws);
assert(stubws->is_client); /* check client */
api.closure = stubws;
api.itf = &client_api_itf;
- api.group = NULL;
+ api.group = stubws; /* serialize for reconnections */
return api;
}
{
return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
}
+
+void afb_stub_ws_client_robustify(struct afb_stub_ws *stubws, struct fdev *(*reopen)(void*), void *closure, void (*release)(void*))
+{
+ assert(stubws->is_client); /* check client */
+
+ if (stubws->robust.release)
+ stubws->robust.release(stubws->robust.closure);
+
+ stubws->robust.reopen = reopen;
+ stubws->robust.closure = closure;
+ stubws->robust.release = release;
+}