15 static struct sync_flag received;
16 static struct sync_flag bar_learned_baz;
17 static struct sync_flag baz_learned_bar;
19 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
23 if(len == 5 && !memcmp(data, "Hello", 5)) {
24 set_sync_flag(&received, true);
28 static void bar_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
32 if(!strcmp(node->name, "baz")) {
33 set_sync_flag(&bar_learned_baz, true);
37 static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
41 if(!strcmp(node->name, "bar")) {
42 set_sync_flag(&baz_learned_bar, true);
47 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
49 // Create three instances.
51 const char *name[3] = {"foo", "bar", "baz"};
52 meshlink_handle_t *mesh[3];
55 for(int i = 0; i < 3; i++) {
57 assert(asprintf(&path, "trio2_conf.%d", i) != -1 && path);
59 assert(meshlink_destroy(path));
60 mesh[i] = meshlink_open(path, name[i], "trio2", DEV_CLASS_BACKBONE);
64 meshlink_add_address(mesh[i], "localhost");
66 data[i] = meshlink_export(mesh[i]);
70 // first node knows the two other nodes
72 for(int i = 1; i < 3; i++) {
73 assert(meshlink_import(mesh[i], data[0]));
74 assert(meshlink_import(mesh[0], data[i]));
76 assert(meshlink_get_node(mesh[i], name[0]));
77 assert(meshlink_get_node(mesh[0], name[i]));
80 // second and third node should not know each other yet
82 assert(!meshlink_get_node(mesh[1], name[2]));
83 assert(!meshlink_get_node(mesh[2], name[1]));
87 meshlink_set_node_status_cb(mesh[1], bar_status_cb);
88 meshlink_set_node_status_cb(mesh[2], baz_status_cb);
90 for(int i = 0; i < 3; i++) {
92 assert(meshlink_start(mesh[i]));
95 // the nodes should now learn about each other
97 assert(wait_sync_flag(&bar_learned_baz, 5));
98 assert(wait_sync_flag(&baz_learned_bar, 5));
100 // Check that the second and third node autoconnect to each other
102 devtool_edge_t *edges = NULL;
104 assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
107 // Stop the nodes nodes
109 for(int i = 0; i < 3; i++) {
110 meshlink_stop(mesh[i]);
113 // Start just the other two nodes
115 for(int i = 1; i < 3; i++) {
116 assert(meshlink_start(mesh[i]));
119 assert(meshlink_get_node(mesh[1], name[2]));
120 assert(meshlink_get_node(mesh[2], name[1]));
122 // Communication should still be possible
124 meshlink_set_receive_cb(mesh[1], receive_cb);
126 for(int i = 0; i < 25; i++) {
127 assert(meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5));
129 if(wait_sync_flag(&received, 1)) {
134 assert(wait_sync_flag(&received, 1));
138 for(int i = 0; i < 3; i++) {
139 meshlink_close(mesh[i]);