9 /* the callback interface for wsj1 */
10 static struct afb_wsj1_itf itf =
12 .on_hangup = AFBClient::onHangup,
13 .on_call = AFBClient::onCall,
14 .on_event = AFBClient::onEvent
18 AFBClient::AFBClient()
22 bool AFBClient::init()
24 /* get the default event loop */
25 int rc = sd_event_default(&loop);
27 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
32 /* connect the websocket wsj1 to the uri given by the first argument */
33 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
35 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
43 void AFBClient::requestSurface(const char *label)
45 const char functionParamName[] = "{\"drawing_name\":\"";
46 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
47 strcpy(parameter, functionParamName);
48 strcat(parameter, label);
49 strcat(parameter, "\"}");
50 printf("requestSurface(%s): %s\n", label, parameter);
51 call(wmAPI, "request_surface", parameter);
54 void AFBClient::activateSurface(const char *label)
56 const char functionParamName[] = "{\"drawing_name\":\"";
57 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
58 strcpy(parameter, functionParamName);
59 strcat(parameter, label);
60 strcat(parameter, "\"}");
61 printf("activateSurface(%s): %s\n", label, parameter);
62 call(wmAPI, "activate_surface", parameter);
65 void AFBClient::deactivateSurface(const char *label)
69 void AFBClient::endDraw(const char *label)
73 /* called when wsj1 receives a method invocation */
74 void AFBClient::onCall(void *closure, afb_wsj1 *wsj1)
78 printf("ON-CALL %s/%s:\n%s\n", wmAPI, verb,
79 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
80 JSON_C_TO_STRING_PRETTY));
82 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
84 fprintf(stderr, "replying failed: %m\n");
88 /* called when wsj1 receives an event */
89 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
92 printf("ON-EVENT %s:\n%s\n", event,
93 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
94 JSON_C_TO_STRING_PRETTY));
99 /* called when wsj1 hangsup */
100 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
102 printf("ON-HANGUP\n");
107 /* called when wsj1 receives a reply */
108 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
111 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
112 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
113 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
114 JSON_C_TO_STRING_PRETTY));
121 void AFBClient::call(const char *api, const char *verb, const char *object)
128 /* allocates an id for the request */
129 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
131 /* send the request */
132 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
134 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
139 void AFBClient::event(const char *event, const char *object)
144 rc = afb_wsj1_send_event_s(wsj1, event, object);
146 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
150 void AFBClient::emitSignalOrCall(const char *api, const char *verb, const char *object)
152 if (object == NULL || object[0] == 0)
154 if (api[0] == '!' && api[1] == 0)
157 call(api, verb, object);