]> git.meshlink.io Git - meshlink/blob - src/edge.c
No C99 initialisers, gcc 2.95.3 doesn't like it.
[meshlink] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2000-2003 Guus Sliepen <guus@sliepen.eu.org>,
4                   2000-2003 Ivo Timmermans <ivo@o2w.nl>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: edge.c,v 1.1.2.25 2003/07/30 11:50:45 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "avl_tree.h"
26 #include "edge.h"
27 #include "logger.h"
28 #include "netutl.h"
29 #include "node.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 avl_tree_t *edge_weight_tree;   /* Tree with all edges, sorted on weight */
34
35 static int edge_compare(const edge_t *a, const edge_t *b)
36 {
37         return strcmp(a->to->name, b->to->name);
38 }
39
40 static int edge_weight_compare(const edge_t *a, const edge_t *b)
41 {
42         int result;
43
44         result = a->weight - b->weight;
45
46         if(result)
47                 return result;
48
49         result = strcmp(a->from->name, b->from->name);
50
51         if(result)
52                 return result;
53
54         return strcmp(a->to->name, b->to->name);
55 }
56
57 void init_edges(void)
58 {
59         cp();
60
61         edge_weight_tree =
62                 avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
63 }
64
65 avl_tree_t *new_edge_tree(void)
66 {
67         cp();
68
69         return avl_alloc_tree((avl_compare_t) edge_compare, NULL);
70 }
71
72 void free_edge_tree(avl_tree_t *edge_tree)
73 {
74         cp();
75
76         avl_delete_tree(edge_tree);
77 }
78
79 void exit_edges(void)
80 {
81         cp();
82
83         avl_delete_tree(edge_weight_tree);
84 }
85
86 /* Creation and deletion of connection elements */
87
88 edge_t *new_edge(void)
89 {
90         cp();
91
92         return (edge_t *) xmalloc_and_zero(sizeof(edge_t));
93 }
94
95 void free_edge(edge_t *e)
96 {
97         cp();
98
99         free(e);
100 }
101
102 void edge_add(edge_t *e)
103 {
104         cp();
105
106         avl_insert(edge_weight_tree, e);
107         avl_insert(e->from->edge_tree, e);
108
109         e->reverse = lookup_edge(e->to, e->from);
110
111         if(e->reverse)
112                 e->reverse->reverse = e;
113 }
114
115 void edge_del(edge_t *e)
116 {
117         cp();
118
119         if(e->reverse)
120                 e->reverse->reverse = NULL;
121
122         avl_delete(e->from->edge_tree, e);
123         avl_delete(edge_weight_tree, e);
124 }
125
126 edge_t *lookup_edge(node_t *from, node_t *to)
127 {
128         edge_t v;
129         
130         cp();
131
132         v.from = from;
133         v.to = to;
134
135         return avl_search(from->edge_tree, &v);
136 }
137
138 void dump_edges(void)
139 {
140         avl_node_t *node, *node2;
141         node_t *n;
142         edge_t *e;
143         char *address;
144
145         cp();
146
147         logger(LOG_DEBUG, _("Edges:"));
148
149         for(node = node_tree->head; node; node = node->next) {
150                 n = (node_t *) node->data;
151                 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
152                         e = (edge_t *) node2->data;
153                         address = sockaddr2hostname(&e->address);
154                         logger(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
155                                    e->from->name, e->to->name, address, e->options, e->weight);
156                         free(address);
157                 }
158         }
159
160         logger(LOG_DEBUG, _("End of edges."));
161 }