-extern "C" {
-extern struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure);
-extern int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, const char *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure);
-extern int afb_wsj1_msg_is_reply_ok(struct afb_wsj1_msg *msg);
-extern int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object);
-static inline int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token);
+namespace {
+
+constexpr const int token_maxlen = 20;
+constexpr const char *const wmAPI = "winman";
+
+#ifdef NDEBUG
+#define TRACE()
+#else
+#define TRACE() \
+ ScopeTrace __attribute__((unused)) trace_scope_here__(__PRETTY_FUNCTION__)
+
+struct ScopeTrace {
+ static int indent;
+ char const *f{};
+ ScopeTrace(char const *func) : f(func) {
+ fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
+ }
+ ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
+};
+int ScopeTrace::indent = 0;
+#endif
+
+/* called when wsj1 receives a method invocation */
+void onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
+{
+ TRACE();
+ UNUSED(closure);
+ int rc;
+ printf("ON-CALL %s/%s:\n%s\n", api, verb,
+ json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
+ JSON_C_TO_STRING_PRETTY));
+ fflush(stdout);
+ rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
+ if (rc < 0)
+ fprintf(stderr, "replying failed: %m\n");
+}
+
+/* called when wsj1 receives an event */
+void onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
+{
+ TRACE();
+ UNUSED(closure);
+ printf("ON-EVENT %s:\n%s\n", event,
+ json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
+ JSON_C_TO_STRING_PRETTY));
+ fflush(stdout);
+}
+
+/* called when wsj1 hangsup */
+void onHangup(void *closure, afb_wsj1 *wsj1)
+{
+ TRACE();
+ UNUSED(closure);
+ UNUSED(wsj1);
+ printf("ON-HANGUP\n");
+ fflush(stdout);
+ exit(0);
+}
+
+/* called when wsj1 receives a reply */
+void onReply(void *closure, afb_wsj1_msg *msg)
+{
+ TRACE();
+ printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
+ afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
+ json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
+ JSON_C_TO_STRING_PRETTY));
+ fflush(stdout);
+ free(closure);