+static void on_pws_reply_success(void *closure, void *request, struct json_object *result, const char *info)
+{
+ if (raw)
+ printf("ON-REPLY-SUCCESS %s: [%s] %s\n", (char*)request, info?:"", json_object_to_json_string(result));
+ if (human)
+ printf("ON-REPLY-SUCCESS %s: %s\n%s\n", (char*)request, info?:"", json_object_to_json_string_ext(result, JSON_C_TO_STRING_PRETTY));
+ fflush(stdout);
+ free(request);
+ dec_callcount();
+}
+
+static void on_pws_reply_fail(void *closure, void *request, const char *status, const char *info)
+{
+ printf("ON-REPLY-FAIL %s: %s [%s]\n", (char*)request, status?:"?", info?:"");
+ fflush(stdout);
+ free(request);
+ dec_callcount();
+}
+
+static void on_pws_event_create(void *closure, const char *event_name, int event_id)
+{
+ printf("ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name);
+ fflush(stdout);
+}
+
+static void on_pws_event_remove(void *closure, const char *event_name, int event_id)
+{
+ printf("ON-EVENT-REMOVE: [%d:%s]\n", event_id, event_name);
+ fflush(stdout);
+}
+
+static void on_pws_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
+{
+ printf("ON-EVENT-SUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
+ fflush(stdout);
+}
+
+static void on_pws_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
+{
+ printf("ON-EVENT-UNSUBSCRIBE %s: [%d:%s]\n", (char*)request, event_id, event_name);
+ fflush(stdout);
+}
+
+static void on_pws_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
+{
+ if (raw)
+ printf("ON-EVENT-PUSH: [%d:%s] %s\n", event_id, event_name, json_object_to_json_string(data));
+ if (human)
+ printf("ON-EVENT-PUSH: [%d:%s]\n%s\n", event_id, event_name, json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
+ fflush(stdout);
+}
+
+static void on_pws_event_broadcast(void *closure, const char *event_name, struct json_object *data)
+{
+ if (raw)
+ printf("ON-EVENT-BROADCAST: [%s] %s\n", event_name, json_object_to_json_string(data));
+ if (human)
+ printf("ON-EVENT-BROADCAST: [%s]\n%s\n", event_name, json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
+ fflush(stdout);
+}
+
+static void on_pws_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args)
+{
+ if (raw)
+ printf("ON-SUBCALL %s: %s/%s %s\n", (char*)request, api, verb, json_object_to_json_string(args));
+ if (human)
+ printf("ON-SUBCALL %s: %s/%s\n%s\n", (char*)request, api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_PRETTY));
+ afb_proto_ws_subcall_reply(subcall, 1, NULL);
+ fflush(stdout);
+}
+
+/* makes a call */
+static void pws_call(const char *verb, const char *object)
+{
+ static int num = 0;
+ char *key;
+ int rc;
+ struct json_object *o;
+
+ /* allocates an id for the request */
+ rc = asprintf(&key, "%d:%s", ++num, verb);
+
+ /* send the request */
+ callcount++;
+ if (object == NULL || object[0] == 0 || !strcmp(object, "null"))
+ o = NULL;
+ else {
+ o = json_tokener_parse(object);
+ if (!o)
+ o = json_object_new_string(object);
+ }
+ rc = afb_proto_ws_client_call(pws, verb, o, sessionid, key);
+ if (rc < 0) {
+ fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object?:"");
+ dec_callcount();
+ }
+}
+
+/* called when pws hangsup */
+static void on_pws_hangup(void *closure)
+{
+ printf("ON-HANGUP\n");
+ fflush(stdout);
+ exit(0);
+}
+
+