X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=fcedd2a6e538fdc5681cb3b71b7ba6c4bc9c0046;hb=19fb390ec60890d55bafe7a4c887b1453509f7ef;hp=9977c7d47349815600e971ee2e8b7f756251bf0a;hpb=67966a2f96613d833f493ef3773c442d35f8ed31;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index 9977c7d4..fcedd2a6 100644 --- a/src/main.c +++ b/src/main.c @@ -32,14 +32,17 @@ #include #include +#include + #include "afb-config.h" #include "afb-hswitch.h" +#include "afb-apis.h" #include "afb-api-so.h" #include "afb-hsrv.h" #include "afb-hreq.h" #include "session.h" #include "verbose.h" -#include "utils-upoll.h" +#include "afb-common.h" #include "afb-plugin.h" @@ -74,8 +77,6 @@ #define SET_MODE 18 #define SET_READYFD 19 -static struct afb_hsrv *start(struct afb_config * config); - // Command line structure hold cli --command + help text typedef struct { int val; // command number within application @@ -486,6 +487,72 @@ static void daemonize(struct afb_config *config) fflush (stderr); } +/*--------------------------------------------------------- + | http server + | Handles the HTTP server + +--------------------------------------------------------- */ +static int init_http_server(struct afb_hsrv *hsrv, struct afb_config * config) +{ + int idx; + + if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_websocket_switch, NULL, 20)) + return 0; + + if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_apis, NULL, 10)) + return 0; + + for (idx = 0; idx < config->aliascount; idx++) + if (!afb_hsrv_add_alias (hsrv, config->aliasdir[idx].url, config->aliasdir[idx].path, 0)) + return 0; + + if (!afb_hsrv_add_alias(hsrv, "", config->rootdir, -10)) + return 0; + + if (!afb_hsrv_add_handler(hsrv, config->rootbase, afb_hswitch_one_page_api_redirect, NULL, -20)) + return 0; + + return 1; +} + +static struct afb_hsrv *start_http_server(struct afb_config * config) +{ + int rc; + struct afb_hsrv *hsrv; + + if (afb_hreq_init_download_path("/tmp")) { /* TODO: sessiondir? */ + fprintf(stderr, "unable to set the tmp directory\n"); + return NULL; + } + + hsrv = afb_hsrv_create(); + if (hsrv == NULL) { + fprintf(stderr, "memory allocation failure\n"); + return NULL; + } + + if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout) + || !init_http_server(hsrv, config)) { + fprintf (stderr, "Error: initialisation of httpd failed"); + afb_hsrv_put(hsrv); + return NULL; + } + + if (verbosity) { + fprintf (stderr, "AFB:notice Waiting port=%d rootdir=%s\n", config->httpdPort, config->rootdir); + fprintf (stderr, "AFB:notice Browser URL= http:/*localhost:%d\n", config->httpdPort); + } + + rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15); + if (!rc) { + fprintf (stderr, "Error: starting of httpd failed"); + afb_hsrv_put(hsrv); + return NULL; + } + + return hsrv; +} + + /*--------------------------------------------------------- | main | Parse option and launch action @@ -494,6 +561,7 @@ static void daemonize(struct afb_config *config) int main(int argc, char *argv[]) { struct afb_hsrv *hsrv; struct afb_config *config; + struct sd_event *eventloop; // open syslog if ever needed openlog("afb-daemon", 0, LOG_DAEMON); @@ -513,7 +581,7 @@ int main(int argc, char *argv[]) { if (config->ldpaths) afb_api_so_add_pathset(config->ldpaths); - ctxStoreInit(CTX_NBCLIENTS, config->cntxTimeout, config->token); + ctxStoreInit(CTX_NBCLIENTS, config->cntxTimeout, config->token, afb_apis_count()); if (!afb_hreq_init_cookie(config->httpdPort, config->rootapi, DEFLT_CNTX_TIMEOUT)) { fprintf (stderr, "ERR: initialisation of cookies failed\n"); exit (1); @@ -547,7 +615,7 @@ int main(int argc, char *argv[]) { } - hsrv = start (config); + hsrv = start_http_server(config); if (hsrv == NULL) exit(1); @@ -558,8 +626,9 @@ int main(int argc, char *argv[]) { } // infinite loop + eventloop = afb_common_get_event_loop(); for(;;) - upoll_wait(30000); + sd_event_run(eventloop, 30000000); if (verbosity) fprintf (stderr, "hoops returned from infinite loop [report bug]\n"); @@ -567,60 +636,3 @@ int main(int argc, char *argv[]) { return 0; } -static int init(struct afb_hsrv *hsrv, struct afb_config * config) -{ - int idx; - - if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_websocket_switch, NULL, 20)) - return 0; - - if (!afb_hsrv_add_handler(hsrv, config->rootapi, afb_hswitch_apis, NULL, 10)) - return 0; - - for (idx = 0; idx < config->aliascount; idx++) - if (!afb_hsrv_add_alias (hsrv, config->aliasdir[idx].url, config->aliasdir[idx].path, 0)) - return 0; - - if (!afb_hsrv_add_alias(hsrv, "", config->rootdir, -10)) - return 0; - - if (!afb_hsrv_add_handler(hsrv, config->rootbase, afb_hswitch_one_page_api_redirect, NULL, -20)) - return 0; - - return 1; -} - -static struct afb_hsrv *start(struct afb_config * config) -{ - int rc; - struct afb_hsrv *hsrv; - - hsrv = afb_hsrv_create(); - if (hsrv == NULL) { - fprintf(stderr, "memory allocation failure\n"); - return NULL; - } - - if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout) - || !init(hsrv, config)) { - fprintf (stderr, "Error: initialisation of httpd failed"); - afb_hsrv_put(hsrv); - return NULL; - } - - if (verbosity) { - fprintf (stderr, "AFB:notice Waiting port=%d rootdir=%s\n", config->httpdPort, config->rootdir); - fprintf (stderr, "AFB:notice Browser URL= http:/*localhost:%d\n", config->httpdPort); - } - - rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15); - if (!rc) { - fprintf (stderr, "Error: starting of httpd failed"); - afb_hsrv_put(hsrv); - return NULL; - } - - return hsrv; -} - -