14 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
17 static struct timeval tv0;
21 gettimeofday(&tv0, NULL);
24 gettimeofday(&tv, NULL);
25 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
28 fprintf(stderr, "(%s) ", mesh->name);
31 fprintf(stderr, "[%d] %s\n", level, text);
34 static bool received = false;
36 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
40 fprintf(stderr, "RECEIVED SOMETHING\n");
42 if(len == 5 && !memcmp(data, "Hello", 5)) {
48 // Create three instances.
50 const char *name[3] = {"foo", "bar", "baz"};
51 meshlink_handle_t *mesh[3];
54 for(int i = 0; i < 3; i++) {
58 ret_val = asprintf(&path, "trio_conf.%d", i);
61 mesh[i] = meshlink_open(path, name[i], "trio", 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 for(int i = 0; i < 3; i++) {
88 assert(meshlink_start(mesh[i]));
91 // the nodes should now learn about each other
93 assert_after(meshlink_get_node(mesh[1], name[2]), 5);
94 assert_after(meshlink_get_node(mesh[2], name[1]), 5);
96 // Send a packet, expect it is received
98 meshlink_set_receive_cb(mesh[1], receive_cb);
99 assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15);
101 // Check that the second and third node have autoconnected to each other
103 devtool_edge_t *edges = NULL;
105 assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
107 // Stop the first node
109 meshlink_stop(mesh[0]);
112 // Communication should still be possible
114 assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15);
116 // Stop the other nodes
118 for(int i = 1; i < 3; i++) {
119 meshlink_stop(mesh[i]);
124 // Start just the other two nodes
126 meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
128 for(int i = 1; i < 3; i++) {
129 assert(meshlink_start(mesh[i]));
132 assert(meshlink_get_node(mesh[1], name[2]));
133 assert(meshlink_get_node(mesh[2], name[1]));
135 // Communication should still be possible
138 assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25);
142 for(int i = 0; i < 3; i++) {
143 meshlink_close(mesh[i]);