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

145 lines
3.3 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 {
6 years ago
struct llarp_alloc mem;
7 years ago
struct llarp_router *router;
6 years ago
struct llarp_threadpool *worker;
6 years ago
struct llarp_logic *logic;
7 years ago
struct llarp_config *config;
6 years ago
struct llarp_nodedb *nodedb;
7 years ago
struct llarp_ev_loop *mainloop;
6 years ago
char nodedb_dir[256];
6 years ago
int exitcode;
};
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;
6 years ago
7 years ago
if (!strcmp(section, "router")) {
if (!strcmp(key, "threads")) {
int workers = atoi(val);
6 years ago
if (workers > 0 && m->worker == NULL) {
m->worker = llarp_init_threadpool(workers);
}
}
}
6 years ago
if (!strcmp(section, "netdb")) {
6 years ago
if (!strcmp(key, "dir")) {
strncpy(m->nodedb_dir, val, sizeof(m->nodedb_dir));
}
}
}
6 years ago
static void progress() {
printf(".");
7 years ago
fflush(stdout);
6 years ago
}
int shutdown_llarp(struct llarp_main *m) {
printf("Shutting down ");
progress();
6 years ago
if(m->router)
llarp_stop_router(m->router);
6 years ago
progress();
6 years ago
if(m->mainloop)
llarp_ev_loop_stop(m->mainloop);
6 years ago
progress();
6 years ago
if(m->worker)
llarp_threadpool_stop(m->worker);
6 years ago
6 years ago
progress();
6 years ago
if(m->worker)
llarp_threadpool_join(m->worker);
6 years ago
6 years ago
progress();
6 years ago
if (m->logic) llarp_logic_stop(m->logic);
6 years ago
6 years ago
progress();
llarp_free_router(&m->router);
6 years ago
6 years ago
progress();
llarp_free_config(&m->config);
6 years ago
6 years ago
progress();
llarp_ev_loop_free(&m->mainloop);
6 years ago
6 years ago
progress();
llarp_free_threadpool(&m->worker);
6 years ago
6 years ago
progress();
6 years ago
6 years ago
llarp_free_logic(&m->logic);
6 years ago
progress();
6 years ago
6 years ago
printf("\n");
7 years ago
fflush(stdout);
6 years ago
return m->exitcode;
}
7 years ago
struct llarp_main llarp = {
6 years ago
{0,0,0},
0,
0,
0,
0,
0,
0,
6 years ago
{0},
1
};
6 years ago
7 years ago
int main(int argc, char *argv[]) {
const char *conffname = "daemon.ini";
7 years ago
if (argc > 1) conffname = argv[1];
6 years ago
llarp_mem_jemalloc(&llarp.mem);
struct llarp_alloc * mem = &llarp.mem;
llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop);
6 years ago
printf("%s loading config file %s\n", LLARP_VERSION, conffname);
7 years ago
if (!llarp_load_config(llarp.config, conffname)) {
struct llarp_config_iterator iter;
iter.user = &llarp;
iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter);
7 years ago
6 years ago
llarp.nodedb = llarp_nodedb_new(mem);
6 years ago
if (llarp.nodedb_dir[0]) {
llarp.nodedb_dir[sizeof(llarp.nodedb_dir) - 1] = 0;
char *dir = llarp.nodedb_dir;
if (llarp_nodedb_ensure_dir(dir)) {
6 years ago
// ensure worker thread pool
6 years ago
if (!llarp.worker) llarp.worker = llarp_init_threadpool(2);
6 years ago
6 years ago
llarp.router = llarp_init_router(mem, llarp.worker, llarp.mainloop);
6 years ago
if (llarp_configure_router(llarp.router, llarp.config)) {
6 years ago
6 years ago
llarp.logic = llarp_init_logic(mem);
6 years ago
printf("starting router\n");
6 years ago
6 years ago
llarp_run_router(llarp.router, llarp.logic);
6 years ago
6 years ago
printf("running mainloop\n");
6 years ago
llarp.exitcode = 0;
6 years ago
llarp_ev_loop_run(llarp.mainloop);
} else
printf("Failed to configure router\n");
} else
printf("failed to initialize nodedb at %s\n", dir);
6 years ago
} else
printf("no nodedb defined\n");
return shutdown_llarp(&llarp);
7 years ago
} else
7 years ago
printf("Failed to load config %s\n", conffname);
7 years ago
6 years ago
return 1;
7 years ago
}