lokinet/daemon/main.c

95 lines
2.5 KiB
C
Raw Normal View History

2018-01-25 16:24:33 +00:00
#include <llarp.h>
2018-01-08 13:49:05 +00:00
#include <stdio.h>
2018-01-27 01:18:10 +00:00
#include <string.h>
2018-01-08 13:49:05 +00:00
2018-01-29 14:27:24 +00:00
struct llarp_main {
struct llarp_router *router;
2018-04-30 16:14:20 +00:00
struct llarp_threadpool *worker;
struct llarp_threadpool *logic;
2018-01-29 14:27:24 +00:00
struct llarp_config *config;
struct llarp_ev_loop *mainloop;
2018-01-27 01:18:10 +00:00
};
2018-01-29 14:27:24 +00:00
void iter_main_config(struct llarp_config_iterator *itr, const char *section,
const char *key, const char *val) {
struct llarp_main *m = (struct llarp_main *)itr->user;
2018-04-05 14:23:14 +00:00
if (!strcmp(section, "router")) {
if (!strcmp(key, "threads")) {
2018-01-27 01:18:10 +00:00
int workers = atoi(val);
2018-04-30 16:14:20 +00:00
if (workers > 0 && m->worker == NULL) {
2018-04-05 14:23:14 +00:00
printf("%s: %d worker threads\n", section, workers);
2018-04-30 16:14:20 +00:00
m->worker = llarp_init_threadpool(workers);
2018-01-27 01:18:10 +00:00
}
}
}
}
2018-04-30 16:14:29 +00:00
static void progress() {
2018-01-27 01:18:10 +00:00
printf(".");
2018-04-05 14:23:14 +00:00
fflush(stdout);
2018-04-30 16:14:20 +00:00
}
int shutdown_llarp(struct llarp_main *m) {
printf("Shutting down ");
progress();
llarp_stop_router(m->router);
progress();
2018-01-27 01:18:10 +00:00
llarp_ev_loop_stop(m->mainloop);
2018-04-30 16:14:20 +00:00
progress();
llarp_threadpool_stop(m->worker);
progress();
llarp_threadpool_join(m->worker);
progress();
2018-04-30 16:14:29 +00:00
if (m->logic) llarp_threadpool_wait(m->logic);
2018-04-30 16:14:20 +00:00
progress();
2018-04-30 16:14:29 +00:00
if (m->logic) llarp_threadpool_join(m->logic);
2018-04-30 16:14:20 +00:00
progress();
2018-01-27 01:18:10 +00:00
llarp_free_router(&m->router);
2018-04-30 16:14:20 +00:00
progress();
2018-01-27 01:18:10 +00:00
llarp_free_config(&m->config);
2018-04-30 16:14:20 +00:00
progress();
2018-01-27 01:18:10 +00:00
llarp_ev_loop_free(&m->mainloop);
2018-04-30 16:14:20 +00:00
progress();
llarp_free_threadpool(&m->worker);
progress();
2018-04-30 16:14:29 +00:00
if (m->logic) llarp_free_threadpool(&m->logic);
2018-04-30 16:14:20 +00:00
progress();
printf("\n");
2018-04-05 14:23:14 +00:00
fflush(stdout);
2018-02-01 17:06:49 +00:00
return 0;
2018-01-27 01:18:10 +00:00
}
2017-09-28 17:02:05 +00:00
2018-04-30 16:14:20 +00:00
struct llarp_main llarp;
2018-01-29 14:27:24 +00:00
int main(int argc, char *argv[]) {
2018-04-30 16:14:20 +00:00
memset(&llarp, 0, sizeof(struct llarp_main));
2018-01-29 14:27:24 +00:00
const char *conffname = "daemon.ini";
2018-02-01 13:21:00 +00:00
if (argc > 1) conffname = argv[1];
2018-04-04 15:19:11 +00:00
llarp_mem_stdlib();
2018-01-27 01:18:10 +00:00
llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop);
2018-01-25 16:24:33 +00:00
printf("%s loaded\n", LLARP_VERSION);
2018-01-29 14:27:24 +00:00
if (!llarp_load_config(llarp.config, conffname)) {
2018-01-25 16:11:45 +00:00
printf("Loaded config %s\n", conffname);
2018-01-27 01:18:10 +00:00
struct llarp_config_iterator iter;
iter.user = &llarp;
iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter);
2018-04-05 14:43:16 +00:00
2018-04-30 16:14:20 +00:00
if (!llarp.worker) llarp.worker = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.worker);
2018-04-05 14:43:16 +00:00
2018-04-05 14:23:14 +00:00
if (llarp_configure_router(llarp.router, llarp.config)) {
2018-04-30 16:14:20 +00:00
llarp.logic = llarp_init_threadpool(1);
printf("starting router\n");
llarp_run_router(llarp.router, llarp.logic);
printf("running mainloop\n");
2018-01-27 01:18:10 +00:00
llarp_ev_loop_run(llarp.mainloop);
2018-01-29 14:27:24 +00:00
} else
2018-01-25 16:11:45 +00:00
printf("Failed to configure router\n");
2018-01-29 14:27:24 +00:00
} else
2018-01-25 16:11:45 +00:00
printf("Failed to load config %s\n", conffname);
2018-01-29 14:27:24 +00:00
2018-01-27 01:18:10 +00:00
return shutdown_llarp(&llarp);
2017-09-28 17:02:05 +00:00
}