lifo-dijkstraserv/src/main.c

131 lines
2.7 KiB
C
Raw Normal View History

2020-05-01 16:33:48 +02:00
#include <stdio.h>
2020-05-02 16:27:44 +02:00
#include <stdlib.h>
#include <getopt.h>
2020-05-02 17:31:26 +02:00
#include <glib.h>
2020-05-02 16:27:44 +02:00
2020-05-02 17:02:26 +02:00
#include "dijkstragraph.h"
2020-05-02 17:31:26 +02:00
#include "dijkstrasearch.h"
2020-05-03 19:59:05 +02:00
#include "dijkstraserv.h"
2020-05-02 16:27:44 +02:00
#include "geojson.h"
#include "logging.h"
2020-05-01 20:48:36 +02:00
struct dijkstra_node *start;
struct dijkstra_node *dest;
2020-05-02 16:51:12 +02:00
void add_test_map(struct dijkstra_solver *solver)
2020-05-01 16:33:48 +02:00
{
2020-05-02 16:51:12 +02:00
dijkstra_path_new_to_list(solver,
5,0,
5,10,
2020-05-01 20:48:36 +02:00
"Vertical",
1);
2020-05-02 16:51:12 +02:00
dijkstra_path_new_to_list(solver,
2020-05-01 20:48:36 +02:00
0,5,
10,5,
"Horizontal",
1);
2020-05-02 16:27:44 +02:00
}
2020-05-01 20:48:36 +02:00
2020-05-02 16:27:44 +02:00
void help(const char *exec)
{
fprintf(stderr,
"Usage: %s [OPTION] [FILE]...\n"
"Start the lifo-dijkstraserv routing server.\n\n"
" -s\tset output to silent\n"
" -v\tincrease verbosity. Maybe used multiple times.\n"
" -p\tport number (default 6802)\n",
exec);
}
int main(int argc, char *argv[])
{
int opt;
int i;
2020-05-02 16:51:12 +02:00
struct dijkstra_solver *solver;
2020-05-02 16:27:44 +02:00
GList *l = NULL;
struct dijkstra_node *node;
struct dijkstra_path *path;
2020-05-03 19:59:05 +02:00
int port = 6802;
struct dijkstra_serv *serv;
2020-05-02 17:31:26 +02:00
/* Init stuff */
set_log_level(LL_DEBUG);
2020-05-02 16:27:44 +02:00
2020-05-02 17:31:26 +02:00
/* Process arguments */
2020-05-03 19:59:05 +02:00
while((opt = getopt(argc, argv, "vsp")) != -1) {
2020-05-02 16:27:44 +02:00
switch(opt)
2020-05-02 16:51:12 +02:00
{
case 'v':
set_log_level(get_log_level() + 1);
break;
case 's':
set_log_level(0);
break;
2020-05-03 19:59:05 +02:00
case 'p':
port = atoi(argv[i]);
break;
2020-05-02 16:51:12 +02:00
case ':':
case '?':
help(argv[0]);
return 1;
default:
break;
}
2020-05-02 16:27:44 +02:00
}
if (optind == argc) {
help(argv[0]);
return 1;
}
2020-05-02 17:31:26 +02:00
/* Generate solver */
2020-05-02 16:51:12 +02:00
solver = dijkstra_solver_new();
if (!solver) {
report(LL_CRITICAL, "Memory allocation failure.");
return 2;
}
2020-05-02 17:31:26 +02:00
/* Load map data -> Generate graph */
2020-05-02 16:27:44 +02:00
for (i = optind; i < argc; ++i) {
2020-05-02 16:51:12 +02:00
add_geojson_to_dijkstra(solver, argv[i]);
2020-05-02 16:27:44 +02:00
}
if (should_report(LL_NOISY)) {
2020-05-02 16:51:12 +02:00
for (l = solver->nodes; l != NULL; l = l->next) {
2020-05-02 16:27:44 +02:00
node = (struct dijkstra_node*) l->data;
fprintf(stderr, "Node %5d @%p: %f,\t%f [%d paths]\n", node->uid, node, node->position.x, node->position.y, g_list_length(node->paths));
2020-05-02 16:27:44 +02:00
}
printf("\n");
2020-05-02 16:51:12 +02:00
for (l = solver->paths; l != NULL; l = l->next) {
2020-05-02 16:27:44 +02:00
path = (struct dijkstra_path*) l->data;
fprintf(stderr, "Path %p: %5d (%f,%f) -> %5d (%f,%f) cost %d \t\"%s\"\n", path,
2020-05-02 16:59:13 +02:00
path->source->uid, path->source->position.x, path->source->position.y,
path->destination->uid, path->destination->position.x, path->destination->position.y,
path->weight,
2020-05-02 16:27:44 +02:00
path->name);
}
printf("\n");
2020-05-01 20:48:36 +02:00
}
2020-05-02 16:27:44 +02:00
report(LL_INFO, "Created graph with %d nodes and %d paths.",
2020-05-02 16:51:12 +02:00
g_list_length(solver->nodes),
g_list_length(solver->paths));
2020-05-01 20:48:36 +02:00
2020-05-03 19:59:05 +02:00
/* Start server */
serv = dijkstra_serv_new(solver, port);
if (!serv) {
2020-05-02 17:31:26 +02:00
dijkstra_solver_free(solver);
2020-05-03 19:59:05 +02:00
return -4;
}
2020-05-03 19:59:05 +02:00
dijkstra_serv_run(serv);
2020-05-02 17:31:26 +02:00
2020-05-02 16:51:12 +02:00
dijkstra_solver_free(solver);
2020-05-01 20:48:36 +02:00
2020-05-01 16:33:48 +02:00
return 0;
}