aboutsummaryrefslogtreecommitdiffstats
path: root/src/main-afs-supervisor.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2018-04-09 18:16:07 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2018-06-15 17:57:36 +0200
commit4521c1e7ae5371ab9d639adc617d17fb4e8ded0c (patch)
treea8a1416a2d58c16ab3993c7e4dc405fc71daab6a /src/main-afs-supervisor.c
parent63682b4da9d3e892d1d0a671de860adc43068142 (diff)
api-v3: First draft
This commit introduces the bindings v3 API for bindings. The documentation has still to be improved and will come very soon. Change-Id: I8f9007370e29f671fdfd1da87fff7372a17db7af Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/main-afs-supervisor.c')
-rw-r--r--src/main-afs-supervisor.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/main-afs-supervisor.c b/src/main-afs-supervisor.c
new file mode 100644
index 00000000..14f1c3b0
--- /dev/null
+++ b/src/main-afs-supervisor.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (C) 2016, 2017, 2018 "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.
+ */
+
+#define _GNU_SOURCE
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+#include <systemd/sd-daemon.h>
+
+#include "afb-common.h"
+#include "afb-hsrv.h"
+#include "afb-hswitch.h"
+#include "afb-hreq.h"
+#include "afb-apiset.h"
+#include "afb-api-ws.h"
+#include "afb-session.h"
+
+#include "afs-supervisor.h"
+#include "afs-config.h"
+
+#include "verbose.h"
+#include "jobs.h"
+#include "process-name.h"
+
+/* the main config */
+struct afs_config *main_config;
+
+/* the main apiset */
+struct afb_apiset *main_apiset;
+
+/*************************************************************************************/
+
+static int init_http_server(struct afb_hsrv *hsrv)
+{
+ if (!afb_hsrv_add_handler
+ (hsrv, main_config->rootapi, afb_hswitch_websocket_switch, main_apiset, 20))
+ return 0;
+
+ if (!afb_hsrv_add_handler
+ (hsrv, main_config->rootapi, afb_hswitch_apis, main_apiset, 10))
+ return 0;
+
+ if (main_config->roothttp != NULL) {
+ if (!afb_hsrv_add_alias
+ (hsrv, "", afb_common_rootdir_get_fd(), main_config->roothttp,
+ -10, 1))
+ return 0;
+ }
+
+ if (!afb_hsrv_add_handler
+ (hsrv, main_config->rootbase, afb_hswitch_one_page_api_redirect, NULL,
+ -20))
+ return 0;
+
+ return 1;
+}
+
+static struct afb_hsrv *start_http_server()
+{
+ int rc;
+ struct afb_hsrv *hsrv;
+
+ if (afb_hreq_init_download_path(main_config->uploaddir)) {
+ ERROR("unable to set the upload directory %s", main_config->uploaddir);
+ return NULL;
+ }
+
+ hsrv = afb_hsrv_create();
+ if (hsrv == NULL) {
+ ERROR("memory allocation failure");
+ return NULL;
+ }
+
+ if (!afb_hsrv_set_cache_timeout(hsrv, main_config->cacheTimeout)
+ || !init_http_server(hsrv)) {
+ ERROR("initialisation of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ NOTICE("Waiting port=%d rootdir=%s", main_config->httpdPort, main_config->rootdir);
+ NOTICE("Browser URL= http://localhost:%d", main_config->httpdPort);
+
+ rc = afb_hsrv_start(hsrv, (uint16_t) main_config->httpdPort, 15);
+ if (!rc) {
+ ERROR("starting of httpd failed");
+ afb_hsrv_put(hsrv);
+ return NULL;
+ }
+
+ return hsrv;
+}
+
+static void start(int signum, void *arg)
+{
+ struct afb_hsrv *hsrv;
+ int rc;
+
+ /* check illness */
+ if (signum) {
+ ERROR("start aborted: received signal %s", strsignal(signum));
+ exit(1);
+ }
+
+ /* set the directories */
+ mkdir(main_config->workdir, S_IRWXU | S_IRGRP | S_IXGRP);
+ if (chdir(main_config->workdir) < 0) {
+ ERROR("Can't enter working dir %s", main_config->workdir);
+ goto error;
+ }
+ if (afb_common_rootdir_set(main_config->rootdir) < 0) {
+ ERROR("failed to set common root directory");
+ goto error;
+ }
+
+ /* configure the daemon */
+ if (afb_session_init(main_config->nbSessionMax, main_config->cntxTimeout, main_config->token)) {
+ ERROR("initialisation of session manager failed");
+ goto error;
+ }
+
+ main_apiset = afb_apiset_create("main", main_config->apiTimeout);
+ if (!main_apiset) {
+ ERROR("can't create main apiset");
+ goto error;
+ }
+
+ /* init the main apiset */
+ rc = afs_supervisor_add(main_apiset, main_apiset);
+ if (rc < 0) {
+ ERROR("Can't create supervision's apiset: %m");
+ goto error;
+ }
+
+ /* export the service if required */
+ if (main_config->ws_server) {
+ rc = afb_api_ws_add_server(main_config->ws_server, main_apiset, main_apiset);
+ if (rc < 0) {
+ ERROR("Can't export (ws-server) api %s: %m", main_config->ws_server);
+ goto error;
+ }
+ }
+
+ /* start the services */
+ if (afb_apiset_start_all_services(main_apiset, 1) < 0)
+ goto error;
+
+ /* start the HTTP server */
+ if (main_config->httpdPort <= 0) {
+ ERROR("no port is defined");
+ goto error;
+ }
+
+ if (!afb_hreq_init_cookie(main_config->httpdPort, main_config->rootapi, main_config->cntxTimeout)) {
+ ERROR("initialisation of HTTP cookies failed");
+ goto error;
+ }
+
+ hsrv = start_http_server();
+ if (hsrv == NULL)
+ goto error;
+
+ /* ready */
+ sd_notify(1, "READY=1");
+ afs_supervisor_discover();
+ return;
+error:
+ exit(1);
+}
+
+/**
+ * initialize the supervision
+ */
+int main(int ac, char **av)
+{
+ /* scan arguments */
+ main_config = afs_config_parse_arguments(ac, av);
+ if (main_config->name) {
+ verbose_set_name(main_config->name, 0);
+ process_name_set_name(main_config->name);
+ process_name_replace_cmdline(av, main_config->name);
+ }
+ /* enter job processing */
+ jobs_start(3, 0, 10, start, av[1]);
+ WARNING("hoops returned from jobs_enter! [report bug]");
+ return 1;
+}
+