+/// object will be json_object_put
+int api_call(struct sd_event *loop, struct afb_wsj1 *wsj1, const char *verb,
+ json_object *object,
+ const std::function<void(bool, json_object *)> &onReply) {
+ TRACE();
+
+ // We need to wrap the actual onReply call once in order to
+ // *look* like a normal functions pointer (std::functions<>
+ // with captures cannot convert to function pointers).
+ // Alternatively we could setup a local struct and use it as
+ // closure, but I think it is cleaner this way.
+ int call_rc = 0;
+ std::atomic<bool> returned{};
+ returned.store(false, std::memory_order_relaxed);
+ std::function<void(bool, json_object *)> wrappedOnReply =
+ [&returned, &call_rc, &onReply](bool ok, json_object *j) {
+ TRACEN(wrappedOnReply);
+ call_rc = ok ? 0 : -EINVAL;
+ // We know it failed, but there may be an explanation in the
+ // json object.
+ {
+ TRACEN(onReply);
+ onReply(ok, j);
+ }
+ returned.store(true, std::memory_order_release);
+ };
+
+ // make the actual call, use wrappedOnReply as closure
+ int rc = afb_wsj1_call_j(
+ wsj1, wmAPI, verb, object,
+ [](void *closure, afb_wsj1_msg *msg) {
+ TRACEN(callClosure);
+ auto *onReply =
+ reinterpret_cast<std::function<void(bool, json_object *)> *>(
+ closure);
+ (*onReply)(!(afb_wsj1_msg_is_reply_ok(msg) == 0),
+ afb_wsj1_msg_object_j(msg));
+ },
+ &wrappedOnReply);
+
+ if (rc < 0) {
+ fprintf(
+ stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
+ json_object_to_json_string_ext(object, JSON_C_TO_STRING_PRETTY));
+ // Call the reply handler regardless with a NULL json_object*
+ onReply(false, nullptr);
+ } else {
+ // We need to dispatch until "returned" got set, this is necessary
+ // if events get triggered by the call (and would be dispatched before
+ // the actual call-reply).
+ while (!returned.load(std::memory_order_consume)) {
+ std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
+ if (!returned.load(std::memory_order_consume)) {
+ dispatch_internal(loop);
+ }
+ }
+
+ // return the actual API call result
+ rc = call_rc;
+ }
+
+ return rc;
+}
+
+} // namespace
+
+// _ ___ _ _ _
+// ___| | __ _ ___ ___ |_ _|_ __ ___ _ __ | | (_)_ __ ___ _ __ | |
+// / __| |/ _` / __/ __| | || '_ ` _ \| '_ \| | | | '_ ` _ \| '_ \| |
+// | (__| | (_| \__ \__ \ | || | | | | | |_) | | | | | | | | | |_) | |
+// \___|_|\__,_|___/___/ |___|_| |_| |_| .__/|_| |_|_| |_| |_| .__/|_|
+// |_| |_|
+AFBClient::Impl::Impl() : wsj1{}, loop{}, labels(), handlers() { TRACE(); }
+
+AFBClient::Impl::~Impl() {
+ TRACE();
+ afb_wsj1_unref(wsj1);
+ sd_event_unref(loop);
+}
+
+int AFBClient::Impl::init(int port, char const *token) {
+ TRACE();
+ char *uribuf = nullptr;
+ int rc = -1;
+
+ if (this->loop != nullptr && this->wsj1 != nullptr) {
+ fputs("AFBClient instance is already initialized!\n", stderr);
+ rc = -EALREADY;
+ goto fail;
+ }
+
+ if ((token == nullptr) || strlen(token) > token_maxlen) {
+ fputs("Token is invalid\n", stderr);
+ rc = -EINVAL;
+ goto fail;
+ }
+
+ for (char const *p = token; *p != 0; p++) {
+ if (isalnum(*p) == 0) {
+ fputs("Token is invalid\n", stderr);
+ rc = -EINVAL;
+ goto fail;
+ }
+ }
+
+ if (port < 1 && port > 0xffff) {
+ fputs("Port is invalid\n", stderr);
+ rc = -EINVAL;
+ goto fail;
+ }
+