15 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
18 static struct timeval tv0;
22 gettimeofday(&tv0, NULL);
25 gettimeofday(&tv, NULL);
26 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
29 fprintf(stderr, "(%s) ", mesh->name);
32 fprintf(stderr, "[%d] %s\n", level, text);
35 static bool received = false;
37 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
41 fprintf(stderr, "RECEIVED SOMETHING\n");
43 if(len == 5 && !memcmp(data, "Hello", 5)) {
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 mesh[i] = meshlink_open(path, name[i], "trio2", DEV_CLASS_BACKBONE);
63 meshlink_add_address(mesh[i], "localhost");
65 data[i] = meshlink_export(mesh[i]);
69 meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
71 // first node knows the two other nodes
73 for(int i = 1; i < 3; i++) {
74 assert(meshlink_import(mesh[i], data[0]));
75 assert(meshlink_import(mesh[0], data[i]));
77 assert(meshlink_get_node(mesh[i], name[0]));
78 assert(meshlink_get_node(mesh[0], name[i]));
81 // second and third node should not know each other yet
83 assert(!meshlink_get_node(mesh[1], name[2]));
84 assert(!meshlink_get_node(mesh[2], name[1]));
88 for(int i = 0; i < 3; i++) {
90 assert(meshlink_start(mesh[i]));
93 // the nodes should now learn about each other
95 assert_after(meshlink_get_node(mesh[1], name[2]), 5);
96 assert_after(meshlink_get_node(mesh[2], name[1]), 5);
98 // Check that the second and third node autoconnect to each other
100 devtool_edge_t *edges = NULL;
102 assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
105 // Stop the nodes nodes
107 for(int i = 0; i < 3; i++) {
108 meshlink_stop(mesh[i]);
111 // Start just the other two nodes
113 meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
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
125 meshlink_set_receive_cb(mesh[1], receive_cb);
126 assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25);
130 for(int i = 0; i < 3; i++) {
131 meshlink_close(mesh[i]);