From 8b9a73bac0690908db9acc01c28a3ffd148ec4f3 Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Fri, 23 Feb 2018 15:12:55 +0100 Subject: [PATCH] MINOR: spoa-server: Load files Declare files to be executed at the begining and execute it. The binding between the engine and the file is done throught the extension. --- contrib/spoa_server/spoa.c | 42 ++++++++++++++++++++++++++++++++++++-- contrib/spoa_server/spoa.h | 1 + 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/contrib/spoa_server/spoa.c b/contrib/spoa_server/spoa.c index 73b7133857..e484315f65 100644 --- a/contrib/spoa_server/spoa.c +++ b/contrib/spoa_server/spoa.c @@ -99,6 +99,23 @@ bool debug = false; pthread_key_t worker_id; static struct ps *ps_list = NULL; static struct ps_message *ps_messages = NULL; +static int nfiles = 0; +static char **files = NULL; + +static inline void add_file(const char *file) +{ + nfiles++; + files = realloc(files, sizeof(*files) * nfiles); + if (files == NULL) { + fprintf(stderr, "Out of memory error\n"); + exit(EXIT_FAILURE); + } + files[nfiles - 1] = strdup(file); + if (files[nfiles - 1] == NULL) { + fprintf(stderr, "Out of memory error\n"); + exit(EXIT_FAILURE); + } +} void ps_register(struct ps *ps) { @@ -963,6 +980,8 @@ spoa_worker(void *data) int *info = (int *)data; int csock, lsock = info[0]; struct ps *ps; + int i; + int len; signal(SIGPIPE, SIG_IGN); pthread_setspecific(worker_id, &info[1]); @@ -971,6 +990,20 @@ spoa_worker(void *data) for (ps = ps_list; ps != NULL; ps = ps->next) ps->init_worker(&w); + /* Load files */ + for (i = 0; i < nfiles; i++) { + len = strlen(files[i]); + for (ps = ps_list; ps != NULL; ps = ps->next) + if (strcmp(files[i] + len - strlen(ps->ext), ps->ext) == 0) + break; + if (ps == NULL) { + LOG("Can't load file \"%s\"\n", files[i]); + goto out; + } + if (!ps->load_file(&w, files[i])) + goto out; + } + while (1) { socklen_t sz = sizeof(client); @@ -1040,11 +1073,13 @@ int process_create(pid_t *pid, void *(*ps)(void *), void *data) static void usage(char *prog) { - fprintf(stderr, "Usage: %s [-h] [-d] [-p ] [-n ]\n", prog); + fprintf(stderr, "Usage: %s [-h] [-d] [-p ] [-n ] -f \n", prog); fprintf(stderr, " -h Print this message\n"); fprintf(stderr, " -d Enable the debug mode\n"); fprintf(stderr, " -p Specify the port to listen on (default: 12345)\n"); fprintf(stderr, " -n Specify the number of workers (default: 5)\n"); + fprintf(stderr, " -f Specify the file whoch contains the processing code.\n"); + fprintf(stderr, " This argument can specified more than once.\n"); } int @@ -1060,7 +1095,7 @@ main(int argc, char **argv) nbworkers = NUM_WORKERS; port = DEFAULT_PORT; - while ((opt = getopt(argc, argv, "hdn:p:")) != -1) { + while ((opt = getopt(argc, argv, "hdn:p:f:")) != -1) { switch (opt) { case 'h': usage(argv[0]); @@ -1074,6 +1109,9 @@ main(int argc, char **argv) case 'p': port = atoi(optarg); break; + case 'f': + add_file(optarg); + break; default: usage(argv[0]); return EXIT_FAILURE; diff --git a/contrib/spoa_server/spoa.h b/contrib/spoa_server/spoa.h index 22d6788513..ca8518122a 100644 --- a/contrib/spoa_server/spoa.h +++ b/contrib/spoa_server/spoa.h @@ -90,6 +90,7 @@ struct ps { char *ext; int (*init_worker)(struct worker *w); int (*exec_message)(struct worker *w, void *ref, int nargs, struct spoe_kv *args); + int (*load_file)(struct worker *w, const char *file); }; struct ps_message {