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 {
|
2018-05-16 18:13:18 +00:00
|
|
|
struct llarp_alloc mem;
|
2018-01-29 14:27:24 +00:00
|
|
|
struct llarp_router *router;
|
2018-04-30 16:14:20 +00:00
|
|
|
struct llarp_threadpool *worker;
|
2018-05-18 17:50:21 +00:00
|
|
|
struct llarp_threadpool *netio;
|
2018-04-30 18:18:18 +00:00
|
|
|
struct llarp_logic *logic;
|
2018-01-29 14:27:24 +00:00
|
|
|
struct llarp_config *config;
|
2018-05-10 23:32:46 +00:00
|
|
|
struct llarp_nodedb *nodedb;
|
2018-01-29 14:27:24 +00:00
|
|
|
struct llarp_ev_loop *mainloop;
|
2018-05-10 23:32:46 +00:00
|
|
|
char nodedb_dir[256];
|
2018-05-16 15:49:16 +00:00
|
|
|
int exitcode;
|
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-05-16 15:49:16 +00:00
|
|
|
|
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) {
|
|
|
|
m->worker = llarp_init_threadpool(workers);
|
2018-01-27 01:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 15:49:16 +00:00
|
|
|
if (!strcmp(section, "netdb")) {
|
2018-05-10 23:32:46 +00:00
|
|
|
if (!strcmp(key, "dir")) {
|
|
|
|
strncpy(m->nodedb_dir, val, sizeof(m->nodedb_dir));
|
|
|
|
}
|
|
|
|
}
|
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();
|
2018-05-16 14:09:02 +00:00
|
|
|
if(m->router)
|
|
|
|
llarp_stop_router(m->router);
|
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-05-16 14:09:02 +00:00
|
|
|
if(m->mainloop)
|
|
|
|
llarp_ev_loop_stop(m->mainloop);
|
2018-05-18 17:50:21 +00:00
|
|
|
|
|
|
|
progress();
|
|
|
|
if(m->netio)
|
|
|
|
{
|
|
|
|
llarp_threadpool_stop(m->netio);
|
|
|
|
llarp_threadpool_join(m->netio);
|
|
|
|
}
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-05-16 15:30:05 +00:00
|
|
|
if(m->worker)
|
|
|
|
llarp_threadpool_stop(m->worker);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-05-16 15:30:05 +00:00
|
|
|
|
|
|
|
if(m->worker)
|
|
|
|
llarp_threadpool_join(m->worker);
|
2018-05-18 17:50:21 +00:00
|
|
|
|
2018-05-16 14:09:02 +00:00
|
|
|
progress();
|
2018-04-30 18:18:18 +00:00
|
|
|
if (m->logic) llarp_logic_stop(m->logic);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-01-27 01:18:10 +00:00
|
|
|
llarp_free_router(&m->router);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-01-27 01:18:10 +00:00
|
|
|
llarp_free_config(&m->config);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-01-27 01:18:10 +00:00
|
|
|
llarp_ev_loop_free(&m->mainloop);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
|
|
|
llarp_free_threadpool(&m->worker);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 18:18:18 +00:00
|
|
|
llarp_free_logic(&m->logic);
|
2018-04-30 16:14:20 +00:00
|
|
|
progress();
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-04-30 16:14:20 +00:00
|
|
|
printf("\n");
|
2018-04-05 14:23:14 +00:00
|
|
|
fflush(stdout);
|
2018-05-16 15:49:16 +00:00
|
|
|
return m->exitcode;
|
2018-01-27 01:18:10 +00:00
|
|
|
}
|
2017-09-28 17:02:05 +00:00
|
|
|
|
2018-05-16 13:56:51 +00:00
|
|
|
struct llarp_main llarp = {
|
2018-05-16 18:13:18 +00:00
|
|
|
{0,0,0},
|
2018-05-16 13:56:51 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2018-05-18 17:50:21 +00:00
|
|
|
0,
|
2018-05-16 15:49:16 +00:00
|
|
|
{0},
|
|
|
|
1
|
2018-05-16 13:56:51 +00:00
|
|
|
};
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-18 17:50:21 +00:00
|
|
|
void run_netio(void * user)
|
|
|
|
{
|
|
|
|
struct llarp_ev_loop * loop = user;
|
|
|
|
llarp_ev_loop_run(loop);
|
|
|
|
}
|
|
|
|
|
2018-01-29 14:27:24 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
const char *conffname = "daemon.ini";
|
2018-02-01 13:21:00 +00:00
|
|
|
if (argc > 1) conffname = argv[1];
|
2018-05-16 18:13:18 +00:00
|
|
|
llarp_mem_jemalloc(&llarp.mem);
|
|
|
|
struct llarp_alloc * mem = &llarp.mem;
|
2018-01-27 01:18:10 +00:00
|
|
|
llarp_new_config(&llarp.config);
|
|
|
|
llarp_ev_loop_alloc(&llarp.mainloop);
|
2018-05-16 15:49:16 +00:00
|
|
|
printf("%s loading config file %s\n", LLARP_VERSION, conffname);
|
2018-01-29 14:27:24 +00:00
|
|
|
if (!llarp_load_config(llarp.config, 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-05-16 18:13:18 +00:00
|
|
|
llarp.nodedb = llarp_nodedb_new(mem);
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
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)) {
|
2018-05-16 14:09:02 +00:00
|
|
|
// ensure worker thread pool
|
2018-05-10 23:32:46 +00:00
|
|
|
if (!llarp.worker) llarp.worker = llarp_init_threadpool(2);
|
2018-05-18 17:50:21 +00:00
|
|
|
// ensire net io thread
|
|
|
|
llarp.netio = llarp_init_threadpool(1);
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-05-16 18:13:18 +00:00
|
|
|
llarp.router = llarp_init_router(mem, llarp.worker, llarp.mainloop);
|
2018-05-10 23:32:46 +00:00
|
|
|
|
|
|
|
if (llarp_configure_router(llarp.router, llarp.config)) {
|
2018-05-16 14:09:02 +00:00
|
|
|
|
2018-05-18 13:17:58 +00:00
|
|
|
llarp.logic = llarp_init_logic(mem);
|
2018-05-10 23:32:46 +00:00
|
|
|
printf("starting router\n");
|
|
|
|
llarp_run_router(llarp.router, llarp.logic);
|
2018-05-18 17:50:21 +00:00
|
|
|
// run io loop
|
|
|
|
struct llarp_thread_job netjob = {
|
|
|
|
.user = llarp.mainloop,
|
|
|
|
.work = &run_netio
|
|
|
|
};
|
|
|
|
llarp_threadpool_queue_job(llarp.netio, netjob);
|
|
|
|
printf("running\n");
|
2018-05-16 15:49:16 +00:00
|
|
|
llarp.exitcode = 0;
|
2018-05-18 17:50:21 +00:00
|
|
|
llarp_logic_mainloop(llarp.logic);
|
2018-05-10 23:32:46 +00:00
|
|
|
} else
|
|
|
|
printf("Failed to configure router\n");
|
|
|
|
} else
|
|
|
|
printf("failed to initialize nodedb at %s\n", dir);
|
2018-05-16 15:49:16 +00:00
|
|
|
} else
|
|
|
|
printf("no nodedb defined\n");
|
|
|
|
|
|
|
|
return shutdown_llarp(&llarp);
|
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-05-16 15:49:16 +00:00
|
|
|
return 1;
|
2017-09-28 17:02:05 +00:00
|
|
|
}
|