8 #define UNUSED(x) (void)(x)
10 const char * AFBClient::wmURI = "ws://localhost:1700/api?wm";
11 const char * AFBClient::wmAPI = "winman";
13 AFBClient::AFBClient() : itf()
15 ///* itinializing the callback interface for wsj1 */
16 itf.on_hangup = AFBClient::onHangup;
17 itf.on_call = AFBClient::onCall;
18 itf.on_event = AFBClient::onEvent;
21 bool AFBClient::init()
23 /* get the default event loop */
24 int rc = sd_event_default(&loop);
26 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
31 /* connect the websocket wsj1 to the uri given by the first argument */
32 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
34 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
42 void AFBClient::requestSurface(const char *label)
44 const char functionParamName[] = "{\"drawing_name\":\"";
45 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
46 strcpy(parameter, functionParamName);
47 strcat(parameter, label);
48 strcat(parameter, "\"}");
49 printf("requestSurface(%s): %s\n", label, parameter);
50 call(AFBClient::wmAPI, "request_surface", parameter);
53 void AFBClient::activateSurface(const char *label)
55 const char functionParamName[] = "{\"drawing_name\":\"";
56 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
57 strcpy(parameter, functionParamName);
58 strcat(parameter, label);
59 strcat(parameter, "\"}");
60 printf("activateSurface(%s): %s\n", label, parameter);
61 call(AFBClient::wmAPI, "activate_surface", parameter);
64 void AFBClient::deactivateSurface(const char *label)
69 void AFBClient::endDraw(const char *label)
74 /* called when wsj1 receives a method invocation */
75 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
80 printf("ON-CALL %s/%s:\n%s\n", api, verb,
81 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
82 JSON_C_TO_STRING_PRETTY));
84 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
86 fprintf(stderr, "replying failed: %m\n");
90 /* called when wsj1 receives an event */
91 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
95 printf("ON-EVENT %s:\n%s\n", event,
96 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
97 JSON_C_TO_STRING_PRETTY));
102 /* called when wsj1 hangsup */
103 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
107 printf("ON-HANGUP\n");
112 /* called when wsj1 receives a reply */
113 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
116 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
117 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
118 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
119 JSON_C_TO_STRING_PRETTY));
126 void AFBClient::call(const char *api, const char *verb, const char *object)
133 /* allocates an id for the request */
134 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
136 /* send the request */
137 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
139 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
144 void AFBClient::event(const char *event, const char *object)
149 rc = afb_wsj1_send_event_s(wsj1, event, object);
151 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
155 void AFBClient::emitSignalOrCall(const char *api, const char *verb, const char *object)
157 if (object == NULL || object[0] == 0)
159 if (api[0] == '!' && api[1] == 0)
162 call(api, verb, object);