1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/daemon/main.c

80 lines
2.1 KiB
C
Raw Normal View History

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