Add pws to be able to start apps
[apps/onscreenapp.git] / pws / pws.cpp
1 /*
2  * Copyright (c) 2020 Collabora Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cstdio>
18 #include <cstdlib>
19
20 #include <systemd/sd-event.h>
21 #include <json-c/json.h>
22 #include <assert.h>
23 #include "pws.h"
24
25 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
26 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
27 #endif
28
29 #define PWS_DEBUG
30
31 static void
32 idle(struct pws_data_source *pws_d_source)
33 {
34         struct sd_event *loop = pws_d_source->loop;
35
36         for (;;) {
37                 if (!pws_d_source->callcount)
38                         break;
39
40                 sd_event_run(loop, TIMEOUT_SD_LOOP);
41         }
42 }
43
44 static void
45 dec_callcount(struct pws_data_source *pws_d_source)
46 {
47         if (!pws_d_source)
48                 return;
49
50         pws_d_source->callcount--;
51 }
52
53 static void
54 inc_callcount(struct pws_data_source *pws_d_source)
55 {
56         if (!pws_d_source)
57                 return;
58
59         pws_d_source->callcount++;
60 }
61
62 static void
63 on_pws_reply(void *closure, void *request, struct json_object *result,
64              const char *error, const char *info)
65 {
66         struct pws_data_source *pws_d_source =
67                 static_cast<struct pws_data_source *>(closure);
68
69         assert(pws_d_source != NULL);
70
71 #ifdef PWS_DEBUG
72         fprintf(stdout, "ON-REPLY %s: %s %s\n%s\n", (char*) request, error,
73                         info ?: "",
74                         json_object_to_json_string_ext(result,
75                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
76
77         fflush(stdout);
78 #endif
79
80         /* in case of an error do not set the reply */
81         if (!info && !error) {
82                 /* should be cleaned-up after a proper request */
83                 assert(pws_d_source->reply_valid == false);
84
85                 pws_d_source->reply = result;
86                 pws_d_source->reply_valid = true;
87         } else {
88                 fprintf(stdout, "ON-REPLY: err: %s, request: %s, info %s\n",
89                                 error, (char *) request, info);
90         }
91
92         /* necessary when getting the reply to exit idle() */
93         dec_callcount(pws_d_source);
94 }
95
96 static void
97 on_pws_event_create(void *closure, uint16_t event_id, const char *event_name)
98 {
99         struct pws_data_source *pws_d_source =
100                 static_cast<struct pws_data_source *>(closure);
101         (void) pws_d_source;
102
103 #ifdef PWS_DEBUG
104         fprintf(stdout, "ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name);
105         fflush(stdout);
106 #endif
107 }
108
109 static void
110 on_pws_event_remove(void *closure, uint16_t event_id)
111 {
112         struct pws_data_source *pws_d_source =
113                 static_cast<struct pws_data_source *>(closure);
114
115         (void) pws_d_source;
116 #ifdef PWS_DEBUG
117         fprintf(stdout, "ON-EVENT-REMOVE: [%d]\n", event_id);
118         fflush(stdout);
119 #endif
120 }
121
122 static void
123 on_pws_event_subscribe(void *closure, void *request, uint16_t event_id)
124 {
125         struct pws_data_source *pws_d_source =
126                 static_cast<struct pws_data_source *>(closure);
127
128         (void) pws_d_source;
129 #ifdef PWS_DEBUG
130         fprintf(stdout, "ON-EVENT-SUBSCRIBE %s: [%d]\n", (char*)request, event_id);
131         fflush(stdout);
132 #endif
133 }
134
135 static void
136 on_pws_event_unsubscribe(void *closure, void *request, uint16_t event_id)
137 {
138         struct pws_data_source *pws_d_source =
139                 static_cast<struct pws_data_source *>(closure);
140
141         (void) pws_d_source;
142 #ifdef PWS_DEBUG
143         fprintf(stdout, "ON-EVENT-UNSUBSCRIBE %s: [%d]\n", (char*)request, event_id);
144         fflush(stdout);
145 #endif
146 }
147
148 static void
149 on_pws_event_push(void *closure, uint16_t event_id, struct json_object *data)
150 {
151         struct pws_data_source *pws_d_source =
152                 static_cast<struct pws_data_source *>(closure);
153
154         (void) pws_d_source;
155 #ifdef PWS_DEBUG
156         fprintf(stdout, "ON-EVENT-PUSH: [%d]\n%s\n",
157                         event_id,
158                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_NOSLASHESCAPE));
159         fprintf(stdout, "ON-EVENT-PUSH: [%d]\n%s\n",
160                         event_id,
161                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY |
162                                                              JSON_C_TO_STRING_NOSLASHESCAPE));
163
164         fflush(stdout);
165 #endif
166 }
167
168 static void
169 on_pws_event_broadcast(void *closure, const char *event_name,
170                       struct json_object *data,
171                       const afb_proto_ws_uuid_t uuid, uint8_t hop)
172 {
173         struct pws_data_source *pws_d_source =
174                 static_cast<struct pws_data_source *>(closure);
175
176         (void) pws_d_source;
177         (void) uuid;
178         (void) hop;
179
180 #ifdef PWS_DEBUG
181         fprintf(stdout, "ON-EVENT-BROADCAST: [%s]\n%s\n",
182                         event_name,
183                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_NOSLASHESCAPE));
184         fprintf(stdout, "ON-EVENT-BROADCAST: [%s]\n%s\n",
185                         event_name,
186                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY |
187                                                              JSON_C_TO_STRING_NOSLASHESCAPE));
188         fflush(stdout);
189 #endif
190 }
191
192 /* called when pws hangsup */
193 static void
194 on_pws_hangup(void *closure)
195 {
196         struct pws_data_source *pws_d_source =
197                 static_cast<struct pws_data_source *>(closure);
198
199         (void) pws_d_source;
200 #ifdef PWS_DEBUG
201         printf("ON-HANGUP\n");
202         fflush(stdout);
203 #endif
204
205         exit(EXIT_FAILURE);
206 }
207
208 /* makes a call */
209 static int
210 pws_call(const char *verb, const char *object,
211          struct pws_data_source *pws_d_source)
212 {
213         char *key;
214         struct json_object *o;
215         enum json_tokener_error jerr;
216
217         struct afb_proto_ws *pws = pws_d_source->pws;
218         int rc;
219
220         assert(pws != NULL);
221
222         /* allocates an id for the request */
223         rc = asprintf(&key, "%d:%s", ++pws_d_source->num_id, verb);
224         if (rc < 0)
225                 return -1;
226
227         /* echo the command if asked */
228         fprintf(stdout, "SEND-CALL: %s %s\n", verb, object?:"null");
229
230         inc_callcount(pws_d_source);
231
232         if (object == NULL || object[0] == 0) {
233                 o = NULL;
234         } else {
235                 o = json_tokener_parse_verbose(object, &jerr);
236                 if (jerr != json_tokener_success)
237                         o = json_object_new_string(object);
238         }
239
240         /* send the request */
241         rc = afb_proto_ws_client_call(pws, verb, o, 0, 0, key, NULL);
242         json_object_put(o);
243         if (rc < 0) {
244                 fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object ?: "");
245                 dec_callcount(pws_d_source);
246                 free(key);
247                 return -1;
248         }
249
250         free(key);
251         return 0;
252 }
253
254 /* the callback interface for pws */
255 static struct afb_proto_ws_client_itf pws_itf = {
256         .on_reply = on_pws_reply,
257         .on_event_create = on_pws_event_create,
258         .on_event_remove = on_pws_event_remove,
259         .on_event_subscribe = on_pws_event_subscribe,
260         .on_event_unsubscribe = on_pws_event_unsubscribe,
261         .on_event_push = on_pws_event_push,
262         .on_event_broadcast = on_pws_event_broadcast,
263 };
264
265 struct pws_data_source *
266 pws_data_source_init(const char *connect_to)
267 {
268         struct afb_proto_ws *pws;
269         struct sd_event *sd_loop;
270         struct pws_data_source *pws_d_source = nullptr;
271
272         if (!connect_to) {
273                 fprintf(stderr, "Failed to get a connect_to\n");
274                 return nullptr;
275         }
276
277         if (sd_event_default(&sd_loop) < 0) {
278                 fprintf(stderr, "Failed to get a default event\n");
279                 return nullptr;
280         }
281
282         pws_d_source =
283                 static_cast<struct pws_data_source *>(calloc(1, sizeof(struct pws_data_source)));
284
285         if (!pws_d_source) {
286                 fprintf(stderr, "Failed to allocate memory\n");
287                 return nullptr;
288         }
289
290         pws = afb_ws_client_connect_api(sd_loop, connect_to,
291                                         &pws_itf, pws_d_source);
292         if (!pws) {
293                 fprintf(stderr, "Failed to create a afb_proto_ws\n");
294                 return nullptr;
295         }
296
297         afb_proto_ws_on_hangup(pws, on_pws_hangup);
298
299         pws_d_source->pws = pws;
300         pws_d_source->loop = sd_loop;
301         pws_d_source->callcount = 0;
302         pws_d_source->num_id = 0;
303
304         pws_d_source->reply = nullptr;
305         pws_d_source->reply_valid = false;
306
307         return pws_d_source;
308 }
309
310 static int
311 pws_do_call(struct pws_data_source *pws_d_source,
312             const char *verb, const char *object)
313 {
314         if (!verb || !object || !pws_d_source)
315                 return -1;
316
317         if (pws_call(verb, object, pws_d_source) < 0)
318                 return -1;
319
320         idle(pws_d_source);
321
322         return 0;
323 }
324
325 void
326 pws_data_source_destroy(struct pws_data_source *pws_d_source)
327 {
328         assert(pws_d_source != nullptr);
329
330         sd_event_unref(pws_d_source->loop);
331         free(pws_d_source);
332 }
333
334 void
335 pws_data_source_reply_destroy(struct pws_data_source *pws)
336 {
337         assert(pws->reply_valid == true);
338         pws->reply_valid = false;
339         free(pws->reply);
340 }
341
342
343 int
344 pws_start_process(struct pws_data_source *pws, const char *afm_name)
345 {
346         int rid = -1;
347
348         fprintf(stdout, "pws_start_process() with afm_name %s\n", afm_name);
349         if (pws_do_call(pws, "start", afm_name) < 0)
350                 return -1;
351
352         if (pws->reply_valid) {
353                 struct json_object *result = pws->reply;
354
355                 rid = json_object_get_int(result);
356                 fprintf(stdout, "ON-REPLY %s\n",
357                                 json_object_to_json_string_ext(result,
358                                         JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
359
360                 pws_data_source_reply_destroy(pws);
361         }
362
363         return rid;
364 }
365
366 bool
367 pws_stop_process(struct pws_data_source *pws, const char *afm_name)
368 {
369         bool term = false;
370         if (pws_do_call(pws, "terminate", afm_name) < 0)
371                 return term;
372
373         if (pws->reply_valid) {
374                 struct json_object *result = pws->reply;
375
376                 term = json_object_get_boolean(result);
377 #ifdef PWS_DEBUG
378                 fprintf(stdout, "ON-REPLY %s\n",
379                                 json_object_to_json_string_ext(result,
380                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
381 #endif
382                 pws_data_source_reply_destroy(pws);
383         }
384
385         return term;
386 }
387
388
389 int
390 pws_check_process_is_running(struct pws_data_source *pws, const char *afm_name)
391 {
392         int rid = -1;
393
394         if (pws_do_call(pws, "state", afm_name) < 0)
395                 return rid;
396
397         if (pws->reply_valid) {
398                 struct json_object *result = pws->reply;
399                 struct json_object *obj;
400
401                 json_object_object_get_ex(result, "runid", &obj);
402                 if (obj) {
403                         rid = json_object_get_int(obj);
404                         fprintf(stdout, "Found rid %d\n", rid);
405                 }
406 #ifdef PWS_DEBUG
407                 fprintf(stdout, "ON-REPLY: %s\n",
408                                 json_object_to_json_string_ext(result,
409                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
410 #endif
411                 pws_data_source_reply_destroy(pws);
412         }
413
414         return rid;
415 }
416
417 size_t
418 pws_list_runners(struct pws_data_source *pws)
419 {
420         size_t items = 0;
421         if (pws_do_call(pws, "runners", "true") < 0)
422                 return items;
423
424         if (pws->reply_valid) {
425                 struct json_object *result = pws->reply;
426
427                 /* at least one is running */
428                 items = json_object_array_length(result);
429
430 #ifdef PWS_DEBUG
431                 fprintf(stdout, "found %ld items\n", items);
432                 fprintf(stdout, "ON-REPLY: %s\n",
433                                 json_object_to_json_string_ext(result,
434                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
435 #endif
436                 pws_data_source_reply_destroy(pws);
437         }
438
439         return items;
440 }
441
442 size_t
443 pws_list_runnables(struct pws_data_source *pws)
444 {
445         size_t items = 0;
446         if (pws_do_call(pws, "runnables", "true") < 0)
447                 return items;
448
449         if (pws->reply_valid) {
450                 struct json_object *result = pws->reply;
451
452                 items = json_object_array_length(result);
453 #ifdef PWS_DEBUG
454                 fprintf(stdout, "found %ld items\n", items);
455                 fprintf(stdout, "ON-REPLY: %s\n",
456                                 json_object_to_json_string_ext(result,
457                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
458 #endif
459                 pws_data_source_reply_destroy(pws);
460         }
461
462         return items;
463 }
464
465 /*
466  * need to free(json_string) once you're done with it
467  *
468  * call pws_data_source_reply_destroy(pws) once you're done with it.
469  */
470 size_t
471 pws_get_list_runnables(struct pws_data_source *pws,  struct json_object **json)
472 {
473         size_t items = 0;
474         if (pws_do_call(pws, "runnables", "true") < 0)
475                 return items;
476
477         fprintf(stdout, "pws_get_list_runnables()\n");
478
479         if (pws->reply_valid) {
480                 struct json_object *result = pws->reply;
481
482                 items = json_object_array_length(result);
483 #ifdef PWS_DEBUG
484                 fprintf(stdout, "found %ld items\n", items);
485                 fprintf(stdout, "ON-REPLY: %s\n",
486                                 json_object_to_json_string_ext(result,
487                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
488 #endif
489                 if (items == 0) {
490                         fprintf(stdout, "pws_get_list_runnables() turn items %ld, bails sooner\n", items);
491                         *json = NULL;
492                         return items;
493                 }
494
495                 fprintf(stdout, "pws_get_list_runnables() json reply is set\n");
496                 *json = result;
497         }
498
499         fprintf(stdout, "pws_get_list_runnables() turn items %ld\n", items);
500         return items;
501 }