2001-10-10 08:49:47 +00:00
|
|
|
/*
|
2001-10-28 08:41:19 +00:00
|
|
|
edge.c -- edge tree management
|
2003-07-12 17:41:48 +00:00
|
|
|
Copyright (C) 2000-2003 Guus Sliepen <guus@sliepen.eu.org>,
|
|
|
|
2000-2003 Ivo Timmermans <ivo@o2w.nl>
|
2001-10-10 08:49:47 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2003-07-24 12:08:16 +00:00
|
|
|
$Id: edge.c,v 1.1.2.23 2003/07/24 12:08:15 guus Exp $
|
2001-10-10 08:49:47 +00:00
|
|
|
*/
|
|
|
|
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "system.h"
|
2001-10-10 08:49:47 +00:00
|
|
|
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "avl_tree.h"
|
2002-09-04 13:48:52 +00:00
|
|
|
#include "edge.h"
|
2003-07-06 22:11:37 +00:00
|
|
|
#include "logger.h"
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "netutl.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "utils.h"
|
2001-10-10 08:49:47 +00:00
|
|
|
#include "xalloc.h"
|
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
|
2001-10-10 08:49:47 +00:00
|
|
|
|
2003-07-24 12:08:16 +00:00
|
|
|
static int edge_compare(const edge_t *a, const edge_t *b)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
return strcmp(a->to->name, b->to->name);
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|
|
|
|
|
2003-07-24 12:08:16 +00:00
|
|
|
static int edge_weight_compare(const edge_t *a, const edge_t *b)
|
2001-10-28 22:42:49 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
int result;
|
2002-02-18 16:25:19 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
result = a->weight - b->weight;
|
2002-02-18 16:25:19 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
if(result)
|
|
|
|
return result;
|
2002-09-06 12:19:16 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
result = strcmp(a->from->name, b->from->name);
|
2002-09-06 12:19:16 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
if(result)
|
|
|
|
return result;
|
2002-09-06 12:19:16 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
return strcmp(a->to->name, b->to->name);
|
2001-10-28 22:42:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
void init_edges(void)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
edge_weight_tree =
|
|
|
|
avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 22:42:49 +00:00
|
|
|
avl_tree_t *new_edge_tree(void)
|
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
return avl_alloc_tree((avl_compare_t) edge_compare, NULL);
|
2001-10-28 22:42:49 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void free_edge_tree(avl_tree_t *edge_tree)
|
2001-10-28 22:42:49 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
avl_delete_tree(edge_tree);
|
2001-10-28 22:42:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
void exit_edges(void)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
avl_delete_tree(edge_weight_tree);
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Creation and deletion of connection elements */
|
|
|
|
|
2001-10-28 08:41:19 +00:00
|
|
|
edge_t *new_edge(void)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
return (edge_t *) xmalloc_and_zero(sizeof(edge_t));
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void free_edge(edge_t *e)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
free(e);
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void edge_add(edge_t *e)
|
2001-10-27 12:13:17 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
avl_insert(edge_weight_tree, e);
|
|
|
|
avl_insert(e->from->edge_tree, e);
|
|
|
|
|
|
|
|
e->reverse = lookup_edge(e->to, e->from);
|
|
|
|
|
|
|
|
if(e->reverse)
|
|
|
|
e->reverse->reverse = e;
|
2001-10-27 12:13:17 +00:00
|
|
|
}
|
|
|
|
|
2002-09-09 22:33:31 +00:00
|
|
|
void edge_del(edge_t *e)
|
2001-10-27 12:13:17 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
cp();
|
|
|
|
|
|
|
|
if(e->reverse)
|
|
|
|
e->reverse->reverse = NULL;
|
|
|
|
|
|
|
|
avl_delete(e->from->edge_tree, e);
|
|
|
|
avl_delete(edge_weight_tree, e);
|
2001-10-27 12:13:17 +00:00
|
|
|
}
|
|
|
|
|
2003-07-24 12:08:16 +00:00
|
|
|
edge_t *lookup_edge(const node_t *from, const node_t *to)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2003-07-24 12:08:16 +00:00
|
|
|
edge_t v = {
|
|
|
|
.from = from,
|
|
|
|
.to = to
|
|
|
|
};
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
cp();
|
|
|
|
|
|
|
|
return avl_search(from->edge_tree, &v);
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 10:16:18 +00:00
|
|
|
void dump_edges(void)
|
2001-10-10 08:49:47 +00:00
|
|
|
{
|
2002-09-09 21:25:28 +00:00
|
|
|
avl_node_t *node, *node2;
|
|
|
|
node_t *n;
|
|
|
|
edge_t *e;
|
|
|
|
char *address;
|
|
|
|
|
|
|
|
cp();
|
|
|
|
|
2003-07-12 17:41:48 +00:00
|
|
|
logger(LOG_DEBUG, _("Edges:"));
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
for(node = node_tree->head; node; node = node->next) {
|
|
|
|
n = (node_t *) node->data;
|
|
|
|
for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
|
|
|
|
e = (edge_t *) node2->data;
|
|
|
|
address = sockaddr2hostname(&e->address);
|
2003-07-12 17:41:48 +00:00
|
|
|
logger(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
|
2002-09-09 21:25:28 +00:00
|
|
|
e->from->name, e->to->name, address, e->options, e->weight);
|
|
|
|
free(address);
|
|
|
|
}
|
2002-09-06 10:23:52 +00:00
|
|
|
}
|
2002-02-18 16:25:19 +00:00
|
|
|
|
2003-07-12 17:41:48 +00:00
|
|
|
logger(LOG_DEBUG, _("End of edges."));
|
2001-10-10 08:49:47 +00:00
|
|
|
}
|