add_library(afm
afm-db.c
afm-launch.c
+ afm-launch-mode.c
afm-run.c
)
--- /dev/null
+/*
+ Copyright 2015 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#include <string.h>
+
+#include "afm-launch-mode.h"
+
+static const char s_mode_local[] = "local";
+static const char s_mode_remote[] = "remote";
+
+enum afm_launch_mode launch_mode_of_string(const char *s)
+{
+ if (s) {
+ if (!strcmp(s, s_mode_local))
+ return mode_local;
+ if (!strcmp(s, s_mode_remote))
+ return mode_remote;
+ }
+ return invalid_launch_mode;
+}
+
+const char *name_of_launch_mode(enum afm_launch_mode m)
+{
+ switch (m) {
+ case mode_local: return s_mode_local;
+ case mode_remote: return s_mode_remote;
+ default: return "(INVALID LAUNCH MODE)";
+ }
+}
--- /dev/null
+/*
+ Copyright 2015 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+enum afm_launch_mode {
+ invalid_launch_mode = 0,
+ mode_local = 1,
+ mode_remote = 2
+};
+
+#define default_launch_mode mode_local
+
+#define launch_mode_is_valid(x) ((x)==mode_local || (x)==mode_remote)
+
+enum afm_launch_mode launch_mode_of_string(const char *s);
+const char *name_of_launch_mode(enum afm_launch_mode m);
+
extern char **environ;
#include "verbose.h"
+#include "afm-launch-mode.h"
#include "afm-launch.h"
#include "secmgr-wrap.h"
return rc;
}
-int afm_launch(struct afm_launch_desc *desc, pid_t children[2])
+int afm_launch(struct afm_launch_desc *desc, pid_t children[2], char **uri)
{
char datadir[PATH_MAX];
int ikl, rc;
/* should be init */
assert(groupid != 0);
+ assert(launch_mode_is_valid(desc->mode));
/* init */
children[0] = 0;
const char **plugins;
int width;
int height;
+ enum afm_launch_mode mode;
};
int afm_launch_initialize();
-int afm_launch(struct afm_launch_desc *desc, pid_t children[2]);
+int afm_launch(struct afm_launch_desc *desc, pid_t children[2], char **uri);
#include "verbose.h"
#include "utils-dir.h"
#include "utils-json.h"
-#include "afm-run.h"
+#include "afm-launch-mode.h"
#include "afm-launch.h"
+#include "afm-run.h"
enum appstate {
as_starting,
/**************** handle afm_launch_desc *********************/
-static int fill_launch_desc(struct json_object *appli, struct afm_launch_desc *desc)
+static int fill_launch_desc(struct json_object *appli, enum afm_launch_mode mode, struct afm_launch_desc *desc)
{
json_object *pub;
+ assert(launch_mode_is_valid(mode));
+
/* main items */
if(!j_read_object_at(appli, "public", &pub)
|| !j_read_string_at(appli, "path", &desc->path)
/* finaly */
desc->home = homeappdir;
+ desc->mode = mode;
return 0;
};
/**************** API handling ************************/
-int afm_run_start(struct json_object *appli)
+int afm_run_start(struct json_object *appli, enum afm_launch_mode mode, char **uri)
{
static struct apprun *runner;
struct afm_launch_desc desc;
int rc;
sigset_t saved, blocked;
+ assert(launch_mode_is_valid(mode));
+
/* prepare to launch */
- rc = fill_launch_desc(appli, &desc);
+ rc = fill_launch_desc(appli, mode, &desc);
if (rc)
return rc;
runner = createrunner(appli);
sigprocmask(SIG_BLOCK, &blocked, &saved);
/* launch now */
- rc = afm_launch(&desc, runner->pids);
+ rc = afm_launch(&desc, runner->pids, uri);
if (rc < 0) {
/* fork failed */
sigprocmask(SIG_SETMASK, &saved, NULL);
limitations under the License.
*/
-extern int afm_run_start(struct json_object *appli);
+extern int afm_run_start(struct json_object *appli, enum afm_launch_mode mode, char **uri);
extern int afm_run_terminate(int runid);
extern int afm_run_stop(int runid);
extern int afm_run_continue(int runid);
#include <stdio.h>
#include <time.h>
#include <getopt.h>
+#include <string.h>
#include <json.h>
#include "verbose.h"
#include "utils-jbus.h"
+#include "utils-json.h"
#include "afm.h"
#include "afm-db.h"
+#include "afm-launch-mode.h"
#include "afm-run.h"
static const char appname[] = "afm-user-daemon";
static void on_start(struct jreq *jreq, struct json_object *obj)
{
- const char *appid;
- struct json_object *appli;
+ const char *appid, *modestr;
+ char *uri;
+ struct json_object *appli, *resp;
int runid;
char runidstr[20];
-
- appid = getappid(obj);
- INFO("method start called for %s", appid);
- if (appid == NULL)
+ enum afm_launch_mode mode;
+
+ /* get the parameters */
+ mode = invalid_launch_mode;
+ if (j_read_string(obj, &appid)) {
+ mode = default_launch_mode;
+ } else if (j_read_string_at(obj, "id", &appid)) {
+ if (j_read_string_at(obj, "mode", &modestr)) {
+ mode = launch_mode_of_string(modestr);
+ } else {
+ mode = default_launch_mode;
+ }
+ }
+ if (!launch_mode_is_valid(mode)) {
jbus_reply_error_s(jreq, error_bad_request);
+ return;
+ }
+
+ /* get the application */
+ INFO("method start called for %s mode=%s", appid, mode);
+ appli = afm_db_get_application(afdb, appid);
+ if (appli == NULL) {
+ jbus_reply_error_s(jreq, error_not_found);
+ return;
+ }
+
+ /* launch the application */
+ uri = NULL;
+ runid = afm_run_start(appli, mode, &uri);
+ if (runid <= 0) {
+ jbus_reply_error_s(jreq, error_cant_start);
+ free(uri);
+ return;
+ }
+
+ if (uri == NULL) {
+ /* returns only the runid */
+ snprintf(runidstr, sizeof runidstr, "%d", runid);
+ runidstr[sizeof runidstr - 1] = 0;
+ jbus_reply_s(jreq, runidstr);
+ return;
+ }
+
+ /* returns the runid and its uri */
+ resp = json_object_new_object();
+ if (resp != NULL && j_add_integer(resp, "runid", runid) && j_add_string(resp, "uri", uri))
+ jbus_reply_j(jreq, resp);
else {
- appli = afm_db_get_application(afdb, appid);
- if (appli == NULL)
- jbus_reply_error_s(jreq, error_not_found);
- else {
- runid = afm_run_start(appli);
- if (runid <= 0)
- jbus_reply_error_s(jreq, error_cant_start);
- else {
- snprintf(runidstr, sizeof runidstr, "%d", runid);
- runidstr[sizeof runidstr - 1] = 0;
- jbus_reply_s(jreq, runidstr);
- }
- }
+ afm_run_stop(runid);
+ jbus_reply_error_s(jreq, error_cant_start);
}
+ json_object_put(resp);
+ free(uri);
}
static void on_stop(struct jreq *jreq, struct json_object *obj)