aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-02-14 15:16:48 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2016-02-14 15:16:48 +0100
commit3a6db27dec6fee245708074c301b42e1cbedd716 (patch)
treeae4f6437adc980e8c199246aa9913660b7d988f5
parent96b44f68e34f41c145b54ad2f7c4051c7d07ecb0 (diff)
adds mode management
Change-Id: I73240e346a06924c314961871e789c69e279cb48 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--include/local-def.h2
-rw-r--r--plugins/afm-main-plugin/afm-main-plugin.c46
-rw-r--r--src/config.c3
-rw-r--r--src/main.c7
4 files changed, 49 insertions, 9 deletions
diff --git a/include/local-def.h b/include/local-def.h
index bf353e94..7ad4e705 100644
--- a/include/local-def.h
+++ b/include/local-def.h
@@ -184,8 +184,8 @@ typedef struct {
int apiTimeout;
int cntxTimeout; // Client Session Context timeout
int pluginCount; // loaded plugins count
- AFB_aliasdir *aliasdir; // alias mapping for icons,apps,...
AFB_Mode mode; // mode of listening
+ AFB_aliasdir *aliasdir; // alias mapping for icons,apps,...
} AFB_config;
typedef struct {
diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c
index fa184bf8..3f9de713 100644
--- a/plugins/afm-main-plugin/afm-main-plugin.c
+++ b/plugins/afm-main-plugin/afm-main-plugin.c
@@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#define _GNU_SOURCE /* See feature_test_macros(7) */
+#include <stdio.h>
#include "local-def.h"
@@ -33,6 +35,10 @@ static char _runners_[] = "runners";
static char _state_[] = "state";
static char _install_[] = "install";
static char _uninstall_[] = "uninstall";
+static const char _mode_[] = "mode";
+static const char _local_[] = "local";
+static const char _remote_[]= "remote";
+static const char _uri_[] = "uri";
static struct jbus *jbus;
@@ -117,9 +123,43 @@ static struct json_object *call_void__runnables(AFB_request *request, AFB_PostIt
return embed(request, _runnables_, call_void(request, item));
}
-static struct json_object *call_appid__runid(AFB_request *request, AFB_PostItem *item)
+static struct json_object *call_start(AFB_request *request, AFB_PostItem *item)
{
- return embed(request, _runid_, call_appid(request, item));
+ struct json_object *resp;
+ const char *id, *mode;
+ char *query;
+ int rc;
+
+ /* get the id */
+ id = getQueryValue(request, _id_);
+ if (id == NULL) {
+ request->errcode = MHD_HTTP_BAD_REQUEST;
+ return NULL;
+ }
+ /* get the mode */
+ mode = getQueryValue(request, _mode_);
+ if (mode == NULL) {
+ mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
+ }
+
+ /* create the query */
+ rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
+ if (rc < 0) {
+ request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
+ return NULL;
+ }
+
+ /* calls the service */
+ resp = jbus_call_sj_sync(jbus, _start_, query);
+ if (verbose)
+ fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL");
+ free(query);
+
+ /* embed if needed */
+ if (json_object_get_type(resp) == json_type_string)
+ resp = embed(request, _runid_, resp);
+ request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
+ return resp;
}
static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item)
@@ -157,7 +197,7 @@ static AFB_restapi plug_apis[] =
{
{_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables, "Get list of runnable applications"},
{_detail_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"},
- {_start_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid__runid, "Start an application"},
+ {_start_ , AFB_SESSION_CHECK, (AFB_apiCB)call_start, "Start an application"},
{_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"},
{_stop_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"},
{_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"},
diff --git a/src/config.c b/src/config.c
index bd03b6ff..a64e131f 100644
--- a/src/config.c
+++ b/src/config.c
@@ -61,7 +61,8 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) {
// TBD integrate alias-dir array with config-file
session->config->aliasdir = cliconfig->aliasdir;
-
+ session->config->mode = cliconfig->mode;
+
// default HTTP port
if (cliconfig->httpdPort == 0) session->config->httpdPort=1234;
else session->config->httpdPort=cliconfig->httpdPort;
diff --git a/src/main.c b/src/main.c
index 533776a1..57660edf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -419,10 +419,9 @@ int main(int argc, char *argv[]) {
case SET_MODE:
if (optarg == 0) goto needValueForOption;
- /* TODO setting directly session->config isn't the expected path but... see configLoadFile, configStoreFile (JOBOL) */
- if (!strcmp(optarg, "local")) session->config->mode = AFB_MODE_LOCAL;
- else if (!strcmp(optarg, "remote")) session->config->mode = AFB_MODE_REMOTE;
- else if (!strcmp(optarg, "global")) session->config->mode = AFB_MODE_GLOBAL;
+ if (!strcmp(optarg, "local")) cliconfig.mode = AFB_MODE_LOCAL;
+ else if (!strcmp(optarg, "remote")) cliconfig.mode = AFB_MODE_REMOTE;
+ else if (!strcmp(optarg, "global")) cliconfig.mode = AFB_MODE_GLOBAL;
else goto badMode;
break;