/*
- Copyright 2015, 2016 IoT.bzh
+ Copyright 2015, 2016, 2017 IoT.bzh
author: José Bollo <jose.bollo@iot.bzh>
#include "verbose.h"
#include "utils-jbus.h"
#include "utils-json.h"
+#include "utils-systemd.h"
#include "afm.h"
-#include "afm-db.h"
-#include "afm-launch-mode.h"
-#include "afm-run.h"
+#include "afm-udb.h"
+#include "afm-urun.h"
/*
* name of the application
*/
static const char appname[] = "afm-user-daemon";
+/*
+ * string for printing version
+ */
+static const char versionstr[] =
+ "\n"
+ " %s version="AFM_VERSION"\n"
+ "\n"
+ " Copyright (C) 2015, 2016, 2017 \"IoT.bzh\"\n"
+ " AFB comes with ABSOLUTELY NO WARRANTY.\n"
+ " Licence Apache 2\n"
+ "\n";
+
/*
* string for printing usage
*/
static const char usagestr[] =
- "usage: %s [-q] [-v] [-m mode] [-r rootdir]... [-a appdir]...\n"
+ "usage: %s [option(s)]\n"
"\n"
- " -a appdir adds an application directory\n"
- " -r rootdir adds a root directory of applications\n"
- " -m mode set default launch mode (local or remote)\n"
" -d run as a daemon\n"
" -u addr address of user D-Bus to use\n"
" -s addr address of system D-Bus to use\n"
" -q quiet\n"
" -v verbose\n"
+ " -V version\n"
"\n";
/*
* Option definition for getopt_long
*/
-static const char options_s[] = "hdqvr:a:m:";
+static const char options_s[] = "hdqvV";
static struct option options_l[] = {
- { "root", required_argument, NULL, 'r' },
- { "application", required_argument, NULL, 'a' },
- { "mode", required_argument, NULL, 'm' },
{ "user-dbus", required_argument, NULL, 'u' },
{ "system-dbus", required_argument, NULL, 's' },
{ "daemon", no_argument, NULL, 'd' },
{ "quiet", no_argument, NULL, 'q' },
{ "verbose", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
/*
* Handle to the database of applications
*/
-static struct afm_db *afdb;
+static struct afm_udb *afudb;
/*
* Returned error strings
{
struct json_object *resp;
INFO("method runnables called");
- resp = afm_db_application_list(afdb);
+ resp = afm_udb_applications_public(afudb);
jbus_reply_j(smsg, resp);
json_object_put(resp);
}
/* wants details for appid */
INFO("method detail called for %s", appid);
- resp = afm_db_get_application_public(afdb, appid);
+ resp = afm_udb_get_application_public(afudb, appid);
reply(smsg, resp, error_not_found);
json_object_put(resp);
}
*/
static void on_start(struct sd_bus_message *smsg, struct json_object *obj, void *unused)
{
- const char *appid, *modestr;
+ const char *appid;
char *uri;
struct json_object *appli, *resp;
int runid;
char runidstr[20];
- enum afm_launch_mode mode;
/* get the parameters */
- mode = invalid_launch_mode;
- if (j_read_string(obj, &appid)) {
- mode = get_default_launch_mode();
- } else if (j_read_string_at(obj, "id", &appid)) {
- if (j_read_string_at(obj, "mode", &modestr)) {
- mode = launch_mode_of_name(modestr);
- } else {
- mode = get_default_launch_mode();
+ if (!j_read_string(obj, &appid)) {
+ if (!j_read_string_at(obj, "id", &appid)) {
+ jbus_reply_error_s(smsg, error_bad_request);
+ return;
}
}
- if (!is_valid_launch_mode(mode)) {
- jbus_reply_error_s(smsg, error_bad_request);
- return;
- }
/* get the application */
- INFO("method start called for %s mode=%s", appid,
- name_of_launch_mode(mode));
- appli = afm_db_get_application(afdb, appid);
+ INFO("method start called for %s", appid);
+ appli = afm_udb_get_application_private(afudb, appid);
if (appli == NULL) {
jbus_reply_error_s(smsg, error_not_found);
return;
/* launch the application */
uri = NULL;
- runid = afm_run_start(appli, mode, &uri);
+ runid = afm_urun_start(appli);
if (runid <= 0) {
jbus_reply_error_s(smsg, error_cant_start);
free(uri);
&& j_add_string(resp, "uri", uri))
jbus_reply_j(smsg, resp);
else {
- afm_run_terminate(runid);
+ afm_urun_terminate(runid);
jbus_reply_error_s(smsg, error_system);
}
json_object_put(resp);
/* get the application */
INFO("method once called for %s", appid);
- appli = afm_db_get_application(afdb, appid);
+ appli = afm_udb_get_application_private(afudb, appid);
if (appli == NULL) {
jbus_reply_error_s(smsg, error_not_found);
return;
}
/* launch the application */
- runid = afm_run_once(appli);
+ runid = afm_urun_once(appli);
if (runid <= 0) {
jbus_reply_error_s(smsg, error_cant_start);
return;
}
/* returns the state */
- resp = afm_run_state(runid);
+ resp = afm_urun_state(afudb, runid);
reply(smsg, resp, error_not_found);
json_object_put(resp);
}
{
int runid, status;
if (onrunid(smsg, obj, "pause", &runid)) {
- status = afm_run_pause(runid);
+ status = afm_urun_pause(runid);
reply_status(smsg, status, error_not_found);
}
}
{
int runid, status;
if (onrunid(smsg, obj, "resume", &runid)) {
- status = afm_run_resume(runid);
+ status = afm_urun_resume(runid);
reply_status(smsg, status, error_not_found);
}
}
{
int runid, status;
if (onrunid(smsg, obj, "terminate", &runid)) {
- status = afm_run_terminate(runid);
+ status = afm_urun_terminate(runid);
reply_status(smsg, status, error_not_found);
}
}
{
struct json_object *resp;
INFO("method runners called");
- resp = afm_run_list();
+ resp = afm_urun_list(afudb);
jbus_reply_j(smsg, resp);
json_object_put(resp);
}
int runid;
struct json_object *resp;
if (onrunid(smsg, obj, "state", &runid)) {
- resp = afm_run_state(runid);
+ resp = afm_urun_state(afudb, runid);
reply(smsg, resp, error_not_found);
json_object_put(resp);
}
*/
static void on_signal_changed(struct json_object *obj, void *unused)
{
+ /* enforce daemon reload */
+ systemd_daemon_reload(1);
+ systemd_unit_restart_name(1, "sockets.target");
+
/* update the database */
- afm_db_update_applications(afdb);
+ afm_udb_update(afudb);
+
/* re-propagate now */
jbus_send_signal_j(user_bus, "changed", obj);
}
int rc;
if (address == NULL)
- return (isuser ? sd_bus_open_user : sd_bus_open_system)(ret);
+ return (isuser ? sd_bus_default_user : sd_bus_default_system)(ret);
rc = sd_bus_new(&b);
if (rc < 0)
int main(int ac, char **av)
{
int i, daemon = 0, rc;
- enum afm_launch_mode mode;
struct sd_event *evloop;
struct sd_bus *sysbus, *usrbus;
const char *sys_bus_addr, *usr_bus_addr;
case 'h':
printf(usagestr, appname);
return 0;
+ case 'V':
+ printf(versionstr, appname);
+ return 0;
case 'q':
if (verbosity)
verbosity--;
case 'd':
daemon = 1;
break;
- case 'r':
- break;
- case 'a':
- break;
- case 'm':
- mode = launch_mode_of_name(optarg);
- if (!is_valid_launch_mode(mode)) {
- ERROR("invalid mode '%s'", optarg);
- return 1;
- }
- set_default_launch_mode(mode);
- break;
case 'u':
usr_bus_addr = optarg;
break;
/* init random generator */
srandom((unsigned int)time(NULL));
- /* init runners */
- if (afm_run_init()) {
- ERROR("afm_run_init failed");
- return 1;
- }
-
- /* init framework */
- afdb = afm_db_create();
- if (!afdb) {
- ERROR("afm_create failed");
- return 1;
- }
- if (afm_db_add_root(afdb, FWK_APP_DIR)) {
- ERROR("can't add root %s", FWK_APP_DIR);
- return 1;
- }
-
- /* second interpretation of arguments */
- optind = 1;
- while ((i = getopt_long(ac, av, options_s, options_l, NULL)) >= 0) {
- switch (i) {
- case 'r':
- if (afm_db_add_root(afdb, optarg)) {
- ERROR("can't add root %s", optarg);
- return 1;
- }
- break;
- case 'a':
- if (afm_db_add_application(afdb, optarg)) {
- ERROR("can't add application %s", optarg);
- return 1;
- }
- break;
- }
- }
-
- /* update the database */
- if (afm_db_update_applications(afdb)) {
- ERROR("afm_update_applications failed");
+ /* init database */
+ afudb = afm_udb_create(1, 1, "afm-appli-");
+ if (!afudb) {
+ ERROR("afm_udb_create failed");
return 1;
}
return 0;
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-