3 /* the callback interface for wsj1 */
4 static struct afb_wsj1_itf itf =
6 .on_hangup = AFBClient::onHangup,
7 .on_call = AFBClient::onCall,
8 .on_event = AFBClient::onEvent
11 AFBClient::AFBClient()
13 /* get the default event loop */
14 int rc = sd_event_default(&loop);
16 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
20 /* connect the websocket wsj1 to the uri given by the first argument */
21 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
23 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
28 void AFBClient::requestSurface(const char *label)
30 const char functionParamName[] = "{\"drawing_name\":\"";
31 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
32 strcpy(parameter, functionParamName);
33 strcat(parameter, label);
34 strcat(parameter, "\"}");
35 printf("requestSurface(%s): %s\n", label, parameter);
36 call(wmAPI, "request_surface", parameter);
39 void AFBClient::activateSurface(const char *label)
41 const char functionParamName[] = "{\"drawing_name\":\"";
42 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
43 strcpy(parameter, functionParamName);
44 strcat(parameter, label);
45 strcat(parameter, "\"}");
46 printf("activateSurface(%s): %s\n", label, parameter);
47 call(wmAPI, "activate_surface", parameter);
50 void AFBClient::deactivateSurface(const char *label)
54 void AFBClient::endDraw(const char *label)
58 /* called when wsj1 receives a method invocation */
59 void AFBClient::onCall(void *closure, afb_wsj1 *wsj1)
62 printf("ON-CALL %s/%s:\n%s\n", api, verb,
63 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
64 JSON_C_TO_STRING_PRETTY));
66 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
68 fprintf(stderr, "replying failed: %m\n");
71 /* called when wsj1 receives an event */
72 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
74 printf("ON-EVENT %s:\n%s\n", event,
75 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
76 JSON_C_TO_STRING_PRETTY));
80 /* called when wsj1 hangsup */
81 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
83 printf("ON-HANGUP\n");
88 /* called when wsj1 receives a reply */
89 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
91 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
92 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
93 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
94 JSON_C_TO_STRING_PRETTY));
100 void AFBClient::call(const char *api, const char *verb, const char *object)
106 /* allocates an id for the request */
107 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
109 /* send the request */
110 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
112 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
116 void AFBClient::event(const char *event, const char *object)
120 rc = afb_wsj1_send_event_s(wsj1, event, object);
122 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
125 void AFBClient::emitSignalOrCall(const char *api, const char *verb, const char *object)
127 if (object == NULL || object[0] == 0)
129 if (api[0] == '!' && api[1] == 0)
132 call(api, verb, object);