X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fgraph.c;h=c04cef40e01e48cc8fc67471e6c4c51a903a915f;hb=f02d3ed3e135b5326003e7f69f8331ff6a3cc219;hp=eadb36a7c9d8f6c9f40684943b4ecdbda68d30d5;hpb=0d1ac68c59db87141616f69bcd3d79c705b1ecd0;p=tinc diff --git a/src/graph.c b/src/graph.c index eadb36a7..c04cef40 100644 --- a/src/graph.c +++ b/src/graph.c @@ -58,15 +58,12 @@ #include "subnet.h" #include "utils.h" -static bool graph_changed = true; - /* Implementation of Kruskal's algorithm. Running time: O(EN) Please note that sorting on weight is already done by add_edge(). */ -void mst_kruskal(void) -{ +void mst_kruskal(void) { avl_node_t *node, *next; edge_t *e; node_t *n; @@ -101,7 +98,13 @@ void mst_kruskal(void) /* Starting point */ - ((edge_t *) edge_weight_tree->head->data)->from->status.visited = true; + for(node = edge_weight_tree->head; node; node = node->next) { + e = node->data; + if(e->from->status.reachable) { + e->from->status.visited = true; + break; + } + } /* Add safe edges */ @@ -143,8 +146,7 @@ void mst_kruskal(void) Running time: O(E) */ -void sssp_bfs(void) -{ +void sssp_bfs(void) { avl_node_t *node, *next, *to; edge_t *e; node_t *n; @@ -305,34 +307,22 @@ void sssp_bfs(void) } } -void graph(void) -{ - mst_kruskal(); - sssp_bfs(); - graph_changed = true; -} - - - /* Dump nodes and edges to a graphviz file. The file can be converted to an image with dot -Tpng graph_filename -o image_filename.png -Gconcentrate=true */ -void dump_graph(void) -{ +static void dump_graph(int fd, short events, void *data) { avl_node_t *node; node_t *n; edge_t *e; char *filename = NULL, *tmpname = NULL; FILE *file; - if(!graph_changed || !get_config_string(lookup_config(config_tree, "GraphDumpFile"), &filename)) + if(!get_config_string(lookup_config(config_tree, "GraphDumpFile"), &filename)) return; - graph_changed = false; - ifdebug(PROTOCOL) logger(LOG_NOTICE, "Dumping graph"); if(filename[0] == '|') { @@ -368,7 +358,21 @@ void dump_graph(void) pclose(file); } else { fclose(file); +#ifdef HAVE_MINGW + unlink(filename); +#endif rename(tmpname, filename); free(tmpname); } } + +void graph(void) { + static struct event ev; + + sssp_bfs(); + mst_kruskal(); + + if(!timeout_initialized(&ev)) + timeout_set(&ev, dump_graph, NULL); + event_add(&ev, &(struct timeval){5, 0}); +}