2 edge.c -- edge tree management
3 Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
4 2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
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.
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.
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.
20 $Id: edge.c,v 1.2 2002/04/09 15:26:00 zarq Exp $
32 #include "net.h" /* Don't ask. */
42 avl_tree_t *edge_tree; /* Tree with all known edges (replaces active_tree) */
43 avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
45 int edge_compare(edge_t *a, edge_t *b)
49 result = strcmp(a->from.node->name, b->from.node->name);
54 return strcmp(a->to.node->name, b->to.node->name);
57 /* Evil edge_compare() from a parallel universe ;)
59 int edge_compare(edge_t *a, edge_t *b)
63 return (result = strcmp(a->from.node->name, b->from.node->name)) || (result = strcmp(a->to.node->name, b->to.node->name)), result;
68 int edge_name_compare(edge_t *a, edge_t *b)
71 char *name_a1, *name_a2, *name_b1, *name_b2;
73 if(strcmp(a->from.node->name, a->to.node->name) < 0)
74 name_a1 = a->from.node->name, name_a2 = a->to.node->name;
76 name_a1 = a->to.node->name, name_a2 = a->from.node->name;
78 if(strcmp(b->from.node->name, b->to.node->name) < 0)
79 name_b1 = b->from.node->name, name_b2 = b->to.node->name;
81 name_b1 = b->to.node->name, name_b2 = b->from.node->name;
83 result = strcmp(name_a1, name_b1);
88 return strcmp(name_a2, name_b2);
91 int edge_weight_compare(edge_t *a, edge_t *b)
95 result = a->weight - b->weight;
100 return edge_name_compare(a, b);
103 void init_edges(void)
106 edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
107 edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
111 avl_tree_t *new_edge_tree(void)
114 return avl_alloc_tree((avl_compare_t)edge_name_compare, NULL);
118 void free_edge_tree(avl_tree_t *edge_tree)
121 avl_delete_tree(edge_tree);
125 void exit_edges(void)
128 avl_delete_tree(edge_tree);
132 /* Creation and deletion of connection elements */
134 edge_t *new_edge(void)
138 e = (edge_t *)xmalloc_and_zero(sizeof(*e));
143 void free_edge(edge_t *e)
150 void edge_add(edge_t *e)
153 avl_insert(edge_tree, e);
154 avl_insert(edge_weight_tree, e);
155 avl_insert(e->from.node->edge_tree, e);
156 avl_insert(e->to.node->edge_tree, e);
160 void edge_del(edge_t *e)
163 avl_delete(edge_tree, e);
164 avl_delete(edge_weight_tree, e);
165 avl_delete(e->from.node->edge_tree, e);
166 avl_delete(e->to.node->edge_tree, e);
170 edge_t *lookup_edge(node_t *from, node_t *to)
177 result = avl_search(edge_tree, &v);
185 return avl_search(edge_tree, &v);
188 void dump_edges(void)
192 char *from_udp, *to_udp;
194 syslog(LOG_DEBUG, _("Edges:"));
196 for(node = edge_tree->head; node; node = node->next)
198 e = (edge_t *)node->data;
199 from_udp = sockaddr2hostname(&e->from.udpaddress);
200 to_udp = sockaddr2hostname(&e->to.udpaddress);
201 syslog(LOG_DEBUG, _(" %s at %s - %s at %s options %lx weight %d"),
202 e->from.node->name, from_udp,
203 e->to.node->name, to_udp,
204 e->options, e->weight);
209 syslog(LOG_DEBUG, _("End of edges."));