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