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 AFBClient& AFBClient::operator =(const AFBClient &that)
27 AFBClient::~AFBClient()
31 bool AFBClient::init()
33 /* get the default event loop */
34 int rc = sd_event_default(&loop);
36 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
41 /* connect the websocket wsj1 to the uri given by the first argument */
42 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
44 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
52 void AFBClient::requestSurface(const char *label)
54 const char functionParamName[] = "{\"drawing_name\":\"";
55 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
56 strcpy(parameter, functionParamName);
57 strcat(parameter, label);
58 strcat(parameter, "\"}");
59 printf("requestSurface(%s): %s\n", label, parameter);
60 call(AFBClient::wmAPI, "request_surface", parameter);
63 void AFBClient::activateSurface(const char *label)
65 const char functionParamName[] = "{\"drawing_name\":\"";
66 char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
67 strcpy(parameter, functionParamName);
68 strcat(parameter, label);
69 strcat(parameter, "\"}");
70 printf("activateSurface(%s): %s\n", label, parameter);
71 call(AFBClient::wmAPI, "activate_surface", parameter);
74 void AFBClient::deactivateSurface(const char *label)
79 void AFBClient::endDraw(const char *label)
84 /* called when wsj1 receives a method invocation */
85 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
90 printf("ON-CALL %s/%s:\n%s\n", api, verb,
91 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
92 JSON_C_TO_STRING_PRETTY));
94 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
96 fprintf(stderr, "replying failed: %m\n");
100 /* called when wsj1 receives an event */
101 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
105 printf("ON-EVENT %s:\n%s\n", event,
106 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
107 JSON_C_TO_STRING_PRETTY));
112 /* called when wsj1 hangsup */
113 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
117 printf("ON-HANGUP\n");
122 /* called when wsj1 receives a reply */
123 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
126 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
127 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
128 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
129 JSON_C_TO_STRING_PRETTY));
136 void AFBClient::call(const char *api, const char *verb, const char *object)
143 /* allocates an id for the request */
144 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
146 /* send the request */
147 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
149 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
154 void AFBClient::event(const char *event, const char *object)
159 rc = afb_wsj1_send_event_s(wsj1, event, object);
161 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
165 void AFBClient::emitSignalOrCall(const char *api, const char *verb, const char *object)
167 if (object == NULL || object[0] == 0)
169 if (api[0] == '!' && api[1] == 0)
172 call(api, verb, object);