]> git.meshlink.io Git - meshlink/blob - src/edge.c
Skeleton design for opening a reliable channel
[meshlink] / src / edge.c
1 /*
2     edge.c -- edge tree management
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "splay_tree.h"
23 #include "edge.h"
24 #include "logger.h"
25 #include "meshlink_internal.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 static int edge_compare(const edge_t *a, const edge_t *b) {
32         return strcmp(a->to->name, b->to->name);
33 }
34
35 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
36         int result;
37
38         result = a->weight - b->weight;
39
40         if(result)
41                 return result;
42
43         result = strcmp(a->from->name, b->from->name);
44
45         if(result)
46                 return result;
47
48         return strcmp(a->to->name, b->to->name);
49 }
50
51 void init_edges(meshlink_handle_t *mesh) {
52         mesh->edges = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
53 }
54
55 splay_tree_t *new_edge_tree(void) {
56         return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
57 }
58
59 void free_edge_tree(splay_tree_t *edge_tree) {
60         splay_delete_tree(edge_tree);
61 }
62
63 void exit_edges(meshlink_handle_t *mesh) {
64         splay_delete_tree(mesh->edges);
65 }
66
67 /* Creation and deletion of connection elements */
68
69 edge_t *new_edge(void) {
70         return xzalloc(sizeof(edge_t));
71 }
72
73 void free_edge(edge_t *e) {
74         sockaddrfree(&e->address);
75
76         free(e);
77 }
78
79 void edge_add(meshlink_handle_t *mesh, edge_t *e) {
80         splay_insert(mesh->edges, e);
81         splay_insert(e->from->edge_tree, e);
82
83         e->reverse = lookup_edge(e->to, e->from);
84
85         if(e->reverse)
86                 e->reverse->reverse = e;
87 }
88
89 void edge_del(meshlink_handle_t *mesh, edge_t *e) {
90         if(e->reverse)
91                 e->reverse->reverse = NULL;
92
93         splay_delete(mesh->edges, e);
94         splay_delete(e->from->edge_tree, e);
95 }
96
97 edge_t *lookup_edge(node_t *from, node_t *to) {
98         edge_t v;
99
100         v.from = from;
101         v.to = to;
102
103         return splay_search(from->edge_tree, &v);
104 }