123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /*
- edge.c -- edge tree management
- Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
- 2000-2002 Ivo Timmermans <itimmermans@bigfoot.com>
- 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.
- $Id: edge.c,v 1.1.2.6 2002/02/10 21:57:54 guus Exp $
- */
- #include "config.h"
- #include <stdio.h>
- #include <syslog.h>
- #include <string.h>
- #include <avl_tree.h>
- #include <list.h>
- #include "net.h" /* Don't ask. */
- #include "netutl.h"
- #include "config.h"
- #include "conf.h"
- #include <utils.h>
- #include "subnet.h"
- #include "xalloc.h"
- #include "system.h"
- avl_tree_t *edge_tree; /* Tree with all known edges (replaces active_tree) */
- avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
- int edge_compare(edge_t *a, edge_t *b)
- {
- int result;
- result = strcmp(a->from.node->name, b->from.node->name);
-
- if(result)
- return result;
- else
- return strcmp(a->to.node->name, b->to.node->name);
- }
- /* Evil edge_compare() from a parallel universe ;)
- int edge_compare(edge_t *a, edge_t *b)
- {
- int result;
- return (result = strcmp(a->from.node->name, b->from.node->name)) || (result = strcmp(a->to.node->name, b->to.node->name)), result;
- }
- */
- int edge_name_compare(edge_t *a, edge_t *b)
- {
- int result;
- char *name_a1, *name_a2, *name_b1, *name_b2;
-
- if(strcmp(a->from.node->name, a->to.node->name) < 0)
- name_a1 = a->from.node->name, name_a2 = a->to.node->name;
- else
- name_a1 = a->to.node->name, name_a2 = a->from.node->name;
- if(strcmp(b->from.node->name, b->to.node->name) < 0)
- name_b1 = b->from.node->name, name_b2 = b->to.node->name;
- else
- name_b1 = b->to.node->name, name_b2 = b->from.node->name;
- result = strcmp(name_a1, name_b1);
-
- if(result)
- return result;
- else
- return strcmp(name_a2, name_b2);
- }
- int edge_weight_compare(edge_t *a, edge_t *b)
- {
- int result;
-
- result = a->weight - b->weight;
-
- if(result)
- return result;
- else
- return edge_name_compare(a, b);
- }
- void init_edges(void)
- {
- cp
- edge_tree = avl_alloc_tree((avl_compare_t)edge_compare, NULL);
- edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
- cp
- }
- avl_tree_t *new_edge_tree(void)
- {
- cp
- return avl_alloc_tree((avl_compare_t)edge_name_compare, NULL);
- cp
- }
- void free_edge_tree(avl_tree_t *edge_tree)
- {
- cp
- avl_delete_tree(edge_tree);
- cp
- }
- void exit_edges(void)
- {
- cp
- avl_delete_tree(edge_tree);
- cp
- }
- /* Creation and deletion of connection elements */
- edge_t *new_edge(void)
- {
- edge_t *e;
- cp
- e = (edge_t *)xmalloc_and_zero(sizeof(*e));
- cp
- return e;
- }
- void free_edge(edge_t *e)
- {
- cp
- free(e);
- cp
- }
- void edge_add(edge_t *e)
- {
- cp
- avl_insert(edge_tree, e);
- avl_insert(edge_weight_tree, e);
- avl_insert(e->from.node->edge_tree, e);
- avl_insert(e->to.node->edge_tree, e);
- cp
- }
- void edge_del(edge_t *e)
- {
- cp
- avl_delete(edge_tree, e);
- avl_delete(edge_weight_tree, e);
- avl_delete(e->from.node->edge_tree, e);
- avl_delete(e->to.node->edge_tree, e);
- cp
- }
- edge_t *lookup_edge(node_t *from, node_t *to)
- {
- edge_t v, *result;
- cp
- v.from.node = from;
- v.to.node = to;
- result = avl_search(edge_tree, &v);
- if(result)
- return result;
- cp
- v.from.node = to;
- v.to.node = from;
- return avl_search(edge_tree, &v);
- }
- void dump_edges(void)
- {
- avl_node_t *node;
- edge_t *e;
- char *from_address, *to_address;
- cp
- syslog(LOG_DEBUG, _("Edges:"));
- for(node = edge_tree->head; node; node = node->next)
- {
- e = (edge_t *)node->data;
- from_address = address2str(e->from.address);
- to_address = address2str(e->to.address);
- syslog(LOG_DEBUG, _(" %s at %s port %hd - %s at %s port %hd options %ld weight %d"),
- e->from.node->name, from_address, e->from.port,
- e->to.node->name, to_address, e->to.port,
- e->options, e->weight);
- free(from_address);
- free(to_address);
- }
-
- syslog(LOG_DEBUG, _("End of edges."));
- cp
- }
|