You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/daemon/main.c

73 lines
2.0 KiB
C

7 years ago
#include <llarp.h>
7 years ago
#include <stdio.h>
#include <string.h>
7 years ago
7 years ago
struct llarp_main {
struct llarp_router *router;
struct llarp_threadpool *tp;
struct llarp_config *config;
struct llarp_ev_loop *mainloop;
};
7 years ago
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;
if (!strcmp(section, "threadpool")) {
if (!strcmp(key, "workers")) {
int workers = atoi(val);
7 years ago
if (!m->tp && workers > 0) {
m->tp = llarp_init_threadpool(workers);
}
}
}
}
7 years ago
int shutdown_llarp(struct llarp_main *m) {
printf("Shutting down .");
llarp_stop_router(m->router);
printf(".");
llarp_ev_loop_stop(m->mainloop);
printf(".");
llarp_threadpool_join(m->tp);
printf(".");
llarp_free_router(&m->router);
printf(".");
llarp_free_config(&m->config);
printf(".");
llarp_ev_loop_free(&m->mainloop);
printf(".");
llarp_free_threadpool(&m->tp);
printf(".\n");
7 years ago
return 0;
}
7 years ago
7 years ago
int main(int argc, char *argv[]) {
struct llarp_main llarp = {NULL, NULL, NULL, NULL};
const char *conffname = "daemon.ini";
7 years ago
if (argc > 1) conffname = argv[1];
7 years ago
llarp_mem_stdlib();
llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop);
7 years ago
printf("%s loaded\n", LLARP_VERSION);
7 years ago
if (!llarp_load_config(llarp.config, conffname)) {
7 years ago
printf("Loaded config %s\n", conffname);
struct llarp_config_iterator iter;
iter.user = &llarp;
iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter);
7 years ago
7 years ago
if (!llarp.tp) llarp.tp = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.tp);
7 years ago
7 years ago
if (!llarp_configure_router(llarp.router, llarp.config)) {
7 years ago
printf("Running\n");
llarp_run_router(llarp.router, llarp.mainloop);
llarp_ev_loop_run(llarp.mainloop);
7 years ago
} else
7 years ago
printf("Failed to configure router\n");
7 years ago
} else
7 years ago
printf("Failed to load config %s\n", conffname);
7 years ago
return shutdown_llarp(&llarp);
7 years ago
}