]> git.meshlink.io Git - meshlink/blob - test/trio.c
Add meshlink_set_canonical_address().
[meshlink] / test / trio.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <assert.h>
8 #include <sys/time.h>
9
10 #include "meshlink.h"
11 #include "devtools.h"
12 #include "utils.h"
13
14 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
15         (void)mesh;
16
17         static struct timeval tv0;
18         struct timeval tv;
19
20         if(tv0.tv_sec == 0) {
21                 gettimeofday(&tv0, NULL);
22         }
23
24         gettimeofday(&tv, NULL);
25         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
26
27         if(mesh) {
28                 fprintf(stderr, "(%s) ", mesh->name);
29         }
30
31         fprintf(stderr, "[%d] %s\n", level, text);
32 }
33
34 static bool received = false;
35
36 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
37         (void)mesh;
38         (void)source;
39
40         fprintf(stderr, "RECEIVED SOMETHING\n");
41
42         if(len == 5 && !memcmp(data, "Hello", 5)) {
43                 received = true;
44         }
45 }
46
47 int main() {
48         // Create three instances.
49
50         const char *name[3] = {"foo", "bar", "baz"};
51         meshlink_handle_t *mesh[3];
52         char *data[3];
53
54         for(int i = 0; i < 3; i++) {
55                 char *path;
56                 asprintf(&path, "trio_conf.%d", i);
57                 assert(path);
58
59                 mesh[i] = meshlink_open(path, name[i], "trio", DEV_CLASS_BACKBONE);
60                 assert(mesh[i]);
61
62                 meshlink_add_address(mesh[i], "localhost");
63
64                 data[i] = meshlink_export(mesh[i]);
65                 assert(data[i]);
66         }
67
68         // first node knows the two other nodes
69
70         for(int i = 1; i < 3; i++) {
71                 assert(meshlink_import(mesh[i], data[0]));
72                 assert(meshlink_import(mesh[0], data[i]));
73
74                 assert(meshlink_get_node(mesh[i], name[0]));
75                 assert(meshlink_get_node(mesh[0], name[i]));
76         }
77
78         // second and third node should not know each other yet
79
80         assert(!meshlink_get_node(mesh[1], name[2]));
81         assert(!meshlink_get_node(mesh[2], name[1]));
82
83         // start the nodes
84
85         for(int i = 0; i < 3; i++) {
86                 assert(meshlink_start(mesh[i]));
87         }
88
89         // the nodes should now learn about each other
90
91         assert_after(meshlink_get_node(mesh[1], name[2]), 5);
92         assert_after(meshlink_get_node(mesh[2], name[1]), 5);
93
94         // Send a packet, expect it is received
95
96         meshlink_set_receive_cb(mesh[1], receive_cb);
97         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15);
98
99         // Check that the second and third node have autoconnected to each other
100
101         devtool_edge_t *edges = NULL;
102         size_t nedges = 0;
103         assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
104
105         // Stop the first node
106
107         meshlink_stop(mesh[0]);
108         sleep(1);
109
110         // Communication should still be possible
111
112         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15);
113
114         // Stop the other nodes
115
116         for(int i = 1; i < 3; i++) {
117                 meshlink_stop(mesh[i]);
118         }
119
120         sleep(1);
121
122         // Start just the other two nodes
123
124         meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
125
126         for(int i = 1; i < 3; i++) {
127                 assert(meshlink_start(mesh[i]));
128         }
129
130         assert(meshlink_get_node(mesh[1], name[2]));
131         assert(meshlink_get_node(mesh[2], name[1]));
132
133         // Communication should still be possible
134
135         received = false;
136         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25);
137
138         // Clean up.
139
140         for(int i = 0; i < 3; i++) {
141                 meshlink_close(mesh[i]);
142         }
143 }