19 static struct sync_flag received;
20 static struct sync_flag bar_learned_baz;
21 static struct sync_flag baz_learned_bar;
23 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
27 if(len == 5 && !memcmp(data, "Hello", 5)) {
28 set_sync_flag(&received, true);
32 static void bar_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
36 if(!strcmp(node->name, "baz")) {
37 set_sync_flag(&bar_learned_baz, true);
41 static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
45 if(!strcmp(node->name, "bar")) {
46 set_sync_flag(&baz_learned_bar, true);
51 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
53 // Create three instances.
55 const char *name[3] = {"foo", "bar", "baz"};
56 meshlink_handle_t *mesh[3];
59 for(int i = 0; i < 3; i++) {
61 assert(asprintf(&path, "trio_conf.%d", i) != -1 && path);
63 assert(meshlink_destroy(path));
64 mesh[i] = meshlink_open(path, name[i], "trio", DEV_CLASS_BACKBONE);
68 assert(meshlink_add_address(mesh[i], "localhost"));
70 data[i] = meshlink_export(mesh[i]);
74 // first node knows the two other nodes
76 for(int i = 1; i < 3; i++) {
77 assert(meshlink_import(mesh[i], data[0]));
78 assert(meshlink_import(mesh[0], data[i]));
80 assert(meshlink_get_node(mesh[i], name[0]));
81 assert(meshlink_get_node(mesh[0], name[i]));
84 // second and third node should not know each other yet
86 assert(!meshlink_get_node(mesh[1], name[2]));
87 assert(!meshlink_get_node(mesh[2], name[1]));
91 meshlink_set_node_status_cb(mesh[1], bar_status_cb);
92 meshlink_set_node_status_cb(mesh[2], baz_status_cb);
94 for(int i = 0; i < 3; i++) {
96 assert(meshlink_start(mesh[i]));
99 // the nodes should now learn about each other
101 assert(wait_sync_flag(&bar_learned_baz, 5));
102 assert(wait_sync_flag(&baz_learned_bar, 5));
104 // Send a packet, expect it is received
106 meshlink_set_receive_cb(mesh[1], receive_cb);
108 for(int i = 0; i < 15; i++) {
109 assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5));
111 if(wait_sync_flag(&received, 1)) {
116 assert(wait_sync_flag(&received, 15));
118 // Check that the second and third node have autoconnected to each other
120 devtool_edge_t *edges = NULL;
122 assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
125 // Stop the first node
127 meshlink_stop(mesh[0]);
130 // Communication should still be possible
132 set_sync_flag(&received, false);
134 for(int i = 0; i < 15; i++) {
135 assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5));
137 if(wait_sync_flag(&received, 1)) {
142 assert(wait_sync_flag(&received, 15));
144 // Stop the other nodes
146 for(int i = 1; i < 3; i++) {
147 meshlink_stop(mesh[i]);
152 // Start just the other two nodes
154 for(int i = 1; i < 3; i++) {
155 assert(meshlink_start(mesh[i]));
158 assert(meshlink_get_node(mesh[1], name[2]));
159 assert(meshlink_get_node(mesh[2], name[1]));
161 // Communication should still be possible
163 set_sync_flag(&received, false);
165 for(int i = 0; i < 15; i++) {
166 assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5));
168 if(wait_sync_flag(&received, 1)) {
173 assert(wait_sync_flag(&received, 1));
177 for(int i = 0; i < 3; i++) {
178 meshlink_close(mesh[i]);