+static int
+is_application_running(afb_req_t request, std::string id)
+{
+ bool app_still_running = false;
+ struct json_object *jobj = nullptr;
+
+ HS_AfmMainProxy afm_proxy;
+
+ // note this is sync, so this might block if afm-system-daemon is down
+ afm_proxy.ps(request->api, &jobj);
+
+ if (jobj) {
+ const int len = json_object_array_length(jobj);
+ for (int i = 0; i < len; i++) {
+ struct json_object *aid;
+ struct json_object *item =
+ json_object_array_get_idx(jobj, i);
+
+ bool isFound = json_object_object_get_ex(item, "id", &aid);
+ if (isFound) {
+ const char *str_appid = json_object_get_string(aid);
+ if (strcmp(str_appid, id.c_str()) == 0) {
+ app_still_running = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (!app_still_running) {
+ // we don't remove it from the context list as we're haven't really subscribed,
+ // and we just need to remove it from client_list, which happens here. We also
+ // return AFB_REQ_NOT_STARTED_APPLICATION which will attempt to start it (again).
+ HS_ClientManager::instance()->removeClient(id);
+ return AFB_REQ_NOT_STARTED_APPLICATION;
+ }
+
+ return 0;
+}
+